How to get the menu name of an IAction? - eclipse

I've defined a menu and an action in plugin.xml.
When
IAction.run(IAction action){}
is called can I retrieve the name of the menu of this action?
The reason: I've a lot of actions which trigger different wizards. I have to use the menu names as wizard titles. So I thought if I trigger the wizards from the run() method I can retrieve the menu names and set the wizard titles. Otherwise I'll have to copy all the menu names from plugin.properties to messages.properties.

Call the getText method of the action parameter:
public void run(IAction action)
{
String text = action.getText();
... more
}

Related

Eclipse hide action or actionset in Navigate menu

I am extending an extension point org.eclipse.ui.actionSets where I am creating element actionSet which contains action element inside. This asctionset is placed inside Navigate menu of eclipse For example:
<actionSet
label="%CNavigationActionSet.label"
description="%CNavigationActionSet.description"
visible="false"
id="xxx.ui.NavigationActionSet">
<action
class="xxx.opentype.OpenTypeAction"
definitionId="xxx.navigate.opentype"
helpContextId="xxxui.open_type_action"
icon="icons/xxx.gif"
id="xxx.ui.actions.OpenType"
label="Open Element"
menubarPath="navigate/open.ext2"
toolbarPath="org.eclipse.search.searchActionSet/Search"
tooltip="%OpenTypeAction.tooltip">
</action>
</actionSet>
Now I want to hide this action when certain conditions are met in Eclipse. Is there a way to hide (set visibility to false etc.) of this action in navigate menu?
Right now best what I figured out is to disable action when conditions are met, but this is not enough:
public class OpenTypeAction implements IWorkbenchWindowActionDelegate {
//... some code
#Override
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(!areConditionsMet());
}
//... some code
}
/Edit: Solution for me would be also to hide whole actionSet instead of action, is that possible?
/Edit2: To clarify what my conditions are - my editor can contain files with multiple file extensions, I want to show this action only for files which end with certain file extension.

How to get the Mpart from the part's handled tool item?

I want to to get the Mpart from which the handle tool item belongs when user clicks on handled toolitem of the toolbar (Here the command which will be executed on clicking is common across these toolitems).I tried using activepart but it seems if any other part is active in other partstack this will return wrong value .Any pointer on this will be helpful.Thanks in advance.
Use #Named(IServiceConstants.ACTIVE_PART) to get the correct active part in your handler #Execute or #CanExecute methods:
#Execute
public void execute(#Named(IServiceConstants.ACTIVE_PART) MPart activePart)

Add icons to elements in ElementListSelectionDialog

I have the following code to create a dialog in a RCP Eclipse application using the ElementListSelectionDialog class:
ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
dialog.setTitle("test");
dialog.setMessage("test");
dialog.setMultipleSelection(false);
dialog.setElements(new String[]{"test1", "test2", "test3"});
dialog.open();
The previous code generates this dialog:
This is fine but I also want to add icons to the elements in the list, similar to how it looks the web.xml editor:
You need to extend the LabelProvider you are passing to the ElementListSelectionDialog constructor and override the
public Image getImage(Object element)
method. This will be called for each of the objects you add to the dialog with the setElements method.

Eclipse Plugin Open Preferences Page From Menu Item

I have a
public class PrefMenu extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
implementing the init() and createFieldMethods(). And I do see it in Window>Preferences>PREFMENU_NAME after adding the class to the preferencePage extension.
But how do I open the preference page from a menu-item? I created a command and a handler and the execute()-Method (which works with other commands) does...
//other commands
} else if (commandID.equals(PREFERENCES_COMMAND_ID)){
final PrefMenu prefMenu = new PrefMenu();
prefMenu.init(PlatformUI.getWorkbench());
}
Yet nothing happens when I click on the menu-Item. In the debug mode I see it simply executes the init()-Method and returns. But I want it to open up the Preferences-Window and only close it when I click on OK or Cancel.
Use org.eclipse.ui.dialogs.PreferencesUtil to do this:
String id = ... your preference page id
Shell shell = ... parent shell to use ...
PreferencesUtil.createPreferenceDialogOn(shell, id, new String[] {id}, null).open()
This will open just your preference page in the preferences dialog. You can adjust the string array to include other pages or specify null for the array to show all preference pages.

Eclipse RCP: how to get Show View menu instead of a dialog

I've added to my perspective's org.eclipse.ui.menus
<command
commandId="org.eclipse.ui.views.showView"
style="pulldown">
</command>
This adds Show View item to main menu, but this item is not a menu (as in the Eclipse Window menu). Instead pressing it shows a dialog where I can select a view. How do I get a menu instead?
You have to create ContributionItem class like below:
public class MyShowViewContributionItem extends org.eclipse.ui.internal.ShowViewMenu {
public MyShowViewContributionItem() {
this("om.myplugin.myShowViewId");
}
public MyShowViewContributionItem(String id) {
super(org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow(), id);
}
}
then in your plugin.xml org.eclipse.ui.menus extension:
<menu
label="My Show View">
<dynamic
class="com.myplugin.MyShowViewContributionItem"
id="com.myplugin.myShowViewId">
</dynamic>
</menu>
Cheers,
Max
Just to share on my recent experiment in trying to do the same thing, what Max suggested in his answer will work but leaves you using internal code (resulting in a 'Discouraged Access' warning).
Another approach is to build the menu through your applications action bar advisor. Although, this approach will leave you to having to write code (oppose to use providing menu contributions in the plugin XML definition). Consider the following example:
public class ApplicationActionBarAdvisor extends ActionBarAdvisor
{
private IContributionItem contributionOpenPerspective;
private IContributionItem contributionShowView;
...
protected void makeActions(IWorkbenchWindow window)
{
...
contributionOpenPerspective = ContributionItemFactory.
PERSPECTIVES_SHORTLIST.create(window);
contributionShowView = ContributionItemFactory.
VIEWS_SHORTLIST.create(window);
...
}
protected void fillMenuBar(IMenuManager menuBar)
{
...
MenuManager windowMenu = new MenuManager("&Window",
IWorkbenchActionConstants.M_WINDOW);
menuBar.add(windowMenu);
MenuManager openPerspectiveMenu = new MenuManager("&Open Perspective");
openPerspectiveMenu.add(perspectivesContribution);
windowMenu.add(openPerspectiveMenu);
MenuManager showViewMenu = new MenuManager("Show &View");
showViewMenu.add(viewsContribution);
windowMenu.add(showViewMenu);
...
}
}
A possible downside to this approach is with the interaction between menus created in the advisor and menus created by menu contributions. Since advisor menu items are created before menu contributions, you are left to deal with adding more sorting logic in your menu contributions. This might be fine for most people, however, you lose the 'feel' of a centralized menu structure from org.eclipse.ui.menus (even if the feeling is an illusion when other plugins come into play with their own menu contributions).
I've also included the building of a perspective menu as well; completely option, but I added it if anyone was attempting to perform the same menu building with perspectives.