Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Contextual menu on a custom widget
Contextual menu on a custom widget [message #62674] Fri, 23 November 2007 17:55 Go to next message
Eclipse UserFriend
Originally posted by: cedric.gaillot.thalesraytheon-fr.com

Hi,

I'm actually trying to add a contextual menu on a custom widget (a
cartographic map).

I would like to update the content of the contextual menu in function of
the position of a click on the cartographic map.

I'm using the MenuManager class.

But I encounter this problem :

When i click a first time on the cartographic map, the contextual menu is
correctly displayed.

But when I clicked elsewhere on the map, the contextual menu isn't updated
because its content is still identical to the previous one.

In fact, it seems that, on further clicks, I don't go in the
menuAboutToShow() method. But when I lost the focus on the custom widget
and then retry to click on the map, the contextual menu is correctly
updated.

Is there a specific action to say to the MenuManager to notify the
MenuListener even when there is still the focus on the widget ?

The executed code :

//////////////////////////////////////////////////////////// //////////////

public class CartographyViewpart extends ViewPart implements
GxBaseListener {

private MenuManager manager = null;

private ArrayList<Action> actionsList = new ArrayList<Action>();

public void createPartControl(final Composite parent) {

...

// The custom widget
openLayersMapBrowser = new OpenLayersMap(composite, SWT.NONE);

...

//I catch mouse click on the custom widget from javascript

((IEventAdapter) openLayersMapBrowser.getAdapter(IEventAdapter.class))
addListener(MouseListener.class, new MouseListener() {
public void mouseClicked(MouseEvent event) {
if (event.getButton() == MouseEvent.RIGHTBUTTON) {
ArrayList<ModelObject> modelObjectList = openLayersMapBrowser
.isUnder(event.getLocation(), event.getToleranceInDegrees());

System.out.println(".mouseClicked()");

//I Build the action to add to the contextual menu
addActions(modelObjectList);

} else {
System.out.println(".mouseClicked()2");
}
}
});

//Initializer MenuManager
hookContextMenu();
}

private void hookContextMenu() {

manager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
System.out.println(".menuAboutToShow()");
}
});
getSite().registerContextMenu(manager, this.openLayersMapBrowser);

Menu menu = manager.createContextMenu(this.openLayersMapBrowser);
openLayersMapBrowser.setMenu(menu);

}

private void addActions(ArrayList<ModelObject> modelObjectList) {

actionsList.clear();
Iterator<ModelObject> iter = modelObjectList.iterator();
while (iter.hasNext()) {
final ModelObject modelObject = iter.next();
Action action = new Action(modelObject.getName()) {
public void run() {
ModelObjectSelection.getInstance().setSelectedModelObject(
modelObject);
}
};
actionsList.add(action);
}
}

private void fillContextMenu(IMenuManager manager) {

Iterator<Action> iter = actionsList.iterator();
while (iter.hasNext()) {
Action action = iter.next();
manager.add(action);
}
}

...

}

//////////////////////////////////////////////////////////// //////////////

Thanks for your help,

Regards,

Cédric G.
Re: Contextual menu on a custom widget [message #63334 is a reply to message #62674] Tue, 27 November 2007 11:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rherrmann.innoopract.com

Hi,

I am not entirely sure that I got you right, but I would rather see the
source of the problem in the lower layers.
Can you make sure that the mouse events from the client-side are
correctly sent and processed by the LCA of the cartographic map widget?
I looks like the MouseEvent is a custom event, but only events defined
by RWT can be processed (note that IEventAdapter and EventAdapter reside
in internal packages).
Until MouseEvents arrive in RWT, you could try to use the SelectionEvent
instead.
If you still have trouble getting your custom widget to work, feel free
to share the source here and we may find a solution.

Cheers,
Rüdiger


Cédric G. wrote:
> Hi,
>
> I'm actually trying to add a contextual menu on a custom widget (a
> cartographic map).
> I would like to update the content of the contextual menu in function of
> the position of a click on the cartographic map.
>
> I'm using the MenuManager class.
>
> But I encounter this problem :
> When i click a first time on the cartographic map, the contextual menu
> is correctly displayed.
>
> But when I clicked elsewhere on the map, the contextual menu isn't
> updated because its content is still identical to the previous one.
>
> In fact, it seems that, on further clicks, I don't go in the
> menuAboutToShow() method. But when I lost the focus on the custom widget
> and then retry to click on the map, the contextual menu is correctly
> updated.
>
> Is there a specific action to say to the MenuManager to notify the
> MenuListener even when there is still the focus on the widget ?
>
> The executed code :
>
> //////////////////////////////////////////////////////////// //////////////
>
> public class CartographyViewpart extends ViewPart implements
> GxBaseListener {
>
> private MenuManager manager = null;
>
> private ArrayList<Action> actionsList = new ArrayList<Action>();
>
> public void createPartControl(final Composite parent) {
>
> ...
>
> // The custom widget
> openLayersMapBrowser = new OpenLayersMap(composite, SWT.NONE);
>
> ...
>
> //I catch mouse click on the custom widget from javascript
> ((IEventAdapter) openLayersMapBrowser.getAdapter(IEventAdapter.class))
> addListener(MouseListener.class, new MouseListener() {
> public void mouseClicked(MouseEvent event) {
> if (event.getButton() == MouseEvent.RIGHTBUTTON) {
> ArrayList<ModelObject> modelObjectList = openLayersMapBrowser
> .isUnder(event.getLocation(), event.getToleranceInDegrees());
>
> System.out.println(".mouseClicked()");
>
> //I Build the action to add to the contextual menu
> addActions(modelObjectList);
>
> } else {
> System.out.println(".mouseClicked()2");
> }
> }
> });
>
> //Initializer MenuManager
> hookContextMenu();
> }
>
> private void hookContextMenu() {
>
> manager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
> manager.setRemoveAllWhenShown(true);
> manager.addMenuListener(new IMenuListener() {
> public void menuAboutToShow(IMenuManager manager) {
> fillContextMenu(manager);
> System.out.println(".menuAboutToShow()");
> }
> });
> getSite().registerContextMenu(manager, this.openLayersMapBrowser);
>
> Menu menu = manager.createContextMenu(this.openLayersMapBrowser);
> openLayersMapBrowser.setMenu(menu);
>
> }
>
> private void addActions(ArrayList<ModelObject> modelObjectList) {
>
> actionsList.clear();
> Iterator<ModelObject> iter = modelObjectList.iterator();
> while (iter.hasNext()) {
> final ModelObject modelObject = iter.next();
> Action action = new Action(modelObject.getName()) {
> public void run() {
> ModelObjectSelection.getInstance().setSelectedModelObject(
> modelObject);
> }
> };
> actionsList.add(action);
> }
> }
>
> private void fillContextMenu(IMenuManager manager) {
>
> Iterator<Action> iter = actionsList.iterator();
> while (iter.hasNext()) {
> Action action = iter.next();
> manager.add(action);
> }
> }
>
> ...
>
> }
>
> //////////////////////////////////////////////////////////// //////////////
>
> Thanks for your help,
>
> Regards,
>
> Cédric G.
>
Re: Contextual menu on a custom widget [message #63584 is a reply to message #63334] Fri, 30 November 2007 08:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cedric.gaillot.thalesraytheon-fr.com

Hi,

In fact, each click on the custom widget is well transmitted to RAP in my
LCA class with the readData method below :

//---------------------------------------------------------- ------------

/**
* {@inheritDoc}
*/
public void readData(final Widget widget) {
MouseEvent event = null;

if (WidgetLCAUtil.wasEventSent(widget,
MouseEventJSConst.EVENT_WIDGET_CLICKED)) {
event = createMouseEvent(widget, MouseEvent.WIDGET_CLICKED);
}

if (event != null) {

final Display display = Display.getCurrent();
if (display != null) {
final MouseEvent eventToProcess = event;
display.syncExec(new Runnable() {
@SuppressWarnings("restriction")
public void run() {
eventToProcess.processEvent();
}
});
}
}
}

//---------------------------------------------------------- ----------------

In this method I create a custom Java Event by creating an instance of my
own MouseEvent class which inherits from Event.

My Mouse Event is created with this method :

//---------------------------------------------------------- ----------------

private static MouseEvent createMouseEvent(final Widget widget,
final int type) {

double x;
double y;
double lat;
double lon;
String JSbutton = null;
int button;
double wheelDelta;
double degreesTolerance;

if (widget instanceof Control) {
x = Double.parseDouble(WidgetLCAUtil.readPropertyValue(widget,
MouseEventJSConst.MOUSE_EVENT_X_PROPERTY));
y = Double.parseDouble(WidgetLCAUtil.readPropertyValue(widget,
MouseEventJSConst.MOUSE_EVENT_Y_PROPERTY));
lat = Double.parseDouble(WidgetLCAUtil.readPropertyValue(widget,
MouseEventJSConst.MOUSE_EVENT_LAT_PROPERTY));
lon = Double.parseDouble(WidgetLCAUtil.readPropertyValue(widget,
MouseEventJSConst.MOUSE_EVENT_LON_PROPERTY));

JSbutton = WidgetLCAUtil.readPropertyValue(widget,
MouseEventJSConst.MOUSE_EVENT_BUTTON_PROPERTY);
if (TypeButton.MIDDLE.getButtonJSDescription().equals(JSbutton) ) {
button = MouseEvent.MIDDLEBUTTON;
} else if (TypeButton.LEFT.getButtonJSDescription()
.equals(JSbutton)) {
button = MouseEvent.LEFTBUTTON;
} else if (TypeButton.RIGHT.getButtonJSDescription().equals(
JSbutton)) {
button = MouseEvent.RIGHTBUTTON;
} else {
button = MouseEvent.NONE;
}
wheelDelta = Double.parseDouble(WidgetLCAUtil.readPropertyValue(
widget, MouseEventJSConst.MOUSE_EVENT_WHEELDELTA_PROPERTY));

degreesTolerance = Double
.parseDouble(WidgetLCAUtil
.readPropertyValue(
widget,
MouseEventJSConst.MOUSE_EVENT_DEGREES_TOLERANCE_PROPERTY));

} else {
x = 0;
y = 0;
lat = 0;
lon = 0;
button = MouseEvent.NONE;
wheelDelta = 0;
degreesTolerance = 0;
}

return new MouseEvent(widget, type, x, y, lat, lon, button, wheelDelta,
degreesTolerance);
}

//---------------------------------------------------------- ----------------

The main problem I have is when I listen the fire of a creation of a
MouseEvent event in the CartographyViewpart class (See the code in my
first post). The event is well catched but when I try to update the
content of the contextual menu with MenuManager, it doesn't work unless I
lost and focus again on the widget.

Regards,

Cédric G.
Re: Contextual menu on a custom widget [message #63608 is a reply to message #63584] Fri, 30 November 2007 13:35 Go to previous messageGo to next message
Martin Dilger is currently offline Martin DilgerFriend
Messages: 54
Registered: July 2009
Member
Perhaps you should try to activate UICallback, this sounds like this is
the solution to this problem.

In your EntryPoint implementation, try
UICallback.activateUICallback(getClass());
Sorry, I donŽt have time to look the correct syntax up now, but search
this group, there were several posts on this topic.

Martin
Re: Contextual menu on a custom widget [message #63652 is a reply to message #63608] Fri, 30 November 2007 14:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rherrmann.innoopract.com

Activating UICallbacks won't help.

The syncExec in readData() should be removed. Even though I doubt that
this solves the problem as readData() is executed in the "UI thread" and
thus syncExec directly executes the runnable.


Martin Dilger wrote:
> Perhaps you should try to activate UICallback, this sounds like this is
> the solution to this problem.
>
> In your EntryPoint implementation, try
> UICallback.activateUICallback(getClass());
> Sorry, I donŽt have time to look the correct syntax up now, but search
> this group, there were several posts on this topic.
> Martin
>
Re: Contextual menu on a custom widget [message #63695 is a reply to message #63652] Fri, 30 November 2007 16:15 Go to previous messageGo to next message
Martin Dilger is currently offline Martin DilgerFriend
Messages: 54
Registered: July 2009
Member
Hi,

you are absolutely right, I didnŽt read property, activating UICallback
wonŽt help here, sorry.

Martin
Re: Contextual menu on a custom widget [message #63741 is a reply to message #63695] Fri, 30 November 2007 16:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cedric.gaillot.thalesraytheon-fr.com

Hi,

Thanks a lot for yours answers.

I have tried this two modifications, but it seems to have no effect on the
behavior of the contextual menu.

Otherwise, I have modified some things in the functions of my
CarthographicViewPart class (The ones displayed in my first post) :

I) - First I replace the created Action objects with ContributionItem
objects :

//---------------------------------------------------------- --------------

private void addActions(ArrayList<ModelObject> modelObjectList) {

actionsList.clear();

Iterator<ModelObject> iter = modelObjectList.iterator();
while (iter.hasNext()) {
final ModelObject modelObject = iter.next();

Action action = new Action(modelObject.getName()) {
public void run() {
ModelObjectSelection.getInstance().setSelectedModelObject(
modelObject);
}
};
actionsList.add(new ModelObjectContributionItem(action));
}
}

//---------------------------------------------------------- --------------

II) In second, in the hookContextMenu function, I use IMenuListener2
interface :

//---------------------------------------------------------- --------------

private void hookContextMenu() {

manager = new OurMenuManager("#PopupMenu"); //$NON-NLS-1$
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener2() {

@Override
public void menuAboutToHide(IMenuManager manager) {
System.out.println(".menuAboutToHide()");

}

public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
System.out.println(".menuAboutToShow()");
}
});
getSite().registerContextMenu(manager, this.openLayersMapBrowser);

Menu menu = manager.createContextMenu(this.openLayersMapBrowser);
openLayersMapBrowser.setMenu(menu);

}

//---------------------------------------------------------- --------------

III) Third, I have subclassed MenuManager with OurMenuManager to add some
log :

//---------------------------------------------------------- --------------

class OurMenuManager extends MenuManager {

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#addMenuListener(org.ecl ipse.jface.action.IMenuListener)
*/
@Override
public void addMenuListener(IMenuListener listener) {
System.out.println("OurMenuManager.addMenuListener()");
super.addMenuListener(listener);
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#createMenuBar(org.eclip se.swt.widgets.Decorations)
*/
@Override
public Menu createMenuBar(Decorations parent) {
System.out.println("OurMenuManager.createMenuBar()");
return super.createMenuBar(parent);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#dispose()
*/
@Override
public void dispose() {
System.out.println("OurMenuManager.dispose()");
super.dispose();
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#findMenuUsingPath(java. lang.String)
*/
@Override
public IMenuManager findMenuUsingPath(String path) {
System.out.println("OurMenuManager.findMenuUsingPath()");
return super.findMenuUsingPath(path);
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#findUsingPath(java.lang .String)
*/
@Override
public IContributionItem findUsingPath(String path) {
System.out.println("OurMenuManager.findUsingPath()");
return super.findUsingPath(path);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getId()
*/
@Override
public String getId() {
System.out.println("OurMenuManager.getId()");
return super.getId();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getMenu()
*/
@Override
public Menu getMenu() {
System.out.println("OurMenuManager.getMenu()");
return super.getMenu();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getMenuText()
*/
@Override
public String getMenuText() {
System.out.println("OurMenuManager.getMenuText()");
return super.getMenuText();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getOverrides()
*/
@Override
public IContributionManagerOverrides getOverrides() {
System.out.println("OurMenuManager.getOverrides()");
return super.getOverrides();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getParent()
*/
@Override
public IContributionManager getParent() {
System.out.println("OurMenuManager.getParent()");
return super.getParent();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#getRemoveAllWhenShown()
*/
@Override
public boolean getRemoveAllWhenShown() {
System.out.println("OurMenuManager.getRemoveAllWhenShown()");
return super.getRemoveAllWhenShown();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#isDynamic()
*/
@Override
public boolean isDynamic() {
System.out.println("OurMenuManager.isDynamic()");
return super.isDynamic();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#isEnabled()
*/
@Override
public boolean isEnabled() {
System.out.println("OurMenuManager.isEnabled()");
return super.isEnabled();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#isGroupMarker()
*/
@Override
public boolean isGroupMarker() {
System.out.println("OurMenuManager.isGroupMarker()");
return super.isGroupMarker();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#isSeparator()
*/
@Override
public boolean isSeparator() {
System.out.println("OurMenuManager.isSeparator()");
return super.isSeparator();
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#isSubstituteFor(org.ecl ipse.jface.action.IContributionItem)
*/
@Override
public boolean isSubstituteFor(IContributionItem item) {
System.out.println("OurMenuManager.isSubstituteFor()");
return super.isSubstituteFor(item);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#isVisible()
*/
@Override
public boolean isVisible() {
System.out.println("OurMenuManager.isVisible()");
return super.isVisible();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#markDirty()
*/
@Override
public void markDirty() {
System.out.println("OurMenuManager.markDirty()");
super.markDirty();
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#removeMenuListener(org. eclipse.jface.action.IMenuListener)
*/
@Override
public void removeMenuListener(IMenuListener listener) {
System.out.println("OurMenuManager.removeMenuListener()");
super.removeMenuListener(listener);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#saveWidgetState()
*/
@Override
public void saveWidgetState() {
System.out.println("OurMenuManager.saveWidgetState()");
super.saveWidgetState();
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#setOverrides(org.eclips e.jface.action.IContributionManagerOverrides)
*/
@Override
public void setOverrides(IContributionManagerOverrides newOverrides) {
System.out.println("OurMenuManager.setOverrides()");
super.setOverrides(newOverrides);
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#setParent(org.eclipse.j face.action.IContributionManager)
*/
@Override
public void setParent(IContributionManager manager) {
System.out.println("OurMenuManager.setParent()");
super.setParent(manager);
}

/* (non-Javadoc)
* @see
org.eclipse.jface.action.MenuManager#setRemoveAllWhenShown(b oolean)
*/
@Override
public void setRemoveAllWhenShown(boolean removeAll) {
System.out.println("OurMenuManager.setRemoveAllWhenShown()");
super.setRemoveAllWhenShown(removeAll);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#setVisible(boolean)
*/
@Override
public void setVisible(boolean visible) {
System.out.println("OurMenuManager.setVisible()");
super.setVisible(visible);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#update()
*/
@Override
public void update() {
System.out.println("OurMenuManager.update()");
super.update();
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#update(boolean)
*/
@Override
public void update(boolean force) {
System.out.println("OurMenuManager.update(boolean force)");
super.update(force);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#update(java.lang.String )
*/
@Override
public void update(String property) {
System.out.println("OurMenuManager.update(property)");
super.update(property);
}

/* (non-Javadoc)
* @see org.eclipse.jface.action.MenuManager#updateAll(boolean)
*/
@Override
public void updateAll(boolean force) {
System.out.println("OurMenuManager.updateAll()");
super.updateAll(force);
}

public OurMenuManager() {
}

public OurMenuManager(String text) {
super(text);
}

@Override
public void fill(Composite parent) {
System.out.println("OurMenuManager.fill(Composite parent)");
super.fill(parent);
}
@Override
public void fill(CoolBar parent, int index) {
System.out.println("OurMenuManager.fill(CoolBar parent, int index)");
super.fill(parent, index);
}
@Override
public void fill(Menu parent, int index) {
System.out.println("OurMenuManager.fill(Menu parent, int index)");
super.fill(parent, index);
}
@Override
public void fill(ToolBar parent, int index) {
System.out.println("OurMenuManager.fill(ToolBar parent, int index)");
super.fill(parent, index);
}

@Override
public Menu createContextMenu(Control parent) {
System.out.println("OurMenuManager.createContextMenu()");
return super.createContextMenu(parent);
}

protected void update(boolean force, boolean recursive) {
System.out.println("OurMenuManager.update()");
super.update(force, recursive);
}
}


//---------------------------------------------------------- --------------

So when I click a first time on the cartographic map, I got the following
log :

//---------------------------------------------------------- --------------
mouseClicked() -->The catch of the click on the custom widget
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
OurMenuManager.markDirty()
OurMenuManager.getParent()
menuAboutToShow() --> The MenuListener is notified
OurMenuManager.getRemoveAllWhenShown()
OurMenuManager.update()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
OurMenuManager.getOverrides()
menuAboutToHide()

//---------------------------------------------------------- --------------

But when I click a second time, I just have this log :

//---------------------------------------------------------- --------------

mouseClicked()

//---------------------------------------------------------- --------------

I think there is a problem with the implementation of my contextual menu,
because in the third point, I noticed the menu listener are no more
notified when a change on the Actions list occurs.

However, it's seems to be possible to change dynamically the content of a
contextual menu like in the following demo :

http://rap.eclipse.org/rapdemo/rms

Regards,

Cédric G.
Re: Contextual menu on a custom widget [message #64173 is a reply to message #63741] Tue, 04 December 2007 08:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rherrmann.innoopract.com

Hi,

I am sorry, but from the various snippets I can't really see what's
going on and what goes wrong.
If you could attach a minimal self-contained plug-in that reproduces the
behavior, we could probably track down the problem.

Cheers,
Rüdiger


Cédric G. wrote:
> Hi,
>
> Thanks a lot for yours answers.
>
> I have tried this two modifications, but it seems to have no effect on
> the behavior of the contextual menu.
>
> Otherwise, I have modified some things in the functions of my
> CarthographicViewPart class (The ones displayed in my first post) :
>
> I) - First I replace the created Action objects with ContributionItem
> objects :
>
> //---------------------------------------------------------- --------------
>
> private void addActions(ArrayList<ModelObject> modelObjectList) {
>
> actionsList.clear();
>
> Iterator<ModelObject> iter = modelObjectList.iterator();
> while (iter.hasNext()) {
> final ModelObject modelObject = iter.next();
>
> Action action = new Action(modelObject.getName()) {
> public void run() {
>
> ModelObjectSelection.getInstance().setSelectedModelObject(
> modelObject);
> }
> };
> actionsList.add(new ModelObjectContributionItem(action));
> }
> }
>
> //---------------------------------------------------------- --------------
>
> II) In second, in the hookContextMenu function, I use IMenuListener2
> interface :
>
> //---------------------------------------------------------- --------------
>
> private void hookContextMenu() {
>
> manager = new OurMenuManager("#PopupMenu"); //$NON-NLS-1$
> manager.setRemoveAllWhenShown(true);
> manager.addMenuListener(new IMenuListener2() {
>
> @Override
> public void menuAboutToHide(IMenuManager manager) {
> System.out.println(".menuAboutToHide()");
>
> }
>
> public void menuAboutToShow(IMenuManager manager) {
> fillContextMenu(manager);
> System.out.println(".menuAboutToShow()");
> }
> });
> getSite().registerContextMenu(manager, this.openLayersMapBrowser);
>
> Menu menu = manager.createContextMenu(this.openLayersMapBrowser);
> openLayersMapBrowser.setMenu(menu);
>
> }
>
> //---------------------------------------------------------- --------------
>
> III) Third, I have subclassed MenuManager with OurMenuManager to add
> some log :
>
> //---------------------------------------------------------- --------------
>
> class OurMenuManager extends MenuManager {
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#addMenuListener(org.ecl ipse.jface.action.IMenuListener)
>
> */
> @Override
> public void addMenuListener(IMenuListener listener) {
> System.out.println("OurMenuManager.addMenuListener()");
> super.addMenuListener(listener);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#createMenuBar(org.eclip se.swt.widgets.Decorations)
>
> */
> @Override
> public Menu createMenuBar(Decorations parent) {
> System.out.println("OurMenuManager.createMenuBar()");
> return super.createMenuBar(parent);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#dispose()
> */
> @Override
> public void dispose() {
> System.out.println("OurMenuManager.dispose()");
> super.dispose();
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#findMenuUsingPath(java. lang.String)
> */
> @Override
> public IMenuManager findMenuUsingPath(String path) {
> System.out.println("OurMenuManager.findMenuUsingPath()");
> return super.findMenuUsingPath(path);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#findUsingPath(java.lang .String)
> */
> @Override
> public IContributionItem findUsingPath(String path) {
> System.out.println("OurMenuManager.findUsingPath()");
> return super.findUsingPath(path);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#getId()
> */
> @Override
> public String getId() {
> System.out.println("OurMenuManager.getId()");
> return super.getId();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#getMenu()
> */
> @Override
> public Menu getMenu() {
> System.out.println("OurMenuManager.getMenu()");
> return super.getMenu();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#getMenuText()
> */
> @Override
> public String getMenuText() {
> System.out.println("OurMenuManager.getMenuText()");
> return super.getMenuText();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#getOverrides()
> */
> @Override
> public IContributionManagerOverrides getOverrides() {
> System.out.println("OurMenuManager.getOverrides()");
> return super.getOverrides();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#getParent()
> */
> @Override
> public IContributionManager getParent() {
> System.out.println("OurMenuManager.getParent()");
> return super.getParent();
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#getRemoveAllWhenShown()
> */
> @Override
> public boolean getRemoveAllWhenShown() {
> System.out.println("OurMenuManager.getRemoveAllWhenShown()");
> return super.getRemoveAllWhenShown();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#isDynamic()
> */
> @Override
> public boolean isDynamic() {
> System.out.println("OurMenuManager.isDynamic()");
> return super.isDynamic();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#isEnabled()
> */
> @Override
> public boolean isEnabled() {
> System.out.println("OurMenuManager.isEnabled()");
> return super.isEnabled();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#isGroupMarker()
> */
> @Override
> public boolean isGroupMarker() {
> System.out.println("OurMenuManager.isGroupMarker()");
> return super.isGroupMarker();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#isSeparator()
> */
> @Override
> public boolean isSeparator() {
> System.out.println("OurMenuManager.isSeparator()");
> return super.isSeparator();
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#isSubstituteFor(org.ecl ipse.jface.action.IContributionItem)
>
> */
> @Override
> public boolean isSubstituteFor(IContributionItem item) {
> System.out.println("OurMenuManager.isSubstituteFor()");
> return super.isSubstituteFor(item);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#isVisible()
> */
> @Override
> public boolean isVisible() {
> System.out.println("OurMenuManager.isVisible()");
> return super.isVisible();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#markDirty()
> */
> @Override
> public void markDirty() {
> System.out.println("OurMenuManager.markDirty()");
> super.markDirty();
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#removeMenuListener(org. eclipse.jface.action.IMenuListener)
>
> */
> @Override
> public void removeMenuListener(IMenuListener listener) {
> System.out.println("OurMenuManager.removeMenuListener()");
> super.removeMenuListener(listener);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#saveWidgetState()
> */
> @Override
> public void saveWidgetState() {
> System.out.println("OurMenuManager.saveWidgetState()");
> super.saveWidgetState();
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#setOverrides(org.eclips e.jface.action.IContributionManagerOverrides)
>
> */
> @Override
> public void setOverrides(IContributionManagerOverrides
> newOverrides) {
> System.out.println("OurMenuManager.setOverrides()");
> super.setOverrides(newOverrides);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#setParent(org.eclipse.j face.action.IContributionManager)
>
> */
> @Override
> public void setParent(IContributionManager manager) {
> System.out.println("OurMenuManager.setParent()");
> super.setParent(manager);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#setRemoveAllWhenShown(b oolean)
> */
> @Override
> public void setRemoveAllWhenShown(boolean removeAll) {
> System.out.println("OurMenuManager.setRemoveAllWhenShown()");
> super.setRemoveAllWhenShown(removeAll);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#setVisible(boolean)
> */
> @Override
> public void setVisible(boolean visible) {
> System.out.println("OurMenuManager.setVisible()");
> super.setVisible(visible);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#update()
> */
> @Override
> public void update() {
> System.out.println("OurMenuManager.update()");
> super.update();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#update(boolean)
> */
> @Override
> public void update(boolean force) {
> System.out.println("OurMenuManager.update(boolean force)");
> super.update(force);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.action.MenuManager#update(java.lang.String )
> */
> @Override
> public void update(String property) {
> System.out.println("OurMenuManager.update(property)");
> super.update(property);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.action.MenuManager#updateAll(boolean)
> */
> @Override
> public void updateAll(boolean force) {
> System.out.println("OurMenuManager.updateAll()");
> super.updateAll(force);
> }
>
> public OurMenuManager() {
> }
>
> public OurMenuManager(String text) {
> super(text);
> }
>
> @Override
> public void fill(Composite parent) {
> System.out.println("OurMenuManager.fill(Composite parent)");
> super.fill(parent);
> }
> @Override
> public void fill(CoolBar parent, int index) {
> System.out.println("OurMenuManager.fill(CoolBar parent, int
> index)");
> super.fill(parent, index);
> }
> @Override
> public void fill(Menu parent, int index) {
> System.out.println("OurMenuManager.fill(Menu parent, int
> index)");
> super.fill(parent, index);
> }
> @Override
> public void fill(ToolBar parent, int index) {
> System.out.println("OurMenuManager.fill(ToolBar parent, int
> index)");
> super.fill(parent, index);
> }
>
> @Override
> public Menu createContextMenu(Control parent) {
> System.out.println("OurMenuManager.createContextMenu()");
> return super.createContextMenu(parent);
> }
>
> protected void update(boolean force, boolean recursive) {
> System.out.println("OurMenuManager.update()");
> super.update(force, recursive);
> }
> }
>
>
> //---------------------------------------------------------- --------------
>
> So when I click a first time on the cartographic map, I got the
> following log :
>
> //---------------------------------------------------------- --------------
> mouseClicked() -->The catch of the click on the custom widget
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> OurMenuManager.markDirty()
> OurMenuManager.getParent()
> menuAboutToShow() --> The MenuListener is notified
> OurMenuManager.getRemoveAllWhenShown()
> OurMenuManager.update()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> OurMenuManager.getOverrides()
> menuAboutToHide()
>
> //---------------------------------------------------------- --------------
>
> But when I click a second time, I just have this log :
>
> //---------------------------------------------------------- --------------
>
> mouseClicked()
>
> //---------------------------------------------------------- --------------
>
> I think there is a problem with the implementation of my contextual
> menu, because in the third point, I noticed the menu listener are no
> more notified when a change on the Actions list occurs.
>
> However, it's seems to be possible to change dynamically the content of
> a contextual menu like in the following demo :
>
> http://rap.eclipse.org/rapdemo/rms
>
> Regards,
>
> Cédric G.
>
>
Re: Contextual menu on a custom widget [message #64824 is a reply to message #64173] Fri, 07 December 2007 13:34 Go to previous message
Eclipse UserFriend
Originally posted by: cedric.gaillot.thalesraytheon-fr.com

Hi,

I finally found the problem is located. It's seems to be not specific to
Custom Widget.

As an exemple, I get the same problem when adding a context menu on a
Composite.

But not with a TableViewer.

Here the code of a Viewpart with a composite Object and a popup menu
attached to it:

package com.trs.rap.widgets.map;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener2;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;

public class TestViewpart extends ViewPart {

private MenuManager manager = null;

private MyComposite myComposite = null;

private boolean test = false;

public void createPartControl(final Composite parent) {

// A grid (one column as layout)
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
parent.setLayout(gridLayout);

myComposite = new MyComposite(parent, SWT.BORDER);
myComposite.setLayout(gridLayout);

hookContextMenu();
}

/**
* Hook a contextual menu to the cartographic map.
*/
private void hookContextMenu() {

manager = new MenuManager("#PopupMenu1"); //$NON-NLS-1$
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener2() {

@Override
public void menuAboutToHide(IMenuManager manager) {
System.out.println(".menuAboutToHide()");
}

public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
System.out.println(".menuAboutToShow()");
}
});
getSite().registerContextMenu(manager, this.myComposite);
Menu menu = manager.createContextMenu(myComposite);
myComposite.setMenu(menu);
}

/**
* Fill the contextual menu with actions.
*
* @param manager
* The menu manager
*/
private void fillContextMenu(IMenuManager manager) {

Action action = null;

if (test) {

action = new Action("TEST1") {
public void run() {
}
};

test = false;

} else {
action = new Action("TEST2") {
public void run() {
}
};

test = true;
}

manager.add(action);

}


@Override
public void setFocus() {
// TODO Auto-generated method stub

}
}

class MyComposite extends Composite implements ISelectionProvider {

public MyComposite(Composite parent, int style) {
super(parent, style);
// TODO Auto-generated constructor stub
}

@Override
public void addSelectionChangedListener(ISelectionChangedListener
listener) {
// TODO Auto-generated method stub

}

@Override
public ISelection getSelection() {
// TODO Auto-generated method stub
return null;
}

@Override
public void removeSelectionChangedListener(
ISelectionChangedListener listener) {
// TODO Auto-generated method stub

}

@Override
public void setSelection(ISelection selection) {
// TODO Auto-generated method stub

}


}



So, with the code above, the content of the popup menu should display
TEST1 then TEST2 each time I click on the composite. It doesn't work with
Composite althougth it works with other widget like TableViewer.

My opinion is that selection changes on the ISelectionProvider interface
are used to rebuilt the popup menu, but I don't still manage to find how.

Regards,

Cédric G.
Previous Topic:some question about resource registry
Next Topic:List listener problem
Goto Forum:
  


Current Time: Fri Apr 26 07:50:56 GMT 2024

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

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

Back to the top