How to add a button to the main toolbar in Eclipse programmatically - eclipse-plugin

I've a question. I cannot find the way, how to add buttons to main toolbar programmatically. My problem is, that I've the task to dynamically (based on XML configuration file) build menus and toolbar. I found how to add a menu item programmatically, but not toolbar button.
Tutorials mostly show how to create buttons and menus using plugin descriptor (plugin.xml), but not how to do it programatically. It seems, that it is out of bounds of Eclipse plugin philosophy.
I've just found this:
There might be layout problems with this approach. I also don't
believe the framework will try and re-create your dynamic item except
at random toolbarmanager updates. With Menus they can be updated on an
SWT.Show event, which is why CompoundContributionItem only applies to
Menus.
What shall I do? Can I say Sorry, there is no way to build toolbar dynamically. I can do it just for menus? Collegue says, that it must be possible, but he does neither know how.

The only way to be able to create main toolbar entries programmatically is in an RCP app, where you supply the ActionBarAdvisor for the workbench window. This isn't dynamic, however, just called on window creation.
Another way to do it would be to use org.eclipse.ui.menus and contribute org.eclipse.ui.menus.ExtensionContributionFactory. It also works only on workbench window creation (not really dynamic), but you could read your own XML and provide IContributionItems for the main menu or toolbar.
How dynamic are you trying to be? Most solutions work well on startup/window creation.
PW

Whenever you try to do something programmatically in Eclipse that is normally done through plugin definitions you are walking on thin ice. I've tried it on a few occasions and it rarely ended up being easy or good.
Instead, think of what it is that you only know at runtime and need to be able to change on the fly. Is it the name or icon of the button? That can be changed at runtime.
Take a look at runtime commands, they can be confusing to define properly, but with them you can for example create buttons that are only visible if a condition is active. That condition could be set at runtime.

Related

IntelliJ Plugin Development: LookupElement with class preview

For a custom language I created a CompletionContributor. Everything works fine. But I'm limited to the information I can display (only in the list or the bottom "advertisement" and only 1 line).
When trying to auto-complete on a java class name it will display more information on the selected line in a small side window. I would like to exploit that mechanism but I really don't know how it is done.
When looking at the options provided, I can use a custom LookupElementRenderer but there is no method in LookupElementPresentation related to the right window.
Any idea how it is done?
Are you referring to documentation popup which may be displayed if the corresponding setting (Setting->Editor->General->Code completion->Show the documentation popup in) is turned on?
If you want the feature to work for your language you have to use lang.documentationProvider extension point.

How to reopen ViewPart?

I am working to make an Eclipse Plugin. I used a plugin project template that generated a View class which extends ViewPart. I think that it is part of SWT.
My problem is that the View is like a window inside of the main Frame which has buttons for close, minimize. I clicked on the X button of the View by mistake. Now I cannot make it visible again even if I relaunch the Eclipse Application.
Now, my Eclipse application looks like this:
It had some panels and buttons before. But I cannot make the View visible again. How should I make to bring it back? I have tried to delete the plugin project from Eclipse and import it again. But it did not work.
I bet that there it is an easy way to make the View visible again but I do not know how. Is there any setting through MANIFEST file? Or other file?
Specify the -clearPersistedState argument when you run the RCP (this assumes you are using Eclipse 4.x).
1) quick solution
restart workbench with clear workspace option checked under run configuration ..
2) Full Solution
you should add a menu in menu bar to open your view so that you can open your view when ever you want
create a command say openMyMenu
create a handler for it and call below code from handler execute method.
add that command to main menu bar..
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(<view ID>);

Editor context Menu - Eclipse-RCP

I have an Eclipse RCP application. I have created an Editor. There are few context menu (default), when I right click on the Editor. I have not created these menus.
Please let me know, How to remove the context menu of the Editor?
It needs different approach by which editor you extends.
Let me know What you extends, than I can answer more efficient one.
In general way:
IWorkbenchParSite#registerContextMenu(...) will be used, So find where calls that, override it. It is not recommend. Because by doing this, Menu Extensions which is contributed for your editor will not work anymore.
If you mean the system menu that appears on editor tabs and view tabs, that menu is provided by the presentation (2.1, Classic, Default, etc). There is no tweak to simply modify it.
The 2 ways to remove it would be:
write your own presentation, using
the
org.eclipse.ui.presentations.StackPresentation
API and matching extension point.
Writing a presentation is a involved
undertaking.
Change the internal classes in the
org.eclipse.ui.workbench plugin
and patch that plugin in your RCP
app.
If you use Text or StyleText you will get the system default menu (cut,copy,paste, maybe something about encoding or input). If you are not going to supply your own menu, simply create an empty SWT Menu and set it:
Menu emptyMenu = new Menu(text);
text.setMenu(emptyMenu);
Eclipse also has a text editing framework, if you need more than a basic text box you should check it out. http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Text_Editors

How can I put an IEditorPart in a modal dialog?

(I'm something of an Eclipse newbie, so apologise for any dumbness on my part...)
I have a number of editors (derived from IEditorPart) in my RCP app, and a requirement has arisen that one particular editor needs to be also available in a modal dialog box (along with some extra controls) opened by one of the other editor classes. The editor to be embedded consists of the main viewer control, toolbar, and a couple of dozen helper classes (label providers, comparators, etc).
The options before me appear to be:
Find a way to put an editor area, IWorkbenchWindow-style, into a (JFace or SWT) Dialog.
Not had much luck searching for how to do this
Create a new WorkBenchWindow with a Perspective that just the editor area visible, and no views. Make this modal.
On trying this, the new window seemed to inherit things (menus etc) from its parent window.
Refactor the editor in question so all of it now resides on a single control, then embed this control in both the editor and the dialog.
Potentially time-consuming, given the number of places the helper classes refer back to the main editor object.
If it turns out that this is a truly perverse and anti-idiomatic thing to want to do, in Eclipse terms, can you suggest a wiser course of action?
Thanks
EditorParts are meant to be inside workbenchwindows. There is a lot of code that depends on this behaviour. So I would suggest not to do (1). If the result is acceptable (a workbench window, which is modal), the easier way is to hang on to (2).

Display a pulldown item in Eclipse programatically

I know that I can declare actions in plugin.xml that specify style="pulldown" in order to make a pulldown style toolbar button, but I want to be able to do it programatically, using the IToolBarManager and IContributionItem interfaces.
I've looked through the interfaces, and the implementations of various things in eclipse, but I can't see how I'd do it. Ideally I want to add such a toolbar item from an IEditorActionBarContributor implementation.
Anyone got any ideas?
In answer to my own question, you need to create an Action object, and then call setMenuCreator on the action. Then when you add the action to the toolbar you'll get a drop down menu.