MySelectionAction.getSelectedObjects() is always empty [message #166621] |
Wed, 02 February 2005 09:04  |
Eclipse User |
|
|
|
Originally posted by: sinleeh.nowhere.com
Dear All,
Please pardon this newbie question.
I was trying to display a context menu for a GEF editpart. Using
LogicEditor as a guide,
(1) I define an MySelectionAction that inherits from a SelectionAction,
(2) added it to the context menu MyContextMenuProvider (child of
ContextMenuProvider) in buildContextMenu()
When running my GEF plugin I can see the context menu displaying an entry
for MySelectionAction. However, when I try to query its
getSelectedObjects(), the list is always empty. I am sure I miss something
and will be grateful if someone can point it out to me.
Thanking you in advance and best regards,
Sinleeh
|
|
|
|
Re: MySelectionAction.getSelectedObjects() is always empty [message #166662 is a reply to message #166654] |
Wed, 02 February 2005 12:11   |
Eclipse User |
|
|
|
Originally posted by: sinleeh.nowhere.com
Dear Randy et al,
Thanks for the advice. I reworked the relevent part but still the same
problem exists. I include what I think is the relevent parts only. If
there is something else needed, then it is safe to assume that I missed
it. As you can see, the code is based on LogicEditor. When I run
MyPipelineEditor, I manage to get the context menu and see the menuitem
for my Action, i.e. "Parameters ..." but it is always grey-out as
MyParameterAction. calculateEnabled() will always return false since
either getSelectedObjects() or getSelection always return empty or null.
Any help appreciated.
Thanks,
Cinly
//############################
public class MyPipelineEditor extends GraphicalEditorWithPalette {
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
GraphicalViewer viewer = getGraphicalViewer();
ContextMenuProvider provider
= new MyContextMenuProvider(viewer, getActionRegistry());
viewer.setContextMenu(provider);
getSite().registerContextMenu("mymenu", provider, viewer);
}
protected void createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
IAction action = new GFXParameterAction((IWorkbenchPart)this,
getActionRegistry());
registry.registerAction(action);
getSelectionActions().add(action.getId());
}
}
//############################
public class MyParameterAction extends SelectionAction {
static public String ID = "GFX Parameter Action";
public MyParameterAction(IWorkbenchPart part) {
super(part);
setId(ID);
setText("Parameters ...");
}
/* (non-Javadoc)
* @see
org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnab led()
*/
protected boolean calculateEnabled() {
//EITHER USE THIS SECTION
Object selected = getSelection(); //always equals null
if(selected == null) return false;
//OR THIS SECTION
if(getSelectedObjects().isEmpty) return false; //always return false
//....
}
}
//############################
public class GFXContextMenuProvider extends ContextMenuProvider {
private ActionRegistry actionRegistry;
public GFXContextMenuProvider(EditPartViewer viewer, ActionRegistry
registry) {
super(viewer);
setActionRegistry(registry);
}
public void buildContextMenu(IMenuManager menu) {
menu.add(new Separator("GFX_PARAMETER"));
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
menu.appendToGroup("GFX_PARAMETER",
getActionRegistry().getAction(GFXParameterAction.ID));
}
public ActionRegistry getActionRegistry() {
return actionRegistry;
}
public void setActionRegistry(ActionRegistry actionRegistry) {
this.actionRegistry = actionRegistry;
}
}
Randy Hudson wrote:
> Actions should not hook themselves to notifiers because it leads to memory
> leaks. Our approach is for the editor to manage actions and listen to
> selection, then call update() on the selection action. See
> GraphicalEDitor#selectionChanged().
> "Sinleeh" <sinleeh@nowhere.com> escreveu na mensagem
> news:ctqmlr$es8$1@www.eclipse.org...
>> Dear All,
>>
>> Please pardon this newbie question.
>>
>> I was trying to display a context menu for a GEF editpart. Using
>> LogicEditor as a guide,
>>
>> (1) I define an MySelectionAction that inherits from a SelectionAction,
>> (2) added it to the context menu MyContextMenuProvider (child of
>> ContextMenuProvider) in buildContextMenu()
>>
>> When running my GEF plugin I can see the context menu displaying an entry
>> for MySelectionAction. However, when I try to query its
>> getSelectedObjects(), the list is always empty. I am sure I miss something
>> and will be grateful if someone can point it out to me.
>>
>> Thanking you in advance and best regards,
>> Sinleeh
>>
>>
|
|
|
Oops.. error in posted source code [message #166670 is a reply to message #166662] |
Wed, 02 February 2005 12:21  |
Eclipse User |
|
|
|
Originally posted by: sinleeh.nowhere.com
I found a few inconsistency in the code I submitted. Essentially I tried
to make it more userfriendly by removing replacing names to generic names
but obviously I failed this time.
Please note that
MyContextMenuProvider is the same as GFXContextMenuProvider
and
MyParameterAction is the same as GFXParameterAction
Sorry for the inconvience. Corrected code posted here:
//############################
public class MyPipelineEditor extends GraphicalEditorWithPalette {
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
GraphicalViewer viewer = getGraphicalViewer();
ContextMenuProvider provider
= new MyContextMenuProvider(viewer, getActionRegistry());
viewer.setContextMenu(provider);
getSite().registerContextMenu("mymenu", provider, viewer);
}
protected void createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
IAction action = new MyParameterAction((IWorkbenchPart)this,
getActionRegistry());
registry.registerAction(action);
getSelectionActions().add(action.getId());
}
}
//############################
public class MyParameterAction extends SelectionAction {
static public String ID = "GFX Parameter Action";
public MyParameterAction(IWorkbenchPart part) {
super(part);
setId(ID);
setText("Parameters ...");
}
protected boolean calculateEnabled() {
//EITHER USE THIS SECTION
Object selected = getSelection(); //always equals null
if(selected == null) return false;
//OR THIS SECTION
if(getSelectedObjects().isEmpty) return false; //always return
false
//....
}
}
//############################
public class MyContextMenuProvider extends ContextMenuProvider {
private ActionRegistry actionRegistry;
public MyContextMenuProvider(EditPartViewer viewer, ActionRegistry
registry) {
super(viewer);
setActionRegistry(registry);
}
public void buildContextMenu(IMenuManager menu) {
menu.add(new Separator("GFX_PARAMETER"));
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
menu.appendToGroup("GFX_PARAMETER",
getActionRegistry().getAction(MyParameterAction.ID));
}
public ActionRegistry getActionRegistry() {
return actionRegistry;
}
public void setActionRegistry(ActionRegistry actionRegistry) {
this.actionRegistry = actionRegistry;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.06015 seconds