From 39100f76f6f2d00f80f6753948b5d89ca2fd26b9 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 17 Nov 2022 17:34:59 +0100 Subject: [PATCH 1/3] Remove redundant row query --- .../src/net/sf/openrocket/gui/main/SimulationPanel.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index 04c657986..abc649ddd 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -208,13 +208,10 @@ public class SimulationPanel extends JPanel { } // Show context menu else if (e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() == 1) { - // Get the row that the right-click action happened on - int r = simulationTable.rowAtPoint(e.getPoint()); - // Select new row - if (!simulationTable.isRowSelected(r)) { - if (r >= 0 && r < simulationTable.getRowCount()) { - simulationTable.setRowSelectionInterval(r, r); + if (!simulationTable.isRowSelected(row)) { + if (row >= 0 && row < simulationTable.getRowCount()) { + simulationTable.setRowSelectionInterval(row, row); } else { return; } From 6f2cbf31ae3a2e8a71e84cf620a4e2b49b50d141 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 17 Nov 2022 17:35:27 +0100 Subject: [PATCH 2/3] [#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); + } + } } From 6bd2ac05aa6c13ffe73ba38cf7a7a1816bc61ddd Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 17 Nov 2022 19:17:09 +0100 Subject: [PATCH 3/3] Add insert fin point context menu action --- core/resources/l10n/messages.properties | 1 + .../configdialog/FreeformFinSetConfig.java | 52 ++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index f2f28d500..3609b0d06 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -1031,6 +1031,7 @@ FreeformFinSetConfig.lbl.ctrlClick = Ctrl+click: Delete point FreeformFinSetConfig.lbl.scaleFin = Scale Fin FreeformFinSetConfig.lbl.exportCSV = Export CSV FreeformFinSetConfig.lbl.deletePoint = Delete point +FreeformFinSetConfig.lbl.insertPoint = Insert 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 fd737b6e3..13a6c7a57 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/FreeformFinSetConfig.java @@ -91,6 +91,7 @@ public class FreeformFinSetConfig extends FinSetConfig { private ScaleScrollPane figurePane = null; private ScaleSelector selector; + private FinPointAction insertFinPointAction; private FinPointAction deleteFinPointAction; public FreeformFinSetConfig(OpenRocketDocument d, RocketComponent component, JDialog parent) { @@ -294,8 +295,10 @@ public class FreeformFinSetConfig extends FinSetConfig { }); // Context menu for table + insertFinPointAction = new InsertPointAction(); deleteFinPointAction = new DeletePointAction(); pm = new JPopupMenu(); + pm.add(insertFinPointAction); pm.add(deleteFinPointAction); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @@ -457,16 +460,32 @@ public class FreeformFinSetConfig extends FinSetConfig { } /** - * Delete the selected point in the fin point table. + * Insert a new fin point between the currently selected point and the next point. + * The coordinates of the new point will be the average of the two points. + */ + private void insertPoint() { + int currentPointIdx = table.getSelectedRow(); + if (currentPointIdx == -1 || currentPointIdx >= table.getRowCount() - 1) { + return; + } + final FreeformFinSet finSet = (FreeformFinSet) component; + Coordinate currentPoint = finSet.getFinPoints()[currentPointIdx]; + Coordinate nextPoint = finSet.getFinPoints()[currentPointIdx + 1]; + Point2D.Double toAdd = new Point2D.Double((currentPoint.x + nextPoint.x) / 2, (currentPoint.y + nextPoint.y) / 2); + finSet.addPoint(currentPointIdx + 1, toAdd); + } + + /** + * Delete the currently selected fin point. */ private void deletePoint() { - int row = table.getSelectedRow(); - if (row == -1) { + int currentPointIdx = table.getSelectedRow(); + if (currentPointIdx == -1) { return; } final FreeformFinSet finSet = (FreeformFinSet) component; try { - finSet.removePoint(row); + finSet.removePoint(currentPointIdx); } catch (IllegalFinPointException ex) { throw new RuntimeException(ex); } @@ -477,9 +496,12 @@ public class FreeformFinSetConfig extends FinSetConfig { } private void updateActionStates() { - if (deleteFinPointAction != null) { - deleteFinPointAction.updateEnabledState(); + if (insertFinPointAction == null) { // If one of the actions is null, the rest will be too + return; } + + insertFinPointAction.updateEnabledState(); + deleteFinPointAction.updateEnabledState(); } private class FinPointScrollPane extends ScaleScrollPane { @@ -767,6 +789,24 @@ public class FreeformFinSetConfig extends FinSetConfig { public abstract void updateEnabledState(); } + private class InsertPointAction extends FinPointAction { + public InsertPointAction() { + putValue(NAME, trans.get("FreeformFinSetConfig.lbl.insertPoint")); + this.putValue(SMALL_ICON, Icons.FILE_NEW); + } + + @Override + public void actionPerformed(ActionEvent e) { + insertPoint(); + } + + @Override + public void updateEnabledState() { + // You can't add to the last fin point + setEnabled(table.getSelectedRow() < table.getRowCount() - 1); + } + } + private class DeletePointAction extends FinPointAction { public DeletePointAction() { putValue(NAME, trans.get("FreeformFinSetConfig.lbl.deletePoint"));