日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

eclipse popupMene高級(jí)教程:使用Visibility定制彈出菜單

 江江385 2013-03-22
這幾天一直在研究popupMenu的Action,想要達(dá)到的目是選中不同的東西彈出不同的菜單。網(wǎng)上有很多介紹的文章,不過很多文章都是一些翻譯過來并且作者沒有自己實(shí)踐過的東西,浪費(fèi)了我很多時(shí)間,最后自己通過摸索,終于找出了這個(gè)問題的解決辦法。 

1.文章類型: 
這是一篇解決問題的教程,不討論技術(shù)的背景知識(shí),比如運(yùn)用到何種設(shè)計(jì)模式,以及實(shí)現(xiàn)背后的原理等問題。 

2.此文對(duì)您的幫助: 
看完此文后您將能夠創(chuàng)建一個(gè)由選擇內(nèi)容指定彈出項(xiàng)的動(dòng)態(tài)菜單。 

setp1.創(chuàng)建一個(gè)rcp工程,模板使用有一個(gè)view的 

step2.創(chuàng)建popupMenu 
step2_1.新建類Action1: 
Java代碼  收藏代碼
  1. package solonote.example.popupmenu;  
  2.   
  3. import org.eclipse.jface.action.IAction;  
  4. import org.eclipse.jface.viewers.ISelection;  
  5. import org.eclipse.ui.IObjectActionDelegate;  
  6. import org.eclipse.ui.IWorkbenchPart;  
  7.   
  8. public class Action1 implements IObjectActionDelegate {  
  9.   
  10.     @Override  
  11.     public void setActivePart(IAction action, IWorkbenchPart targetPart) {  
  12.         // TODO Auto-generated method stub  
  13.   
  14.     }  
  15.   
  16.     @Override  
  17.     public void run(IAction action) {  
  18.         // TODO Auto-generated method stub  
  19.   
  20.     }  
  21.   
  22.     @Override  
  23.     public void selectionChanged(IAction action, ISelection selection) {  
  24.         // TODO Auto-generated method stub  
  25.   
  26.     }  
  27.   
  28. }  


step2_2.添加擴(kuò)展點(diǎn): 

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <?eclipse version="3.2"?>  
  3. <plugin>  
  4.   
  5.    <extension  
  6.          id="application"  
  7.          point="org.eclipse.core.runtime.applications">  
  8.       <application>  
  9.          <run  
  10.                class="solonote.example.popupmenu.Application">  
  11.          </run>  
  12.       </application>  
  13.    </extension>  
  14.    <extension  
  15.          point="org.eclipse.ui.perspectives">  
  16.       <perspective  
  17.             name="Perspective"  
  18.             class="solonote.example.popupmenu.Perspective"  
  19.             id="solonote.example.popupMenu.perspective">  
  20.       </perspective>  
  21.    </extension>  
  22.    <extension  
  23.          point="org.eclipse.ui.views">  
  24.       <view  
  25.             name="View"  
  26.             class="solonote.example.popupmenu.View"  
  27.             id="solonote.example.popupMenu.view">  
  28.       </view>  
  29.    </extension>  
  30.    <!-- 彈出菜單的定義 -->  
  31.    <extension  
  32.          point="org.eclipse.ui.popupMenus">  
  33.       <objectContribution  
  34.             id="objectContribution"  
  35.             objectClass="java.lang.Object">  
  36.          <action  
  37.                class="solonote.example.popupmenu.Action1"  
  38.                id="solonote.example.popupMenu.action1"  
  39.                label="label">  
  40.          </action>  
  41.       </objectContribution>  
  42.    </extension>  
  43.   
  44. </plugin>  


step2_3設(shè)置彈出菜單的彈出位置 
修改View類 
Java代碼  收藏代碼
  1. package solonote.example.popupmenu;  
  2.   
  3. import org.eclipse.jface.action.MenuManager;  
  4. import org.eclipse.jface.viewers.IStructuredContentProvider;  
  5. import org.eclipse.jface.viewers.ITableLabelProvider;  
  6. import org.eclipse.jface.viewers.LabelProvider;  
  7. import org.eclipse.jface.viewers.TableViewer;  
  8. import org.eclipse.jface.viewers.Viewer;  
  9. import org.eclipse.swt.SWT;  
  10. import org.eclipse.swt.graphics.Image;  
  11. import org.eclipse.swt.widgets.Composite;  
  12. import org.eclipse.swt.widgets.Menu;  
  13. import org.eclipse.ui.ISharedImages;  
  14. import org.eclipse.ui.PlatformUI;  
  15. import org.eclipse.ui.part.ViewPart;  
  16.   
  17. public class View extends ViewPart {  
  18.     public static final String ID = "solonote.example.popupMenu.view";  
  19.   
  20.     private TableViewer viewer;  
  21.   
  22.     /** 
  23.      * The content provider class is responsible for providing objects to the 
  24.      * view. It can wrap existing objects in adapters or simply return objects 
  25.      * as-is. These objects may be sensitive to the current input of the view, 
  26.      * or ignore it and always show the same content (like Task List, for 
  27.      * example). 
  28.      */  
  29.     class ViewContentProvider implements IStructuredContentProvider {  
  30.         public void inputChanged(Viewer v, Object oldInput, Object newInput) {  
  31.         }  
  32.   
  33.         public void dispose() {  
  34.         }  
  35.   
  36.         public Object[] getElements(Object parent) {  
  37.             return new SimpleDTO[] { new SimpleDTO("One"), new SimpleDTO("Two"),  new SimpleDTO("Three") };  
  38.               
  39.         }  
  40.     }  
  41.   
  42.     class ViewLabelProvider extends LabelProvider implements  
  43.             ITableLabelProvider {  
  44.         public String getColumnText(Object obj, int index) {  
  45.             SimpleDTO dto = (SimpleDTO) obj;  
  46.             return dto.getName();  
  47.         }  
  48.   
  49.         public Image getColumnImage(Object obj, int index) {  
  50.             return getImage(obj);  
  51.         }  
  52.   
  53.         public Image getImage(Object obj) {  
  54.             return PlatformUI.getWorkbench().getSharedImages().getImage(  
  55.                     ISharedImages.IMG_OBJ_ELEMENT);  
  56.         }  
  57.     }  
  58.   
  59.     /** 
  60.      * This is a callback that will allow us to create the viewer and initialize 
  61.      * it. 
  62.      */  
  63.     public void createPartControl(Composite parent) {  
  64.         viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL  
  65.                 | SWT.V_SCROLL);  
  66.         viewer.setContentProvider(new ViewContentProvider());  
  67.         viewer.setLabelProvider(new ViewLabelProvider());  
  68.         viewer.setInput(getViewSite());  
  69.           
  70.         //初始化彈出菜單  
  71.         MenuManager popupMenuManager = new MenuManager("#PopupMenu");  
  72.         popupMenuManager.setRemoveAllWhenShown(true);  
  73.         Menu popupMenu = popupMenuManager.createContextMenu(viewer.getTable());  
  74.         viewer.getTable().setMenu(popupMenu);  
  75.         //設(shè)置選擇提供者和彈出菜單  
  76.         getSite().setSelectionProvider(viewer);  
  77.         getSite().registerContextMenu(popupMenuManager, viewer);  
  78.     }  
  79.   
  80.     /** 
  81.      * Passing the focus request to the viewer's control. 
  82.      */  
  83.     public void setFocus() {  
  84.         viewer.getControl().setFocus();  
  85.     }  
  86. }  


好的step2已經(jīng)結(jié)束了,現(xiàn)在您可以運(yùn)行一下程序,看看效果. 

接下來將要進(jìn)行的是,只有選中Three時(shí)菜單才彈出,選擇其他兩個(gè)則不彈出。 
確切的說是只有選擇Three時(shí)      
<objectContribution 
            id="objectContribution" 
            objectClass="java.lang.Object"> 
節(jié)點(diǎn)中的Action才顯示. 

接下來: 
step3_1修改擴(kuò)展點(diǎn) 增加visibility項(xiàng) 
Java代碼  收藏代碼
  1. <!-- 彈出菜單的定義 -->  
  2.  <extension  
  3.        point="org.eclipse.ui.popupMenus">  
  4.     <objectContribution  
  5.           id="objectContribution"  
  6.           objectClass="java.lang.Object">  
  7.        <action  
  8.              class="solonote.example.popupmenu.Action1"  
  9.              id="solonote.example.popupMenu.action1"  
  10.              label="label">  
  11.        </action>  
  12.        <visibility>  
  13.           <objectState  
  14.                 name="name"  
  15.                 value="Three">  
  16.           </objectState>  
  17.        </visibility>  
  18.     </objectContribution>  
  19.  </extension>  


修改被選中的DTO,讓其實(shí)現(xiàn)IActionFilter接口 
Java代碼  收藏代碼
  1. package solonote.example.popupmenu;  
  2.   
  3. import org.eclipse.ui.IActionFilter;  
  4.   
  5. /** 
  6.  * Viewer顯示的項(xiàng) 
  7.  * @author solonote 
  8.  * @version 0.1.0 2007-12-26 上午11:49:41 
  9.  */  
  10. public class SimpleDTO implements IActionFilter{  
  11.     /** 
  12.      * 顯示的名字 
  13.      */  
  14.     private String name;  
  15.   
  16.     /** 
  17.      * 返回顯示的名字 
  18.      * @return 顯示的名字 
  19.      */  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     /** 
  25.      * 設(shè)置顯示的名字 
  26.      * @param name 顯示的名字 
  27.      */  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32.     public SimpleDTO(String name) {  
  33.         super();  
  34.         this.name = name;  
  35.     }  
  36.   
  37.     /** 
  38.      * 選擇時(shí)候是否顯示Action的判斷 
  39.      * <visibility> 
  40.             <objectState 
  41.                   name="name" 
  42.                   value="Three"> 
  43.             </objectState> 
  44.          </visibility> 
  45.      * @param target 選擇的東西 
  46.      * @param name plugin.xml里配置的name 
  47.      * @param value plugin.xml里配置的value 
  48.      */  
  49.     @Override  
  50.     public boolean testAttribute(Object target, String name, String value) {  
  51.         System.out.println(name);  
  52.         System.out.println(value);  
  53.         System.out.println(target);  
  54.         SimpleDTO dto = (SimpleDTO) target;  
  55.         if(dto.getName().equals(value))  
  56.             return true;      
  57.         return false;  
  58.     }  
  59.       
  60.       
  61. }  


然后就完成了,當(dāng)然實(shí)際運(yùn)用中我運(yùn)用了Adapter將DTO適配成IActionFilter接口,這樣DTO就不用和IActionFilter接口耦合了,或許以后的文章會(huì)接受如何適配。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多