Skip to content

Commit

Permalink
[359] New implementation of move with arrow keys
Browse files Browse the repository at this point in the history
Bug: #359
  • Loading branch information
lredor committed Apr 26, 2024
1 parent 205de3b commit 85ee1c6
Show file tree
Hide file tree
Showing 2 changed files with 357 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 THALES GLOBAL SERVICES.
* Copyright (c) 2017, 2024 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,14 +12,23 @@
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.palette;

import java.util.Optional;

import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.internal.editparts.NoteAttachmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.internal.figures.BorderItemContainerFigure;
import org.eclipse.gmf.runtime.diagram.ui.services.palette.SelectionToolEx;
import org.eclipse.sirius.diagram.ui.edit.api.part.AbstractDiagramEdgeEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.AbstractDEdgeNameEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNodeListElementEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNodeNameEditPart;
import org.eclipse.sirius.diagram.ui.tools.internal.part.SiriusDiagramGraphicalViewer;
import org.eclipse.swt.events.KeyEvent;

/**
* Specific Sirius SelectionToolEx to use findMouseEventTargetAt instead of findObjectAtExcluding. This allows to
Expand Down Expand Up @@ -76,4 +85,93 @@ private boolean isSiriusSpecificEditPart(EditPart editPart) {
return editPart instanceof DNodeNameEditPart || editPart instanceof AbstractDiagramEdgeEditPart || editPart instanceof AbstractDEdgeNameEditPart
|| editPart instanceof DNodeListElementEditPart;
}


/**
* Method overridden to allow arrow key press with modifier.
*
* @see org.eclipse.gmf.runtime.diagram.ui.services.palette.SelectionToolEx#handleKeyDown(org.eclipse.swt.events.KeyEvent)
*/
@Override
protected boolean handleKeyDown(KeyEvent e) {
Optional<Boolean> optionalLocalresult = specificHandleKeyDown(e);
if (optionalLocalresult.isEmpty()) {
return super.handleKeyDown(e);
} else {
return optionalLocalresult.get().booleanValue();
}
}

/**
* Method inspired by org.eclipse.gmf.runtime.diagram.ui.services.palette.SelectionToolEx.handleKeyDown(KeyEvent) to
* authorize arrow keys with modifiers ({@link #acceptArrowKey(KeyEvent)} instead of
* {@link SelectionToolEx#acceptArrowKeyOnly(KeyEvent)}; Mainly for the "Alt" modifier to disable the snap.
*
* @see org.eclipse.gmf.runtime.diagram.ui.services.palette.SelectionToolEx#handleKeyDown(org.eclipse.swt.events.KeyEvent)
*/
protected Optional<Boolean> specificHandleKeyDown(KeyEvent e) {
Optional<Boolean> optionalLocalresult = Optional.empty();
if (acceptArrowKey(e) && getState() == STATE_INITIAL && !getCurrentViewer().getSelectedEditParts().isEmpty()) {

EditPart selectedEP = (EditPart) getCurrentViewer().getSelectedEditParts().get(0);

if (selectedEP instanceof GraphicalEditPart) {

GraphicalEditPart gep = (GraphicalEditPart) selectedEP;

/*
* The shape we'll be moved in the direction of the arrow key if: 1) It has the appropriate edit policy
* that supports shape moving installed on the editpart 2) The editparts figure's parent layout manager
* is some sort of XYLayout In all other cases we just change the selection based on arrow key
* (implemented in GEF).
*/
if (gep.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE) instanceof NonResizableEditPolicy && gep.getFigure().getParent() != null
&& (gep.getFigure().getParent().getLayoutManager() instanceof XYLayout || gep.getFigure().getParent() instanceof BorderItemContainerFigure)) {

resetHover();

if (getDragTracker() != null) {
getDragTracker().deactivate();
}

setState(STATE_ACCESSIBLE_DRAG_IN_PROGRESS);

setTargetEditPart(gep);

updateTargetRequest();
DragTracker dragTracker = gep.getDragTracker(getTargetRequest());
if (dragTracker != null) {
setDragTracker(dragTracker);
dragTracker.keyDown(e, getCurrentViewer());
lockTargetEditPart(gep);
optionalLocalresult = Optional.of(true);
}
optionalLocalresult = Optional.of(false);
}
}
}
return optionalLocalresult;
}

/**
* As for method {@link #handleKeyDown(KeyEvent)}, this method is overridden to "finish" the move in case of arrow
* key press.
*
* @see org.eclipse.gmf.runtime.diagram.ui.services.palette.SelectionToolEx#handleKeyUp(org.eclipse.swt.events.KeyEvent)
*/
@Override
protected boolean handleKeyUp(KeyEvent e) {
boolean returnVal = super.handleKeyUp(e);
if (acceptArrowKey(e)) {
// In superclass SelectionToolEx.handleKeyUp(KeyEvent), it was "if (acceptArrowKeyOnly(e) &&
// !isUsingTraverseHandles) {".
if (getDragTracker() != null) {
getDragTracker().commitDrag();
}
setDragTracker(null);
setState(STATE_INITIAL);
unlockTargetEditPart();
}
return returnVal;
}
}
Loading

0 comments on commit 85ee1c6

Please sign in to comment.