<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 25,  trackbacks - 0

    上一篇文章我們知道了Eclipse彈出菜單的基本用法。其實Eclipse的彈出菜單可以用來做很多文章,簡單一點的根據文件類別,我們可以進行不同的文件操作,比如Ant的build.xml我們可以用來build,Java文件我們可以用Java Editor打開,這些基于文件類型的操作我們都可以很容易的實現。但是還有一種情況,如果文件類型一樣,我們想進行不同的操作,該怎么實現呢?實際上這樣的應用很多,比如同樣是Java文件,含有main方法的Java文件有Run和Debug的選項,其它的都沒有。還有現在的框架都是基于XML文件進行配置的,如果一個項目使用了多個框架,我們怎么根據不同的XML文件進行框架的區分呢?答案就是enablement的test。

    <!ELEMENT test EMPTY>
    <!ATTLIST test
    property CDATA #REQUIRED
    args     CDATA #IMPLIED
    value    CDATA #IMPLIED>

    This element is used to evaluate the property state of the object in focus. The set of testable properties can be extended using the propery tester extension point. The test expression returns EvaluationResult.NOT_LOADED if teh property tester doing the actual testing isn't loaded yet.

    • property - the name of an object's property to test.
    • args - additional arguments passed to the property tester. Multiple arguments are seperated by commas. Each individual argument is converted into a Java base type using the same rules as defined for the value attribute of the test expression.
    • value - the expected value of the property. Can be omitted if the property is a boolean property. The test expression is supposed to return EvaluationResult.TRUE if the property matches the value and EvaluationResult.FALSE otherwise. The value attribute is converted into a Java base type using the following rules:
      • the string "true" is converted into Boolean.TRUE
      • the string "false" is converted into Boolean.FALSE
      • if the string contains a dot then the interpreter tries to convert the value into a Float object. If this fails the string is treated as a java.lang.String
      • if the string only consists of numbers then the interpreter converts the value in an Integer object.
      • in all other cases the string is treated as a java.lang.String
      • the conversion of the string into a Boolean, Float, or Integer can be suppressed by surrounding the string with single quotes. For example, the attribute value="'true'" is converted into the string "true"


    比如我們要讓含有main方法的Java文件它的右鍵彈出菜單包含一個額外的選項“This is main class”,需要編寫如下的Plugin.xml:

    < plugin >
       
    < extension
             
    point ="org.eclipse.ui.popupMenus" >

          
    < objectContribution
            
    id ="Advanced.PopupMenus"

            objectClass
    ="java.lang.Object" >
         
    < action  id ="Advanced.PopupMenus.Action"
            label
    ="AdvancedPopupMenus"
            style
    ="pulldown"
            menubarPath
    ="additions"
            class
    ="advancedpopupmenus.popup.actions.AdvancedPopupMenusAction"  
            enablesFor
    ="+" >

         
    </ action >
         
    < enablement >
              
    < test  property ="advancedpopupmenus.popup.visable" />
         
    </ enablement >   
         
    </ objectContribution >

       
    </ extension >
       
    < extension  point ="org.eclipse.core.expressions.propertyTesters" >
       
    < propertyTester
       
    namespace ="advancedpopupmenus.popup"

       properties
    ="visable"
       type
    ="java.lang.Object"
       class
    ="advancedpopupmenus.popup.actions.VisablePropertyTester"
       id
    ="advancedpopupmenus.popup.propertyTesters.visable" >   
       
    </ propertyTester >
          
       
    </ extension >

    </ plugin >

    我們需要檢測在當前情況下是否需要顯示這個菜單項,使用擴展點 org.eclipse.core.expressions.propertyTesters
    <!ELEMENT propertyTester EMPTY>
    <!ATTLIST propertyTester
    id         CDATA #REQUIRED
    type       CDATA #REQUIRED
    namespace  CDATA #REQUIRED
    properties CDATA #REQUIRED
    class      CDATA #REQUIRED>

    id - unique identifier for the property tester
    type - the type to be extended by this property tester
    namespace - a unique id determining the name space the properties are added to
    properties - a comma separated list of properties provided by this property tester
    class - the name of the class that implements the testing methods. The class must be public and extend org.eclipse.core.expressions.PropertyTester with a public 0-argument constructor. 

    這里只須注意
    propertyTester的namespace和properties正好對應test的property。

    至于檢測的邏輯我們在advancedpopupmenus.popup.actions.VisablePropertyTester中實現,這個類必須繼承自org.eclipse.core.expressions.PropertyTester

    package  advancedpopupmenus.popup.actions;

    import
     org.eclipse.core.expressions.PropertyTester;
    import
     org.eclipse.jdt.core.IMethod;
    import
     org.eclipse.jdt.core.IType;
    import
     org.eclipse.jdt.core.JavaModelException;
    import
     org.eclipse.jdt.internal.core.CompilationUnit;

    public   class  VisablePropertyTester  extends
     PropertyTester
    {
        
    public   boolean
     test( Object receiver, String property, Object[] args,
                Object expectedValue )
        {
            
    if  (  ! ( receiver  instanceof
     CompilationUnit ) )
                
    return   false
    ;
            CompilationUnit unit 
    =
     (CompilationUnit) receiver;
            
    try

            {
                IType[] types 
    =  unit.getTypes( );
                
    if  ( types  ==   null
     )
                    
    return   false
    ;
                
    for  (  int  i  =   0 ; i  <  types.length; i ++
     )
                {
                    IMethod[] methods 
    =
     types[i].getMethods( );
                    
    if  ( methods  ==   null
     )
                        
    return   false
    ;
                    
    for  (  int  j  =   0 ; j  <  methods.length; j ++
     )
                    {
                        
    if
     ( methods[j].isMainMethod( ) )
                            
    return   true
    ;
                    }
                }
            }
            
    catch
     ( JavaModelException e )
            {
                e.printStackTrace( );
            }
            
    return   false
    ;
        }
    }

    我們只要判斷接受的Java文件中是否含有main方法,如果有,則返回True,沒有則返回False。

    如果我們是要接受一個Web開發的配置文件,我們可以這樣寫:

    < plugin >
       
    < extension
             
    point ="org.eclipse.ui.popupMenus" >

          
    < objectContribution
            
    id ="Advanced.PopupMenus"

            objectClass
    ="org.eclipse.core.resources.IFile"
            nameFilter
    ="*.xml" >    
            
    < action  id ="Advanced.PopupMenus.Action"

               label
    ="This is web xml"
               style
    ="pulldown"
               menubarPath
    ="additions"
               class
    ="advancedpopupmenus.popup.actions.AdvancedPopupMenusAction"     
               enablesFor
    ="+" >

            
    </ action >
            
    < enablement >
                 
    < test  property ="advancedpopupmenus.popup.visable" />
            
    </ enablement >   
         
    </ objectContribution >

       
    </ extension >
       
    < extension  point ="org.eclipse.core.expressions.propertyTesters" >
          
    < propertyTester
                
    namespace ="advancedpopupmenus.popup"

                properties
    ="visable"
                type
    ="org.eclipse.core.resources.IFile"
                class
    ="advancedpopupmenus.popup.actions.VisablePropertyTester"
                id
    ="advancedpopupmenus.popup.propertyTesters.visable" >         
          
    </ propertyTester >
          
       
    </ extension >

    </ plugin >

    注意和上一個例子不同的地方,objectClass,nameFileter和type(在上一個例子中,我們也可以使用objectClass="org.eclipse.core.resources.IFile" nameFilter ="*.java" ),相應的我們的VisablePropertyTester類也要做一些改動:

    package  advancedpopupmenus.popup.actions;

    import
     javax.xml.parsers.DocumentBuilder;
    import
     javax.xml.parsers.DocumentBuilderFactory;
    import
     org.eclipse.core.expressions.PropertyTester;
    import
     org.eclipse.core.resources.IFile;
    import
     org.w3c.dom.Document;
    import
     org.w3c.dom.DocumentType;

    public   class  VisablePropertyTester  extends
     PropertyTester
    {
        
    public   boolean
     test( Object receiver, String property, Object[] args,
                Object expectedValue )
        {
            
    if  (  ! ( receiver  instanceof
     IFile ) )
                
    return   false
    ;
            IFile xml 
    =
     (IFile) receiver;
            
    try

            {
                DocumentBuilderFactory dbf 
    =  DocumentBuilderFactory.newInstance( );
                DocumentBuilder db 
    =
     dbf.newDocumentBuilder( );
                Document doc 
    =
     db.parse( xml.getContents( ) );
                DocumentType type 
    =
     doc.getDoctype( );
                
    if (type.getSystemId( ).equalsIgnoreCase(  " http://java.sun.com/j2ee/dtds/web-app_2_2.dtd "  )) return   true
    ;
            }
            
    catch
     ( Exception e )
            {
                e.printStackTrace( );
            }        
            
    return   false
    ;
        }
    }

    這樣根據不同的xml SystemID,我們就能夠知道到底這是哪一種框架的配置文件了。

    posted on 2008-08-12 15:26 Daniel 閱讀(1089) 評論(0)  編輯  收藏 所屬分類: Eclipse的相關
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 一个人看www免费高清字幕| 国产成人精品日本亚洲18图| 亚洲av日韩av永久在线观看| 57PAO成人国产永久免费视频| 精品无码一区二区三区亚洲桃色 | 97性无码区免费| 亚洲一二成人精品区| 免费人成毛片动漫在线播放| 国产成人亚洲综合无码精品 | 国产91免费视频| 亚洲国产av一区二区三区丶| 日本免费一区二区在线观看| 亚洲av乱码一区二区三区香蕉| 2021免费日韩视频网| 亚洲精品无码久久久久牙蜜区| 成人免费淫片在线费观看| 亚洲日产乱码一二三区别 | 国产亚洲精品免费| 亚洲人午夜射精精品日韩| 国产成人无码区免费A∨视频网站 国产成人涩涩涩视频在线观看免费 | 亚洲高清一区二区三区| 成人性生活免费视频| 久久无码av亚洲精品色午夜 | 大陆一级毛片免费视频观看| 亚洲国产精品99久久久久久| 亚洲精品和日本精品| 3344在线看片免费| 亚洲va在线va天堂va手机| 免费看无码自慰一区二区| 免费激情网站国产高清第一页| 亚洲无线一二三四区手机| 99在线观看免费视频| 亚洲一本一道一区二区三区| 亚洲精品老司机在线观看| 国产精品视频白浆免费视频| 2020亚洲男人天堂精品| 亚洲精品无码AV中文字幕电影网站 | 在线a亚洲v天堂网2018| 久久综合九色综合97免费下载| 亚洲日韩国产精品乱-久| 亚洲片国产一区一级在线观看|