From 0bd6cac05eed49e79daa188a5589312ecd5d81c2 Mon Sep 17 00:00:00 2001 From: iobyte Date: Sat, 27 May 2023 22:44:02 +0000 Subject: [PATCH] trackeditor: add delete objects to terrain object map properties git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@8949 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 44c408246f59b7e66c3ffdddf63d375425a79b70 Former-commit-id: 2bbd3015a05b68f049798e2b41ce1afb67a1c8f1 --- .../gui/properties/ObjectMapProperties.java | 135 ++++++++++++++++++ .../gui/properties/ReliefProperties.java | 10 +- src/tools/trackeditor/utils/Properties.java | 2 +- 3 files changed, 139 insertions(+), 8 deletions(-) diff --git a/src/tools/trackeditor/gui/properties/ObjectMapProperties.java b/src/tools/trackeditor/gui/properties/ObjectMapProperties.java index f7ba2917..f18d4013 100644 --- a/src/tools/trackeditor/gui/properties/ObjectMapProperties.java +++ b/src/tools/trackeditor/gui/properties/ObjectMapProperties.java @@ -4,12 +4,18 @@ import java.awt.Color; import java.awt.Component; import java.awt.Graphics2D; import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.Set; import java.util.Vector; @@ -20,12 +26,15 @@ import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; +import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextField; +import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; @@ -539,6 +548,12 @@ public class ObjectMapProperties extends PropertyPanel break; } } + + public void removeRowAt(int row) + { + data.removeElementAt(row); + fireTableRowsDeleted(row - 1, data.size() - 1); + } } public void setUpNameColumn(JTable table, TableColumn nameColumn, Set names) @@ -584,6 +599,27 @@ public class ObjectMapProperties extends PropertyPanel setUpNameColumn(table, table.getColumnModel().getColumn(1), names); + table.addMouseListener(new MouseAdapter() + { + @Override + public void mouseClicked(MouseEvent me) + { + if (SwingUtilities.isRightMouseButton(me) == true) + { + int row = table.rowAtPoint(me.getPoint()); + if (row != -1) + { + table.setRowSelectionInterval(row, row); + if (me.getComponent() instanceof JTable ) + { + JPopupMenu popup = createPopupMenu(objectTablePanel); + popup.show(me.getComponent(), me.getX(), me.getY()); + } + } + } + } + }); + add(scrollPane); } @@ -592,6 +628,105 @@ public class ObjectMapProperties extends PropertyPanel model.fireTableDataChanged(); } } + + public JPopupMenu createPopupMenu(ObjectTablePanel panel) + { + JPopupMenu popupMenu = new JPopupMenu(); + JMenuItem deleteItem = new JMenuItem("Delete Object"); + JMenuItem deleteAllColorItem = new JMenuItem("Delete All Objects With This Color"); + JMenuItem deleteAllNameItem = new JMenuItem("Delete All Objects With This Name"); + + deleteItem.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + int row = panel.table.getSelectedRow(); + if (row != -1) + { + if (JOptionPane.showConfirmDialog(null, "Delete this object?", "Delete Object", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + panel.model.removeRowAt(panel.table.convertRowIndexToModel(row)); + } + } + } + }); + deleteAllColorItem.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + int row = panel.table.getSelectedRow(); + if (row != -1) + { + if (JOptionPane.showConfirmDialog(null, "Delete all objects with this color?", "Delete Objects With Color", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + int color = data.elementAt(panel.table.convertRowIndexToModel(row)).color; + Vector toDelete = new Vector(); + for (int i = 0; i < data.size(); i++) + { + if (color == data.elementAt(i).color) + { + toDelete.add(i); + } + } + Collections.sort(toDelete, new Comparator() + { + @Override + public int compare(Integer o1, Integer o2) + { + // Changing the order of the elements + return o2 - o1; + } + }); + for (int i = 0; i < toDelete.size(); i++) + { + panel.model.removeRowAt(toDelete.elementAt(i)); + } + } + } + } + }); + deleteAllNameItem.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + int row = panel.table.getSelectedRow(); + if (row != -1) + { + if (JOptionPane.showConfirmDialog(null, "Delete all objects with this name?", "Delete Objects with Name", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + String name = new String(data.elementAt(panel.table.convertRowIndexToModel(row)).name); + Vector toDelete = new Vector(); + for (int i = 0; i < data.size(); i++) + { + if (name.equals(data.elementAt(i).name)) + { + toDelete.add(i); + } + } + Collections.sort(toDelete, new Comparator() + { + @Override + public int compare(Integer o1, Integer o2) + { + // Changing the order of the elements + return o2 - o1; + } + }); + for (int i = 0; i < toDelete.size(); i++) + { + panel.model.removeRowAt(toDelete.elementAt(i)); + } + } + } + } + }); + + popupMenu.add(deleteItem); + popupMenu.add(deleteAllColorItem); + popupMenu.add(deleteAllNameItem); + + return popupMenu; + } } public void exit() diff --git a/src/tools/trackeditor/gui/properties/ReliefProperties.java b/src/tools/trackeditor/gui/properties/ReliefProperties.java index 757b4a30..649e1fd3 100644 --- a/src/tools/trackeditor/gui/properties/ReliefProperties.java +++ b/src/tools/trackeditor/gui/properties/ReliefProperties.java @@ -314,11 +314,12 @@ public class ReliefProperties extends PropertyPanel { public void actionPerformed(ActionEvent e) { - if (table.getSelectedRow() != -1) + int row = table.getSelectedRow(); + if (row != -1) { if (JOptionPane.showConfirmDialog(null, "Delete this row?", "Delete Row", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { - model.removeRowAt(table.getSelectedRow()); + model.removeRowAt(table.convertRowIndexToModel(row)); } } } @@ -328,11 +329,6 @@ public class ReliefProperties extends PropertyPanel add(scrollPane); } - - void dataChanged() - { - model.fireTableDataChanged(); - } } } diff --git a/src/tools/trackeditor/utils/Properties.java b/src/tools/trackeditor/utils/Properties.java index c1da80c7..d9cfac50 100644 --- a/src/tools/trackeditor/utils/Properties.java +++ b/src/tools/trackeditor/utils/Properties.java @@ -34,7 +34,7 @@ public class Properties private static Properties instance = new Properties(); private Vector propertiesListeners = new Vector(); public final String title = "sd2-trackeditor"; - public final String version = "1.2.17"; + public final String version = "1.2.18"; private String path; private double imageScale = 1;