Creating own toolbar leveraging existing org.eclipse.ui.menus extension - eclipse

I have custom editor for eclipse. For particular reasons this editor provide two toolbar areas which are not based on standard action bars provided by eclipse for editor. This two places are dedicated for other plugins to contribute. My intention is to leverage "org.eclipse.ui.menus" extension point with custom menuContribution/locationURI so other plugins can contribute using this extension and associated toolbar:my.editor.toolbar1 and toolbar:my.editor.toolbar2 as locationURI.
My problem is how to "connect" my ToolBar with particular location. I tried following approach but result are not good. I created custom ToolbarContributionRoot event if I should not and also created CustomContributionFactory which extends ExtensionContributionFactory. It works pretty well, but problem is with pulldown commands which submenu is not resolved correctly.
toolbarManager = new ToolBarManager(SWT.FLAT);
ToolbarContributionRoot toolbarRoot = new ToolbarContributionRoot(toolbarManager);
IServiceLocator workbench = PlatformUI.getWorkbench();
IConfigurationElement[] allMenuElements
= Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.menus");
for (IConfigurationElement menuContribution : allMenuElements) {
String locationURI = menuContribution.getAttribute("locationURI");
if ("toolbar:my.editor.toolbar1".equals(locationURI)) {
try {
ExtensionContributionFactory factory = CustomContributionFactory.create(menuContribution);
factory.createContributionItems(workbench, toolbarRoot);
} catch (CoreException e) {
e.printStackTrace();
}
}
}
toolbar = toolbarManager.createControl(root);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, false, false, 1, 1);
toolbar.setLayoutData(gridData);
toolbar.pack();
plugin.xml of "user" looks like this:
<extension point="org.eclipse.ui.menus" id="my.helper.id">
<menuContribution locationURI="toolbar:my.editor.toolbar1">
<command commandId="my.editor.special.command1" />...
Do you have any suggestions how to blend my custom toolbars and "org.eclipse.ui.menus" extension together?

Correct way how to do it is:
toolbarManager = new ToolBarManager(SWT.FLAT);
IServiceLocator workbench = PlatformUI.getWorkbench();
IMenuService menuService = (IMenuService) workbench.getService(IMenuService.class);
menuService.populateContributionManager(toolbarManager, TOOLBAR_LOCATION);
toolbar = toolbarManager.createControl(root);

Related

Eclipse RCP. How to make different ToolBarItems for different Perspectives

I am quite new to Eclipse RCP development.
In my Eclipse RCP application there are different perspectives. I want them to have different ToolBarItems. According to the official documentation the contents of this toolbar change based on the active perspective. But after tons of googling I still have no idea how.
My best idea is as follows:
First I create the items and I add them to toolbar and coolbar
protected void fillCoolBar(ICoolBarManager coolBar) {
IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
ActionContributionItem saveItem = new ActionContributionItem(saveAction);
saveItem.setId(ApplicationActionBarAdvisor.SAVE);
toolbar.add(saveItem);
ActionContributionItem saveAllItem = new ActionContributionItem(saveAllAction);
saveAllItem.setId("saveAllItem");
toolbar.add(saveAllItem);
coolBar.add(new ToolBarContributionItem(toolbar, "main"));
}
and then, in the Perspective class I override the createInitialLayout() method and I just hide the unnecessary item.
#Override
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
PageLayout pl = (PageLayout) layout;
pl.addHiddenToolBarItemId(ApplicationActionBarAdvisor.SAVE);
pl.setEditorAreaVisible(false);
pl.addStandaloneView(View.ID, false, IPageLayout.LEFT, 0.25f, editorArea);
pl.getViewLayout(View.ID).setCloseable(false);
}
It does not work, but I have no idea what I am missing. Any help is highly appreciated.
Use the org.eclipse.ui.perspectiveExtensions extension point and contriubte an actionSet to the perspective.
For example, this is part of the JDT plugin:
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension targetID="org.eclipse.debug.ui.DebugPerspective">
<perspectiveShortcut id="org.eclipse.jdt.ui.JavaPerspective"/>
<perspectiveShortcut id="org.eclipse.jdt.ui.JavaBrowsingPerspective"/>
<actionSet id="org.eclipse.jdt.ui.JavaActionSet"/>
<showInPart id="org.eclipse.jdt.ui.PackageExplorer"/>
</perspectiveExtension>
Note: Do not use internal classes such as PageLayout they may change at any time and in fact this class was in Eclipse 3 but has been deleted in Eclipse 4.

Open DialectEditor programmatically outside of main editor area (E3/E4 hybrid)

what I want to do:
In my RCP an E3/E4 hybrid I have a project and library based on sirius tree. The User can drag an drop item from the library tree to the project tree. This works fine and was no great problem to build in. So now I want to make the UI more usable. It should looks like this layout:
what works:
After application startup I open my library presentation with the DialectUIManager.
final DialectEditor editor = (DialectEditor)
DialectUIManager.INSTANCE.openEditor(siriusSession, description, monitor);
Okay, this works. But it open it in the editor in the part market as org.eclipse.ui.editorss. This it not what I want
what does not work:
I want to show it in the "Library Part". I can move it manually with the mouse after open the editor, but how can i tell DialectUIManager to open it direct there. Or how can I programmatically it move there.
I do a lot of google research but i don't found a solution. The only thing I found was a hint Pierre-Charles David https:// www. eclipse.org/forums/index.php?t=msg&th=998476&goto=1631138&#msg_1631138
If you need is simply to show the editor outside of the main editor
area, this is possible since Eclipse 4.2 (e4 does not really treat the
main editor area as something special), so you can have your editor
"around" another editor in the middle of other views.
But at this step I stuck. I also ask it in the Sirius Forum but they say its a Eclipse E4 problem
Thanks for help, code snippets or links to correct part of manual.
I've found a solution. It's not very nice, but it works. I execute these code here after the editors have opened.
What the code does:
He is looking for the MPlaceholder which has the ID: org. eclipse. ui. editorss. There he descends until he is with the parts. These are in the Compatibly editor mode. Then he chooses the part we wants to move out of and Attach them to the MPartStack target.
public static void movePart(MApplication application,
EModelService modelService) {
MPart partToMove = null;
MUIElement muiElement =
modelService.find("org.eclipse.ui.editorss", application);
if (muiElement instanceof MPlaceholder) {
MPlaceholder placeholder = (MPlaceholder) muiElement;
MUIElement ref = placeholder.getRef();
if (ref instanceof MArea) {
MArea area = (MArea) ref;
List<MPartSashContainerElement> children = area.getChildren();
for (MPartSashContainerElement mPartSashContainerElement
: children) {
if (mPartSashContainerElement instanceof MPartStack) {
MPartStack partStack = (MPartStack) mPartSashContainerElement;
List<MStackElement> children2 = partStack.getChildren();
for (MStackElement mStackElement : children2) {
if (mStackElement instanceof MPart) {
MPart part = (MPart) mStackElement;
// Library is the Editor Name wiche I want to move
if (part.getLabel().equals("Library")) {
partToMove = part;
break;
}
}
}
}
}
}
}
if (partToMove != null) {
moveElement(modelService, application, partToMove);
}
}
private static void moveElement(EModelService modelService,
MApplication application, MPart part) {
// target PartStack
MUIElement find = modelService.find("de.bsg.onesps.rcp.
partstack.library", application);
if (find instanceof MPartStack) {
MPartStack mPartStack = (MPartStack) find;
mPartStack.getChildren().add(part);
mPartStack.setSelectedElement(part);
}
}

Eclipse RCP: Can I remove Orphaned Perspectives from the Perspective Bar?

I'm developing an RCP that has two product versions, a core app and one with some extensions. If a user opens the core app after having opened the extended app in the same workspace, eclipse detects a perspective used only in the extended app and makes a local copy of it, so it shows up in the perspective toolbar as an orphaned extension.
I created an activity to hide the extended app perspective when running the core app. That hides it from the perspective menu and the perspective shortcut menu, but it doesn't remove it from the perspective toolbar.I also tried detecting orphaned perspectives from the active page of the active workbench window (by looking for angle brackets in the label) and removing them with PlatformUI.getWorkbench().getPerspectiveRegistry().deletePerspective(perspective), but this doesn't affect the perspective toolbar either. The perspective I'm removing is not present in the core app.
Is there a way I can access the perspective toolbar programmatically so I can remove any orphaned perspectives? Or any other approach tha would work?
I thought a good solution would be creating a custom perspective switcher, but that path was blocked by an eclipse bug. There is a suggested workaround, but it did not work for me. I created a custom perspective switcher toolbar, but I could find no way to make it update when perspectives are opened or activated. My attempts are documented here.
I removed the orphan perspectives in a workspace shutdown hook, but for some reason an NPE is thrown by the E4 workbench (LazyStackRenderer line 238) when I select a perspective in the switcher that was opened but not selected when I launched the app.
I got it to work as desired by closing all open perspectives on shutdown, after storing their IDs in a preference value, and then opening them again when the app is launched in the WorkbenchWindowAdvisor. It's a bit of a hack, but it's the only way I could find to avoid the E4 workbench NPE, which also prevents setting the perspective from the toolbar until it's closed and re-opened from the Window menu.
Here's my code.
...
IWorkbench workbench = ...
static final String PERPSECTIVE_ID_1 = ...
static fnal String PERSPECTIVE_ID_2 = ...
static final String PREFERENCE_KEY = ...
workbench.addWorkbenchListener( new IWorkbenchListener() {
public boolean preShutdown( IWorkbench workbench, boolean forced ) {
IPerspectiveDescriptor[] openPerspectives = page.getOpenPerspectives();
page.closeAllPerspectives(false, false);
StringBuilder sb = new StringBuilder();
String delim = "";
for (IPerspectiveDescriptor persp : openPerspectives) {
if (!persp.getId().equals(PERSPECTIVE_ID_1) && !persp.getId().equals(PERSPECTIVE_ID_2) {
sb.append(delim + persp.getId());
delim = ";";
}
}
getPreferenceStore().setValue(PREF_KEY, sb.toString());
return true;
}
public void postShutdown( IWorkbench workbench ) {
}
});
class MyWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
static final String PRODUCT_ID_1 = ...
static final String PRODUCT_ID_2 = ...
static final String PREFERENCE_KEY = ...
...
#Override
public void postWindowOpen() {
IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
String savedOpenPerspectiveStr = getPreferenceStore().getString(PREFERENCE_KEY);
if (!"".equals(savedOpenPerspectiveStr)) {
List<IPerspectiveDescriptor> openPerspectives = new ArrayList<IPerspectiveDescriptor>();
String[] perspectiveIds = savedOpenPerspectiveStr.split(";");
if (perspectiveIds.length == 0) {
openPerspectives.add(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(savedOpenPerspectiveStr));
} else {
for (String id : perspectiveIds) {
openPerspectives.add(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(id));
}
}
//successively setting perspectives causes them to appear in the perspective switcher toolbar
for (IPerspectiveDescriptor persp : openPerspectives) {
page.setPerspective(persp);
}
}
//now we set the appropriate perspective
if (Platform.getProduct().getId().equals(PRODUCT_ID_1)) {
page.setPerspective(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(PERSPECTIVE_ID_1));
} else if (Platform.getProduct().getId().equals(PRODUCT_ID_2)) {
page.setPerspective(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(PERSPECTIVE_ID_2));
}
}
...
}

Remove "File, edit,...etc" menus from Eclipse RCP application

I want to remove the File, edit, Source, Refactor, etc. menus from my RCP application
Can I use hideActionSet() ? or what should I do ?
That's right; in your ApplicationWorkbenchWindowAdvisor, override postWindowOpen().
The tricky bit is usually figuring out the names of the actionsets that you want to remove, but you can use the old standby ALT-SHIFT-F2 (the default keybinding for 'Plugin-in Menu Spy') and click on one of the menu items that you want to remove.
Note that if the menu item is disabled, the spy won't give you any info on it.
public void postWindowOpen() {
runApplicationWorkbenchDelegate();
// remove unwanted UI contributions that eclipse makes by default
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (int i = 0; i < windows.length; ++i) {
IWorkbenchPage page = windows[i].getActivePage();
if (page != null) {
// hide generic 'File' commands
page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
// hide 'Convert Line Delimiters To...'
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo");
// hide 'Search' commands
page.hideActionSet("org.eclipse.search.searchActionSet");
// hide 'Annotation' commands
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.annotationNavigation");
// hide 'Forward/Back' type navigation commands
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.navigation");
}
}
}
Although the question is old:
Lars Vogel's tutorial about Eclipse Activities shows how to hide entire menus in an RCP application rather than removing single menu-entries.
EDIT:
Alternatively you can use the MenuManager attached to the workbench window to show or hide Menus/Contributions.
Try the following code to hide all menus:
WorkbenchWindow workbenchWin = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
MenuManager menuManager = workbenchWin.getMenuManager();
IContributionItem[] items = menuManager.getItems();
for(IContributionItem item : items) {
item.setVisible(false);
}

How can I add submenus for pop up menu programatically?

In my plugin, I have a pop up menu with menu item 'X' and I want to add submenu to this menu item
and the number and labels of menu items in the submenu and their action will change.
I think I can';t do this from plugin.xml, so how to do this programatically?
In your plugin.xml, under org.eclipse.ui.menus, add a menuContribution that refers to the id of your "root" menu, i.e. the menu that you want to have your submenus attached to (in this case, menu:myDynamicMenuRoot):
<menuContribution
allPopups="true"
class="com.myCode.menus.MyDynamicMenuContributions"
locationURI="menu:myDynamicMenuRoot">
</menuContribution>
Note that allPopups="true" ensures that your submenus will be added to any menu with the id myDynamicMenuRoot that you add anywhere in your application.
Finally, create a class extending ExtensionContributionFactory, whose job it will be to create your dynamic submenu items. Here I add items based on commands I have defined in my plugin.xml:
public class MyDynamicMenuContributions extends ExtensionContributionFactory {
private static final ImageDescriptor GREEN_STAR = Plugin.getImageDescriptor("icons/green_star.png");
#Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
// build a couple of command-based contribution parameters
CommandContributionItemParameter pAA = new CommandContributionItemParameter(
serviceLocator,
"Submenu_CommandAA",
"my.package.command.myCommandAA",
SWT.PUSH);
pAA.icon = GREEN_STAR;
pAA.label = "Command AA";
CommandContributionItemParameter pBB = new CommandContributionItemParameter(
serviceLocator,
"Submenu_CommandBB",
"my.package.command.myCommandBB",
SWT.PUSH);
pBB.icon = GREEN_STAR;
pBB.label = "Command BB";
// create actual contribution items and add them to the given additions reference
CommandContributionItem itemAA = new CommandContributionItem(pAA);
itemAA.setVisible(true);
additions.addContributionItem(itemAA, null);
CommandContributionItem itemBB = new CommandContributionItem(pBB);
itemBB.setVisible(true);
additions.addContributionItem(itemBB, null);
}
}