Merge pull request #1471 from SiboVG/issue-1054

[fixes #1054] Better register of double-click in 3D view
This commit is contained in:
SiboVG 2022-06-23 00:11:58 +02:00 committed by GitHub
commit 9350dbe1a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 198 additions and 40 deletions

View File

@ -33,6 +33,8 @@ RocketActions.CopyAct.Copy = Copy
RocketActions.CopyAct.ttip.Copy = Copy this component (and subcomponents) to the clipboard.
RocketActions.PasteAct.Paste = Paste
RocketActions.PasteAct.ttip.Paste = Paste the component or simulation on the clipboard to the design.
RocketActions.PasteAct.invalidPosition.msg = Invalid paste position for object '%s', ignoring pasting.
RocketActions.PasteAct.invalidPosition.title = Could not paste
RocketActions.DuplicateAct.Duplicate = Duplicate
RocketActions.DuplicateAct.ttip.Duplicate = Duplicate this component (and subcomponents).
RocketActions.EditAct.Edit = Edit

View File

@ -303,12 +303,34 @@ public class BasicFrame extends JFrame {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if (selRow != -1) {
if (selPath == null) return;
// Double-click
if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2) && !ComponentConfigDialog.isDialogVisible()) {
// Double-click
RocketComponent c = (RocketComponent) selPath.getLastPathComponent();
ComponentConfigDialog.showDialog(BasicFrame.this,
BasicFrame.this.document, c);
} else if ((e.getButton() == MouseEvent.BUTTON3) && (e.getClickCount() == 1)) {
RocketComponent component = (RocketComponent) selPath.getLastPathComponent();
// Multi-component edit if shift/meta key is pressed
if ((e.isShiftDown() || e.isMetaDown()) && tree.getSelectionPaths() != null) {
// Add the other selected components as listeners to the last selected component
for (TreePath p : tree.getSelectionPaths()) {
if (p != null) {
if (p.getLastPathComponent() == component) continue;
RocketComponent c = (RocketComponent) p.getLastPathComponent();
c.clearConfigListeners();
component.addConfigListener(c);
}
}
// Add the selection path to the tree selection
List<TreePath> paths = new LinkedList<>(Arrays.asList(tree.getSelectionPaths()));
paths.add(selPath);
tree.setSelectionPaths(paths.toArray(new TreePath[0]));
}
ComponentConfigDialog.showDialog(BasicFrame.this, BasicFrame.this.document, component);
}
// Context menu
else if ((e.getButton() == MouseEvent.BUTTON3) && (e.getClickCount() == 1)) {
if (!tree.isPathSelected(selPath)) {
// Select new path
tree.setSelectionPath(selPath);
@ -316,7 +338,7 @@ public class BasicFrame extends JFrame {
doComponentTreePopup(e);
}
} else {
} else { // Clicked on blank space
tree.clearSelection();
}
}

View File

@ -5,6 +5,7 @@ import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
@ -270,6 +271,15 @@ public class RocketActions {
RocketComponent originalParent = components.get(i).getParent();
int originalParentIdx = components.indexOf(originalParent);
result.get(originalParentIdx).addChild(result.get(i));
} else if (RocketComponent.listContainsParent(components, components.get(i))){
RocketComponent originalParent = components.get(i);
while (originalParent != components.get(i)) {
if (components.contains(originalParent.getParent())) {
originalParent = originalParent.getParent();
}
}
int originalParentIdx = components.indexOf(originalParent);
result.get(originalParentIdx).addChild(result.get(i));
}
}
@ -378,24 +388,41 @@ public class RocketActions {
* should be pasted. Returns null if the clipboard is empty or if the
* clipboard cannot be pasted to the current selection.
*
* @param clipboard the component on the clipboard.
* @param copyComponent the component to be copy-pasted.
* @param pasteComponent the component where copyComponent should be pasted to.
* @return a Pair with both components defined, or null.
*/
private Pair<RocketComponent, Integer> getPastePosition(RocketComponent clipboard) {
private Pair<RocketComponent, Integer> getPastePosition(RocketComponent copyComponent, RocketComponent pasteComponent) {
if (pasteComponent == null)
return null;
if (copyComponent == null)
return null;
if (pasteComponent.isCompatible(copyComponent))
return new Pair<RocketComponent, Integer>(pasteComponent, pasteComponent.getChildCount());
RocketComponent parent = pasteComponent.getParent();
return getPastePositionFromParent(copyComponent, parent);
}
/**
* Return the component and position to which the current clipboard
* should be pasted. Returns null if the clipboard is empty or if the
* clipboard cannot be pasted to the current selection.
*
* @param copyComponent the component to be copy-pasted.
* @return a Pair with both components defined, or null.
*/
private Pair<RocketComponent, Integer> getPastePosition(RocketComponent copyComponent) {
RocketComponent selected = selectionModel.getSelectedComponent();
if (selected == null)
return null;
return getPastePosition(copyComponent, selected);
}
if (clipboard == null)
return null;
if (selected.isCompatible(clipboard))
return new Pair<RocketComponent, Integer>(selected, selected.getChildCount());
RocketComponent parent = selected.getParent();
if (parent != null && parent.isCompatible(clipboard)) {
int index = parent.getChildPosition(selected) + 1;
return new Pair<RocketComponent, Integer>(parent, index);
private Pair<RocketComponent, Integer> getPastePositionFromParent(RocketComponent component, RocketComponent parent) {
if (parent != null && parent.isCompatible(component)) {
int index = parent.getChildPosition(parent) + 1;
return new Pair<>(parent, index);
}
return null;
@ -727,11 +754,20 @@ public class RocketActions {
document.addUndoPosition("Paste components");
}
List<RocketComponent> successfullyPasted = new LinkedList<>();
for (int i = 0; i < pasted.size(); i++) {
positions.get(i).getU().addChild(pasted.get(i), positions.get(i).getV());
if (positions.get(i) == null) {
JOptionPane.showMessageDialog(null,
String.format(trans.get("RocketActions.PasteAct.invalidPosition.msg"),
pasted.get(i).getComponentName()),
trans.get("RocketActions.PasteAct.invalidPosition.title"), JOptionPane.WARNING_MESSAGE);
} else {
positions.get(i).getU().addChild(pasted.get(i), positions.get(i).getV());
successfullyPasted.add(pasted.get(i));
}
}
selectionModel.setSelectedComponents(pasted);
selectionModel.setSelectedComponents(successfullyPasted);
parentFrame.selectTab(BasicFrame.COMPONENT_TAB);
@ -805,7 +841,14 @@ public class RocketActions {
List<Pair<RocketComponent, Integer>> positions = new LinkedList<>();
for (RocketComponent component : duplicateComponents) {
positions.add(getPastePosition(component));
Pair<RocketComponent, Integer> pos;
if (RocketComponent.listContainsParent(duplicateComponents, component)) {
pos = getPastePosition(component, component.getParent());
} else {
RocketComponent pasteParent = components.get(duplicateComponents.indexOf(component)).getParent();
pos = getPastePosition(component, pasteParent);
}
positions.add(pos);
}
if (duplicateComponents.size() == 1) {
@ -814,6 +857,9 @@ public class RocketActions {
document.addUndoPosition("Duplicate components");
}
Collections.reverse(duplicateComponents);
Collections.reverse(positions);
for (int i = 0; i < duplicateComponents.size(); i++) {
positions.get(i).getU().addChild(duplicateComponents.get(i), positions.get(i).getV());
}

View File

@ -5,6 +5,7 @@ import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
@ -15,6 +16,7 @@ import java.util.EventListener;
import java.util.EventObject;
import java.util.List;
import java.util.LinkedList;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@ -74,6 +76,7 @@ import net.sf.openrocket.util.Chars;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.MathUtil;
import net.sf.openrocket.util.StateChangeListener;
import net.sf.openrocket.utils.CustomClickCountListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -200,10 +203,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
scrollPane = new ScaleScrollPane(figure) {
private static final long serialVersionUID = 1L;
final CustomClickCountListener clickCountListener = new CustomClickCountListener();
@Override
public void mouseClicked(MouseEvent event) {
handleMouseClick(event);
clickCountListener.click();
handleMouseClick(event, clickCountListener.getClickCount());
}
};
scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
@ -237,9 +242,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
});
figure3d.addComponentSelectionListener(new RocketFigure3d.ComponentSelectionListener() {
final CustomClickCountListener clickCountListener = new CustomClickCountListener();
@Override
public void componentClicked(RocketComponent clicked[], MouseEvent event) {
handleComponentClick(clicked, event);
public void componentClicked(RocketComponent[] clicked, MouseEvent event) {
clickCountListener.click();
handleComponentClick(clicked, event, clickCountListener.getClickCount());
}
});
}
@ -551,7 +559,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
*/
public static final int CYCLE_SELECTION_MODIFIER = InputEvent.SHIFT_DOWN_MASK;
private void handleMouseClick(MouseEvent event) {
private void handleMouseClick(MouseEvent event, int clickCount) {
// Get the component that is clicked on
Point p0 = event.getPoint();
Point p1 = scrollPane.getViewport().getViewPosition();
@ -567,7 +575,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
}
if (event.getButton() == MouseEvent.BUTTON1) {
handleComponentClick(clicked, event);
handleComponentClick(clicked, event, clickCount);
} else if (event.getButton() == MouseEvent.BUTTON3) {
List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
@ -593,14 +601,58 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
}
}
private void handleComponentClick(RocketComponent[] clicked, MouseEvent event) {
private void handleComponentClick(RocketComponent[] clicked, MouseEvent event, int clickCount) {
List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
if (clicked == null || clicked.length == 0) return;
// Check for double-click.
// If the shift/meta key is not pressed and the component was not already selected, ignore the double click and treat it as a single click
if (clickCount == 2) {
if (event.isShiftDown() || event.isMetaDown()) {
List<TreePath> paths = new ArrayList<>(Arrays.asList(selectionModel.getSelectionPaths()));
RocketComponent component = selectedComponents.get(selectedComponents.size() - 1);
// Make sure the clicked component is selected
for (RocketComponent c : clicked) {
if (!selectedComponents.contains(c)) {
TreePath path = ComponentTreeModel.makeTreePath(c);
paths.add(path);
selectionModel.setSelectionPaths(paths.toArray(new TreePath[0]));
selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
.map(c1 -> (RocketComponent) c1.getLastPathComponent()).collect(Collectors.toList());
component = c;
break;
}
}
// Multi-component edit if shift/meta key is pressed
for (RocketComponent c : selectedComponents) {
if (c == component) continue;
c.clearConfigListeners();
component.addConfigListener(c);
}
ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this), document, component);
}
// Normal double click (no shift or meta key)
else {
if (!selectedComponents.contains(clicked[0])) {
clickCount = 1;
} else {
TreePath path = ComponentTreeModel.makeTreePath(clicked[0]);
selectionModel.setSelectionPath(path); // Revert to single selection
RocketComponent component = (RocketComponent) path.getLastPathComponent();
ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),
document, component);
return;
}
}
}
// If the shift-button is held, add a newly clicked component to the selection path
if ((event.isShiftDown() || event.isMetaDown()) && event.getClickCount() == 1) {
if (clickCount == 1 && (event.isShiftDown() || event.isMetaDown())) {
List<TreePath> paths = new ArrayList<>(Arrays.asList(selectionModel.getSelectionPaths()));
for (int i = 0; i < clicked.length; i++) {
if (!selectedComponents.contains(clicked[i])) {
@ -622,16 +674,6 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
selectionModel.setSelectionPath(path);
}
}
// Check for double-click
if (event.getClickCount() == 2) {
TreePath path = ComponentTreeModel.makeTreePath(clicked[0]);
selectionModel.setSelectionPath(path); // Revert to single selection
RocketComponent component = (RocketComponent) path.getLastPathComponent();
ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),
document, component);
}
}
/**

View File

@ -0,0 +1,46 @@
package net.sf.openrocket.utils;
import java.util.TimerTask;
/**
* This class is a custom implementation of the mouse click count listener, where you can choose the maximum
* interval between two clicks for them to still be registered as a double click.
*
* @author Sibo Van Gool <sibo.vangool@hotmail.com>
*/
public class CustomClickCountListener {
private final int CLICK_INTERVAL; // Maximum interval between two clicks for them to still be registered as a double click (in ms)
private int clickCnt = 0;
private final java.util.Timer timer = new java.util.Timer("doubleClickTimer", false);
public CustomClickCountListener() {
this.CLICK_INTERVAL = 600; // ms
}
public CustomClickCountListener(int clickInterval) {
this.CLICK_INTERVAL = clickInterval;
}
/**
* Call this method when the mouseClicked event is activated.
*/
public void click() {
clickCnt++;
if (clickCnt == 1) {
timer.schedule(new TimerTask() {
@Override
public void run() {
clickCnt = 0;
}
}, CLICK_INTERVAL);
}
}
/**
* Return the current click count.
* @return the current click count
*/
public int getClickCount() {
return clickCnt;
}
}