Eclipse plugin: toggle menu command fails to find handler - eclipse-plugin

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.

Related

Cannot enable a Ribbon button programmatically

I developed a VSTO 4 add-in for Excel. It works perfect, however, I have a button placed in the custom tab of its Ribbon control that is initially disabled.
After clicked other ribbon button in my custom tab, I need to enable the initially disabled button.
I tried with:
btnCancelar.Visible = true;
In the Click event of a button, but button is not shown. The strange thing is that when debugging, it still does not appear, but if a MessageBox is shown, the button get visible at last.
I don't understand this behaviour. How can I enable or disable a ribbon button dynamically by code?
I'm not sure what your language is used in your project, but I guess you can tranform it to your own language used. I'll show the example here in C#:
First you need to implement a so called Callback function in the RibbonXML definition:
<button id="buttonSomething" label="Content" size="large" getVisible="EnableControl"/>
then the next step is to implement the Callback function:
public bool EnableControl(IRibbonControl control)
{
return true; // visible ... false = invisible
}
VSTO will trigger the getVisible Callback and depending on the return value enable or disable the visible state (don't forget to remove any Visible property from the RibbonXML, otherwise the Callback is not triggered)
In case of the Ribbon Designer you need to make sure your Click signature is correct, the easies way to do that is by double clicking the button on the ribbon designer. This will create the Click method for you, for instance:
I created a Ribbon with the Ribbon designer and added two buttons. Double clicked the first button to get an empty method like below, and added the code.
private void button1_Click(object sender, RibbonControlEventArgs e)
{
// Toggle button visibility and make sure the button is enabled
// Visible (obviously) makes it visible, while Enabled is grayed if
// false. You don't need this it is Enabled by default, so just for
// demo purposes
button2.Visible = !button2.Visible;
button2.Enabled = button2.Visible;
// Force Ribbon Invalidate ...
this.RibbonUI.Invalidate();
// Long running proces
}
This worked perfectly for me, so if it doesn't work for you please provide more details of your coding.
I have created a workaround to this.
It was simple. Just started the long running process in different thread. That way, cancel button is shown when it should and then hidden after the process ends.
I used this code to launch the process in the Ribbon.cs code:
btnCancelar.Visible = true;
Action action = () => {
Formatter.GenerateNewSheet(Formatter.TargetType.ImpresionEtiquetas, frm.CustomerID, workbook, btnCancelar);
};
System.Threading.Tasks.Task.Factory.StartNew(action);
And inside the process method I have this code:
public static bool GenerateNewSheet(TargetType type, string customerID, Excel.Workbook workbook, Microsoft.Office.Tools.Ribbon.RibbonButton btnCancelar)
{
try
{
_cancelled = false;
InfoLog.ClearLog();
switch (type)
{
case TargetType.ImpresionEtiquetas:
return GenerateTagPrinting(customerID, workbook);
}
return false;
}
finally
{
btnCancelar.Visible = false;
}
}
The interesting thing here I have discovered is that Excel is thread safe, so it was not necessary to add a synchronization mechanism neither when adding rows in the new sheet nor when setting Visible property to false again.
Regards
Jaime

DirectoryDialog in custom Wizard with SWT and JFace

I have to create a custom wizard to develop a Eclipse Plug-in. I wish to use a DirectoryDialog but I can't get work with the other elements. I'm seeing that the DirectoyDialog is used in a "extends composite" class, but, is there any way to use in a "wizardPage"?
Thanks at all!
org.eclipse.swt.widgets.DirectoryDialog extends Dialog and can only be used as a pop-up dialog. It cannot be embedded in a wizard.
You can put a Button on the wizard page that displays the directory dialog when clicked.
Use the following code in your WizardPage
Button btnBrowse = new Button(container, SWT.NONE);
btnBrowse.setText("Browse..");
btnBrowse.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event e) {
DirectoryDialog dirDialog = new DirectoryDialog(getShell());
dirDialog.setText("Select the parent directory for tools");
String location = dirDialog.open();
}
});
getShell() api used in 4th line is from WizardPage class.

Application Bar Button Command and Can Execute Method

I have an application bar button that I have hooked up to a command using Prism like so:
<i:Interaction.Behaviors>
<prismInteractivity:ApplicationBarButtonCommand ButtonText="save" CommandBinding="{Binding SaveCommand}" />
</i:Interaction.Behaviors>
The save command looks like this:
SaveCommand = new RelayCommand( Save, CanSave );
The CanSave method is only getting called when the page loads. The button then gets disabled because false is returned the first time (due to validation).
How can I get the CanSave method to fire again so it can be enabled?
I figured this out by looking at the source to the TailSpin Prism demo app.
When you want it to fire again, like when text in a text box has changed, just call:
SaveCommand.RaiseCanExecuteChanged();
So, in my case, when the Name property is set, I want it to run again.
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged( () => Name );
SaveCommand.RaiseCanExecuteChanged();
}
}

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

adding/removing actions toolbar when editor has focus

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.