From 6f2cbf31ae3a2e8a71e84cf620a4e2b49b50d141 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 17 Nov 2022 17:35:27 +0100 Subject: [PATCH] [#1840] Add context menu for fin point deletion --- core/resources/l10n/messages.properties | 1 + .../configdialog/FreeformFinSetConfig.java | 95 ++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index d1462bf8a..f2f28d500 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -1030,6 +1030,7 @@ FreeformFinSetConfig.lbl.clickDrag = Click+drag: Add and move points FreeformFinSetConfig.lbl.ctrlClick = Ctrl+click: Delete point FreeformFinSetConfig.lbl.scaleFin = Scale Fin FreeformFinSetConfig.lbl.exportCSV = Export CSV +FreeformFinSetConfig.lbl.deletePoint = Delete point !TubeFinSetConfig TubeFinSetCfg.lbl.Nbroffins = Number of fins: diff --git a/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java index f3777b3b0..fd737b6e3 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.EventObject; import java.util.List; +import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; @@ -26,6 +27,7 @@ import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JSpinner; @@ -33,9 +35,12 @@ import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; import net.sf.openrocket.gui.adaptors.CustomFocusTraversalPolicy; +import net.sf.openrocket.gui.util.Icons; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,6 +82,7 @@ public class FreeformFinSetConfig extends FinSetConfig { private JTable table = null; private FinPointTableModel tableModel = null; + private JPopupMenu pm; private int dragIndex = -1; private Point dragPoint = null; @@ -84,6 +90,8 @@ public class FreeformFinSetConfig extends FinSetConfig { private FinPointFigure figure = null; private ScaleScrollPane figurePane = null; private ScaleSelector selector; + + private FinPointAction deleteFinPointAction; public FreeformFinSetConfig(OpenRocketDocument d, RocketComponent component, JDialog parent) { super(d, component, parent); @@ -246,11 +254,31 @@ public class FreeformFinSetConfig extends FinSetConfig { } table.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent ev) { + public void mouseClicked(MouseEvent e) { + int row = table.rowAtPoint(e.getPoint()); + + // Context menu on right-click + if (e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() == 1) { + // Select new row + if (!table.isRowSelected(row)) { + if (row >= 0 && row < table.getRowCount()) { + table.setRowSelectionInterval(row, row); + } else { + return; + } + } + + doPopup(e); + } + } + + }); + table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { figure.setSelectedIndex(table.getSelectedRow()); figure.updateFigure(); } - }); JScrollPane tablePane = new JScrollPane(table); @@ -264,6 +292,19 @@ public class FreeformFinSetConfig extends FinSetConfig { dialog.dispose(); } }); + + // Context menu for table + deleteFinPointAction = new DeletePointAction(); + pm = new JPopupMenu(); + pm.add(deleteFinPointAction); + + table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + updateActionStates(); + } + }); + // panel.add(new JLabel("Coordinates:"), "aligny bottom, alignx 50%"); // panel.add(new JLabel(" View:"), "wrap, aligny bottom"); @@ -414,6 +455,32 @@ public class FreeformFinSetConfig extends FinSetConfig { figurePane.revalidate(); } } + + /** + * Delete the selected point in the fin point table. + */ + private void deletePoint() { + int row = table.getSelectedRow(); + if (row == -1) { + return; + } + final FreeformFinSet finSet = (FreeformFinSet) component; + try { + finSet.removePoint(row); + } catch (IllegalFinPointException ex) { + throw new RuntimeException(ex); + } + } + + private void doPopup(MouseEvent e) { + pm.show(e.getComponent(), e.getX(), e.getY()); + } + + private void updateActionStates() { + if (deleteFinPointAction != null) { + deleteFinPointAction.updateEnabledState(); + } + } private class FinPointScrollPane extends ScaleScrollPane { @@ -693,4 +760,28 @@ public class FreeformFinSetConfig extends FinSetConfig { } } } + + private abstract static class FinPointAction extends AbstractAction { + private static final long serialVersionUID = 1L; + + public abstract void updateEnabledState(); + } + + private class DeletePointAction extends FinPointAction { + public DeletePointAction() { + putValue(NAME, trans.get("FreeformFinSetConfig.lbl.deletePoint")); + this.putValue(SMALL_ICON, Icons.EDIT_DELETE); + } + + @Override + public void actionPerformed(ActionEvent e) { + deletePoint(); + } + + @Override + public void updateEnabledState() { + // You can't delete the first or last fin point + setEnabled(table.getSelectedRow() > 0 && table.getSelectedRow() < table.getRowCount() - 1); + } + } }