Merge pull request #1845 from SiboVG/issue-1840

[#1840] Add context menu for fin point deletion
This commit is contained in:
Sibo Van Gool 2022-11-18 19:10:35 +01:00 committed by GitHub
commit d871e504ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 8 deletions

View File

@ -1031,6 +1031,8 @@ 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
FreeformFinSetConfig.lbl.insertPoint = Insert point
!TubeFinSetConfig
TubeFinSetCfg.lbl.Nbroffins = Number of fins:

View File

@ -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;
@ -34,9 +36,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;
@ -78,6 +83,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;
@ -85,6 +91,9 @@ public class FreeformFinSetConfig extends FinSetConfig {
private FinPointFigure figure = null;
private ScaleScrollPane figurePane = null;
private ScaleSelector selector;
private FinPointAction insertFinPointAction;
private FinPointAction deleteFinPointAction;
public FreeformFinSetConfig(OpenRocketDocument d, RocketComponent component, JDialog parent) {
super(d, component, parent);
@ -247,11 +256,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);
@ -265,6 +294,21 @@ public class FreeformFinSetConfig extends FinSetConfig {
dialog.dispose();
}
});
// Context menu for table
insertFinPointAction = new InsertPointAction();
deleteFinPointAction = new DeletePointAction();
pm = new JPopupMenu();
pm.add(insertFinPointAction);
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");
@ -417,6 +461,51 @@ public class FreeformFinSetConfig extends FinSetConfig {
figurePane.revalidate();
}
}
/**
* 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 currentPointIdx = table.getSelectedRow();
if (currentPointIdx == -1) {
return;
}
final FreeformFinSet finSet = (FreeformFinSet) component;
try {
finSet.removePoint(currentPointIdx);
} catch (IllegalFinPointException ex) {
throw new RuntimeException(ex);
}
}
private void doPopup(MouseEvent e) {
pm.show(e.getComponent(), e.getX(), e.getY());
}
private void updateActionStates() {
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 {
@ -696,4 +785,46 @@ public class FreeformFinSetConfig extends FinSetConfig {
}
}
}
private abstract static class FinPointAction extends AbstractAction {
private static final long serialVersionUID = 1L;
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"));
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);
}
}
}

View File

@ -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;
}