Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 11:33 Go to next message
Hans is currently offline HansFriend
Messages: 36
Registered: July 2009
Member
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 #465720 is a reply to message #465712] Tue, 20 December 2005 13:12 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
This is a multipart message in MIME format.
--=_alternative 00488731802570DD_=
Content-Type: text/plain; charset="US-ASCII"

If you use the wizard to create a plugin project and use the "with a view
template", that has the view menu and toolbar actions. You should be able
to get the code from there.
--=_alternative 00488731802570DD_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">If you use the wizard to create a plugin
project and use the &quot;with a view template&quot;, that has the view
menu and toolbar actions. You should be able to get the code from there.</font>
--=_alternative 00488731802570DD_=--
Re: Adding a context menu like in the Eclipse Views [message #465785 is a reply to message #465720] Tue, 20 December 2005 13:48 Go to previous messageGo to next message
Hans is currently offline HansFriend
Messages: 36
Registered: July 2009
Member
Phill_Perryman@Mitel.COM wrote:
>
> If you use the wizard to create a plugin project and use the "with a
> view template", that has the view menu and toolbar actions. You should
> be able to get the code from there.

These use the IActionBars from the view's site. This object already has
a menu manager built-in, and I find it difficult to track (in the
Eclipse source code) where this is ultimatively defined/implemented.

Hans
Re: Adding a context menu like in the Eclipse Views [message #465798 is a reply to message #465712] Tue, 20 December 2005 16:03 Go to previous messageGo to next message
Jeremy Dowdall is currently offline Jeremy DowdallFriend
Messages: 48
Registered: July 2009
Member
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 17:04 Go to previous messageGo to next message
Hans is currently offline HansFriend
Messages: 36
Registered: July 2009
Member
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 19:22 Go to previous messageGo to next message
Jeremy Dowdall is currently offline Jeremy DowdallFriend
Messages: 48
Registered: July 2009
Member
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 #465871 is a reply to message #465785] Wed, 21 December 2005 01:33 Go to previous messageGo to next message
Christopher Goy is currently offline Christopher GoyFriend
Messages: 38
Registered: July 2009
Member
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.

Chris
"Hans" <hans______@yahoo.com> wrote in message
news:do922k$uhr$1@utils.eclipse.org...
> Phill_Perryman@Mitel.COM wrote:
>>
>> If you use the wizard to create a plugin project and use the "with a view
>> template", that has the view menu and toolbar actions. You should be able
>> to get the code from there.
>
> These use the IActionBars from the view's site. This object already has a
> menu manager built-in, and I find it difficult to track (in the Eclipse
> source code) where this is ultimatively defined/implemented.
>
> Hans
Re: Adding a context menu like in the Eclipse Views [message #465873 is a reply to message #465863] Wed, 21 December 2005 08:15 Go to previous messageGo to next message
Hans is currently offline HansFriend
Messages: 36
Registered: July 2009
Member
Jeremy Dowdall wrote:
> 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);
> }
> }
> });

Yes, this is working! Thanks a lot. I expected to be a more
standardized solution for this in Eclipse, but I don't care as long as
it does what I need. It is still a mystery how this is implemented in
the standard Views...

Hans
Re: Adding a context menu like in the Eclipse Views [message #465874 is a reply to message #465871] Wed, 21 December 2005 08:16 Go to previous message
Hans is currently offline HansFriend
Messages: 36
Registered: July 2009
Member
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
Previous Topic:ILazyTreeContentProvider
Next Topic:64-bit version of SWT for Windows
Goto Forum:
  


Current Time: Mon Dec 09 05:23:49 GMT 2024

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

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

Back to the top