In the command handler for a radio state menu item, I prompt the user if they want to continue. If they choose no, I need to reset the selected menu item back to the previous value.
I am using HandlerUtil.updateRadioState(event.getCommand(), oldScope) but the menu stays with the new value.
Any suggestions?
Command Handler
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
if (HandlerUtil.matchesRadioState(event))
return null;
String scope = event.getParameter(RadioState.PARAMETER_ID);
if (MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm restart",
"Switching source of Motors requires a restart. Continue?")) {
int connection = MotorDbPreferencesPage.DATABASE_ACCESS_LOCAL;
if ("Cloud".compareToIgnoreCase(scope) == 0) {
connection = MotorDbPreferencesPage.DATABASE_ACCESS_CLOUD;
}
Activator.getDefault().getPreferenceStore().setValue(MotorDbPreferencesPage.DATABASE_ACCESS, connection);
PlatformUI.getWorkbench().restart();
} else {
String oldScope = "Local";
// switch it back to the way it was before the user selected it
if ("Local".compareToIgnoreCase(scope) == 0) {
oldScope = "Cloud";
}
HandlerUtil.updateRadioState(event.getCommand(), oldScope);
}
return null;
}
Command definition in plugin.xml
<command
id="com.client.eclipse.connection"
name="Connection">
<commandParameter
id="org.eclipse.ui.commands.radioStateParameter"
name="State"
optional="false">
</commandParameter>
<state
class="org.eclipse.ui.handlers.RadioState:Cloud"
id="org.eclipse.ui.commands.radioState">
</state>
</command>
And the menu definition from plugin.xml
<menu
label="Connection">
<command
commandId="com.client.eclipse.connection"
label="Local"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="Local">
</parameter>
</command>
<command
commandId="com.client.eclipse.connection"
label="Cloud"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="Cloud">
</parameter>
</command>
</menu>
It's your responsability to call
HandlerUtil.updateRadioState(event.getCommand(), scope);
to make the GUI displays the new state. Without calling it, the radio state won't change.
It means that in the case of user is aborting, don't call this method at all.
Related
The Open Perspective View shows a list of perspectives to which a user can switch. My custom perspective is there, too. Can I have it not listed or greyed out, if a certain condition bis not met?
The second best alternative that comes to my mind is to have it listed, but the actual switch is not carried out, if b == false.
The best way for me was to use the extension point org.eclipse.ui.activities, as greg-449 suggested, and add a propertyTester. In order to hide perspective related UI elements and not every UI element from the same plugin, one can use isEqualityPattern="true".
<extension point="org.eclipse.ui.activities">
<activity
description="Remove some UI Elements"
id="com.my.plugin.ui.hideUI"
name="HideUI">
<enabledWhen>
<test
forcePluginActivation="false"
property="com.my.plugin.ui.PlanningPropertyTester">
</test>
</enabledWhen>
</activity>
<activityPatternBinding
activityId="com.btc.edm.hidePlanning"
isEqualityPattern="true"
pattern="com.my.plugin.ui/id.of.the.perspective.to.hide">
</activityPatternBinding>
</extension>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.my.plugin.ui.MyPropertyTester"
id="com.my.plugin.ui.propertyTester"
namespace="com.my.plugin.ui"
properties="MyPropertyTester"
type="java.lang.Object">
</propertyTester>
</extension>
It is important to have a fully qualified name for type.
The MyPropertyTester class extends org.eclipse.core.expressions.PropertyTester and overrides the test method.
public class MyPropertyTester extends PropertyTester {
#Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (goodWeather()) {
return true;
return false;
}
}
I would like to add an item to the eclipse context menu only if a text is marked as selected
below is my XML relevant snippet:
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<instanceof value="org.eclipse.jface.text.ITextSelection"/>
</with>
</visibleWhen>
</command>
</menuContribution>
I tried the solution in How do you contribute a command to an editor context menu in Eclipse which does not resolve the issue
What am i missing?
Can you help?
ThanksAvner
Excuse my detailed question, I am a newb to eclipse programming
i tried:
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.test.ide.TextSelectedPropertyTester"
id="com.test.ide.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="com.test.ide.menu.commands.sampleCommand"
icon="icons/sample.png"
id="com.test.ide.menu.toolbars.sampleCommand"
tooltip="Popup test">
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected">
</test>
</adapt>
</with>
</visibleWhen>
</command>
</menuContribution>
created a new package - com.test.ide in it created a new class TextSelectedPropertyTester
however the class is never invoked
package com.test.ide;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.text.ITextSelection;
public class TextSelectedPropertyTester extends PropertyTester
{
#Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
System.out.println("testing");
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}
return false;
}
}
however the class is never used
what am I missing?
The problem is that editors normally always have a text selection set - it will just be zero length if no characters are selected.
I can't see a way to test this using existing expressions so it may be necessary to define your own property tester using the org.eclipse.core.expressions.propertyTester extension point.
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="tested.TextSelectedPropertyTester"
id="tested.propertyTester1"
namespace="tested"
properties="textSelected"
type="org.eclipse.jface.text.ITextSelection">
</propertyTester>
</extension>
use like this:
<visibleWhen>
<with variable="selection">
<adapt type="org.eclipse.jface.text.ITextSelection">
<test
property="tested.textSelected"
forcePluginActivation="true">
</test>
</adapt>
</with>
</visibleWhen>
The property tester is very simple:
public class TextSelectedPropertyTester extends PropertyTester
{
#Override
public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
{
if (receiver instanceof ITextSelection textSel) {
return textSel.getLength() > 0;
}
return false;
}
}
Note as shown the if statement uses a Java 16 instanceof type pattern, if you need to run with an older Java use:
if (receiver instanceof ITextSelection) {
return ((ITextSelection)receiver).getLength() > 0;
}
I define my own editor and have completion proposals like this
public IContentAssistant getContentAssistant(ISourceViewer sv) {
ContentAssistant ca = new ContentAssistant();
IContentAssistProcessor pr = new TagCompletionProcessor();
ca.setContentAssistProcessor(pr, IDocument.DEFAULT_CONTENT_TYPE);
return ca;
}
#Override
public char[] getCompletionProposalAutoActivationCharacters() {
String str = "._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return str.toCharArray();
}
So when I am pressing ctrl-space enter it will work, but I want it should always trigger computeCompletionProposals when any of the above characters are entered.
<extension
point="org.eclipse.ui.editors">
<editor
id="testingpluginproject.editors.XMLEditor"
name="Sample XML Editor"
icon="icons/sample.png"
extensions="xxml"
class="testingpluginproject.editors.XMLEditor"
contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor">
</editor>
</extension>
So what I am missing?
You must call the ContentAssistant enableAutoActivation method to enable auto activation:
ca.enableAutoActivation(true);
You might also want to look at implementing IContentAssistProcessorExtension rather than just IContentAssistProcessor as it provides a better isCompletionProposalAutoActivation method.
Is it possible to add menucontribution, basically a new button, to the toolbar:org.eclipse.ui.main.toolbar dynamically?
I tried with AbstractContributionFactory but it seems to be bugged, since the createContributionItems is never called.
My code so far tries to add a new button to the Eclipse toolbar after pressing another button. The thing is that the createContributionItems is never called so "Creating Stuff" never appears:
//If a certain button is pressed run will be executed
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"Runtimecommands",
"Hello, Eclipse world");
IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
AbstractContributionFactory factory = new AbstractContributionFactory("toolbar:org.eclipse.ui.main.toolbar", null)
#Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
System.out.println("Creating Stuff");
};
menuService.addContributionFactory(factory);
}
And my current plugin.xml
<plugin>
<extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Sample Action Set"
visible="true"
id="com.dynamo.actionSet">
<menu
label="Sample &Menu"
id="sampleMenu">
<separator
name="sampleGroup">
</separator>
</menu>
<action
label="&Sample Action"
icon="icons/sample.gif"
class="com.dynamo.actions.SampleAction"
tooltip="Hello, Eclipse world"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="com.dynamo.actions.SampleAction">
</action>
</actionSet>
</extension>
</plugin>
Tks for your attention.
How to check pro-grammatically whether command contributed as menu item(s) is/are checked/unchecked(If of CHECK BOX type), selected or unselected(if of type RADIO button) inside handlers "execute" method.
See snap shot here https://docs.google.com/file/d/0B3pxBGD-v-ycWVFaeElnSGdyTE0/edit .
Check out this blog: http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
So, first of all ensure that your command has appropriate style:
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="...">
<command commandId="org.eclipse.example.command.toggle"
style="toggle" />
</menuContribution>
</extension>
Then, you can check the state like this:
ICommandService service =(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand("org.eclipse.example.command.toggle");
State state = command.getState("org.eclipse.example.command.toggleState");
System.out.println(state.getValue());
//state.setValue(!(Boolean) state.getValue());
Also, consider taking a look at org.eclipse.ui.handlers.HandlerUtil, it might be sometime helpful.
Hope this helps.
I got solution,
Added this code in handler execute method method
public Object execute(ExecutionEvent event) throws ExecutionException {
Event selEvent = (Event) event.getTrigger();
MenuItem item = (MenuItem) selEvent.widget;
System.Out.Println(item.getSelection());
return null;
}