學無止境  
          日歷
          <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 閱讀(692) 評論(0)  編輯  收藏 所屬分類: Eclipse Plugin
           
          Copyright © lucia Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 乌鲁木齐市| 石屏县| 竹山县| 永登县| 西昌市| 繁峙县| 岳池县| 丰台区| 台中市| 广灵县| 富裕县| 盘锦市| 突泉县| 河间市| 锡林浩特市| 威信县| 重庆市| 六枝特区| 惠州市| 新泰市| 深泽县| 基隆市| 巴中市| 扎赉特旗| 娱乐| 新泰市| 大厂| 凤台县| 彝良县| 九台市| 方城县| 景洪市| 临安市| 西和县| 神农架林区| 深州市| 德化县| 深圳市| 阳朔县| 轮台县| 汽车|