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

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

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

    Feeling

        三人行,必有我?guī)熝?/p>

       ::  :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      185 隨筆 :: 0 文章 :: 392 評(píng)論 :: 0 Trackbacks

    上一篇文章我們知道了Eclipse彈出菜單的基本用法。其實(shí)Eclipse的彈出菜單可以用來做很多文章,簡(jiǎn)單一點(diǎn)的根據(jù)文件類別,我們可以進(jìn)行不同的文件操作,比如Ant的build.xml我們可以用來build,Java文件我們可以用Java Editor打開,這些基于文件類型的操作我們都可以很容易的實(shí)現(xiàn)。但是還有一種情況,如果文件類型一樣,我們想進(jìn)行不同的操作,該怎么實(shí)現(xiàn)呢?實(shí)際上這樣的應(yīng)用很多,比如同樣是Java文件,含有main方法的Java文件有Run和Debug的選項(xiàng),其它的都沒有。還有現(xiàn)在的框架都是基于XML文件進(jìn)行配置的,如果一個(gè)項(xiàng)目使用了多個(gè)框架,我們?cè)趺锤鶕?jù)不同的XML文件進(jìn)行框架的區(qū)分呢?答案就是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文件它的右鍵彈出菜單包含一個(gè)額外的選項(xiàng)“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 >

    我們需要檢測(cè)在當(dāng)前情況下是否需要顯示這個(gè)菜單項(xiàng),使用擴(kuò)展點(diǎn) 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正好對(duì)應(yīng)test的property。

    至于檢測(cè)的邏輯我們?cè)赼dvancedpopupmenus.popup.actions.VisablePropertyTester中實(shí)現(xiàn),這個(gè)類必須繼承自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。

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

    < 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 >

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

    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
    ;
    ????}
    }

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


    評(píng)論

    # re: 擴(kuò)展Eclipse視圖彈出菜單(二) 2006-06-16 20:27 TreeNode
    visable=visible?  回復(fù)  更多評(píng)論
      

    # re: 擴(kuò)展Eclipse視圖彈出菜單(二) 2006-06-16 20:47 寒寒
    。。。。  回復(fù)  更多評(píng)論
      

    # re: 擴(kuò)展Eclipse視圖彈出菜單(二) 2006-06-17 09:06 cnfree
    advancedpopupmenus.popup.visable = true/false ,true 則顯示,false則不顯示。  回復(fù)  更多評(píng)論
      

    # re: 擴(kuò)展Eclipse視圖彈出菜單(二) 2006-06-21 18:18 yangqing
    好,我正想在myeclipse的database view的彈出菜單上加我的東西。不知道是否可以,我試試。  回復(fù)  更多評(píng)論
      

    # re: 擴(kuò)展Eclipse視圖彈出菜單(二) 2011-10-25 16:44 咯咯
    我這樣寫之后系統(tǒng)會(huì)拋錯(cuò),好像是現(xiàn)有的Eclipse popupmenu里的enablement沒有test這個(gè)了,那有什么替代的方法進(jìn)行實(shí)現(xiàn)么  回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    GitHub |  開源中國社區(qū) |  maven倉庫 |  文件格式轉(zhuǎn)換 
    主站蜘蛛池模板: 亚洲国产成人综合| 久久久久成人片免费观看蜜芽 | 精品久久亚洲中文无码| 亚洲福利精品一区二区三区| 免费人成在线观看69式小视频| 国产成人1024精品免费| 国产AV无码专区亚洲AV蜜芽| 亚洲午夜久久久精品电影院| 亚洲性天天干天天摸| 国产精品亚洲w码日韩中文| 国产精品黄页在线播放免费| 日韩版码免费福利视频| 一级毛片免费视频| 国产啪精品视频网站免费尤物| 国产精品福利片免费看| 无人视频在线观看免费播放影院| 亚洲色偷偷色噜噜狠狠99| 亚洲一区二区免费视频| 亚洲首页在线观看| 亚洲人成网www| 亚洲电影中文字幕| 亚洲av无码片在线播放| 亚洲αv久久久噜噜噜噜噜| 亚洲中文字幕无码中文字在线| 四虎永久免费观看| 免费看国产曰批40分钟| 国产在线ts人妖免费视频| 热99re久久精品精品免费| 在线看片人成视频免费无遮挡| 免费无码又黄又爽又刺激| 久久久久久久久免费看无码| 欧洲黑大粗无码免费| 国产网站在线免费观看| 亚洲成AⅤ人影院在线观看| 亚洲av区一区二区三| 亚洲电影日韩精品| 伊人久久大香线蕉亚洲五月天| 中文字幕亚洲无线码a| 亚洲成a人片在线观看无码| 亚洲伦理一区二区| 亚洲黄色中文字幕|