知识大全 关于Eclipse中新建菜单的实现方法

Posted 菜单

篇首语:带不走四季的风,就卷走一路的风景;遇不到心动的人,就孤身潇洒走四方。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 关于Eclipse中新建菜单的实现方法相关的知识,希望对你有一定的参考价值。

关于Eclipse中新建菜单的实现方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  新建菜单里面都是声明过NewWizard的东东

  不过既然已经声明了NewWizard还要在ActionSet或者PopupMenus里面再声明一遍不是很痛苦?

  其实可以如下做法

  先声明一个PullDown的菜单

   <action     class=\\ xxx NewAction\\      hoverIcon=\\ icons/full/objects/new_project gif\\      id=\\ xxx NewAction\\      label=\\ 新建(&amp;N)\\      menubarPath=\\ nus file/sep \\      style=\\ pulldown\\      toolbarPath=\\ sep \\ ></action>

  如果这个菜单用Coolbar 那么需要继承IWorkbenchWindowPulldownDelegate接口 如果要用在Menu上 那么继承IWorkbenchWindowPulldownDelegate 接口 实现基本是一样的

  

  public class NewAction implements IWorkbenchWindowPulldownDelegate  SelectionListener      private IWorkbench workbench;     @SuppressWarnings(\\ unused\\ )     private IWorkbenchWindow window;     private Menu parent = null;     private Menu inMenu = null;     private Menu inToolbar = null;     private IStructuredSelection selection;     /** *//**      * (non Javadoc)      *       * @see  eclipse ui IWorkbenchWindowActionDelegate#dispose()      */     public void dispose()          if (inMenu != null)              inMenu dispose();             inMenu = null;          [Page]        this workbench = null;         this window = null;          /** *//**      * (non Javadoc)      *       * @see  eclipse ui IWorkbenchWindowActionDelegate#init( eclipse ui IWorkbenchWindow)      */     public void init(IWorkbenchWindow window)          this window = window;         this workbench = window getWorkbench();          /** *//**      * (non Javadoc)      *       * @see  eclipse ui IActionDelegate#run( eclipse jface action IAction)      */     public void run(IAction action)           /** *//**      * (non Javadoc)      *       * @see  eclipse ui IActionDelegate#selectionChanged( eclipse jface action IAction      *       eclipse jface viewers ISelection)      */     public void selectionChanged(IAction action  ISelection selection)          this selection = (IStructuredSelection) selection;         if (parent != null)              // 找到新建菜单项             // 重新创建其子菜单             MenuItem[] items = parent getItems();             if (items != null)                  MenuItem parentItem = null;                 for (int index =   count = items length; index < count; index++)  [Page]                    MenuItem item = items[index];                     Object data = item getData();                     if (data instanceof ActionSetContributionItem)                          IContributionItem innerItem = ((ActionSetContributionItem) data)                                  getInnerItem();                         if (innerItem getId() equals(getClass() getName()))                              parentItem = item;                             break;                                                                                if (parentItem != null)                      Menu menu = getMenu(parent);                     parentItem setMenu(menu);                     parentItem setImage(null); [Page]                                                /** *//**      * (non Javadoc)      *       * @see  eclipse ui IWorkbenchWindowPulldownDelegate#getMenu( eclipse swt widgets Control)      */     public Menu getMenu(Control parent)          if (inToolbar != null)             inToolbar dispose();         inToolbar = new Menu(parent);         return createMenu(inToolbar);          /** *//**      * (non Javadoc)      *       * @see  eclipse ui IWorkbenchWindowPulldownDelegate #getMenu( eclipse swt widgets Menu)      */     public Menu getMenu(Menu parent)          if (inMenu != null)             inMenu dispose();         this parent = parent;         inMenu = new Menu(parent);         return createMenu(inMenu);          /** *//**      * @param parent      * @return      */     private Menu createMenu(Menu menu)  boolean hasMenu = false;         IWizardRegistry registry = workbench getNewWizardRegistry();         IWizardCategory rootCategory = registry getRootCategory();         IWizardCategory[] categories = rootCategory getCategories(); [Page]        if (categories != null)              reverseArray(categories);             for (IWizardCategory category : categories)                  hasMenu = createMenuItems(menu  category  hasMenu) || hasMenu;                               if (hasMenu)             new MenuItem(menu  SWT SEPARATOR);         MenuItem mnuOther = new MenuItem(menu  SWT NONE);         mnuOther setText(\\ 其他(&R) \\ );         mnuOther addSelectionListener(new SelectionAdapter()              /** *//**              * (non Javadoc)              *               * @see  eclipse swt events SelectionAdapter#widgetSelected( eclipse swt events SelectionEvent)              */             @Override             public void widgetSelected(SelectionEvent e)                  NewWizardDialog dialog = new NewWizardDialog(workbench getActiveWorkbenchWindow()                          getShell()  selection);                 dialog open();                      ); [Page]        return menu;          /** *//**      * 创建菜单      *       * @param menu      * @param category      * @param hasMenu 是否在之前已经有菜单      * @return 是否成功创建了菜单      */     private boolean createMenuItems(Menu menu  IWizardCategory category  boolean hasMenu)          boolean create = false;         IWizardDescriptor[] wizards = category getWizards();         if (wizards != null)              reverseArray(wizards);             List<IWizardDescriptor> shown = new ArrayList<IWizardDescriptor>();             for (IWizardDescriptor wizard : wizards)                  IConfigurationElement element = (IConfigurationElement) wizard                          getAdapter(IConfigurationElement class);                 IConfigurationElement[] children = element                          getChildren(IWorkbenchRegistryConstants TAG_ENABLEMENT);if (children != null && children length !=  )                      IConfigurationElement enablement = children[ ];                     ActionExpression exp = new ActionExpression(enablement); [Page]                    if (!exp isEnabledFor(this selection))                          continue;                                                       shown add(wizard);                          if (shown size() !=  )                  create = true;                 if (hasMenu)                      new MenuItem(menu  SWT SEPARATOR);                                  for (IWizardDescriptor wizard : shown)                      MenuItem item = new MenuItem(menu  SWT NONE);                     item setText(wizard getLabel());                     if (wizard getImageDescriptor() != null)                         item setImage(wizard getImageDescriptor() createImage());                     item setData(wizard);                     item addSelectionListener(this); [Page]                                               return create;          /** *//**      * 反转数组      *       * @param array      */     private void reverseArray(Object[] array)          if (array != null)              int count = array length;             int last = count    ;             for (int index =  ; index < count /  ; index++)                  Object temp = array[index];                 array[index] = array[last   index];                 array[last] = temp;                                /** *//**      * (non Javadoc)      * * @see  eclipse swt events SelectionListener#widgetDefaultSelected( eclipse swt events SelectionEvent)      */     public void widgetDefaultSelected(SelectionEvent e)          // do nothing          /** *//**      * (non Javadoc)      *       * @see  eclipse swt events SelectionListener#widgetSelected( eclipse swt events SelectionEvent)      */ [Page]    public void widgetSelected(SelectionEvent e)          MenuItem item = (MenuItem) e getSource();         IWizardDescriptor desc = (IWizardDescriptor) item getData();         try              IWorkbenchWizard wizard = desc createWizard();             wizard init(this workbench  this selection);             WizardDialog dialog = new WizardDialog(workbench getActiveWorkbenchWindow() getShell()                     wizard);             dialog open();          catch (CoreException ex)              // TODO 处理异常              

  这里有几个重点 是我自己摸索出来的

  第一个是IWorkbenchWindowPulldownDelegate 是继承IWorkbenchWindowPulldownDelegate的 因此只要实现这一个就可以在menu和coolbar 个地方用了 实际上 如果你只在一个地方用的话 另外一个方法返回null就可以了 因为永远不会被调用的 getMenu(Menu)是用在菜单的 getMenu(Control)是用在Coolbar的

  第二个就是getMenu(Menu)方法只会被调用一次 就是初始化的时候 而getMenu(Control)方法每次你打开菜单都会被调用 因此selectionChanged里面也只需要处理菜单的内容 而不需要处理coolbar的内容

  第三就是selectionChanged的操作

   /** *//**      * (non Javadoc)      *       * @see  eclipse ui IActionDelegate#selectionChanged( eclipse jface action IAction      *       eclipse jface viewers ISelection)      */     public void selectionChanged(IAction action  ISelection selection)          this selection = (IStructuredSelection) selection; [Page]        if (parent != null)              // 找到新建菜单项             // 重新创建其子菜单             MenuItem[] items = parent getItems();             if (items != null)                  MenuItem parentItem = null;                 for (int index =   count = items length; index < count; index++)                      MenuItem item = items[index];                     Object data = item getData();                     if (data instanceof ActionSetContributionItem)                          IContributionItem innerItem = ((ActionSetContributionItem) data)                                  getInnerItem();if (innerItem getId() equals(getClass() getName()))                              parentItem = item;                             break;                          [Page]                                                      if (parentItem != null)                      Menu menu = getMenu(parent);                     parentItem setMenu(menu);                     parentItem setImage(null);                                            

  parent是在getMenu(menu)中记录下来的 等于是上级菜单 说到这里 我们来说一下SWT里面菜单的构造(不一定对 只是我的理解而已) 所有你可以看到的带文字的条目都是MenuItem MenuItem分为 种 一种是没有子菜单的 一种是有子菜单的

  没有子菜单的很简单 不说了 有子菜单的就是SWT CASCADE类型的 这个时候MenuItem会产生一个Menu 而Menu是一个容器 基本可以认为弹出菜单的整体 里面可以包含很多MenuItem 就是这么个关系

  说完这个 我们来看SelectionChanged方法 Parent是个Menu 实际上是[文件]菜单 在里面查找id为类名的MenuItem 注意看Plugin xml的声明 类名和ID是一致的 实际上找到的就是[新建]菜单 然后重新创建菜单 并且设置成为MenuItem的Menu 这样就可以了

  实际上JDT就是这么做的 但是具体的实现可能不一样 我也没有仔细的研究他的做法 因为系统比较庞大 研究的话时间太厉害了

  至于PopupMenus 处理的方式是差不多的 略微有些修改而已 这里就不再把代码贴出来了

  还有这一段

   IConfigurationElement element = (IConfigurationElement) wizard                          getAdapter(IConfigurationElement class);                 IConfigurationElement[] children = element [Page]                         getChildren(IWorkbenchRegistryConstants TAG_ENABLEMENT);                 if (children != null && children length !=  )                      IConfigurationElement enablement = children[ ];                     ActionExpression exp = new ActionExpression(enablement);                     if (!exp isEnabledFor(this selection))                          continue;                                      

cha138/Article/program/Java/ky/201311/28387

相关参考

知识大全 关于java.util.regex 包中新增字符替换方法的比较

  代码如下:  importjavautilregex*;    publicclassregex     publicregex()          publicstaticStringrepl

知识大全 Eclipse GEF技巧总汇

EclipseGEF技巧总汇  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  大纲视图右键菜单项受

知识大全 关于在Eclipse下使用Subversion教程

关于在Eclipse下使用Subversion教程  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!环

知识大全 关于Eclipse插件开发之定制向导(图)

关于Eclipse插件开发之定制向导(图)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Inv

知识大全 关于Eclipse中UI程序的线程的讨论

关于Eclipse中UI程序的线程的讨论  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!虽然各个操作

知识大全 如何在硬碟建置Linux系统之关于 root 使用者

如何在硬碟建置Linux系统之关于root使用者  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

如何理解自建自用?

一、对于《财政部国家税务总局关于调整房地产市场若干税收政策的通知》(财税字[1999]210号)该文件第一款“关于营业税和契税的政策问题”中的“个人自建自用住房,销售时免征营业税”规定,何谓自建自用,

知识大全 业务系统设计之系统菜单设计

  很久没写文章了前几天有几个朋友问了我很多关于业务系统(ERPDRPCRM等)设计里很多概念性东西花了很长时间给他解释最后他门还是知其然不知其所以然这对于刚跨出学校大门的朋友来说理解起确实有点枯燥这

中新世Miocene,Epoch

第三纪时期的第四个世,大约从距今2600万年前开始,持续大约1400万年。它的名称是查尔斯·莱尔爵士1833年定的,意思是“不那么新”。在意大利、法国南部、奥地利、罗马尼亚、比利时和荷兰出露有中新世海

知识大全 Oracle10g中新的SQLoptimizerhints

Oracle10g中新的SQLoptimizerhints  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一