2012-09-06 80 views
4

我有一個eclipse rcp(版本Indigo 3.7)應用程序(eclipse插件項目)。我閱讀了Lars Vogel的教程「Eclipse Commands Advanced」(0.2-2.1版,11.04.2009-24.09.2011)。如何使用SourceProvider條件啓用和禁用菜單項(eclipse rcp Indigo 3.7)

首先,我創建了menuContribution菜單和一些命令的菜單擴展。

第二,我用一些命令創建了命令擴展。

第三我定義了處理程序的擴展(處理程序與enabledWhen表達式)。

第四我寫了一個變量的服務擴展。

我plugin.xml的樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<?eclipse version="3.4"?> 
<plugin> 
    <extension id="application" point="org.eclipse.core.runtime.applications"> 
     ... 
    </extension> 
    <extension point="org.eclipse.ui.perspectives"> 
     ... 
    </extension> 
    <extension id="product" point="org.eclipse.core.runtime.products"> 
     ... 
    </extension> 
    <extension point="org.eclipse.ui.views"> 
     ... 
    </extension> 
    <extension point="org.eclipse.ui.menus"> 
     <menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu"> 
      <menu id="fileMenuItem" label="File" mnemonic="F"> 
       <command commandId="de.falk.rcp37.drawfigures.commands.new" id="newFileMenuItem" label="New" mnemonic="N" style="push"> 
       </command> 
       <command commandId="de.falk.rcp37.drawfigures.commands.open" id="openFileMenuItem" label="Open..." mnemonic="O" style="push"> 
       </command> 
       <separator name="de.falk.rcp37.drawfigures.menu.file.separator1" visible="true"> 
       </separator> 
       <command commandId="de.falk.rcp37.drawfigures.commands.close" id="closeFileMenuItem" label="Close" mnemonic="C" style="push"> 
       </command> 
       <separator name="de.falk.rcp37.drawfigures.menu.file.separator2" visible="true"> 
       </separator> 
       <command commandId="de.falk.rcp37.drawfigures.commands.save" id="saveFileMenuItem" label="Save" mnemonic="S" style="push"> 
       </command> 
       <command commandId="de.falk.rcp37.drawfigures.commands.saveas" id="saveAsFileMenuItem" label="Save as..." mnemonic="a" style="push"> 
       </command> 
       <separator name="de.falk.rcp37.drawfigures.menu.file.separator3" visible="true"> 
       </separator> 
       <command commandId="de.falk.rcp37.drawfigures.commands.exit" id="exitFileMenuItem" label="Exit" mnemonic="x" style="push"> 
       </command> 
       <command commandId="de.falk.rcp37.drawfigures.commands.change" id="changeFileMenuItem" label="Change" mnemonic="h" style="push"> 
       </command> 
      </menu> 
     </menuContribution> 
    </extension> 
    <extension point="org.eclipse.ui.commands"> 
     <command id="de.falk.rcp37.drawfigures.commands.new" name="newFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.open" name="openFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.close" name="closeFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.save" name="saveFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.saveas" name="saveAsFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.exit" name="exitFileCommand"></command> 
     <command id="de.falk.rcp37.drawfigures.commands.change" name="changeFileCommand"></command> 
    </extension> 
    <extension point="org.eclipse.ui.handlers"> 
     <handler class="de.falk.rcp37.drawfigures.handler.NewHandler" commandId="de.falk.rcp37.drawfigures.commands.new"></handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.OpenHandler" commandId="de.falk.rcp37.drawfigures.commands.open"></handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.CloseHandler" commandId="de.falk.rcp37.drawfigures.commands.close"> 
      <enabledWhen> 
       <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate"> 
        <equals value="true"></equals> 
       </with> 
      </enabledWhen> 
     </handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.SaveHandler" commandId="de.falk.rcp37.drawfigures.commands.save"> 
      <enabledWhen> 
       <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate"> 
        <equals value="true"></equals> 
       </with> 
      </enabledWhen> 
     </handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.SaveAsHandler" commandId="de.falk.rcp37.drawfigures.commands.saveas"> 
      <enabledWhen> 
       <with variable="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate"> 
        <equals value="true"></equals> 
       </with> 
      </enabledWhen> 
     </handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.ExitHandler" commandId="de.falk.rcp37.drawfigures.commands.exit"></handler> 
     <handler class="de.falk.rcp37.drawfigures.handler.ChangeHandler" commandId="de.falk.rcp37.drawfigures.commands.change"></handler> 
    </extension> 
    <extension point="org.eclipse.ui.services"> 
     <sourceProvider provider="de.falk.rcp37.drawfigures.miscellaneous.MenuItemsStateSourceProvider"> 
      <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate" priorityLevel="workbench"></variable> 
      <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate" priorityLevel="workbench"></variable> 
      <variable name="de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate" priorityLevel="workbench"></variable> 
     </sourceProvider> 
    </extension> 
</plugin> 

另外我實現了MenuItemsStateSourceProvider:

package de.falk.rcp37.drawfigures.miscellaneous; 
import java.util.HashMap; 
import java.util.Map; 

import org.eclipse.ui.AbstractSourceProvider; 

public class MenuItemsStateSourceProvider extends AbstractSourceProvider { 
    public static final String ID = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider"; 
    public static final String DRAWING_CLOSE_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.closestate"; 
    public static final String DRAWING_SAVE_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.savestate"; 
    public static final String DRAWING_SAVEAS_STATE = "de.falk.rcp37.drawfigures.miscellaneous.menuitemsstatesourceprovider.saveasstate"; 

    @Override 
    public void dispose() { 

    } 

    @SuppressWarnings("rawtypes") 
    @Override 
    public Map getCurrentState() { 
     HashMap<String, String> hashMap = new HashMap<String, String>(); 
     hashMap.put(ID, ID); 
     hashMap.put(DRAWING_CLOSE_STATE, "false"); 
     hashMap.put(DRAWING_SAVE_STATE, "false"); 
     hashMap.put(DRAWING_SAVEAS_STATE, "false"); 
     return hashMap; 
} 

    @Override 
    public String[] getProvidedSourceNames() { 
     return new String[] { ID, DRAWING_CLOSE_STATE, DRAWING_SAVE_STATE, DRAWING_SAVEAS_STATE }; 
    } 

public void setDrawingNewed(boolean drawingNewed) { 
    if (ModelDefinitions.currentDrawing.getNewed() != drawingNewed) { 
      ModelDefinitions.currentDrawing.setNewed(drawingNewed); 
      fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVEAS_STATE, ModelDefinitions.currentDrawing.getSaveAsStateString()); 
    } 
} 
public void setDrawingOpend(boolean drawingOpend) { 
     if (ModelDefinitions.currentDrawing.getOpend() != drawingOpend) { 
      ModelDefinitions.currentDrawing.setOpend(drawingOpend); 
    fireSourceChanged(ISources.WORKBENCH, DRAWING_CLOSE_STATE, ModelDefinitions.currentDrawing.getCloseStateString()); 
      fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVE_STATE, ModelDefinitions.currentDrawing.getSaveStateString()); 
      fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVEAS_STATE, ModelDefinitions.currentDrawing.getSaveAsStateString()); 
     } 
} 
public void setDrawingChanged(boolean drawingChanged) { 
     if (ModelDefinitions.currentDrawing.getChanged() != drawingChanged) { 
      ModelDefinitions.currentDrawing.setChanged(drawingChanged); 
      fireSourceChanged(ISources.WORKBENCH, DRAWING_SAVE_STATE, ModelDefinitions.currentDrawing.getSaveStateString()); 
     } 
} 
} 

而不同的處理程序:

public class ChangeHandler extends AbstractHandler { 
@Override 
public Object execute(ExecutionEvent event) throws ExecutionException { 

     // doing some CHANGE operations... 

    // Get the source provider service 
    ISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil.getActiveWorkbenchWindow(event).getService(ISourceProviderService.class); 
    // Now get my service 
    MenuItemsStateSourceProvider menuItemsStateSourceProvider = (MenuItemsStateSourceProvider)sourceProviderService.getSourceProvider(MenuItemsStateSourceProvider.ID); 
    menuItemsStateSourceProvider.setDrawingChanged(true); 

    return null; 
    } 
} 

或:

public class NewHandler extends AbstractHandler { 
@Override 
public Object execute(ExecutionEvent event) throws ExecutionException { 

     // doing some NEW operations... 

    // Get the source provider service 
    ISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil.getActiveWorkbenchWindow(event).getService(ISourceProviderService.class); 
    // Now get my service 
    MenuItemsStateSourceProvider menuItemsStateSourceProvider = (MenuItemsStateSourceProvider)sourceProviderService.getSourceProvider(MenuItemsStateSourceProvider.ID); 
    menuItemsStateSourceProvider.setDrawingNewed(true); 
    menuItemsStateSourceProvider.setDrawingOpend(false); 
    menuItemsStateSourceProvider.setDrawingChanged(false); 

    return null; 
} 
} 

等(所有處理器)...

終於在繪畫類:

public String getCloseStateString() { 
    return (opend) ? "true" : "false"; 
} 
public String getSaveStateString() { 
    return (opend && changed) ? "true" : "false"; 
} 
public String getSaveAsStateString() { 
    return (opend || newed) ? "true" : "false"; 
} 

public void setNewed(boolean newed) { 
    this.newed = newed; 
} 
public void setOpend(boolean opend) { 
    this.opend = opend; 
} 
public void setChanged(boolean changed) { 
    this.changed = changed; 
} 

但沒有任何作品!誰能幫忙?

感謝入魔

回答

5

ISourceProvider.getCurrentState預期(見的Javadoc)一Map<String, Object>作爲返回值。對啓用的處理程序的檢查使用Boolean值,而不是String

如果更改MenuItemsStateSourceProvidergetCurrentState方法

@SuppressWarnings("rawtypes") 
@Override 
public Map getCurrentState() { 
    HashMap<String, Object> hashMap = new HashMap<String, Object>(); 
    hashMap.put(ID, ID); 
    hashMap.put(DRAWING_CLOSE_STATE, Boolean.TRUE); 
    hashMap.put(DRAWING_SAVE_STATE, Boolean.FALSE); 
    hashMap.put(DRAWING_SAVEAS_STATE, Boolean.FALSE); 
    return hashMap; 
} 

Close菜單項將被啓用和禁用他人。 所有的狀態值必須是Boolean

+0

非常感謝!我不知道Lars Vogel爲什麼使用Map oRUMOo