adding/removing actions toolbar when editor has focus - eclipse-plugin

I'm extending Eclipse using the Eclipse plugin infrastructure, and I've come into a problem:
I created a text editor and I would like to add actions to the Eclipse toolbar when my editor is open and has focus. For example:
textViewer.getTextWidget().addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
/* add actions */
}
public void focusLost(FocusEvent e) {
/* remove actions */
}
});
The following example of extensionPoint: ActionSet, add the action button to the toolbar permanently:
<action
class="MyActionClass"
id="MyActionID"
label="MyActionLabel"
menubarPath="MyActionMenuBarPath"
toolbarPath="MyActionToolBarPath" <-- this property
...
</action>
how to make this dynamically?

Thank you for your response, I found a simple way to do this, simply add the following extension point if the buttons are ActionSet:
<extension
point="org.eclipse.ui.actionSetPartAssociations">
<actionSetPartAssociation
targetID="myActionSetId">
<part
id="myEditorId">
</part>
</actionSetPartAssociation>

You could look at the Eclipse implementation of similar dynamic toolbar updates.
For example, the Breadcrumb bare can only be activated for Java Editor, and the toolbar "Toggle Breadcrumb" button will not be visible for any other type of editors.
alt text http://img109.imageshack.us/img109/359/eclipsetoolbarupdate.png
That is a ToggleBreadcrumbAction, declared in plugin.xml as
<actionSet
label="%javaEditorPresentationActionSet.label"
visible="false"
id="org.eclipse.jdt.ui.text.java.actionSet.presentation">
<action
allowLabelUpdate="true"
style="toggle"
toolbarPath="org.eclipse.ui.edit.text.actionSet.presentation/Presentation"
id="org.eclipse.jdt.ui.edit.text.java.toggleMarkOccurrences"
definitionId="org.eclipse.jdt.ui.edit.text.java.toggleMarkOccurrences"
disabledIcon="$nl$/icons/full/dtool16/mark_occurrences.gif"
icon="$nl$/icons/full/etool16/mark_occurrences.gif"
helpContextId="toggle_mark_occurrences_action_context"
label="%toggleMarkOccurrences.label"
retarget="true"
tooltip="%toggleMarkOccurrences.tooltip">
</action>
<action
allowLabelUpdate="true"
style="toggle"
toolbarPath="org.eclipse.ui.edit.text.actionSet.presentation/Presentation"
id="org.eclipse.jdt.ui.edit.text.java.toggleBreadcrumb"
definitionId="org.eclipse.jdt.ui.edit.text.java.toggleBreadcrumb"
disabledIcon="$nl$/icons/full/dtool16/toggle_breadcrumb.gif"
icon="$nl$/icons/full/etool16/toggle_breadcrumb.gif"
helpContextId="toggle_mini_browser_action_context"
label="%toggleBreadcrumb.label"
retarget="true"
tooltip="%toggleBreadcrumb.tooltip">
</action>
</actionSet>
You can try the same kind of definition.

Related

How to create a dynamic submenu in contextmenu for outlook (VSTO)

I have a ribbon xml where I want to add something similar as in the picture. I tried create a button and connect the menu to the button but I never got the arrow indication there is a underlying menu. I have no faith at all in that button is the correct element to use. Been googling for hours now and would be happy if anyone can send me in some kind of direction. There is no problem for me to add the element in the context menu, the problem is the dynamic menu linked to the first element.
The control type you're looking for is dynamicMenu
Here is the ribbon XML:
<dynamicMenu id="mycustomid" label="My custom label" getContent="GetMyCustomContent" />
And the code:
public string GetMyCustomContent(IRibbonControl control)
{
return "<menu xmlns=\"http://schemas.microsoft.com/office/2009/07/customui\">"
+ "<button id=\"anotherid\" label=\"another label\" onAction=\"DoWhatever\"/>"
+ "</menu>";
}
public string DoWhatever(IRibbonControl control)
{
}

How to open html page contents directly in eclipse CSH

I am using the dynamic help content in order to load default CSH for particular page in eclipse (policy in my case)
Following is the code snippet::
return new IContextProvider() {
public IContext getContext(Object target) {
return HelpSystem
.getContext("com.dummy.summary");
}
public int getContextChangeMask() {
return IContextProvider.SELECTION;
}
public String getSearchExpression(Object target) {
return null;
}
I have added the entry of CONTEXT_ID in corresponding xml file.
<context id="summary" title="Dummy POLICY">
<topic href="html/PII/policy_summary.htm" label="Policy Summary"/>
</context>
Currently when I open the page and press F1 button for fetching context sensitive help
for that particular page, on right side of eclipse a help dialog appears with Title
"Dummy Policy" and link "Policy Summary" appears and on click these link (policy_summary.htm) pages opens.
What I want is to open these policy_summary.htm page directly in the dialog box appearing on right side of the window on pressing F1 key.How to achieve that?
You can include a description element in the context, the contents of this are shown on the dialog.
An example from the JDT context helps:
<context id="typing_preference_page_context">
<description>On this page you can indicate your smart typing preferences for the Java editor.
The preferences on this page are only considered if <b>Smart Insert Mode</b> appears in the status line. This can be toggled in the <b>Edit</b> menu.</description>
<topic label="Java editor preferences" href="reference/preferences/java/ref-preferences-editor.htm"/>
<topic label="Java editor concepts" href="concepts/concept-java-editor.htm"/>
<topic label="Java editor reference" href="reference/views/ref-java-editor.htm"/>
</context>

disable menu based on value of a preference variable

I have a menu item XYZ. I have a preference page MyPref which has a check box and a couple of text fields. When the check box in MyPref is check, I want menu XYZ to be enabled else it should be disabled. Is there a way to achieve this.
Yes, the way certainly exist.
1). Create an entry in your IPreferenceStore - just get the class which extends AbstractUIPlugin and do the following:
IPreferenceStore store = IExtendAbstractUIPlugin.getDefault()
.getPreferenceStore();
then store some value which will reflect your checkbox state:
preferenceStore.setValue("XYZMenuPreference", false); // if disabled
or
preferenceStore.setValue("XYZMenuPreference", true); // if enabled
2). Create a Property Tester extention to your plugin.xml:
<extension point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="org.xyz.PropertyTester"
id="org.xyz.PropertyTester"
type="java.lang.Object"
namespace="org.xyz"
properties="XYZmenu">
</propertyTester>
</extension>
3). When declaring your menu handler in plugin.xml you should add the following:
<handler
class="your-menu-handler-class"
commandId="your-command-id">
<enabledWhen>
<with variable="selection">
<test property="org.xyz.XYZmenu" value="true" forcePluginActivation="true"/>
</with>
</enabledWhen>
</handler>
4). Now you need a class "org.xyz.PropertyTester" (as defined in plugin.xml) which will extend the org.eclipse.core.expressions.PropertyTester and override method test(<some args>) where he must check the property value:
if (property.equals("XYZmenu"){
IPreferenceStore store = IExtendAbstractUIPlugin.getDefault()
.getPreferenceStore();
return store.getBoolean("XYZMenuPreference");
}
5). After that add a change listener to your checkbox and use it to re-evaluate a visibility of your menu item:
IEvaluationService service = (IEvaluationService) PlatformUI
.getWorkbench().getService(IEvaluationService.class);
service.requestEvaluation("org.xyz.XYZmenu");
it will force the method test() from your org.xyz.PropertyTester class to be called and will enable your menu item if preference
"XYZMenuPreference" is set to true.
upd.
namespace - a unique id determining the name space the properties are added to
properties - a comma separated list of properties provided by this property tester
this is from official eclipse tutorial so you can feel free to define any namespace and property name, but the you should use it like in point 5.

Eclipse plugin: toggle menu command fails to find handler

I am trying to use toggle menu option but probably doing some mistake. Following code created toggle menu item and When I click this option for the first time after loading, it works fine but when I click again to toggle it back to old state, I get following error:
org.eclipse.core.commands.NotHandledException: There is no handler to execute for command my.commands.compileAutomatically
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:485)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
....
Menu:
<command
commandId="my.commands.compileAutomatically"
label="Compile Automatically"
style="toggle">
</command>
Command:
<command
defaultHandler="my.handlers.CompileAutomaticallyHandler"
id="my.commands.compileAutomatically"
name="Compile Automatically">
<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>
</command>
Handler:
public class CompileAutomaticallyHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Command command = event.getCommand();
boolean oldValue = HandlerUtil.toggleCommandState(command);
System.out.println(oldValue);
return null;
}
}
Could somebody help me understand what is wrong with my code that it finds handler only once regardless of current menu state?
thanks
Try to override isHandled() and return false in CompileAutomaticallyHandler.

How to add a pulldown button in a view's toolbar?

I need to add a pulldown button to a view's toolbar in an Eclipse plugin.
Actually buttons in the toolbar are added like that :
<extension point="org.eclipse.ui.viewActions">
<viewContribution id="..." targetId="$MyViewId$">
<action id="..."
toolbarPath="action1"
class="Class extending Action and implementing IViewActionDelegate">
</action>
</viewContribution>
</extension>
I've figured it out. Two ways: one using org.eclipse.ui.viewActions extension, the other with org.eclipse.ui.menus
Using org.eclipse.ui.viewActions extension (eclipse >= 3.5)
action's style must set to pulldown
<extension point="org.eclipse.ui.viewActions">
<viewContribution id="..." targetId="$MyViewId$">
<action id="..."
toolbarPath="action1"
class="xxx.MyAction"
style="pulldown">
</action>
</viewContribution>
</extension>
action class must implement IViewActionDelegate (required for an action contributing to a view toolbar) and IMenuCreator (defines the menu behavior).
public class RetrieveViolationsViewActionDelegate implements IViewActionDelegate, IMenuCreator
{
private IAction action;
private Menu menu;
// IViewActionDelegate methods
...
// IMenuCreator methods
public void selectionChanged(IAction action, ISelection selection)
{
if (action != this.action)
{
action.setMenuCreator(this);
this.action = action;
}
}
public void dispose()
{
if (menu != null)
{
menu.dispose();
}
}
public Menu getMenu(Control parent)
{
Menu menu = new Menu(parent);
addActionToMenu(menu, new ClassImplemententingIAction());
return menu;
}
public Menu getMenu(Menu parent)
{
// Not use
return null;
}
private void addActionToMenu(Menu menu, IAction action)
{
ActionContributionItem item= new ActionContributionItem(action);
item.fill(menu, -1);
}
}
Using org.eclipse.ui.menus (eclipse >= 3.3)
Add a new menucontribution to the org.eclipse.ui.menus extension point.
Set the location URI to toolbar:IdOfYourView
Add a toolbar to this extension and a new command to this new toolbar.
Change the command style to pulldown
Create a new menucontribution and set the locationURI to menu:IdOfThePullDownCommand
Add commands to this menu.
More info