Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Adding a context menu like in the Eclipse Views
Adding a context menu like in the Eclipse Views [message #465712] |
Tue, 20 December 2005 06:33  |
Eclipse User |
|
|
|
I need to add a context menu to a toolbar, to get a similar look and
feel like the arrow-pointing-down in various Eclipse views. For example
the Package Explorer's toolbar has a button on the right which on
mouse-down displays a menu (with items such as Show, Select working set
etc). How is this implemented?
I couldn't dig deep enough into the Eclipse source to see how this is
implemented, but my first attempts were to add an Action to the Toolbar
with the image as icon and then I try to add a mouse listener to the
item. However, at the point when I want to add the listener, the
corresponding Button control is not created yet.
Any ideas - thanks!
Hans
|
|
| | |
Re: Adding a context menu like in the Eclipse Views [message #465798 is a reply to message #465712] |
Tue, 20 December 2005 11:03   |
Eclipse User |
|
|
|
http://www.eclipse.org/swt/snippets/
under "ToolBar, ToolItem" is "place a drop down menu in a tool bar"
this will, however, create a toolbar with a down arrow centered
vertically. to get it more like the eclipse view's, AFAIK, you'll have
to use your own icon (make it 9x16 pixels) by changing:
final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
to
final ToolItem item = new ToolItem (toolBar, SWT.FLAT);
and then setting the image with item.setImage(image)
Hans wrote:
> I need to add a context menu to a toolbar, to get a similar look and
> feel like the arrow-pointing-down in various Eclipse views. For example
> the Package Explorer's toolbar has a button on the right which on
> mouse-down displays a menu (with items such as Show, Select working set
> etc). How is this implemented?
>
> I couldn't dig deep enough into the Eclipse source to see how this is
> implemented, but my first attempts were to add an Action to the Toolbar
> with the image as icon and then I try to add a mouse listener to the
> item. However, at the point when I want to add the listener, the
> corresponding Button control is not created yet.
>
> Any ideas - thanks!
> Hans
|
|
|
Re: Adding a context menu like in the Eclipse Views [message #465862 is a reply to message #465798] |
Tue, 20 December 2005 12:04   |
Eclipse User |
|
|
|
Thanks, Jeremy. The snippets page is a very useful reference. Your
suggestion with the smaller image is good (actually 10x16 is the correct
size).
However, the remaining problem is that I don't see how I can react on
SWT.MouseDown - the only event that seems to work is SWT.Selection.
SWT.MouseDown does not seem to trigger any event.
final ToolItem item = new ToolItem (toolBar, SWT.FLAT);
item.addListener(SWT.Selection, new Listener () {
public void handleEvent (Event event) {
openContextMenu(toolBar, item);
}
});
Hans
Jeremy Dowdall wrote:
> http://www.eclipse.org/swt/snippets/
> under "ToolBar, ToolItem" is "place a drop down menu in a tool bar"
>
> this will, however, create a toolbar with a down arrow centered
> vertically. to get it more like the eclipse view's, AFAIK, you'll have
> to use your own icon (make it 9x16 pixels) by changing:
> final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
> to
> final ToolItem item = new ToolItem (toolBar, SWT.FLAT);
> and then setting the image with item.setImage(image)
>
>
> Hans wrote:
>
>> I need to add a context menu to a toolbar, to get a similar look and
>> feel like the arrow-pointing-down in various Eclipse views. For
>> example the Package Explorer's toolbar has a button on the right which
>> on mouse-down displays a menu (with items such as Show, Select working
>> set etc). How is this implemented?
>>
>> I couldn't dig deep enough into the Eclipse source to see how this is
>> implemented, but my first attempts were to add an Action to the
>> Toolbar with the image as icon and then I try to add a mouse listener
>> to the item. However, at the point when I want to add the listener,
>> the corresponding Button control is not created yet.
>>
>> Any ideas - thanks!
>> Hans
|
|
|
Re: Adding a context menu like in the Eclipse Views [message #465863 is a reply to message #465862] |
Tue, 20 December 2005 14:22   |
Eclipse User |
|
|
|
Hans,
thanks for catching this - I breezed right by once the menu was
dropping where I wanted it!
upon further inspection, the only solution I'm seeing is adding the
mouseDown listener to the toolbar itself and checking to see if the
item's bounds contain the event's x & y coordinates:
toolBar.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
if(item.getBounds().contains(e.x, e.y)) {
openContextMenu(toolBar, item);
}
}
});
any other thoughts?
Hans wrote:
> Thanks, Jeremy. The snippets page is a very useful reference. Your
> suggestion with the smaller image is good (actually 10x16 is the correct
> size).
>
> However, the remaining problem is that I don't see how I can react on
> SWT.MouseDown - the only event that seems to work is SWT.Selection.
> SWT.MouseDown does not seem to trigger any event.
>
>
> final ToolItem item = new ToolItem (toolBar, SWT.FLAT);
> item.addListener(SWT.Selection, new Listener () {
> public void handleEvent (Event event) {
> openContextMenu(toolBar, item);
> }
> });
>
> Hans
>
>
> Jeremy Dowdall wrote:
>
>> http://www.eclipse.org/swt/snippets/
>> under "ToolBar, ToolItem" is "place a drop down menu in a tool bar"
>>
>> this will, however, create a toolbar with a down arrow centered
>> vertically. to get it more like the eclipse view's, AFAIK, you'll
>> have to use your own icon (make it 9x16 pixels) by changing:
>> final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
>> to
>> final ToolItem item = new ToolItem (toolBar, SWT.FLAT);
>> and then setting the image with item.setImage(image)
>>
>>
>> Hans wrote:
>>
>>> I need to add a context menu to a toolbar, to get a similar look and
>>> feel like the arrow-pointing-down in various Eclipse views. For
>>> example the Package Explorer's toolbar has a button on the right
>>> which on mouse-down displays a menu (with items such as Show, Select
>>> working set etc). How is this implemented?
>>>
>>> I couldn't dig deep enough into the Eclipse source to see how this is
>>> implemented, but my first attempts were to add an Action to the
>>> Toolbar with the image as icon and then I try to add a mouse listener
>>> to the item. However, at the point when I want to add the listener,
>>> the corresponding Button control is not created yet.
>>>
>>> Any ideas - thanks!
>>> Hans
|
|
| | |
Re: Adding a context menu like in the Eclipse Views [message #465874 is a reply to message #465871] |
Wed, 21 December 2005 03:16  |
Eclipse User |
|
|
|
Christopher Goy wrote:
> Hello Hans,
> Inside of the View class you can get a reference to the MenuManager via:
> getViewSite().getActionBars().getMenuManager();
> Then you can add your actions to this MenuManager and you will have a little
> down arrow icon on the top right of your view. Hope this works.
This would work, but I am not inside of a View but have a
custom-tailored composite on a ScrolledForm. Thanks anyway.
Hans
|
|
|
Goto Forum:
Current Time: Tue Jul 15 04:30:40 EDT 2025
Powered by FUDForum. Page generated in 0.55151 seconds
|