Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Useful EMF UI command (handler) for moving items up or down, with optional copying
Useful EMF UI command (handler) for moving items up or down, with optional copying [message #542330] Thu, 24 June 2010 14:26
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

On the Mac, I've never managed to move EObject instances up and down in
a tree, to restructure children within a parent using drag'n drop (as I
think you can on Windows). Anyhow, it is useful to have a command and
keyboard bindings for moving "lines" in the EMF editors up and down, to
reorder them, like you can with text in the text editor.

Below is an implementation of a command (handler) that supports moving
items up or down, with optional copying. At the bottom, you'll find
markup to insert into plugin.xml, to utilize the command handler.

Hallvard

P.S: Now I expect someone to tell me how this is already implemented. It
was a good command handler implementation exercise, anyway.

/*********************************************************** ********************
* Copyright (c) 2010 Hallvard Traetteberg.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Hallvard Traetteberg - initial API and implementation

************************************************************ ******************/
package org.eclipse.e4.emf.javascript.ui;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.MoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

public class NudgeEObjectCommandHandler extends AbstractHandler {

public NudgeEObjectCommandHandler() {
super();
}

public void dispose() {
}

private EObject getSelection(IWorkbenchWindow wbw) {
return (wbw != null && wbw.getActivePage() != null ?
getSelection(wbw.getActivePage().getSelection()) : null);
}
private EditingDomain getEditingDomainProvider(IWorkbenchWindow wbw) {
if (wbw == null || wbw.getActivePage() == null) {
return null;
}
IWorkbenchPart part = wbw.getActivePage().getActivePart();
return (part instanceof IEditingDomainProvider ?
((IEditingDomainProvider) part).getEditingDomain() : null);
}

private EObject getSelection(ISelection selection) {
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof EObject) {
return (EObject) (((IStructuredSelection) selection).getFirstElement());
}
return null;
}

private final String movementDeltaParameterId =
"org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta";
private final String shouldCopyParameterId =
"org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy";

public Object execute(ExecutionEvent event) throws ExecutionException {
EObject eObject =
getSelection(HandlerUtil.getActiveWorkbenchWindow(event));
EReference containmentFeature = eObject.eClass().eContainmentFeature();
if (containmentFeature != null && containmentFeature.isMany()) {
EList<EObject> siblings = (EList<EObject>)
eObject.eContainer().eGet(containmentFeature);
int pos = siblings.indexOf(eObject);
String movementDelta = event.getParameter(movementDeltaParameterId);
boolean shouldCopy =
"true".equals(event.getParameter(shouldCopyParameterId));
int d = 0;
if (movementDelta != null) {
try {
d = Integer.parseInt(movementDelta);
} catch (NumberFormatException e) {
}
}
int newPos = pos + d;
if (newPos < 0) {
newPos = 0;
} else if (newPos >= siblings.size()) {
newPos = siblings.size() - 1;
}
if (shouldCopy) {
eObject = EcoreUtil.copy(eObject);
}
if (shouldCopy || newPos != pos) {
EditingDomain editingDomain =
getEditingDomainProvider(HandlerUtil.getActiveWorkbenchWindo w(event));
Command command = (shouldCopy ? new AddCommand(editingDomain,
siblings, eObject, newPos) : new MoveCommand(editingDomain, siblings,
eObject, newPos));
if (editingDomain != null) {
editingDomain.getCommandStack().execute(command);
} else {
command.execute();
}
}
}
return eObject;
}

public boolean isEnabled() {
EObject eObject =
getSelection(PlatformUI.getWorkbench().getActiveWorkbenchWin dow());
return (eObject != null);
}

public boolean isHandled() {
return true;
}

public void addHandlerListener(IHandlerListener handlerListener) {
}
public void removeHandlerListener(IHandlerListener handlerListener) {
}
}

<extension point="org.eclipse.ui.commands">
<command
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
name="Move EObject (up or down)">
<commandParameter

id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta "
name="movementDelta"
optional="true">
</commandParameter>
<commandParameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy"
name="shouldCopy"
optional="true">
</commandParameter>
</command>
</extension>

<extension point="org.eclipse.ui.handlers">
<handler

class="org.eclipse.e4.emf.javascript.ui.NudgeEObjectCommandHandler "
commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand">
</handler>
</extension>

<extension point="org.eclipse.ui.bindings">
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+PAGE_UP"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta "
value="0x80000000"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+M3+PAGE_UP"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta "
value="0x80000000"/>
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy" value="true"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+ARROW_UP"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta " value="-1"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+M3+ARROW_UP"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta " value="-1"/>
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy" value="true"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+ARROW_DOWN"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta " value="1"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+M3+ARROW_DOWN"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta " value="1"/>
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy" value="true"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M3+PAGE_DOWN"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta "
value="0x7fffffff"/>
</key>
<key commandId="org.eclipse.e4.emf.ui.NudgeEObjectCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+M3+PAGE_DOWN"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.movementDelta "
value="0x7fffffff"/>
<parameter
id="org.eclipse.e4.emf.ui.NudgeEObjectCommand.shouldCopy" value="true"/>
</key>
</extension>
Previous Topic:[Net4j] Termination Review Successful
Next Topic:[EMF] Missing EMF - References / API in Helios
Goto Forum:
  


Current Time: Thu Apr 25 18:13:32 GMT 2024

Powered by FUDForum. Page generated in 0.03381 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top