[#1054] Custom double click listener for design view
This commit is contained in:
parent
6e995e71ab
commit
c542b72b33
@ -5,6 +5,7 @@ import java.awt.BorderLayout;
|
|||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.InputEvent;
|
import java.awt.event.InputEvent;
|
||||||
@ -15,6 +16,7 @@ import java.util.EventListener;
|
|||||||
import java.util.EventObject;
|
import java.util.EventObject;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.TimerTask;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ThreadFactory;
|
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.Coordinate;
|
||||||
import net.sf.openrocket.util.MathUtil;
|
import net.sf.openrocket.util.MathUtil;
|
||||||
import net.sf.openrocket.util.StateChangeListener;
|
import net.sf.openrocket.util.StateChangeListener;
|
||||||
|
import net.sf.openrocket.utils.CustomClickCountListener;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -200,10 +203,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
|
|||||||
|
|
||||||
scrollPane = new ScaleScrollPane(figure) {
|
scrollPane = new ScaleScrollPane(figure) {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
final CustomClickCountListener clickCountListener = new CustomClickCountListener();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent event) {
|
public void mouseClicked(MouseEvent event) {
|
||||||
handleMouseClick(event);
|
clickCountListener.click();
|
||||||
|
handleMouseClick(event, clickCountListener.getClickCount());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
|
scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
|
||||||
@ -237,9 +242,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
|
|||||||
});
|
});
|
||||||
|
|
||||||
figure3d.addComponentSelectionListener(new RocketFigure3d.ComponentSelectionListener() {
|
figure3d.addComponentSelectionListener(new RocketFigure3d.ComponentSelectionListener() {
|
||||||
|
final CustomClickCountListener clickCountListener = new CustomClickCountListener();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void componentClicked(RocketComponent clicked[], MouseEvent event) {
|
public void componentClicked(RocketComponent[] clicked, MouseEvent event) {
|
||||||
handleComponentClick(clicked, 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;
|
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
|
// Get the component that is clicked on
|
||||||
Point p0 = event.getPoint();
|
Point p0 = event.getPoint();
|
||||||
Point p1 = scrollPane.getViewport().getViewPosition();
|
Point p1 = scrollPane.getViewport().getViewPosition();
|
||||||
@ -567,7 +575,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.getButton() == MouseEvent.BUTTON1) {
|
if (event.getButton() == MouseEvent.BUTTON1) {
|
||||||
handleComponentClick(clicked, event);
|
handleComponentClick(clicked, event, clickCount);
|
||||||
} else if (event.getButton() == MouseEvent.BUTTON3) {
|
} else if (event.getButton() == MouseEvent.BUTTON3) {
|
||||||
List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
|
List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
|
||||||
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
|
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
|
||||||
@ -593,14 +601,29 @@ 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())
|
List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
|
||||||
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
|
.map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
|
||||||
|
|
||||||
if (clicked == null || clicked.length == 0) return;
|
if (clicked == null || clicked.length == 0) return;
|
||||||
|
|
||||||
|
// Check for double-click. If the component was not already selected, ignore the double click and treat it as a single click
|
||||||
|
if (clickCount == 2) {
|
||||||
|
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 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()));
|
List<TreePath> paths = new ArrayList<>(Arrays.asList(selectionModel.getSelectionPaths()));
|
||||||
for (int i = 0; i < clicked.length; i++) {
|
for (int i = 0; i < clicked.length; i++) {
|
||||||
if (!selectedComponents.contains(clicked[i])) {
|
if (!selectedComponents.contains(clicked[i])) {
|
||||||
@ -622,16 +645,6 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
|
|||||||
selectionModel.setSelectionPath(path);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user