Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Where to action registry code from GEF redbook
Where to action registry code from GEF redbook [message #234269] Sat, 19 May 2007 01:55 Go to next message
Eclipse UserFriend
Originally posted by: mlevison.gmail.com

In the EMF/GEF redbook there is some sample code that demonstrates
installing zoom menu and tool bar items. Unfortunately I can't get to
work as advertised.

The first problem is where to put the code discussed in Example 4-6
which is similar to this code:

ZoomManager zoomManager = getZoomManager();
IAction zoomIn = new ZoomInAction(zoomManager);
IAction zoomOut = new ZoomOutAction(zoomManager);
ActionRegistry actionRegistry = getActionRegistry();
actionRegistry.registerAction(zoomIn);
actionRegistry.registerAction(zoomOut);

The redbook says put it inside the editor part - but gives not hint as
to which method. If I put it inside the init method - I get a null
pointer exception as I try to access as I attempt
ScalableFreeFormEdit.getZoomManager() - presumably because my
RootEditPart is ready yet. If do it inside the configureGraphicalViewer
I get in trouble with my contributeMenu code in the actionBarContribiutor:

public void contributeToMenu(IMenuManager menuManager) {
super.contributeToMenu(menuManager);

System.out.println("contributeToMenu");

// add a "View" menu after "Edit"
MenuManager viewMenu = new MenuManager("View");
viewMenu.add(getAction(GEFActionConstants.ZOOM_IN));
viewMenu.add(getAction(GEFActionConstants.ZOOM_OUT));
}

In this case the exception occurs because the action has been registered
and so getAction returns null.

So clearly there is a better place to register the actions within the
EditPart - I just can't figure it out.

One last detail - before I tried to add the menu items I just tried to
add the ZoomComboContributionItem to either the coolbar or toolbar as below:

public void contributeToCoolBar(ICoolBarManager coolBarManager) {
super.contributeToCoolBar(coolBarManager);

System.out.println("contributeToCoolBar");

coolBarManager.add(new Separator());
coolBarManager.add(new ZoomComboContributionItem(getPage()));
}

and nothing appeared in the application's toolbar.

Is there an up to date working set of example code that demos adding
zoom items to the menu and coolbar via an ActionBarContributor? If not
would someone be prepare to spend an hour or two on the phone on
Tuesday? If you did then I would delighted to write and publish an fresh
example. (Heck if you're in the Ottawa area - lets do this over coffee)

Tired and confused in Ottawa.
Mark Levison
Re: Where to action registry code from GEF redbook [message #234288 is a reply to message #234269] Sun, 20 May 2007 21:34 Go to previous messageGo to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
Mark Levison wrote:
> In the EMF/GEF redbook there is some sample code that demonstrates
> installing zoom menu and tool bar items. Unfortunately I can't get to
> work as advertised.
>
> The first problem is where to put the code discussed in Example 4-6
> which is similar to this code:
>
> ZoomManager zoomManager = getZoomManager();
> IAction zoomIn = new ZoomInAction(zoomManager);
> IAction zoomOut = new ZoomOutAction(zoomManager);
> ActionRegistry actionRegistry = getActionRegistry();
> actionRegistry.registerAction(zoomIn);
> actionRegistry.registerAction(zoomOut);
>
> The redbook says put it inside the editor part - but gives not hint as
> to which method. If I put it inside the init method - I get a null
> pointer exception as I try to access as I attempt
> ScalableFreeFormEdit.getZoomManager() - presumably because my
> RootEditPart is ready yet. If do it inside the configureGraphicalViewer
> I get in trouble with my contributeMenu code in the actionBarContribiutor:
>
> public void contributeToMenu(IMenuManager menuManager) {
> super.contributeToMenu(menuManager);
>
> System.out.println("contributeToMenu");
>
> // add a "View" menu after "Edit"
> MenuManager viewMenu = new MenuManager("View");
> viewMenu.add(getAction(GEFActionConstants.ZOOM_IN));
> viewMenu.add(getAction(GEFActionConstants.ZOOM_OUT));
> }
>
> In this case the exception occurs because the action has been registered
> and so getAction returns null.
>
> So clearly there is a better place to register the actions within the
> EditPart - I just can't figure it out.
>
> One last detail - before I tried to add the menu items I just tried to
> add the ZoomComboContributionItem to either the coolbar or toolbar as
> below:
>
> public void contributeToCoolBar(ICoolBarManager coolBarManager) {
> super.contributeToCoolBar(coolBarManager);
>
> System.out.println("contributeToCoolBar");
>
> coolBarManager.add(new Separator());
> coolBarManager.add(new ZoomComboContributionItem(getPage()));
> }
>
> and nothing appeared in the application's toolbar.
>
> Is there an up to date working set of example code that demos adding
> zoom items to the menu and coolbar via an ActionBarContributor? If not
> would someone be prepare to spend an hour or two on the phone on
> Tuesday? If you did then I would delighted to write and publish an fresh
> example. (Heck if you're in the Ottawa area - lets do this over coffee)
>
> Tired and confused in Ottawa.
> Mark Levison


In my Graphical Editor I have a method thus:

/**
* Add some extra Actions - after the graphical viewer has been created
*/
protected void createAdditionalActions() {
ZoomManager zoomManager =
(ZoomManager)getAdapter(ZoomManager.class);

List<String> zoomLevels = new ArrayList<String>(3);
zoomLevels.add(ZoomManager.FIT_ALL);
zoomLevels.add(ZoomManager.FIT_WIDTH);
zoomLevels.add(ZoomManager.FIT_HEIGHT);
zoomManager.setZoomLevelContributions(zoomLevels);

IAction zoomIn = new ZoomInAction(zoomManager);
IAction zoomOut = new ZoomOutAction(zoomManager);
getActionRegistry().registerAction(zoomIn);
getActionRegistry().registerAction(zoomOut);

IHandlerService service =
(IHandlerService)getEditorSite().getService(IHandlerService. class);
service.activateHandler(zoomIn.getActionDefinitionId(), new
ActionHandler(zoomIn));
service.activateHandler(zoomOut.getActionDefinitionId(), new
ActionHandler(zoomOut));

// Show Grid Action
IAction showGrid = new ToggleGridAction(getGraphicalViewer());
getActionRegistry().registerAction(showGrid);
}

This is called from configureGraphicalViewer() thus:

protected void configureGraphicalViewer() {
super.configureGraphicalViewer();

GraphicalViewer viewer = getGraphicalViewer();

/*
* We'll have a Zoom Manager
*/
ScalableFreeformRootEditPart rootPart = new
ScalableFreeformRootEditPart();
viewer.setRootEditPart(rootPart);

// Key handler
viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer));

// create a drop target listener for this palette viewer
// this will enable model element creation by dragging a
CombinatedTemplateCreationEntries
// from the palette into the editor
getGraphicalViewer().addDropTargetListener(new
DiagramTemplateTransferDropTargetListener(viewer));

// Part Factory
viewer.setEditPartFactory(new LDEditPartFactory());

// Context menu
registerContextMenu();

// Other Actions
createAdditionalActions();

// Set Content
viewer.setContents(getModel());
}

HTH,

PB
Re: Where to action registry code from GEF redbook [message #234372 is a reply to message #234288] Tue, 22 May 2007 16:33 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mlevison.gmail.com

Thanks for the help Phillip - the code gives me some great ideas.

Phillip Beauvoir wrote:
>
> In my Graphical Editor I have a method thus:
>
> /**
> * Add some extra Actions - after the graphical viewer has been created
> */
> protected void createAdditionalActions() {
> ZoomManager zoomManager =
> (ZoomManager)getAdapter(ZoomManager.class);
>
> ...
> getActionRegistry().registerAction(zoomIn);
> getActionRegistry().registerAction(zoomOut);
>
...
> getActionRegistry().registerAction(showGrid);
> }
>
> This is called from configureGraphicalViewer() thus:
>
> protected void configureGraphicalViewer() {
> super.configureGraphicalViewer();
> ...
> ScalableFreeformRootEditPart rootPart = new
> ScalableFreeformRootEditPart();
> viewer.setRootEditPart(rootPart);
>
> ...
> createAdditionalActions();
> }

However I'm still stuck - when do you contribute your actions to the
menu and coolbar?

Using an ActionBarContributor (registered for my edit part in my plugin
xml) I see things get created in the following sequence:

ActionBarContributor.buildActions
ActionBarContributor.contributeToMenu
ActionBarContributor.contributeToCoolBar
EditPart.Init
EditPart.configureGraphicalViewer
EditPart.createAdditionalActions

So as you can see the ActionBarContributor gets called before anything
in the EditPart is created - so there is no ScaleableFreeFormEditPart or
a ZoomManager etc. before the contributor is called.

Clearly the authors of the redbook expected this to work some how - I
just not sure how that is.

Can I delay all the calls to the ActionBarContributor? Do ditch
ActionBarContributor and just make all my contributions directly from
the EditPart?

Still as confused as ever
Mark Levison
Re: Where to action registry code from GEF redbook [message #234379 is a reply to message #234288] Tue, 22 May 2007 16:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mlevison.gmail.com

Thanks for the help Phillip - the code gives me some great ideas.

Phillip Beauvoir wrote:
>
> In my Graphical Editor I have a method thus:
>
> /**
> * Add some extra Actions - after the graphical viewer has been
created
> */
> protected void createAdditionalActions() {
> ZoomManager zoomManager =
> (ZoomManager)getAdapter(ZoomManager.class);
>
> ...
> getActionRegistry().registerAction(zoomIn);
> getActionRegistry().registerAction(zoomOut);
>
...
> getActionRegistry().registerAction(showGrid);
> }
>
> This is called from configureGraphicalViewer() thus:
>
> protected void configureGraphicalViewer() {
> super.configureGraphicalViewer();
> ...
> ScalableFreeformRootEditPart rootPart = new
> ScalableFreeformRootEditPart();
> viewer.setRootEditPart(rootPart);
>
> ...
> createAdditionalActions();
> }

However I'm still stuck - when do you contribute your actions to the
menu and coolbar?

Using an ActionBarContributor (registered for my edit part in my plugin
xml) I see things get created in the following sequence:

ActionBarContributor.buildActions
ActionBarContributor.contributeToMenu
ActionBarContributor.contributeToCoolBar
EditPart.Init
EditPart.configureGraphicalViewer
EditPart.createAdditionalActions

So as you can see the ActionBarContributor gets called before anything
in the EditPart is created - so there is no ScaleableFreeFormEditPart or
a ZoomManager etc. before the contributor is called.

Clearly the authors of the redbook expected this to work some how - I
just not sure how that is.

Can I delay all the calls to the ActionBarContributor? Do ditch
ActionBarContributor and just make all my contributions directly from
the EditPart?

Still as confused as ever
Mark Levison
Re: Where to action registry code from GEF redbook [message #234386 is a reply to message #234379] Tue, 22 May 2007 17:49 Go to previous messageGo to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
Mark Levison wrote:
> Thanks for the help Phillip - the code gives me some great ideas.
>
> Phillip Beauvoir wrote:
> >
> > In my Graphical Editor I have a method thus:
> >
> > /**
> > * Add some extra Actions - after the graphical viewer has been
> created
> > */
> > protected void createAdditionalActions() {
> > ZoomManager zoomManager =
> > (ZoomManager)getAdapter(ZoomManager.class);
> >
> > ...
> > getActionRegistry().registerAction(zoomIn);
> > getActionRegistry().registerAction(zoomOut);
> >
> ...
> > getActionRegistry().registerAction(showGrid);
> > }
> >
> > This is called from configureGraphicalViewer() thus:
> >
> > protected void configureGraphicalViewer() {
> > super.configureGraphicalViewer();
> > ...
> > ScalableFreeformRootEditPart rootPart = new
> > ScalableFreeformRootEditPart();
> > viewer.setRootEditPart(rootPart);
> >
> > ...
> > createAdditionalActions();
> > }
>
> However I'm still stuck - when do you contribute your actions to the
> menu and coolbar?
>
> Using an ActionBarContributor (registered for my edit part in my plugin
> xml) I see things get created in the following sequence:
>
> ActionBarContributor.buildActions
> ActionBarContributor.contributeToMenu
> ActionBarContributor.contributeToCoolBar
> EditPart.Init
> EditPart.configureGraphicalViewer
> EditPart.createAdditionalActions
>
> So as you can see the ActionBarContributor gets called before anything
> in the EditPart is created - so there is no ScaleableFreeFormEditPart or
> a ZoomManager etc. before the contributor is called.
>
> Clearly the authors of the redbook expected this to work some how - I
> just not sure how that is.
>
> Can I delay all the calls to the ActionBarContributor? Do ditch
> ActionBarContributor and just make all my contributions directly from
> the EditPart?
>
> Still as confused as ever
> Mark Levison
>


OK,

here's my ActionBarContributor. See if there's any clues there:

public class MyEditorActionBarContributor
extends ActionBarContributor {
IEditorPart editor;

@Override
public void setActiveEditor(IEditorPart editor) {
this.editor = editor;
super.setActiveEditor(editor);
}

/*
* Over-ride this to make this public
* (non-Javadoc)
* @see
org.eclipse.gef.ui.actions.ActionBarContributor#addRetargetA ction(org.eclipse.ui.actions.RetargetAction)
*/
@Override
public void addRetargetAction(RetargetAction action) {
super.addRetargetAction(action);
}

@Override
public void contributeToToolBar(IToolBarManager toolBarManager) {
// Add the Zoom Manager Combo
toolBarManager.add(new ZoomComboContributionItem(getPage()));

toolBarManager.add(new Separator());
toolBarManager.add(getAction(GEFActionConstants.ALIGN_LEFT)) ;
toolBarManager.add(getAction(GEFActionConstants.ALIGN_CENTER ));
toolBarManager.add(getAction(GEFActionConstants.ALIGN_RIGHT) );
toolBarManager.add(new Separator());
toolBarManager.add(getAction(GEFActionConstants.ALIGN_TOP));
toolBarManager.add(getAction(GEFActionConstants.ALIGN_MIDDLE ));
toolBarManager.add(getAction(GEFActionConstants.ALIGN_BOTTOM ));
toolBarManager.add(new Separator());
toolBarManager.add(getAction(GEFActionConstants.MATCH_WIDTH) );
toolBarManager.add(getAction(GEFActionConstants.MATCH_HEIGHT ));
toolBarManager.add(new Separator());
}

@Override
protected void buildActions() {
// Not sure whether to do it this way or just by
addGlobalActionKey(id);
addRetargetAction(new DeleteRetargetAction());
addRetargetAction(new UndoRetargetAction());
addRetargetAction(new RedoRetargetAction());

addRetargetAction(new ZoomInRetargetAction());
addRetargetAction(new ZoomOutRetargetAction());

addRetargetAction(new
AlignmentRetargetAction(PositionConstants.LEFT));
addRetargetAction(new
AlignmentRetargetAction(PositionConstants.CENTER));
addRetargetAction(new
AlignmentRetargetAction(PositionConstants.RIGHT));
addRetargetAction(new
AlignmentRetargetAction(PositionConstants.TOP));
addRetargetAction(new
AlignmentRetargetAction(PositionConstants.MIDDLE));
addRetargetAction(new
AlignmentRetargetAction(PositionConstants.BOTTOM));

addRetargetAction(new MatchWidthRetargetAction());
addRetargetAction(new MatchHeightRetargetAction());

addRetargetAction(new
RetargetAction(GEFActionConstants.TOGGLE_GRID_VISIBILITY,
Messages.CompetencePlanEditorActionBarContributor_0,
IAction.AS_CHECK_BOX));
}

@Override
public void contributeToMenu(IMenuManager menuManager) {
super.contributeToMenu(menuManager);

MenuManager viewMenu = new
MenuManager(Messages.CompetencePlanEditorActionBarContributo r_1);

viewMenu.add(getAction(GEFActionConstants.ZOOM_IN));
viewMenu.add(getAction(GEFActionConstants.ZOOM_OUT));
viewMenu.add(new Separator());
viewMenu.add(getAction(GEFActionConstants.TOGGLE_GRID_VISIBI LITY));
viewMenu.add(new Separator());
viewMenu.add(getAction(GEFActionConstants.MATCH_WIDTH));
viewMenu.add(getAction(GEFActionConstants.MATCH_HEIGHT));
viewMenu.add(new Separator());

menuManager.insertAfter(IWorkbenchActionConstants.M_EDIT,
viewMenu);
}

@Override
protected void declareGlobalActionKeys() {
//addGlobalActionKey(ActionFactory.UNDO.getId());
//addGlobalActionKey(ActionFactory.REDO.getId());
//addGlobalActionKey(ActionFactory.DELETE.getId());
addGlobalActionKey(ActionFactory.SELECT_ALL.getId());
addGlobalActionKey(ActionFactory.PRINT.getId());
}
}
Re: Where to action registry code from GEF redbook [message #234600 is a reply to message #234386] Thu, 24 May 2007 19:25 Go to previous message
Eclipse UserFriend
Originally posted by: mlevison.gmail.com

Phillip - your code was exactly what I needed. I still had some battles
to fight - namely with the getAdapter() method. I'm allergic to weakly
typed methods like this. Anyway I will not bore the world with my tiny
problems.


Thanks for taking the time to help
Mark Levison
----------------------------------------
Aperture vs. Lightroom - best comparisons
http://www.notesfromatooluser.com/2007/02/aperture_vs_lig.ht ml
Scrum in a Nutshell or 5 minutes to learn Scrum
http://www.notesfromatooluser.com/2006/11/scrum_in_a_nuts.ht ml
Getting Things Done!!! Can't Keep Track of all the tasks you have to
do? Need a better Tool to Implement GTD?
http://www.notesfromatooluser.com/2006/12/getting_things_.ht ml
Previous Topic:Looking for a GEF sample application
Next Topic:Animation under OS X
Goto Forum:
  


Current Time: Fri Apr 19 23:21:25 GMT 2024

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

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

Back to the top