Move selected components with arrow keys (and ctrl/shift)

This commit is contained in:
bancika
2012-08-17 10:41:19 +00:00
parent 4cf6f9f4a5
commit a6448676f7
3 changed files with 80 additions and 36 deletions
@@ -9,6 +9,8 @@ import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
@@ -92,13 +94,20 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
canvasPanel = new CanvasPanel(plugInPort); canvasPanel = new CanvasPanel(plugInPort);
canvasPanel.addMouseListener(new MouseAdapter() { canvasPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
canvasPanel.requestFocus();
}
@Override @Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
if (plugInPort.getNewComponentTypeSlot() != null) { if (plugInPort.getNewComponentTypeSlot() != null) {
plugInPort.mouseClicked(e.getPoint(), e.getButton(), false, false, false, 1); plugInPort.mouseClicked(e.getPoint(), e.getButton(),
false, false, false, 1);
} else if (e.isPopupTrigger()) { } else if (e.isPopupTrigger()) {
// Enable actions. // Enable actions.
boolean enabled = !plugInPort.getSelectedComponents().isEmpty(); boolean enabled = !plugInPort.getSelectedComponents()
.isEmpty();
getCutAction().setEnabled(enabled); getCutAction().setEnabled(enabled);
getCopyAction().setEnabled(enabled); getCopyAction().setEnabled(enabled);
try { try {
@@ -123,25 +132,38 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
} }
} }
}); });
canvasPanel.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (plugInPort.keyPressed(e.getKeyCode(),
e.isControlDown(), e.isShiftDown(), e.isAltDown())) {
e.consume();
}
}
});
} }
return canvasPanel; return canvasPanel;
} }
private RulerScrollPane getScrollPane() { private RulerScrollPane getScrollPane() {
if (scrollPane == null) { if (scrollPane == null) {
scrollPane = new RulerScrollPane(getCanvasPanel(), new ProjectDrawingProvider( scrollPane = new RulerScrollPane(getCanvasPanel(),
plugInPort, true, false), new Size(1d, SizeUnit.cm).convertToPixels(), new ProjectDrawingProvider(plugInPort, true, false),
new Size(1d, SizeUnit.in).convertToPixels()); new Size(1d, SizeUnit.cm).convertToPixels(), new Size(1d,
boolean metric = ConfigurationManager.getInstance().readBoolean(Presenter.METRIC_KEY, SizeUnit.in).convertToPixels());
true); boolean metric = ConfigurationManager.getInstance().readBoolean(
Presenter.METRIC_KEY, true);
boolean wheelZoom = ConfigurationManager.getInstance().readBoolean( boolean wheelZoom = ConfigurationManager.getInstance().readBoolean(
IPlugInPort.WHEEL_ZOOM_KEY, false); IPlugInPort.WHEEL_ZOOM_KEY, false);
ConfigurationManager.getInstance().addConfigListener(IPlugInPort.WHEEL_ZOOM_KEY, ConfigurationManager.getInstance().addConfigListener(
new IConfigListener() { IPlugInPort.WHEEL_ZOOM_KEY, new IConfigListener() {
@Override @Override
public void valueChanged(String key, Object value) { public void valueChanged(String key, Object value) {
scrollPane.setWheelScrollingEnabled(!(Boolean) value); scrollPane
.setWheelScrollingEnabled(!(Boolean) value);
} }
}); });
scrollPane.setMetric(metric); scrollPane.setMetric(metric);
@@ -157,13 +179,14 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
@Override @Override
public void mouseWheelMoved(MouseWheelEvent e) { public void mouseWheelMoved(MouseWheelEvent e) {
boolean wheelZoom = ConfigurationManager.getInstance().readBoolean( boolean wheelZoom = ConfigurationManager.getInstance()
IPlugInPort.WHEEL_ZOOM_KEY, false); .readBoolean(IPlugInPort.WHEEL_ZOOM_KEY, false);
if (!wheelZoom) { if (!wheelZoom) {
return; return;
} }
double d = plugInPort.getZoomLevel(); double d = plugInPort.getZoomLevel();
Double[] availableZoomLevels = plugInPort.getAvailableZoomLevels(); Double[] availableZoomLevels = plugInPort
.getAvailableZoomLevels();
if (e.getWheelRotation() > 0) { if (e.getWheelRotation() > 0) {
int i = availableZoomLevels.length - 1; int i = availableZoomLevels.length - 1;
while (i > 0 && availableZoomLevels[i] >= d) { while (i > 0 && availableZoomLevels[i] >= d) {
@@ -172,7 +195,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
plugInPort.setZoomLevel(availableZoomLevels[i]); plugInPort.setZoomLevel(availableZoomLevels[i]);
} else { } else {
int i = 0; int i = 0;
while (i < availableZoomLevels.length - 1 && availableZoomLevels[i] <= d) { while (i < availableZoomLevels.length - 1
&& availableZoomLevels[i] <= d) {
i++; i++;
} }
plugInPort.setZoomLevel(availableZoomLevels[i]); plugInPort.setZoomLevel(availableZoomLevels[i]);
@@ -205,7 +229,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
popupMenu.add(getBringToFrontAction()); popupMenu.add(getBringToFrontAction());
popupMenu.add(getExpandMenu()); popupMenu.add(getExpandMenu());
popupMenu.addSeparator(); popupMenu.addSeparator();
popupMenu.add(ActionFactory.getInstance().createEditProjectAction(plugInPort)); popupMenu.add(ActionFactory.getInstance().createEditProjectAction(
plugInPort));
} }
return popupMenu; return popupMenu;
} }
@@ -221,7 +246,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
public JMenu getExpandMenu() { public JMenu getExpandMenu() {
if (expandMenu == null) { if (expandMenu == null) {
expandMenu = new JMenu("Expand Selection"); expandMenu = new JMenu("Expand Selection");
expandMenu.setIcon(IconLoader.BranchAdd.getIcon()); expandMenu.setIcon(IconLoader.BranchAdd.getIcon());
expandMenu.add(getExpandSelectionAllAction()); expandMenu.add(getExpandSelectionAllAction());
expandMenu.add(getExpandSelectionImmediateAction()); expandMenu.add(getExpandSelectionImmediateAction());
expandMenu.add(getExpandSelectionSameTypeAction()); expandMenu.add(getExpandSelectionSameTypeAction());
@@ -231,7 +256,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private void updateSelectionMenu(int x, int y) { private void updateSelectionMenu(int x, int y) {
getSelectionMenu().removeAll(); getSelectionMenu().removeAll();
for (IDIYComponent<?> component : plugInPort.findComponentsAt(new Point(x, y))) { for (IDIYComponent<?> component : plugInPort
.findComponentsAt(new Point(x, y))) {
JMenuItem item = new JMenuItem(component.getName()); JMenuItem item = new JMenuItem(component.getName());
final IDIYComponent<?> finalComponent = component; final IDIYComponent<?> finalComponent = component;
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@@ -250,72 +276,80 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
public ActionFactory.CutAction getCutAction() { public ActionFactory.CutAction getCutAction() {
if (cutAction == null) { if (cutAction == null) {
cutAction = ActionFactory.getInstance().createCutAction(plugInPort, clipboard, this); cutAction = ActionFactory.getInstance().createCutAction(plugInPort,
clipboard, this);
} }
return cutAction; return cutAction;
} }
public ActionFactory.CopyAction getCopyAction() { public ActionFactory.CopyAction getCopyAction() {
if (copyAction == null) { if (copyAction == null) {
copyAction = ActionFactory.getInstance().createCopyAction(plugInPort, clipboard, this); copyAction = ActionFactory.getInstance().createCopyAction(
plugInPort, clipboard, this);
} }
return copyAction; return copyAction;
} }
public ActionFactory.PasteAction getPasteAction() { public ActionFactory.PasteAction getPasteAction() {
if (pasteAction == null) { if (pasteAction == null) {
pasteAction = ActionFactory.getInstance().createPasteAction(plugInPort, clipboard); pasteAction = ActionFactory.getInstance().createPasteAction(
plugInPort, clipboard);
} }
return pasteAction; return pasteAction;
} }
public ActionFactory.EditSelectionAction getEditSelectionAction() { public ActionFactory.EditSelectionAction getEditSelectionAction() {
if (editSelectionAction == null) { if (editSelectionAction == null) {
editSelectionAction = ActionFactory.getInstance().createEditSelectionAction(plugInPort); editSelectionAction = ActionFactory.getInstance()
.createEditSelectionAction(plugInPort);
} }
return editSelectionAction; return editSelectionAction;
} }
public ActionFactory.DeleteSelectionAction getDeleteSelectionAction() { public ActionFactory.DeleteSelectionAction getDeleteSelectionAction() {
if (deleteSelectionAction == null) { if (deleteSelectionAction == null) {
deleteSelectionAction = ActionFactory.getInstance().createDeleteSelectionAction( deleteSelectionAction = ActionFactory.getInstance()
plugInPort); .createDeleteSelectionAction(plugInPort);
} }
return deleteSelectionAction; return deleteSelectionAction;
} }
public ActionFactory.GroupAction getGroupAction() { public ActionFactory.GroupAction getGroupAction() {
if (groupAction == null) { if (groupAction == null) {
groupAction = ActionFactory.getInstance().createGroupAction(plugInPort); groupAction = ActionFactory.getInstance().createGroupAction(
plugInPort);
} }
return groupAction; return groupAction;
} }
public ActionFactory.UngroupAction getUngroupAction() { public ActionFactory.UngroupAction getUngroupAction() {
if (ungroupAction == null) { if (ungroupAction == null) {
ungroupAction = ActionFactory.getInstance().createUngroupAction(plugInPort); ungroupAction = ActionFactory.getInstance().createUngroupAction(
plugInPort);
} }
return ungroupAction; return ungroupAction;
} }
public ActionFactory.SendToBackAction getSendToBackAction() { public ActionFactory.SendToBackAction getSendToBackAction() {
if (sendToBackAction == null) { if (sendToBackAction == null) {
sendToBackAction = ActionFactory.getInstance().createSendToBackAction(plugInPort); sendToBackAction = ActionFactory.getInstance()
.createSendToBackAction(plugInPort);
} }
return sendToBackAction; return sendToBackAction;
} }
public ActionFactory.BringToFrontAction getBringToFrontAction() { public ActionFactory.BringToFrontAction getBringToFrontAction() {
if (bringToFrontAction == null) { if (bringToFrontAction == null) {
bringToFrontAction = ActionFactory.getInstance().createBringToFrontAction(plugInPort); bringToFrontAction = ActionFactory.getInstance()
.createBringToFrontAction(plugInPort);
} }
return bringToFrontAction; return bringToFrontAction;
} }
public ActionFactory.ExpandSelectionAction getExpandSelectionAllAction() { public ActionFactory.ExpandSelectionAction getExpandSelectionAllAction() {
if (expandSelectionAllAction == null) { if (expandSelectionAllAction == null) {
expandSelectionAllAction = ActionFactory.getInstance().createExpandSelectionAction( expandSelectionAllAction = ActionFactory.getInstance()
plugInPort, ExpansionMode.ALL); .createExpandSelectionAction(plugInPort, ExpansionMode.ALL);
} }
return expandSelectionAllAction; return expandSelectionAllAction;
} }
@@ -323,7 +357,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
public ActionFactory.ExpandSelectionAction getExpandSelectionImmediateAction() { public ActionFactory.ExpandSelectionAction getExpandSelectionImmediateAction() {
if (expandSelectionImmediateAction == null) { if (expandSelectionImmediateAction == null) {
expandSelectionImmediateAction = ActionFactory.getInstance() expandSelectionImmediateAction = ActionFactory.getInstance()
.createExpandSelectionAction(plugInPort, ExpansionMode.IMMEDIATE); .createExpandSelectionAction(plugInPort,
ExpansionMode.IMMEDIATE);
} }
return expandSelectionImmediateAction; return expandSelectionImmediateAction;
} }
@@ -331,14 +366,16 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
public ActionFactory.ExpandSelectionAction getExpandSelectionSameTypeAction() { public ActionFactory.ExpandSelectionAction getExpandSelectionSameTypeAction() {
if (expandSelectionSameTypeAction == null) { if (expandSelectionSameTypeAction == null) {
expandSelectionSameTypeAction = ActionFactory.getInstance() expandSelectionSameTypeAction = ActionFactory.getInstance()
.createExpandSelectionAction(plugInPort, ExpansionMode.SAME_TYPE); .createExpandSelectionAction(plugInPort,
ExpansionMode.SAME_TYPE);
} }
return expandSelectionSameTypeAction; return expandSelectionSameTypeAction;
} }
@Override @Override
public EnumSet<EventType> getSubscribedEventTypes() { public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.PROJECT_LOADED, EventType.ZOOM_CHANGED, EventType.REPAINT); return EnumSet.of(EventType.PROJECT_LOADED, EventType.ZOOM_CHANGED,
EventType.REPAINT);
} }
@Override @Override
@@ -354,7 +391,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
if ((Boolean) params[1]) { if ((Boolean) params[1]) {
// Scroll to the center. // Scroll to the center.
Rectangle visibleRect = canvasPanel.getVisibleRect(); Rectangle visibleRect = canvasPanel.getVisibleRect();
visibleRect.setLocation((canvasPanel.getWidth() - visibleRect.width) / 2, visibleRect.setLocation(
(canvasPanel.getWidth() - visibleRect.width) / 2,
(canvasPanel.getHeight() - visibleRect.height) / 2); (canvasPanel.getHeight() - visibleRect.height) / 2);
canvasPanel.scrollRectToVisible(visibleRect); canvasPanel.scrollRectToVisible(visibleRect);
canvasPanel.revalidate(); canvasPanel.revalidate();
@@ -367,7 +405,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
// one. // one.
double zoomFactor = (Double) params[0] / zoomLevel; double zoomFactor = (Double) params[0] / zoomLevel;
visibleRect.setBounds((int) (visibleRect.x * zoomFactor), visibleRect.setBounds((int) (visibleRect.x * zoomFactor),
(int) (visibleRect.y * zoomFactor), visibleRect.width, visibleRect.height); (int) (visibleRect.y * zoomFactor), visibleRect.width,
visibleRect.height);
canvasPanel.scrollRectToVisible(visibleRect); canvasPanel.scrollRectToVisible(visibleRect);
canvasPanel.revalidate(); canvasPanel.revalidate();
@@ -393,8 +432,9 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
* to the canvasPanel. * to the canvasPanel.
*/ */
public void refresh() { public void refresh() {
MouseEvent event = new MouseEvent(canvasPanel, MouseEvent.MOUSE_MOVED, System MouseEvent event = new MouseEvent(canvasPanel, MouseEvent.MOUSE_MOVED,
.currentTimeMillis(), 0, 1, 1,// canvasPanel.getWidth() / 2, System.currentTimeMillis(), 0, 1, 1,// canvasPanel.getWidth() /
// 2,
// canvasPanel.getHeight() / 2, // canvasPanel.getHeight() / 2,
0, false); 0, false);
canvasPanel.dispatchEvent(event); canvasPanel.dispatchEvent(event);
@@ -634,6 +634,10 @@
<releaseDate>2012-08-16 20:00:00.000 CET</releaseDate> <releaseDate>2012-08-16 20:00:00.000 CET</releaseDate>
<name></name> <name></name>
<changes> <changes>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Move selected components with arrow keys (and ctrl/shift)</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change> <org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType> <changeType>IMPROVEMENT</changeType>
<description>Right-click in the toolbar to select all components of the same type</description> <description>Right-click in the toolbar to select all components of the same type</description>