Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » command enable - 2 questions(initial command enable with plugin state, next enable with setEnable)
command enable - 2 questions [message #528006] Sun, 18 April 2010 17:11 Go to next message
Stella Levin is currently offline Stella LevinFriend
Messages: 89
Registered: July 2009
Member
Hi, I have 2 questions regarding command enable:
1) I have command, defined with "org.eclipse.ui.commands" extension point. This command contributes to menu, like the following.
<extension point="org.eclipse.ui.menus">
   <menuContribution locationURI="menu:navigate?after=navEnd"> 
     <command
            commandId="org...gotodriver.command"
            icon="icons/drv.gif"
            id="org...menu.gotogriver"
 

The question is how to make the command initial enable state to be disable when plugin A is installed and enable when plugin A is loaded. (like it was for action)
<viewerContribution .. 
   <action ... 
      <enablement ...
         <PluginState... 


2) When the plugin is loaded the command controls enable state with setEnable function according to selection in application editor (inherited from TextEditor). But the enable state is not updated for the command in navigate menu and yes updated for the command in context menu. What is absent?
public void setEnabled(Object evaluationContext) {
   super.setEnabled(evaluationContext);		
   if (evaluationContext instanceof IEvaluationContext) {
      IEvaluationContext appContext = (IEvaluationContext) evaluationContext;		
      ISelection selection = (ISelection) appContext .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);		
      if (selection instanceof ITextSelection) {
         ITextSelection textSelection = (ITextSelection) selection;
         String string = textSelection.getText();
         boolean enable = isNeededText(string);
         setBaseEnabled(enable);
      }
   }
}
Re: command enable - 2 questions [message #528068 is a reply to message #528006] Mon, 19 April 2010 08:12 Go to previous messageGo to next message
Stella Levin is currently offline Stella LevinFriend
Messages: 89
Registered: July 2009
Member
The answer for the second question:
2) Add selection listener to the active editor
Object obj = appContext.getVariable(ISources.ACTIVE_EDITOR_NAME);
if ((obj != null) && (obj instanceof ApplicationTextEditor)) {
   ApplicationTextEditor editor = (ApplicationTextEditor) obj;
   ISelectionProvider selectionProvider = editor.getSelectionProvider();
   ISelectionChangedListener selectionListener = 
        new   ISelectionChangedListener() {
	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		ISelection selection = event.getSelection();
		if (selection instanceof ITextSelection) {
		ITextSelection textSelection = (ITextSelection) selection;
		setEnableWithSelection(textSelection.getText());
	}
}
selectionProvider.addSelectionChangedListener(selectionListener);

Still no answer to the first question.
Thanks
Re: command enable - 2 questions [message #528178 is a reply to message #528006] Mon, 19 April 2010 15:17 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Stella wrote:
> The question is how to make the command initial enable state to be
> disable when plugin A is installed and enable when plugin A is loaded.
> (like it was for action) <viewerContribution .. <action ...
> <enablement ...
> <PluginState...

You want a core expression on your handler, enabledWhen, that uses the
Platform property tester:
http://wiki.eclipse.org/Command_Core_Expressions#Property_Te sters


> 2) When the plugin is loaded the command controls enable state with
> setEnable function according to selection in application editor
> (inherited from TextEditor). But the enable state is not updated for the
> command in navigate menu and yes updated for the command in context
> menu. What is absent?

A change in the selection will not cause setEnabled(*) to be called.
Once you switch to programmatic enablement you are responsible for your
enabled state. If you have an enabledWhen clause on your handler, that
will be updated based on a change in the selection.

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: command enable - 2 questions [message #528664 is a reply to message #528178] Wed, 21 April 2010 12:28 Go to previous messageGo to next message
Stella Levin is currently offline Stella LevinFriend
Messages: 89
Registered: July 2009
Member
I have the following definitions in plugin.xml but the command is always in disabled state, when other commands are enabled.
<extension
      point="org.eclipse.ui.commands">
   <command       defaultHandler="org...commands.GotoHigherHierarchyHandler"
         description="Goto Higher Hierarchy Signal"
         id="org...gotohigherhierarchy.command"
         name="Goto Higher Hierarchy">
   </command>
</extension>
<extension
      point="org.eclipse.ui.handlers">
   <handler        class="org...commands.GotoHigherHierarchyHandler"
         commandId="org...gotohigherhierarchy.command">
      <enabledWhen>
         <with
               variable="org.eclipse.core.runtime.Platform">
            <or>
               <test
                     args="org..."
                     property="org.eclipse.core.runtime.bundleState"
                     value="ACTIVE">
               </test>
               <test
                     args="org..."
                     property="org.eclipse.core.runtime.bundleState"
                     value="STARTING">
               </test></or>
         </with>
      </enabledWhen>
   </handler>
</extension>
Re: command enable - 2 questions [message #528746 is a reply to message #528664] Wed, 21 April 2010 15:49 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

If you specify a handler via org.eclipse.ui.handlers (with no
activeWhen) then you don't need to provide a defaultHandler in the
command definition (it's equivalent to o.e.ui.handlers/handler with no
activeWhen and no enabledWhen).

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:Semantic annotation toolkit
Next Topic:accessing compare apis without plugin infrastructure
Goto Forum:
  


Current Time: Fri Apr 26 05:50:51 GMT 2024

Powered by FUDForum. Page generated in 0.03211 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top