prepsano nacitani obrazku z resources

main
Michal 2025-02-20 19:47:43 +01:00
parent 1e7bc533b0
commit c407cec1e5
1 changed files with 42 additions and 36 deletions

View File

@ -8,7 +8,9 @@ import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -80,39 +82,43 @@ public class MapTree extends JTree {
} }
private static void getContextMenuMap(MouseEvent e, Map m) { private static void getContextMenuMap(MouseEvent e, Map m) {
JMenuItem mapAdd = new JMenuItem("Přidat mapu", new ImageIcon("img/add.png")); try {
mapAdd.addActionListener(new ActionListener() { JMenuItem mapAdd = new JMenuItem("Přidat mapu", new ImageIcon(ImageIO.read(MapTree.class.getResourceAsStream("/img/add.png"))));
@Override mapAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { @Override
new AddMapDialog().setVisible(true); public void actionPerformed(ActionEvent e) {
} new AddMapDialog().setVisible(true);
});
JMenuItem mapDelete = new JMenuItem("Smazat mapu", new ImageIcon("img/trash.png"));
mapDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(
null,
"Smazat mapu " + m.getName() + " a všechny její objekty ?",
"Potvrzení akce",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION) {
NettyClient.send(Message.MAP_REMOVE, m.getId());
} }
});
JMenuItem mapDelete = new JMenuItem("Smazat mapu", new ImageIcon(ImageIO.read(MapTree.class.getResourceAsStream("/img/trash.png"))));
mapDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(
null,
"Smazat mapu " + m.getName() + " a všechny její objekty ?",
"Potvrzení akce",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION) {
NettyClient.send(Message.MAP_REMOVE, m.getId());
}
}
});
JPopupMenu menu = new JPopupMenu();
if (Client.user.isAddMap()) {
menu.add(mapAdd);
} }
}); if (Client.user.isRemoveMap()) {
menu.add(mapDelete);
JPopupMenu menu = new JPopupMenu(); }
if (Client.user.isAddMap()) { menu.show(e.getComponent(), e.getX(), e.getY());
menu.add(mapAdd); } catch (IOException ex) {
LogFile.printErr("MapTree error: " + ex.getMessage());
} }
if (Client.user.isRemoveMap()) {
menu.add(mapDelete);
}
menu.show(e.getComponent(), e.getX(), e.getY());
} }
private static void expandAllNodes(JTree tree, int startingIndex, int rowCount) { private static void expandAllNodes(JTree tree, int startingIndex, int rowCount) {
@ -126,13 +132,13 @@ public class MapTree extends JTree {
} }
public static void reload() { public static void reload() {
root.removeAllChildren();
Collections.sort(Client.maps, new MapComparator());
for (Map map : Client.maps) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(map);
root.add(node);
}
try { try {
root.removeAllChildren();
Collections.sort(Client.maps, new MapComparator());
for (Map map : Client.maps) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(map);
root.add(node);
}
model.reload(); model.reload();
expandAllNodes(tree, 0, tree.getRowCount()); expandAllNodes(tree, 0, tree.getRowCount());
} catch (Exception e) { } catch (Exception e) {