學無止境  
          日歷
          <2005年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678
          統計
          • 隨筆 - 9
          • 文章 - 0
          • 評論 - 2
          • 引用 - 0

          導航

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
          I will use one simple example to explain how to add context menu into editor dynamically. "Dynamically" means the menu items will change according to the selected object.
          There are three steps:

          1. Define the action. Please read the following example code:

          /**
           *  What I want to realize is to provide a "copy" menu item in popup menu
           *  whenever the user selects the object "Example".
           */

          public class CopyExampleAction extends org.eclipse.gef.ui.actions.SelectionAction
          {
              private static final String
                  COPY_REQUEST = "COPY";  //$NON-NLS-1$
              public static final String
                  COPY = "COPY";   //$NON-NLS-1$

              Request request;

              public CopyExampleAction(IWorkbenchPart part) {
                  super(part);
                  request = new Request(COPY_REQUEST);
                  setText("copy Example");
                  setId(COPY);
                  setImageDescriptor(
                      ImageDescriptor.createFromFile(ExamplePlugin.class,"icons/copy.gif")); //$NON-NLS-1$
                  setHoverImageDescriptor(getImageDescriptor());
              }

              protected boolean calculateEnabled() {
                  return canPerformAction();
              }

              private boolean canPerformAction() {
                  if (getSelectedObjects().isEmpty())
                      return false;
                  List parts = getSelectedObjects();
                  for (int i=0; i<parts.size(); i++){
                      Object o = parts.get(i);
                      if (!(o instanceof EditPart))
                          return false;
                      EditPart part = (EditPart)o;
                     // This menu item will only be activated if an object of Typ "Example" is selected.
                      if (!(part.getModel() instanceof Example))
                          return false;
                      }
                  }
                  return true;
              }

          // What happened for this action is defined by the edit part of this object.
              private Command getCommand() {
                  List editparts = getSelectedObjects();
                  CompoundCommand cc = new CompoundCommand();
                  cc.setDebugLabel("Copy Example");//$NON-NLS-1$
                  for (int i=0; i < editparts.size(); i++) {
                      EditPart part = (EditPart)editparts.get(i);
                      cc.add(part.getCommand(request));
                  }
                  return cc;
              }

              public void run() {
                  execute(getCommand());
              }
              }

          2. Implement the action. In this step you will decide what will exactly happen after the user clicked this menu item. As you have already seen from the code in the step 1, the command for this action is defined in edit part for the object "Example".
          So the next you will do is to complete the ExampleEditPolicy for ExampleEditPart.

          public class ExampleEditPolicy extends ComponentEditPolicy{
            
              private static final String
              COPY_REQUEST = "COPY",  //$NON-NLS-1$

          public Command getCommand(Request request) {
              if (COPY_REQUEST.equals(request.getType()))
                  return getCopyExampleCommand();
              return super.getCommand(request);
          }

          protected Command getCopyExampleCommand(){
           // The implementation of CopyExampleCommand is simple, will not be explained here.
              CopyExampleCommand command = new CopyExampleCommand();
              command.setCopyObject((Example)getHost().getModel());
              return command;
          }
          }

          3. Add the action into Context Menu.
          There is always a class to provider the context menu, the next step is to add this defined action into the context menu.

          public class ExampleContextMenuProvider extends ContextMenuProvider {
            
              private ActionRegistry actionRegistry;
             
              public ExampleContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
                  super(viewer);
                  setActionRegistry(registry);
              }

              public void buildContextMenu(IMenuManager manager) {
                  GEFActionConstants.addStandardActionGroups(manager);
                  IAction action;
                  action = getActionRegistry().getAction(CopyExampleAction.COPY);
                  if (action.isEnabled())
                      manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
              }   

              private ActionRegistry getActionRegistry() {
                  return actionRegistry;
              }

              private void setActionRegistry(ActionRegistry registry) {
                  actionRegistry = registry;
              }
          }

          4. Add the action into editor. There is a method "createActions" in editor. Add the action in it. That's all.

          protected void createActions() {
            super.createActions();
            ActionRegistry registry = getActionRegistry();
            IAction action;

            action = new CopyExampleAction(this);
            registry.registerAction(action);
            getSelectionActions().add(action.getId());
          }

          posted on 2005-09-07 17:07 lucia 閱讀(687) 評論(0)  編輯  收藏 所屬分類: Eclipse Plugin
           
          Copyright © lucia Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 和硕县| 桃江县| 德庆县| 日照市| 揭西县| 集贤县| 五家渠市| 大渡口区| 巫山县| 乐山市| 陇南市| 那曲县| 井陉县| 望奎县| 乐清市| 眉山市| 湟源县| 博乐市| 陇西县| 会同县| 锡林浩特市| 云林县| 湟源县| 枣阳市| 武陟县| 曲阜市| 临武县| 铁岭县| 中卫市| 庆元县| 衡水市| 察隅县| 临西县| 富民县| 运城市| 即墨市| 河南省| 中超| 偏关县| 正蓝旗| 台前县|