|
|
Re: Using retarget actions from a GEF editor [message #237535 is a reply to message #237499] |
Fri, 03 August 2007 12:01 |
No real name Messages: 2 Registered: July 2009 |
Junior Member |
|
|
Steinar Bang wrote:
>>>>>> Steinar Bang <sb@dod.no>:
>
>> I'm trying to create a CopyAction that a GEF editor can use to copy an
>> edit part, and that will be run from Edit->Copy .
>
>> But I can't figure out where/when to register the CopyAction I've
>> written.
>
>> The documentation says to call
>> getViewSite().getActionBars().setGlobalActionHandler() from the
>> createPartControl() method:
>> http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/wrkAdv_retarget_setting.htm
>
>> But this doesn't work in an editor, since getViewSite() is only
>> available on IView, and trying to cast the result of getSite() to
>> IViewSite (predictably) fails.
>
> The following both compiles, and does not throw exceptions. I've put
> this line into the createPartControlComposite(Composite) method:
> // Provide handlers for retargetable actions
> getEditorSite().getActionBars().setGlobalActionHandler(Actio nFactory.COPY.getId(), copyAction);
>
> However it doesn't work.
>
> I've put a break point in the RetargetAction#partActivated() method,
> and the argument to the setActionHandler() method for
> this==RetargetAction(copy) is also a RetargetAction(copy), instead of
> CopyAction, which it should have been if it was the method I
> registered.
>
> Is there something else I need to do?
3 August 2007 12:01 UTC
I have a GEF based editor that manipulates
connected, directed acyclic graphs (DAG's) consisting of
several hundred or more very complex nodes (several kilobytes of
XML declaration per individual node, in some
cases) and multiple edges per node.
The editor has a successful Copy/Paste
implementation for single or multiple node
selection within the same editor window, or
from open editor window to another open
editor window.
Here is how I did my plug-in. I hope it is some utility.
I have omitted the package names.
(1) Declare a contributorClass attribute in the "editor"
extension point element in plugin.xml
(2) Include an invocation of addRetargetAction()
in my class that extends ActionBarContributor
(3) Declare an overriding method createActions()
in the PrimaryEditor class itself
(4) Create a EditCopyNodesAction that performs the
desired copy to system clipboard action
==========================================
The plugin.xml file defines the following.
<extension point="org.eclipse.ui.editors">
<editor name="DagGraphicalEditor"
extensions="cpxl, xls"
icon="icons/knot_24.gif"
default="true"
class=".....PrimaryEditor"
contributorClass=".....MainActionBarContributor"
id=".....PrimaryEditor" />
==========================================
Entire source code for MainActionBarContributor class
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.gef.ui.actions.*;
import
com.leviathan.dag.workbench.graphicaleditor.actions.EditCopy RetargetAction;
import
com.leviathan.dag.workbench.graphicaleditor.actions.EditPast eRetargetAction;
/**
* Contributes actions to a toolbar.
* This class is tied to the editor in the definition of
editor-extension (see plugin.xml).
*/
public class MainActionBarContributor extends ActionBarContributor {
/**
* Create actions managed by this contributor.
* @see org.eclipse.gef.ui.actions.ActionBarContributor#buildActions ()
*/
protected void buildActions() {
addRetargetAction(new DeleteRetargetAction());
addRetargetAction(new UndoRetargetAction());
addRetargetAction(new RedoRetargetAction());
addRetargetAction(new EditCopyRetargetAction());
addRetargetAction(new EditPasteRetargetAction());
}
/**
* Add icons for actions to the given toolbar.
* @see
org.eclipse.ui.part.EditorActionBarContributor#contributeToT oolBar(org.eclipse.jface.action.IToolBarManager)
*/
public void contributeToToolBar(IToolBarManager toolBarManager) {
toolBarManager.add(getAction(ActionFactory.UNDO.getId()));
toolBarManager.add(getAction(ActionFactory.REDO.getId()));
toolBarManager.add(getAction(ActionFactory.COPY.getId()));
toolBarManager.add(getAction(ActionFactory.PASTE.getId()));
toolBarManager.add(getAction(ActionFactory.DELETE.getId()));
}
/*
* (non-Javadoc)
* @see
org.eclipse.gef.ui.actions.ActionBarContributor#declareGloba lActionKeys()
*/
protected void declareGlobalActionKeys() {
addGlobalActionKey(ActionFactory.PRINT.getId());
addGlobalActionKey(ActionFactory.COPY.getId());
addGlobalActionKey(ActionFactory.PASTE.getId());
addGlobalActionKey(ActionFactory.SELECT_ALL.getId());
}
}
==========================================
Entire source code for EditCopyRetargetAction class
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import
com.leviathan.dag.workbench.graphicaleditor.BundleActivatorP luginModule;
/**
* Provide a RetargetAction used in
* com.leviathan.dag.workbench.graphicaleditor.MainActionBarCon tributor
* for the dag specific class EditCopyNodesAction.
*/
public class EditCopyRetargetAction extends RetargetAction {
/**
* Constructor for EditCopyRetargetAction.
*
* @param actionID
* @param label
*/
public EditCopyRetargetAction() {
super(ActionFactory.COPY.getId(), "copy");
setToolTipText("Copy selected nodes to the system clipboard");
setImageDescriptor(ImageDescriptor.createFromFile(
BundleActivatorPluginModule.class, "icons/copy.gif"));
}
}
==========================================
Entire source code for PrimaryEditor class createActions() method
/**
* Overrides createActions() method
* in org.eclipse.gef.ui.parts.GraphicalEditor
* (which is in turn extended by
org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette
* which this class extends)
*/
protected void createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
IAction action;
action = new EditCopyNodesAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getId());
action = new EditPasteNodesAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getId());
}
==========================================
Entire source code for EditCopyNodesAction class
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;
import com.leviathan.dag.workbench.graphicaleditor.parts.ShapeEditP art;
import com.leviathan.dag.workbench.preferences.DagWorkBenchConstant s;
import org.eclipse.gef.ui.actions.Clipboard;
import org.eclipse.gef.ui.actions.SelectionAction;
/**
* If the current selection contains at least one object of type
* com.leviathan.dag.workbench.graphicaleditor.parts.ShapeEditP art (which
* extends org.eclipse.gef.editparts.AbstractGraphicalEditPart) this
action will
* copy the entire selection to the clipboard.
*
* This is intended as a replacement for the supplied class
* org.eclipse.gef.ui.actions.CopyTemplateAction that I never could
make work
* the way I wanted it to.
*
*/
public class EditCopyNodesAction extends SelectionAction {
/**
* Constructor for EditCopyNodesAction.
*
* @param editor
*/
public EditCopyNodesAction(IWorkbenchPart editor) {
super(editor);
}
/**
* @return <code>true</code>
* @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnab led()
*/
protected boolean calculateEnabled() {
return true;
}
/**
* @see org.eclipse.gef.ui.actions.EditorPartAction#init()
*/
protected void init() {
setId(ActionFactory.COPY.getId());
setText("Copy");
setAccelerator(SWT.CTRL | 'C');
setDescription("Copy");
setToolTipText("Copy");
setImageDescriptor(DagWorkBenchConstants.getCopyImage());
setHoverImageDescriptor(getImageDescriptor());
}
/**
* Set the entire java.util.List from the
* org.eclipse.gef.ui.actions.SelectionAction#getSelectedObject s()
method as
* the contents of the current default clipboard.
*/
public void run() {
List selection = getSelectedObjects();
if (selection != null && selection.size() > 0) {
for (int i = 0; i < selection.size(); i++) {
if (selection.get(i) instanceof ShapeEditPart) {
Clipboard.getDefault().setContents(selection);
return;
}
}
}
}
}
|
|
|
Re: Using retarget actions from a GEF editor [message #237603 is a reply to message #237499] |
Mon, 06 August 2007 11:56 |
Steinar Bang Messages: 108 Registered: July 2009 |
Senior Member |
|
|
Here's a summary of what retarget actions are.
"Retargetable actions" in the eclipse rich client platform, are
actions (IAction implementations) that sit on the main window as
toolbuttons, and menu entries, and that are placeholders that use
another IAction object, known as the "action handler", to do the
actual work.
The action handler used by the retargetable actions, change when the
active part of the workbench changes. I.e. when a different view or
editor is selected, a retargetable action will look up its action
handle, using its own id (as returned by the getId() method) as the
key, in a map associated with the workbench part.
When a retargetable action has no handler, it is disabled. When a
retargetable action has a handler, the action's enabled/disabled
state, is determined by the handler.
To add eg. a "Copy" action to a GEF editor, do the following:
- Subclass SelectionAction to create a Copy action. You have to
replace isEnabled() when the selection contains something that is
sensible to copy, and replace run() to put the copied stuff into
the clipboard. This class' getId() method should return the same
value as ActionFactory.COPY.getId()
- In your graphical editor class, replace the createActions() method
with something like this:
@Override
protected void createActions()
{
super.createActions();
ActionRegistry actionRegistry = getActionRegistry();
IAction copyAction = new CopyAction();
actionRegistry.register(copyAction);
}
(where CopyAction is the action class created in the first step)
Note that CopyAction is not a retargetable action. The only times you
would create retargetable actions are:
- When you add retargetable actions to the workbench menus and
toolbars
- When you create the workbench menus and toolbars manually to get
something different to the defaults
|
|
|
Powered by
FUDForum. Page generated in 0.03811 seconds