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

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.

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 customize the context menu in the solution explorer for a specific project type?

Description
I have developed a Visual Studio extension (VSPackage) which adds a new Project Type to Visual Studio (using CPS Project System). I also have added some Commands to the VSPackage.
When right clicking on my Project Node in the Solution Explorer, I want to have a customized context menu to appear.
Example
For example: in the screen shot below, I want to get rid of the Build command and add a custom command (e.x. mycommand).
I tried..
Setting the Parent of my custom command to IDM_VS_CTXT_PROJNODE.
Question
When I create a new Custom Project Type, How to create a new Context Menu for my Project Nodes in the Solution Explorer?
How to remove/add Commands to the Context Menu only for the Custom Projects:
If I have a C# project, the context menu should be the default one, if I add a MyProjectType project, I want to see a different context menu when right clicking on the Project Node in the Solution Explorer.
You were close with the IDM_VS_CTXT_PROJNODE parent.
Here is how I achieved it in my FluentMigratorRunner extension, which only shows its context menu item for a project if it has a reference to the FluentMigrator NuGet package.
Step 1: Add a submenu to the context menu
<Menus>
<Menu guid="guidCmdSet" id="packageMenu" priority="0x0300" type="Menu">
<Parent guid="guidSHLMainMenu" id="IDG_VS_CTXT_PROJECT_BUILD" />
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<Strings>
<ButtonText>CPSProject</ButtonText>
<CommandName>CPSProject</CommandName>
</Strings>
</Menu>
Note the added special CommandFlag elements.
Step 2: Add a group to the menu
<Groups>
<Group guid="guidCmdSet" id="packageMenuGroup" priority="0x0600">
<Parent guid="guidCmdSet" id="packageMenu" />
</Group>
</Groups>
Step 3: Add a button
<Button guid="guidCmdSet" id="specialBuildActionId" priority="0x0100" type="Button">
<Parent guid="guidCmdSet" id="packageMenuGroup" />
<CommandFlag>DynamicVisibility</CommandFlag>
<Strings>
<ButtonText>Special build</ButtonText>
</Strings>
Step 4: Add the menu in your *Package.cs
protected override async System.Threading.Tasks.Task InitializeAsync(System.Threading.CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Initialize the Fluent Migrator Menu, should only be visible for projects with FluentMigrator reference
var mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
var menuCommandId = new CommandID(packageCmdSetGuidString, 0x1010);
var menuItem = new OleMenuCommand(null, menuCommandId);
menuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;
mcs.AddCommand(menuItem);
}
private void MenuItem_BeforeQueryStatus(object sender, EventArgs e) =>
((OleMenuCommand)sender).Visible = ???;
Note the added BeforeQueryStatus eventhandler.
In that eventhandler you can check the type of the project and return a boolean controlling if the extra context menu should be shown yes or no
I'm not sure about removing the existing Build menu item... but as for having context menu items appear only when right-clicking on your custom project type, <VisibilityConstraints> might help:
(Excerpt from an example usage):
Often a package is being loaded to run a BeforeQueryStatus method for a button to determine its visibility. With <VisibilityConstraints> we can determine the visibility of a button without running BeforeQueryStatus and therefore don't need to load the package before the user clicks the button.
I've only ever used this strategy to show menu items for specific file types, but there are also some term types related to projects that might be more useful for this scenario.

How to add action item to the coolbar of e4 eclipse rcp application?

I am currently trying to port my eclipse 3 rcp application to e4.The major hurdle I am facing is to use action item which i was using in e3.In eclipse 3 application i was creating action item of coolbar by extending action.The code was looking like below spinets.
public class Testaction extends Action {
private IWorkbenchWindow window;
public Testaction (IWorkbenchWindow window, String string) {
setText(string);
setToolTipText(string);
setId("ID");
setImageDescriptor(Activator.getImageDescriptor("/icons/some.png"));
this.window = window;
}
#override
public void run() {
/**
Do something
**/
super.run();
}
was adding it to coolbar through
toolbar.add(demoaction);
But with e4 this part seems to be changed and I understand that there we need to have annotation #Execute which will excute the contribution which we will be giving through setcontribuitionuri as below snippet
part.setContributionURI(
"bundleclass://bundle/bundle.contribuitionclass");
I just want to know whether I can use my old action class here or i need to port everything to newer style .
Any help on this will be appreciated.Thanks in advance...
e4 does not support Actions for model elements in the Application.e4xmi.
The simplest conversion is to use a Direct ToolItem in the tool bar. However using a Handled ToolItem with a Command and Handler is more flexible.
In either case the Image, Label and Tooltip are specified in the Application.e4xmi.

Eclipse RCP: Can I add a toolbar to an Editor Part?

I have succesfully managed to add view-specific toolbars to my RCP application by adding the following block to the org.eclipse.ui.menus extension point:
<menuContribution
allPopups="false"
locationURI="toolbar:my.package.path.views.ClassOfMyView">
<dynamic
class="my.package.path.toolbars.ViewToolBar"
id="MyViewToolbar">
</dynamic>
</menuContribution>
However, trying to do the same with my editorpart does not seem to work:
<menuContribution
allPopups="false"
locationURI="toolbar:my.package.path.views.ClassOfMyEditorPart">
<dynamic
class="my.package.path.toolbars.EditorPartToolBar"
id="MyEditorpartToolbar">
</dynamic>
</menuContribution>
Is there something obvious that I am missing, or is this simply not supported in RCP?
Following up on the hint about the Presentation API given by Tonny Madsen, I've come up with a solution to my problem. My solution is based on the following articles:
Eclipse RCP - Hide minimize / maximize buttons for editors and views (showing me how to supply my own presentation factory)
Incompatibilities between Eclipse 3.7 and 4.2 (helping me figure out why my presentation factory wasn't being used)
Custom RCP editor with the view header (helping me figure out how to make sure my presentation factory was being used)
To make my solution work, I ended up with changes in two files and a new class:
In the plugin.xml file, I added the following extension:
<extension point="org.eclipse.ui.presentationFactories">
<factory
name="Extended Presentation Factory"
class="org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory"
id="org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory"
/>
</extension>
This seems to be ignored by Eclipse 4.x, so to cater for those cases, I added the following line to the plugin_customisation.ini file:
org.eclipse.ui/presentationFactoryId = org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory
I then created the corresponding class:
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.internal.presentations.util.TabbedStackPresentation;
import org.eclipse.ui.presentations.IStackPresentationSite;
import org.eclipse.ui.presentations.StackPresentation;
import org.eclipse.ui.presentations.WorkbenchPresentationFactory;
#SuppressWarnings("restriction")
public class ExtendedPresentationFactory extends WorkbenchPresentationFactory {
private ToolBarManager m_toolBarManager = null;
private ToolBar m_toolbar = null;
#Override
public StackPresentation createEditorPresentation(final Composite parent, final IStackPresentationSite site) {
final TabbedStackPresentation presentation = (TabbedStackPresentation) super.createViewPresentation(parent, site);
m_toolbar = new ToolBar(presentation.getTabFolder().getToolbarParent(), 0);
m_toolBarManager = new ToolBarManager(m_toolbar);
m_toolBarManager.add(new MyAction1());
m_toolBarManager.add(new MyAction2());
m_toolBarManager.add(new MyAction3());
m_toolBarManager.update(true);
presentation.getTabFolder().setToolbar(m_toolbar);
return presentation;
}
}
This adds a toolbar with three buttons to my editor area.
I will still need to fiddle with some of the finer points (placement etc), but for a start it satisfies my need for a toolbar in the editor area.
These are some important visual differences between editors and views:
Editors provide additional actions to the top-level menu and the tool bar; views do not*
Views have an independent menu bar and tool bar; editors do not
There are at most one active editor in a perspective; there can be any number of active views
The are at most one view of each “type”; there can be any number of editors*
Editors are all placed in the “editor area”; views are placed in a number of “view stacks”
Some of these differences (marked with * above) can be ironed out using various functionality of the RCP platform:
The interface ISaveblePart can give a view the same life-cycle as an editor.
The menues extension point can add items to the main menu and tool bar when a view is active.
The views extension point can be used to allow multiple views of the same type.
With Eclipse 4, you can also combine editors and views in the same stack or folder.
But adding a menu or tool bar as seen with views require more work! But it can be done using the Presentation API, where most of the visual difference can be ironed out.
One additional comment: It is not toolbar:my.package.path.views.ClassOfMyView but toolbar:id-of-the-view.

Update property view on click of project explorer, eclipse plugin

I have created a custom project in project explorer. Whenever I click on custom project folder currently it shows default property sheet but I want to customize this property sheet.
I have gone through the tabbed property example but I am not able customize it.
Please can anyone provide me some sample examples or code for same.
Thanks.
how to connect that property view to an editor or project explorer
model classes for you custom project and its folders should implement IAdaptable interface and return an object implementing IPropertySource that describes given element. it will be passed to properties view automatically when you click on the element.
alternativelly, you can avoid implementing IAdaptable and create an IAdapterFactory that converts an instance of you project/folder element into corresponding IPropertySoure but then you have to make Eclipse framework aware of your IAdapterFactory implementation.
public class MyProjectAdapterFactory implements IAdapterFactory {
#Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType== IPropertySource.class && adaptableObject instanceof MyProject){
return new MyProjectPropertySource((MyProject) adaptableObject);
}
return null;
}
#Override
public Class[] getAdapterList() {
return new Class[] { IPropertySource.class };
}
}
register it in you plugin.xml file:
<extension point="org.eclipse.core.runtime.adapters">
<factory adaptableType="my.example.MyProject" class="my.example.MyProjectAdapterFactory">
<adapter type="org.eclipse.ui.views.properties.IPropertySource"/>
</factory>
</extension>
look at the full tutorial: http://www.vogella.de/articles/EclipsePlugIn/article.html