posts - 36, comments - 30, trackbacks - 0, articles - 3

          擴(kuò)展點(diǎn):
          org.eclipse.ui.ide.resourceFilters
          org.eclipse.jdt.ui.javaElementFilters

          org.eclipse.ui.ide.resourceFilters擴(kuò)展點(diǎn)可以給Naivgator視圖的過濾器列表增加過濾項(xiàng),要使用該擴(kuò)展點(diǎn)需要在插件依賴項(xiàng)中追加
          對插件項(xiàng)目org.eclipse.ui.ide的依賴。

          擴(kuò)展清單如下:

          <extension
                     
          point="org.eclipse.ui.ide.resourceFilters">
                  
          <filter
                        
          pattern="*.classpath"
                        selected
          ="true">
                  
          </filter>
          </extension>
          上述代碼的作用就是給Navigator視圖,增加過濾器,過濾后綴名為classpath的文件,該過濾項(xiàng)默認(rèn)為選中狀態(tài)。

          org.eclipse.jdt.ui.javaElementFilters擴(kuò)展點(diǎn)可以給Java透視圖中相應(yīng)視圖的過濾器列表增加過濾項(xiàng),要使用該擴(kuò)展點(diǎn)需要在插件依賴項(xiàng)中追加
          對插件項(xiàng)目org.eclipse.jdt.ui的依賴。

          擴(kuò)展清單如下:

          <extension
                     
          point="org.eclipse.jdt.ui.javaElementFilters">
                  
          <filter
                        
          description="TestFilter Des"
                        enabled
          ="true"
                        id
          ="aaaa"
                        name
          ="TestFilter"
                        pattern
          ="*.properties"
                        targetId
          ="org.eclipse.jdt.ui.PackageExplorer">
                  
          </filter>
           
          </extension>

          上述代碼的作用就是給Package視圖,增加過濾器,過濾點(diǎn)后綴名為properties的文件,該過濾項(xiàng)默認(rèn)為選中狀態(tài)。

          posted @ 2010-07-09 20:59 笑看人生 閱讀(2314) | 評論 (0)編輯 收藏

          擴(kuò)展點(diǎn):
          org.eclipse.ui.bindings
          org.eclipse.ui.contexts

          擴(kuò)展點(diǎn)org.eclipse.ui.bindings是給command增加快捷鍵,結(jié)合(介紹二),給命令com.xxxx.test.command1增加快捷鍵,
          示例代碼如下:

          <extension
                   
          point="org.eclipse.ui.bindings">
                
          <key
                      
          commandId="com.xxxx.test.command1"
                      contextId
          ="org.eclipse.ui.contexts.window"
                      schemeId
          ="org.eclipse.ui.defaultAcceleratorConfiguration"
                      sequence
          ="M2+F7">
                
          </key>  
          </extension>

          這樣快捷鍵Shift+F7在Eclipse工作臺上都會起作用,但是如果想讓命令的快捷鍵只對特定的視圖或者編輯器起作用,那么可以通過org.eclipse.ui.contexts擴(kuò)展點(diǎn)來自定義上下文。

          <extension
                     
          point="org.eclipse.ui.contexts">
                  
          <context
                        
          id="viewmenu.testcontext"
                        name
          ="ViewContext"
                        parentId
          ="org.eclipse.ui.contexts.window">
                  
          </context>
          </extension>

          擴(kuò)展之后,需要修改org.eclipse.ui.bindings擴(kuò)展中的contextId屬性為viewmenu.testcontext。

          另外還需要在需要增加該快捷鍵的編輯器或者視圖對應(yīng)類中增加如下代碼(視圖類在代碼的createControl方法,編輯器類在代碼的init方法):

          IContextService contextService = 
          (IContextService) getSite().getService(IContextService.
          class);
          contextService.activateContext(
          "viewmenu.testcontext");

          posted @ 2010-07-09 20:52 笑看人生 閱讀(2742) | 評論 (0)編輯 收藏

          原文地址:http://www.fengfly.com/plus/view-179398-1.html

          菜單是各種軟件及開發(fā)平臺會提供的必備功能,Eclipse 也不例外,提供了豐富的菜單,包括主菜單(Main Menu),視圖 / 編輯器菜單(ViewPart/Editor Menu)和上下文菜單(Context Menu)。在 Eclipse 中,幾乎所有的 Workbench Part 提供了人性化的菜單,大大方便了用戶的操作。因此,如何擴(kuò)展 Eclipse 的菜單功能,并實(shí)現(xiàn)特定于我們自己插件的菜單,是插件開發(fā)者必須掌握的重要技能,同時(shí),Eclipse 提供了豐富的擴(kuò)展點(diǎn)供開發(fā)人員使用。本文將首先介紹 Eclipse 中的菜單,然后詳細(xì)說明如何進(jìn)行擴(kuò)展,最后以一個(gè)實(shí)例的形式引導(dǎo)讀者深入理解 Eclipse 的菜單功能。

            引言

            Eclipse 具有豐富的菜單功能,給開發(fā)人員提供了很好的用戶體驗(yàn)??傮w而言,Eclipse 菜單種類包括視圖 / 編輯器菜單,主菜單(Main Menu),視圖 / 編輯器菜單(ViewPart/EditorPart Menu)和上下文菜單(Context Menu)。插件開發(fā)人員通過靈活應(yīng)用這些菜單,可以給用戶提供很好的體驗(yàn)。由于視圖和編輯器菜單功能類似,因此本文重點(diǎn)講述視圖菜單(視圖下拉菜單及其工具欄菜單),除此之外,還將講述主菜單和上下文菜單。

            如圖 1 所示為 Project Explorer 視圖的菜單,包括視圖下拉菜單和工具欄菜單(折疊樹節(jié)點(diǎn))。通常而言,出現(xiàn)在視圖工具欄的菜單都會出現(xiàn)在視圖的下拉菜單,也就是說,比較常用的視圖菜單放在視圖的工具欄。

          圖 1. Project Explorer 視圖的菜單

           

           

            如圖 2 所示為 Project Explorer 視圖中的上下文菜單,只有當(dāng)我們右鍵點(diǎn)擊時(shí)才會出現(xiàn)。通常而言,出現(xiàn)頻率較高的菜單項(xiàng)才會出現(xiàn)在菜單中。上下文菜單具有很強(qiáng)的靈活項(xiàng),它可以隨著我們點(diǎn)擊的對象不同,彈出的菜單也會有相應(yīng)的變化。

          圖 2. Project Explorer 視圖中的上下文菜單


            如圖 3 所示為 Eclipse 的主菜單,包括最上面的主菜單項(xiàng)(不可移動)及其下面的工具欄菜單(可以移動,并且 Eclipse 提供了顯示 / 不顯示這些菜單的功能),Eclipse 并不建議我們?yōu)槊恳粋€(gè)插件都添加新的主菜單,這樣容易造成冗余,而且不方便用戶操作。通常,我們可以把菜單項(xiàng)添加到 Eclipse 已有的菜單,如插件的查找功能可以添加一個(gè)查找菜單項(xiàng)到 Eclipse 的 Search 主菜單上。

          圖 3. Eclipse 的主菜單


            前面講到 Eclipse 的各種菜單,那么,如何在開發(fā)插件或 RCP 應(yīng)用程序的時(shí)候添加這些菜單?本文下面的篇幅將詳細(xì)介紹如何擴(kuò)展 Eclipse 的菜單功能,使讀者深入了解 Eclipse 的菜單功能,并能夠開發(fā)具有這些菜單的應(yīng)用程序。因此,必須掌握三方面的內(nèi)容:菜單種類,菜單的擴(kuò)展點(diǎn),菜單控制(顯示 / 隱藏或啟用 / 禁用菜單項(xiàng))。下面從概念上介紹這三方面內(nèi)容,下一小節(jié)將會進(jìn)行詳細(xì)介紹。

            菜單種類

            正如前面所講到的,Eclipse 的菜單包括視圖菜單,主菜單及上下文菜單三個(gè)種類。

            菜單項(xiàng)的擴(kuò)展點(diǎn)

            Eclipse 提供了兩種擴(kuò)展點(diǎn)供用戶添加菜單項(xiàng)到相應(yīng)的位置。這兩種擴(kuò)展點(diǎn)為 org.eclipse.ui.commands(本文簡稱為 Commands 方式)和 org.eclipse.ui.actionSets(本文簡稱為 Actions 方式)。Actions 方式為界面上不同區(qū)域的表現(xiàn)方式提供了相應(yīng)的擴(kuò)展點(diǎn),并且沒有分離其界面表現(xiàn)和內(nèi)在實(shí)現(xiàn)。恰恰相反,Commands 方式通過三步有效的達(dá)到界面表現(xiàn)和內(nèi)部實(shí)現(xiàn)的分離:首先,通過 org.eclipse.ui.commands 擴(kuò)展點(diǎn)創(chuàng)建命令和類別(Category),并且可以把某些命令放在一個(gè)類別(Category)中;然后,通過 org.eclipse.ui.menus 指定命令出現(xiàn)在界面的哪個(gè)區(qū)域(視圖菜單 / 主菜單 / 上下文菜單);最后通過 org.eclipse.ui.handlers 指定命令的實(shí)現(xiàn)。因此,Eclipse 推薦新開發(fā)的插件使用 Commands 來創(chuàng)建您的界面菜單。當(dāng)然,由于 Actions 在現(xiàn)有的插件中用得比較多,如果我們需要擴(kuò)展或基于之前的插件開發(fā),也需要對其進(jìn)行了解。除此之外,針對上下文菜單,雖然 Commands 和 Actions 方式均可以創(chuàng)建上下文菜單,但是 Eclipse 還提供了另外一種創(chuàng)建上下文菜單的擴(kuò)展點(diǎn) org.eclipse.ui.popupMenus(本文簡稱為 popupMenus 方式),本文將就這三種擴(kuò)展點(diǎn)做詳細(xì)的介紹。

            菜單控制

            菜單控制是一個(gè)非常常見的功能,例如,隨著選定的內(nèi)容或當(dāng)前窗口的不同,菜單中的菜單項(xiàng)會有相應(yīng)的變化(顯示 / 隱藏或啟用 / 禁用菜單項(xiàng)),因此,如何控制菜單是插件開發(fā)人員必須掌握的知識。Eclipse 為菜單控制提供了兩種方法,一種是通過擴(kuò)展點(diǎn);另一種是通過 API 的方式編寫程序控制。

            Eclipse 菜單功能及其擴(kuò)展點(diǎn)

            至此,我們對 Eclipse 菜單有了感觀的認(rèn)識。由上一節(jié)我們可知,要深入理解 Eclipse 菜單功能,我們需要從三個(gè)方面去掌握:菜單種類,菜單的擴(kuò)展點(diǎn)和菜單控制。下面將進(jìn)行詳細(xì)講述。

            菜單種類

            針對各種菜單,Eclipse 提供了相應(yīng)的擴(kuò)展點(diǎn),因此,開發(fā)人員可以通過這些擴(kuò)展點(diǎn)把菜單放到界面的不同區(qū)域,詳細(xì)內(nèi)容請參考 2.2 小節(jié)。

            菜單的擴(kuò)展點(diǎn)

            視圖菜單的擴(kuò)展點(diǎn)

            采用 Commands 方式創(chuàng)建視圖菜單,需要引入 org.eclipse.ui.menus 擴(kuò)展點(diǎn);而 Actions 方式需要引入 org.eclipse.ui.actionSets.

            1、視圖菜單(Commands 方式):

            MenuContribution locationURI = “[Scheme]:[id]?[argument-list]”

            其中,Scheme 為該菜單項(xiàng)出現(xiàn)的區(qū)域,menu 為視圖的下拉菜單,toolbar 為視圖的工具欄菜單;id 為菜單區(qū)域 ID;argument-list 為該菜單項(xiàng)出現(xiàn)在指定菜單的位置。

            例如:在 ProbelmView 的下拉菜單加一個(gè)菜單項(xiàng),其 MenuContribution 的 locationURI 應(yīng)為:menu:org.eclipse.ui.views.ProblemView?after=additions;在 ProblemView 的工具欄菜單中加入一個(gè)菜單項(xiàng),其 locationURI 應(yīng)為:toolbar:org.eclipse.ui.views.ProblemView?after=additions。

            2、視圖菜單(Actions 方式):

            采用 Actions 方式創(chuàng)建菜單,需要引入 org.eclipse.ui.actionSets 擴(kuò)展點(diǎn),并通過設(shè)定 action 的 menubarPath 指定下拉菜單 / 菜單項(xiàng)出現(xiàn)的位置;通過設(shè)定 action 的 toolbarPath 設(shè)定工具欄菜單 / 菜單項(xiàng)出現(xiàn)的位置。

            例如,添加一個(gè)下拉菜單項(xiàng)到 Problems 視圖中,其 menubarPath 應(yīng)為:

            org.eclipse.ui.views.ProblemView/additions

            主菜單的擴(kuò)展點(diǎn)

            1、主菜單(Commands 方式)

            通過 Commands 方式把菜單項(xiàng)添加到主菜單及其工具欄上,和視圖菜單一樣,也是通過擴(kuò)展點(diǎn) org.eclipse.ui.menus 實(shí)現(xiàn),需要設(shè)定其 menuContribution 的 locationURI。

            例如,添加一個(gè)菜單(菜單可以包含若干個(gè)菜單項(xiàng))到主菜單一欄中,其 locationURI 為:

            menu:org.eclipse.ui.main.menu?after=additions

            添加一個(gè)菜單到工具欄之中,其 locationURI 為:

            toolbar:org.eclipse.ui.main.toolbar?after=additions

            當(dāng)然,我們也可以把菜單項(xiàng)添加到已經(jīng)存在的菜單當(dāng)中,例如添加一個(gè)菜單項(xiàng)到 Eclipse 的 Search 主菜單當(dāng)中,其 locationURI 為:

            menu:org.eclipse.search.menu?dialogGroup

            2、主菜單(Actions 方式)

            通過 Actions 方式把菜單項(xiàng)添加到主菜單及其工具欄上,和視圖菜單一樣,也是通過擴(kuò)展點(diǎn) org.eclipse.ui.actionSets 實(shí)現(xiàn),需要設(shè)定 action 的 menubarPath 和 toolbarPath 實(shí)現(xiàn)。

            例如,添加一個(gè)菜單項(xiàng)到 Eclipse 的 Search 主菜單中,其 menubarPath 應(yīng)為:

            org.eclipse.search.menu/dialogGroup

            注意:如果采用上述方式添加一個(gè)菜單項(xiàng)到 Search 主菜單,當(dāng)我們運(yùn)行時(shí)并沒有出現(xiàn)添加的菜單項(xiàng),這時(shí)候需要換一個(gè) workspace,其原因是 Eclipse 緩存了與其相關(guān)的某些信息在 workspace 當(dāng)中。

            上下文菜單的擴(kuò)展點(diǎn)

            上下文菜單除了通過 Commands 和 Actions 方式添加,還可以使用擴(kuò)展點(diǎn) org.eclipse.ui.popupMenus 方式添加,下面分別進(jìn)行介紹。

            1、上下文菜單(Commands 方式)

            Commands 方式與添加視圖菜單和主菜單的方式一樣,通過設(shè)定其 menuContribution 的 locationURI 來實(shí)現(xiàn)。

            例如,添加一個(gè)上下文菜單到 Problems 視圖中,其 locationURI 為:

            popup:org.eclipse.ui.views.ProblemView?after=additions。

            如果我們想讓某個(gè)上下文菜單項(xiàng)出現(xiàn)在任何區(qū)域,則可以使用下面的 locationURI:

            popup:org.eclipse.ui.popup.any?after=additions

            2、上下文菜單(Actions 方式)

            Actions 方式?jīng)]有直接提供擴(kuò)展點(diǎn)添加上下文菜單,但是我們可以通過編程的方式實(shí)現(xiàn),如下代碼清單 1 為 TreeViewer 添加上下文菜單,通過 IMenuManager 的 add 方法添加 actions。

          清單 1. 通過 Actions 方式編程實(shí)現(xiàn)添加上下文菜單

           private void hookContextMenu() { 
            IMenuManager fMenuMgr = new MenuManager(“#PopupMenu”); 
            fMenuMgr.setRemoveAllWhenShown(true); 
            // 添加 Actions 
            fMenuMgr.add(action … ) 
            fMenuMgr.createContextMenu(treeViewer.getControl()); 
            treeViewer.getControl().setMenu(fMenu); 
            getSite().registerContextMenu(fMenuMgr, treeViewer); 
           } 

            3、上下文菜單(popupMenus 方式)

            通過 popupMenus 擴(kuò)展點(diǎn)實(shí)現(xiàn)上下文菜單,需要設(shè)定 objectContribution 的 objectClass 屬性把上下文菜單添加到相應(yīng)的區(qū)域。

            例如,如果我們想當(dāng)用戶點(diǎn)擊 Eclipse 中的資源時(shí),彈出的上下文菜單包括某個(gè)菜單項(xiàng),我們可以設(shè)定 objectClass 屬性為:

            org.eclipse.core.resources.IResource

            通過 Commands 方式創(chuàng)建菜單項(xiàng)

            通過 Commands 方式創(chuàng)建菜單項(xiàng),首先需要創(chuàng)建 Command,通過擴(kuò)展點(diǎn) org.eclipse.ui.commands,然后我們可以把這個(gè) Command 放到任何區(qū)域,上一小節(jié)已經(jīng)講到,通過 org.eclipse.ui.menus 擴(kuò)展點(diǎn)確定菜單創(chuàng)建的區(qū)域,最后通過擴(kuò)展點(diǎn) org.eclipse.ui.handlers 定義這個(gè) command 的具體行為。

            在創(chuàng)建 Command 時(shí),我們可以先創(chuàng)建一個(gè) Category,并把相關(guān)的一些命令放到這個(gè) Category 中,這樣有利于管理。代碼清單 2 創(chuàng)建一個(gè) Command(“Show in Glossary Explorer”),并放到一個(gè) Category 中,然后把該 Command 放到 BGSearchResultView 視圖的上下文菜單中,最后通過擴(kuò)展 org.eclipse.ui.handlers 定義該 Command 的實(shí)現(xiàn)類。

          清單 2. 通過 Commands 方式添加菜單項(xiàng)

           <!-- 添加 command --> 
           <extension 
             point="org.eclipse.ui.commands"> 
            <category 
             description="Business Glossary" 
             id="com.ibm.bg.ui.commands.category" 
             name="%category.BusinessGlossary.name"> 
            </category> 
            <command 
             categoryId="com.ibm.bg.ui.commands.category" 
             description="Show in Glossary Explorer" 
             id="com.ibm.bg.ui.commands.BGShowInBrowser" 
             name="%command.ShowInGE.name"> 
            </command> 
           </extension> 
           <!-- 把 Command 放到界面的對應(yīng)區(qū)域 --> 
           <extension 
              point="org.eclipse.ui.menus"> 
            <menuContribution locationURI= 
            "popup:com.ibm.bg.internal.ui.search.BGSearchResultView?after=additions"> 
             <command 
                commandId="com.ibm.bg.ui.commands.BGShowInBrowser" 
                style="push" 
                tooltip="%command.ShowInGE.tooltip"> 
             </command> 
            </menuContribution> 
           </extension> 
           <!-- 定義 command 的實(shí)現(xiàn)類 --> 
           <extension 
             point="org.eclipse.ui.handlers"> 
            <handler 
               class="com.ibm.bg.internal.ui.handlers.BGShowInBrowser" 
               commandId="com.ibm.bg.ui.commands.BGShowInBrowser"> 
            </handler> 
           </extension> 

            通過 Actions 方式創(chuàng)建菜單項(xiàng)

            正如前面講到,Actions 方式?jīng)]有分離界面的表現(xiàn)和內(nèi)部實(shí)現(xiàn),因此,所有這些均通過 action 來完成。如下代碼清單 3 為添加一個(gè) Search 菜單項(xiàng)到 Eclipse 的 Search 主菜單(通過 action 的 menubarPath 指定)中,其中 class 對應(yīng)的值為該 Action 的實(shí)現(xiàn)類,該類需要實(shí)現(xiàn)接口 IWorkbenchWindowActionDelegate。

          清單 3. 通過 Actions 方式添加菜單項(xiàng)

           <extension 
             point="org.eclipse.ui.actionSets"> 
            <actionSet 
               id="com.ibm.bg.ui.workbenchActionSet" 
               label="%category.name.0" 
               visible="true"> 
             <action 
                class="com.ibm.bg.internal.ui.handlers.BGSearchHandler" 
                definitionId="com.ibm.bg.ui.commands.BGSearch" 
                icon="icons/search.png" 
                id="com.ibm.bg.ui.commands.BGSearch" 
                label="%action.searchGlossayInMainMenu.label" 
                menubarPath="org.eclipse.search.menu/dialogGroup" 
                style="push"> 
             </action> 
            </actionSet> 
           </extension> 

            通過 popupMenus 方式創(chuàng)建菜單項(xiàng)

            popupMenus 方式創(chuàng)建上下文菜單項(xiàng)也是通過 action 來實(shí)現(xiàn),下面例子為添加一個(gè)菜單項(xiàng)到用戶右擊 IGraphicalEditPart 對象時(shí)彈出的上下文菜單,通過 menubarPath 指定該 Action 出現(xiàn)的區(qū)域,通過 class 指定該 action 的實(shí)現(xiàn)類,該類需要實(shí)現(xiàn)接口 IObjectActionDelegate。

          清單 4. 通過 popupMenus 方式添加菜單項(xiàng)

           <extension 
             point="org.eclipse.ui.popupMenus"> 
            <objectContribution 
               adaptable="false" 
               id="com.ibm.bg.uml.objectContributions.BGAssignToGlossary" 
               objectClass="org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart"> 
             <action 
                class="com.ibm.bg.internal.uml.actions.BGAssignToGlossary" 
                enablesFor="+" 
                icon="icons/assign.png" 
                id="com.ibm.bg.internal.uml.actions.BGAssignToGlossary" 
                label="%BGAssignToGlossary.item" 
                menubarPath="com.ibm.bg.uml.popupMenuGroup"> 
             </action> 
            </objectContribution> 
           </extension> 

            菜單控制

            視圖菜單的控制主要包括啟用 / 禁用,顯示 / 隱藏菜單。

            通過 Command 方式創(chuàng)建的菜單,可以通過 org.eclipse.ui.commands 的 visibleWhen 屬性控制菜單的隱藏和顯示,通過 org.eclipse.ui.handlers 的 activewhen 或 enabledWhen 控制菜單的啟用或禁用。

            通過 Actions 方式創(chuàng)建的菜單,可以通過 action 的 enablement 屬性控制菜單的啟用 / 禁用。

            通過 popupMenus 方式創(chuàng)建的菜單,可以通過 objectContribution 的 visibility 和 enablement 來設(shè)置該 objectContribution 下的 action 的顯示 / 隱藏和啟用 / 禁用,我們也可以設(shè)置 action 的 enablement 來控制該菜單的啟用 / 禁用。

            這里不詳細(xì)講述 enablement,visibleWhen 和 enabledWhen 的參數(shù)及如何設(shè)置,讀者可以參考第三節(jié)的例子和本文的參考文獻(xiàn)。

            編程實(shí)踐

            本文將結(jié)合前兩節(jié)講到的知識,以例子的形式說明如何創(chuàng)建并且控制菜單。首先創(chuàng)建一個(gè)視圖(Menu Example),然后分別通過 Commands,Actions 和 popupMenus 方式創(chuàng)建若干個(gè)菜單,并添加相應(yīng)的菜單控制點(diǎn)。

            創(chuàng)建 Menu Example 視圖

            擴(kuò)展 org.eclipse.views 創(chuàng)建“Menu Example”視圖,如下代碼清單 5 為創(chuàng)建視圖的 xml 代碼。

          清單 5. 擴(kuò)展 org.eclipse.ui.views 創(chuàng)建視圖

           <extension 
             point="org.eclipse.ui.views"> 
            <category 
               id="com.free.menu.category" 
               name="Menu Example View"> 
            </category> 
            <view 
               category="com.free.menu.category" 
               class="com.free.menu.view.MenuExplorer" 
               id="com.free.menu.view.MenuExplorer" 
               name="Menu Explorer" 
               restorable="true"> 
            </view> 
           </extension> 

            創(chuàng)建 Commands

            采用 Command 方式創(chuàng)建“Menu Example”主菜單(包含 AngryCommand 和 JokeCommand 兩個(gè)菜單項(xiàng)),并且基于這兩個(gè)菜單項(xiàng)創(chuàng)建了 Menu Example 視圖的下拉菜單和工具欄菜單,及其 TreeViewer 的上下文菜單。

            如下代碼清單 6 為擴(kuò)展 org.eclipse.ui.commands 創(chuàng)建 Menu Example 命令和類別,并且包含兩個(gè)命令:Joke Command 和 Angry Command。

          清單 6. 擴(kuò)展 org.eclipse.ui.commands 創(chuàng)建命令

           <extension 
             point="org.eclipse.ui.commands"> 
            <category 
               id="com.free.menu.category" 
               name="Menu Example"> 
            </category> 
            <command 
               categoryId="com.free.menu.category" 
               id="com.free.menu.commands.jokeCommand" 
               name="Joke Command"> 
            </command> 
            <command 
               categoryId="com.free.menu.category" 
               id="com.free.menu.commands.angryCommand" 
               name="Angry Command"> 
            </command> 
           </extension> 

            關(guān)聯(lián) Commands 到主菜單

            如下代碼清單 7 為擴(kuò)展 org.eclipse.ui.menus,并基于前面創(chuàng)建的 Comands,添加一個(gè)主菜單 Menu Example,并且包含 Joke Command 和 Angry Command 菜單項(xiàng)。

          清單 7. 創(chuàng)建 Menu Example 主菜單

           <menuContribution 
             locationURI="menu:org.eclipse.ui.main.menu?after=additions"> 
            <menu 
             id="com.free.menu.MenuExample" 
               label="Menu Example"> 
             <command 
                commandId="com.free.menu.commands.jokeCommand" 
                style="push"> 
             </command> 
             <command 
                commandId="com.free.menu.commands.angryCommand" 
                style="push"> 
             </command> 
            </menu> 
           </menuContribution> 

            Commands 的實(shí)現(xiàn)類

            如下代碼清單 9 所示擴(kuò)展 org.eclipse.ui.handlers 為 Joke Command 和 Angry Command 創(chuàng)建事件處理類,其中 Joke Command 通過 enabledWhen 屬性控制該菜單項(xiàng)是否啟用,當(dāng)我們同時(shí)選擇了兩個(gè)對象時(shí) Joke Command 處于啟用狀態(tài),否則為禁用。

          清單 9. 擴(kuò)展 org.eclipse.ui.handlers 為 Commands 創(chuàng)建實(shí)現(xiàn)類

           <extension 
             point="org.eclipse.ui.handlers"> 
            <handler 
               class="com.free.menu.actions.JokeCommand" 
               commandId="com.free.menu.commands.jokeCommand"> 
             <enabledWhen> 
               <count 
                  value="2"> 
               </count> 
             </enabledWhen> 
            </handler> 
            <handler 
               class="com.free.menu.actions.AngryCommand" 
               commandId="com.free.menu.commands.angryCommand"> 
            </handler> 
           </extension> 

            創(chuàng)建 Action 并關(guān)聯(lián)到 Eclipse 的 Search 主菜單

            采用 Actions 方式在 Eclipse 的主菜單 Search 中添加創(chuàng)建菜單項(xiàng) SmileAction。擴(kuò)展 org.eclipse.ui.actionSets 在 Eclipse 的主菜單 Search 中添加一個(gè)菜單項(xiàng) Smile Action。如下代碼清單 10 所示創(chuàng)建該 action 并添加到 search 主菜單,只有當(dāng)我們選擇至少一個(gè)對象時(shí)(設(shè)置 enablesFor 屬性為“+”),該菜單項(xiàng)才處于啟用狀態(tài)。

          清單 10. 通過 Actions 方式創(chuàng)建菜單項(xiàng)

           <extension 
             point="org.eclipse.ui.actionSets"> 
            <actionSet 
               id="com.free.menu.actionSet.MenuExample" 
               label="Menu Example" 
               visible="true"> 
             <action 
                class="com.free.menu.actions.SmileAction" 
                enablesFor="+" 
                icon="icons/searchres.gif" 
                id="com.free.menu.actions.smileAction" 
                label="Smile Action" 
                menubarPath="org.eclipse.search.menu/dialogGroup" 
                style="push"> 
             </action> 
            </actionSet> 
           </extension> 

            pupupMenus 方式創(chuàng)建 Action 并關(guān)聯(lián)到 IResource 資源的上下文菜單

            擴(kuò)展 org.eclipse.ui.popupMenus 創(chuàng)建菜單“Menu Example”,該菜單包含一個(gè)菜單項(xiàng) HelloAction。當(dāng)我們在 Eclipse 任何區(qū)域右擊 org.eclipse.core.resources.IResource 資源時(shí)彈出的上下文菜單中會出現(xiàn)“Menu Example”菜單。如下代碼清單 11 為創(chuàng)建該上下文菜單的 xml 代碼。

          清單 11. popupMenus 方式創(chuàng)建上下文菜單

           <extension 
             point="org.eclipse.ui.popupMenus"> 
            <objectContribution 
               adaptable="true" 
               id="com.free.menu.popupMenu" 
               objectClass="org.eclipse.core.resources.IResource"> 
             <menu 
                label="Menu Example" 
                path="additions" 
                id="com.free.menu.popupSubMenu"> 
               <separator 
                  name="additions"> 
               </separator> 
             </menu> 
             <action 
                label="Hello Action" 
                class="com.free.menu.popup.actions.HelloAction" 
                menubarPath="com.free.menu.popupSubMenu/additions" 
                enablesFor="1" 
                id="com.free.menu.newAction"> 
             </action> 
            </objectContribution> 
           </extension> 

            pupupMenus 方式創(chuàng)建 Action 并關(guān)聯(lián)到 IResource 資源的上下文菜單

            擴(kuò)展 org.eclipse.ui.popupMenus 創(chuàng)建菜單項(xiàng) GreetAction 和 CryAction,當(dāng)我們右擊 Menu Example 視圖中的 TreeViewer 節(jié)點(diǎn)時(shí)彈出。如下代碼清單 12 所示擴(kuò)展 org.eclipse.ui.popupMenus 為 Menu Example 視圖創(chuàng)建 GreetAction 和 CryAction 上下文菜單項(xiàng)。使用 visiblity 的 objectState 屬性控制菜單項(xiàng)的可見狀態(tài),使用該屬性要求其選擇的對象實(shí)現(xiàn)了 org.eclipse.ui.IActionFilter 接口,具體可參見 Person 類的實(shí)現(xiàn)。

          清單 12. 擴(kuò)展 org.eclipse.ui.popupMenus 創(chuàng)建菜單

           <extension 
             point="org.eclipse.ui.popupMenus"> 
            <objectContribution 
               adaptable="false" 
               id="com.free.menu.views.popupMenu" 
               objectClass="com.free.menu.model.Person"> 
             <action 
                class="com.free.menu.actions.GreetAction" 
                enablesFor="+" 
                id="com.free.menu.actions.greetAction" 
                label="Greet Action" 
                menubarPath="additions"> 
             </action> 
             <visibility> 
               <objectState 
                  name="firstName" 
                  value="Dan"> 
               </objectState> 
             </visibility> 
            </objectContribution> 
           </extension> 
           <extension 
             point="org.eclipse.ui.popupMenus"> 
            <objectContribution 
               adaptable="false" 
               id="com.free.menu.views.popupMenu2" 
               objectClass="com.free.menu.model.Person"> 
             <action 
                class="com.free.menu.actions.CryAction" 
                enablesFor="+" 
                id="com.free.menu.actions.cryAction" 
                label="Cry Action" 
                menubarPath="additions"> 
               <enablement> 
                <objectState 
                   name="firstName" 
                   value="David"> 
                </objectState> 
               </enablement> 
             </action> 
             <visibility> 
               <objectState 
                  name="lastName" 
                  value="Rubel"> 
              </objectState> 
             </visibility> 
            </objectContribution> 
           </extension> 

            Menu Example 視圖的代碼實(shí)現(xiàn)類

            如下代碼清單 13 所示為 Menu Example 視圖的代碼,該視圖中有一個(gè) TreeViewer,并通過函數(shù) hookContextMenu 把上下文菜單關(guān)聯(lián)到 TreeViewer。其中函數(shù) viewMenuAction 用于更新菜單的狀態(tài),它首先獲取視圖菜單,然后調(diào)用 IMenuManager 的 update 方法更新對應(yīng)菜單項(xiàng)的狀態(tài),從而達(dá)到控制菜單的目的。

          清單 13. Menu Example 視圖代碼

           public class MenuExplorer extends ViewPart { 
            private TreeViewer treeViewer; 
            private MenuManager fMenuMgr; 
            private Menu fMenu; 
            private static MenuExplorer fInstance = null; 
            public MenuExplorer() { 
              fInstance = this; 
            } 
            public static MenuExplorer getInstance(){ 
              return fInstance; 
            } 
            public void createPartControl(Composite parent) { 
              treeViewer = new TreeViewer (parent, SWT.MULTI); 
              treeViewer.setLabelProvider(new PersonListLabelProvider()); 
              treeViewer.setContentProvider(new PersonTreeContentProvider()); 
              treeViewer.setInput(Person.example()); 
              this.getSite().setSelectionProvider(treeViewer); 
              hookContextMenu(); 
              fInstance = this; 
               
            } 
            public void setViewMenuActionState(boolean state){     
              JokeCommand.setState(state); 
              viewMenuAction(); 
            } 
            private void viewMenuAction() { 
              IActionBars bars= getViewSite().getActionBars(); 
              final IMenuManager menu= bars.getMenuManager();   
               
              UIOperation.asyncExecCommand(new Runnable(){ 
                public void run() { 
                  menu.update("com.free.menu.commands.jokeAction"); 
                }       
              });     
            } 
            private void hookContextMenu() { 
              fMenuMgr = new MenuManager("#PopupMenu"); 
              fMenuMgr.setRemoveAllWhenShown(true); 
              fMenuMgr.addMenuListener(new IMenuListener() { 
                public void menuAboutToShow(IMenuManager manager) {         
                } 
              }); 
              fMenu = fMenuMgr.createContextMenu(treeViewer.getControl()); 
           
              treeViewer.getControl().setMenu(fMenu); 
              getSite().registerContextMenu(fMenuMgr, treeViewer);        
            }   
            public void setFocus() { 
              treeViewer.getTree().setFocus(); 
           
            } 
           } 

            Person 類的實(shí)現(xiàn)

            如下代碼清單 14 為 Person 類的實(shí)現(xiàn),用于表示 MenuExample 視圖中 TreeViewer 的一個(gè)節(jié)點(diǎn),它實(shí)現(xiàn)了 IActionFilter 接口,通過 testAttribute 來確定是否顯示 / 隱藏菜單(其中 target 表示用戶選擇的對象,name/value 對應(yīng)于 plugin.xml 文件中 objectState 的 name/value).

          清單 14. Person 類實(shí)現(xiàn)

           public class Person implements IActionFilter { 
           
            private String firstName = "John"; 
            private String lastName = "Doe"; 
            protected int age = 37; 
            public Person[] children = new Person[0]; 
            public Person parent = null; 
            public Person(String firstName, String lastName, int age) { 
              this.firstName = firstName; 
              this.lastName = lastName; 
              this.age = age; 
            } 
            public Person(String firstName, String lastName, int age, Person[] children) { 
              this(firstName, lastName, age); 
              this.children = children; 
              for (int i = 0; i < children.length; i++) { 
                children[i].parent = this; 
              } 
            } 
            public String getFirstName() { 
              return this.firstName; 
            } 
            public String getLastName() { 
              return this.lastName; 
            } 
            public static Person[] example() { 
              return new Person[] { 
                  new Person("Dan", "Rubel", 38, new Person[] { 
                      new Person("Beth", "Rubel", 8), 
                      new Person("David", "Rubel", 3) }), 
                  new Person("Eric", "Clayberg", 39, new Person[] { 
                      new Person("Lauren", "Clayberg", 6), 
                      new Person("Lee", "Clayberg", 4) }), 
                  new Person("Mike", "Taylor", 52) }; 
            } 
            public String toString() { 
              return firstName + " " + lastName; 
            } 
            public boolean testAttribute(Object target, String name, String value) { 
           
              if (target instanceof Person) { 
                Person person = (Person) target; 
                if (name.equals("firstName") && value.equals(person.getFirstName())) { 
                  return true; 
                } 
           
                if (name.equals("lastName") && value.equals(person.getLastName())) { 
                  return true; 
                } 
              } 
              return false; 
            } 
           } 

            總結(jié)

            至此為止,已經(jīng)把 Eclipse 菜單功能及其擴(kuò)展點(diǎn)涉及到的類 / 接口 /API 進(jìn)行了詳細(xì)的說明,相信讀者已經(jīng)有清晰的認(rèn)識了。對于前面提到 popupMenus 方式創(chuàng)建上下文菜單,要求選擇的對象實(shí)現(xiàn) IActionFilter 接口,但是,如果開發(fā)人員正在使用 gmf 進(jìn)行開發(fā),那么我們可以不必要求選擇的對象實(shí)現(xiàn) IActionFilter,我們可以通過擴(kuò)展 org.eclipse.gmf.runtime.common.ui.services.action.actionFilterProviders 對菜單項(xiàng)進(jìn)行控制,如下代碼清單 15 為擴(kuò)展該 extension point 的 xml 代碼,我們可以定義多個(gè)屬性(<Attribute> … </Attribute),其中 Attribute 的 name 和 value 對應(yīng)于 visibility 的 objectState 中的 name 和 value。

          清單 15. 通過 actionFilterProviders 擴(kuò)展點(diǎn)實(shí)現(xiàn)對菜單的控制

           <extension 
           point="org.eclipse.gmf.runtime.common.ui.services.action.actionFilterProviders"> 
             <ActionFilterProvider 
                class="com.free.menu.PopupActionFilterProvider"> 
              <Priority 
                 name="Medium"> 
              </Priority> 
              <Attribute 
                 name="com.ibm.bg.uml.search.isSupportedType" 
                 value="supported"> 
              </Attribute> 
             </ActionFilterProvider> 
           </extension> 

            如下代碼清單 16 所示 PopupActionFilterProvider 的實(shí)現(xiàn),它繼承 AbstractActionFilterProvider,只需要實(shí)現(xiàn)其中的 testAttribute 和 provides 方法,當(dāng) testAttribute 返回 true 時(shí),那么該菜單項(xiàng)被啟用,否則禁用。其中 target 對應(yīng)于我們選擇的對象,name 和 value 參數(shù)對應(yīng)于 visiblity 中 objectState 的 name 和 value 的指定值 ( 與前面提到的 Person 類中的 testAttribute 方法類似 )。


          清單 16. actionFilterProviders 擴(kuò)展點(diǎn)實(shí)現(xiàn)類

           public class PopupActionFilterProvider extends AbstractActionFilterProvider { 
           
           
            public PopupActionFilterProvider() { 
            } 
           
            public boolean testAttribute(Object target, String name, String value) { 
                 
            } 
           
            public boolean provides(IOperation operation) { 
              return false; 
            } 
           
           }

           

           

           

          posted @ 2010-07-03 16:00 笑看人生 閱讀(1853) | 評論 (0)編輯 收藏

          擴(kuò)展點(diǎn):
          org.eclipse.ui.menus(確定菜單創(chuàng)建的區(qū)域)
          org.eclipse.ui.commands
          org.eclipse.ui.handlers(command的具體行為)
          org.eclipse.ui.commandImages(comand的圖片)

          擴(kuò)展點(diǎn)org.eclipse.ui.menus用來對菜單進(jìn)行擴(kuò)展,可以對主菜單,工具欄,上下文菜單進(jìn)行擴(kuò)展。

          示例代碼如下:
          <extension
                   
          point="org.eclipse.ui.menus">
                
          <menuContribution
                      
          allPopups="false"
                      locationURI
          ="menu:org.eclipse.ui.main.menu?after=additions">
                   
          <command
                         
          commandId="com.xxxx.test.command1"
                         style
          ="push">
                   
          </command>
                
          </menuContribution>
          </extension>

          其中l(wèi)ocationURI屬性指定菜單擴(kuò)展的位置,上述代碼是對主菜單進(jìn)行擴(kuò)展,如果要對工具欄和上下文菜單進(jìn)行擴(kuò)展,書寫格式如下:

          toolbar:org.eclipse.ui.main.toolbar?after=additions
          popup:org.eclipse.ui.popup.any?after=additions(上下文菜單在任何位置出現(xiàn))
          popup:org.eclipse.ui.views.ProblemView?after=additions(上下文菜單在問題視圖中出現(xiàn))

          commandId屬性指定該menu對應(yīng)的command,一個(gè)menu可以對應(yīng)多個(gè)command。

          command可以通過擴(kuò)展點(diǎn)org.eclipse.ui.commands擴(kuò)展,示例代碼如下:

          <extension
                   
          point="org.eclipse.ui.commands">
          <category
                      
          id="com.xxxx.test.category1"
                      name
          ="MenuTest">
                
          </category>

                
          <command
          categoryId="="com.xxxx.test.category1"
                      id
          ="com.xxxx.test.command1"
                      name
          ="CommandA">
                
          </command>
           
          </extension>

          至于Command具體要做什么,需要通過擴(kuò)展點(diǎn)org.eclipse.ui.handlers來指定,示例代碼如下:

          <extension
                   
          point="org.eclipse.ui.handlers">
                
          <handler
                      
          class="com.xxxx.test.SampleHandler"
                      commandId
          ="com.xxxx.test.command1">
                
          </handler>
           
          </extension>

          還有擴(kuò)展點(diǎn)org.eclipse.ui.commandImages,可以指定Command對應(yīng)的圖標(biāo)。

           <extension
                   
          point="org.eclipse.ui.commandImages">
                
          <image
                      
          commandId="com.xxxx.test.command1"
                      icon
          ="icons/sample.gif">
                
          </image>
            
          </extension>

          posted @ 2010-07-03 15:55 笑看人生 閱讀(2789) | 評論 (0)編輯 收藏

          擴(kuò)展點(diǎn):org.eclipse.core.runtime.preferences
          功能:該擴(kuò)展點(diǎn)主要用來設(shè)置首選項(xiàng)的初始值;

          擴(kuò)展點(diǎn)示例:

          <extension
                   
          point="org.eclipse.core.runtime.preferences">
                
          <initializer
                      
          class="com.xxxx.test.AbstractPreferenceInitializer1">
                
          </initializer>
          </extension>

          initializer指定設(shè)置首選項(xiàng)初始值的類,示例代碼如下:

          public class AbstractPreferenceInitializer1 extends
                  AbstractPreferenceInitializer 
          {    
              @Override
              
          public void initializeDefaultPreferences() {
                  IPreferenceStore prefs 
          = Activator.getDefault().getPreferenceStore();
                  prefs.setDefault(
          "MAX"1000);
              }

          }


          上述代碼設(shè)置屬性MAX的初始值為1000,這個(gè)屬性就可以被首選項(xiàng)使用了。

          使用擴(kuò)展點(diǎn)org.eclipse.ui.preferencePages擴(kuò)展首選項(xiàng)

          擴(kuò)展點(diǎn)示例:

          <extension
                   
          point="org.eclipse.ui.preferencePages">
                
          <page
                      
          class="com.xxxx.test.WorkbenchPreferencePage1"
                      id
          ="com.xxxx.test.page1"
                      name
          ="testName">
                
          </page>
          </extension>

          這樣就可以在WorkbenchPreferencePage1類中使用剛才定義的屬性MAX了,示例代碼如下:

          public class WorkbenchPreferencePage1 extends FieldEditorPreferencePage implements
                  IWorkbenchPreferencePage 
          {

              
          public void init(IWorkbench workbench) {
                  setPreferenceStore(Activator.getDefault().getPreferenceStore());        
              }

              @Override
              
          protected void createFieldEditors() {
                  
          int max = getPreferenceStore().getDefaultInt("MAX");    
                  System.out.println(
          ""+max);
              }
              
          }


          posted @ 2010-07-03 15:22 笑看人生 閱讀(2617) | 評論 (0)編輯 收藏

          原址地址:http://hi.baidu.com/dangjun625/blog/item/a0732c0845181ddc63d98666.html

          MyEclipse has detected that less than 5% of the 64MB of Perm
          Gen (Non-heap memory) space remains. It is strongly recommended
          that you exit and restart MyEclipse with new virtual machine memory
          paramters to increase this memory.   Failure to do so can result in
          data loss. The recommended Eclipse memory parameters are:
          eclipse.exe -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M

          eclipse根目錄下面的 eclipse.ini 配置 從網(wǎng)上搜了些資料

          -vmargs:說明后面是VM的參數(shù)
          -Xms128m:虛擬機(jī)占用系統(tǒng)的最小內(nèi)存
          -Xmx512m:虛擬機(jī)占用系統(tǒng)的最大內(nèi)存的5%為25.6M,理論上要求-Xmx的數(shù)值與-XX:MaxPermSize必須大于25.6M
          -XX:PermSize:最小堆大小。一般報(bào)內(nèi)存不足時(shí),都是說這個(gè)太小, 堆空間剩余小于5%就會警告,建議把這個(gè)稍微設(shè)大一點(diǎn),不過要視自己機(jī)器內(nèi)存大小來設(shè)置
          -XX:MaxPermSize:最大堆大小。這個(gè)也適當(dāng)大些

          把里面的參數(shù)改為
          -vmargs  
          -Xms128M  
          -Xmx512M  
          -XX:PermSize=128M  
          -XX:MaxPermSize=256M
          問題解決!

          從網(wǎng)上的資料看PermSize大一點(diǎn)肯定更好,而且最好是設(shè)置PermSize和MaxPermSize一樣大。理由如下:
          PermSize 和MaxPermSize如果設(shè)置為相同還可以在一定程度上提高性能,因?yàn)?,PermSize在不斷的變化中會需要轉(zhuǎn)移其中的數(shù)據(jù)。如果固定了以后,則可以減少每次擴(kuò)大PermSize帶來的性能損失。

          1、PermGen space簡介
            
            PermGen space的全稱是Permanent Generation space,是指內(nèi)存的永久保存區(qū)域OutOfMemoryError: PermGen space從表面上看就是內(nèi)存益出,解決方法也一定是加大內(nèi)存。
            
            說說為什么會內(nèi)存益出:
           ?。?)這一部分用于存放Class和Meta的信息,Class在被 Load的時(shí)候被放入PermGen space區(qū)域,它和和存放Instance的Heap區(qū)域不同。
           ?。?) GC(Garbage Collection)不會在主程序運(yùn)行期對PermGen space進(jìn)行清理,所以如果你的APP會LOAD很多CLASS 的話,就很可能出現(xiàn)PermGen space錯(cuò)誤。這種錯(cuò)誤常見在web服務(wù)器對JSP進(jìn)行pre compile的時(shí)候。
            
            如果你的WEB APP下都用了大量的第三方j(luò)ar,其大小超過了jvm默認(rèn)的大小(4M)那么就會產(chǎn)生此錯(cuò)誤信息了。

          解決方法: 手動設(shè)置MaxPermSize大小
            
            修改TOMCAT_HOME/bin/catalina.bat,在echo "Using CATALINA_BASE: $CATALINA_BASE"上面加入以下行:
             JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m
            建議:將相同的第三方j(luò)ar文件移置到tomcat/shared/lib目錄下,這樣可以減少jar 文檔重復(fù)占用內(nèi)存

          1。參數(shù)的含義

          -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M

          參數(shù)中-vmargs的意思是設(shè)置JVM參數(shù),所以后面的其實(shí)都是JVM的參數(shù)了,我們首先了解一下JVM內(nèi)存管理的機(jī)制,然后再解釋每個(gè)參數(shù)代表的含義。
          堆(Heap)和非堆(Non-heap)內(nèi)存
          按照官方的說法:“Java 虛擬機(jī)具有一個(gè)堆,堆是運(yùn)行時(shí)數(shù)據(jù)區(qū)域,所有類實(shí)例和數(shù)組的內(nèi)存均從此處分配。堆是在 Java 虛擬機(jī)啟動時(shí)創(chuàng)建的。”“在JVM中堆之外的內(nèi)存稱為非堆內(nèi)存(Non-heap memory)”??梢钥闯鯦VM主要管理兩種類型的內(nèi)存:堆和非堆。簡單來說堆就是Java代碼可及的內(nèi)存,是留給開發(fā)人員使用的;非堆就是JVM留給自己用的,所以方法區(qū)、JVM內(nèi)部處理或優(yōu)化所需的內(nèi)存(如JIT編譯后的代碼緩存)、每個(gè)類結(jié)構(gòu)(如運(yùn)行時(shí)常數(shù)池、字段和方法數(shù)據(jù))以及方法和構(gòu)造方法的代碼都在非堆內(nèi)存中。
          堆內(nèi)存分配
          JVM初始分配的內(nèi)存由-Xms指定,默認(rèn)是物理內(nèi)存的1/64;JVM最大分配的內(nèi)存由-Xmx指定,默認(rèn)是物理內(nèi)存的1/4。默認(rèn)空余堆內(nèi)存小于40%時(shí),JVM就會增大堆直到-Xmx的最大限制;空余堆內(nèi)存大于70%時(shí),JVM會減少堆直到-Xms的最小限制。因此服務(wù)器一般設(shè)置-Xms、-Xmx相等以避免在每次GC 后調(diào)整堆的大小。
          非堆內(nèi)存分配
          JVM使用-XX:PermSize設(shè)置非堆內(nèi)存初始值,默認(rèn)是物理內(nèi)存的1/64;由XX:MaxPermSize設(shè)置最大非堆內(nèi)存的大小,默認(rèn)是物理內(nèi)存的1/4。
          JVM內(nèi)存限制(最大值)
          首先JVM內(nèi)存限制于實(shí)際的最大物理內(nèi)存,假設(shè)物理內(nèi)存無限大的話,JVM內(nèi)存的最大值跟操作系統(tǒng)有很大的關(guān)系。簡單的說就32位處理器雖然可控內(nèi)存空間有4GB,但是具體的操作系統(tǒng)會給一個(gè)限制,這個(gè)限制一般是2GB-3GB(一般來說Windows系統(tǒng)下為1.5G-2G,Linux系統(tǒng)下為2G-3G),而64bit以上的處理器就不會有限制了。

          2. 為什么有的機(jī)器我將-Xmx和-XX:MaxPermSize都設(shè)置為512M之后Eclipse可以啟動,而有些機(jī)器無法啟動?

          通過上面對JVM內(nèi)存管理的介紹我們已經(jīng)了解到JVM內(nèi)存包含兩種:堆內(nèi)存和非堆內(nèi)存,另外JVM最大內(nèi)存首先取決于實(shí)際的物理內(nèi)存和操作系統(tǒng)。所以說設(shè)置VM參數(shù)導(dǎo)致程序無法啟動主要有以下幾種原因:
          1) 參數(shù)中-Xms的值大于-Xmx,或者-XX:PermSize的值大于-XX:MaxPermSize;
          2) -Xmx的值和-XX:MaxPermSize的總和超過了JVM內(nèi)存的最大限制,比如當(dāng)前操作系統(tǒng)最大內(nèi)存限制,或者實(shí)際的物理內(nèi)存等等。說到實(shí)際物理內(nèi)存這里需要說明一點(diǎn)的是,如果你的內(nèi)存是1024MB,但實(shí)際系統(tǒng)中用到的并不可能是1024MB,因?yàn)橛幸徊糠直挥布加昧恕?/p>

          3. 為何將上面的參數(shù)寫入到eclipse.ini文件Eclipse沒有執(zhí)行對應(yīng)的設(shè)置?
          那為什么同樣的參數(shù)在快捷方式或者命令行中有效而在eclipse.ini文件中是無效的呢?這是因?yàn)槲覀儧]有遵守eclipse.ini文件的設(shè)置規(guī)則:
          參數(shù)形如“項(xiàng) 值”這種形式,中間有空格的需要換行書寫,如果值中有空格的需要用雙引號包括起來。比如我們使用-vm C:\Java\jre1.6.0\bin\javaw.exe參數(shù)設(shè)置虛擬機(jī),在eclipse.ini文件中要寫成這樣:
          -vm
          C:\Java\jre1.6.0\bin\javaw.exe
          按照上面所說的,最后參數(shù)在eclipse.ini中可以寫成這個(gè)樣子:
          -vmargs
          -Xms128M
          -Xmx512M
          -XX:PermSize=64M
          -XX:MaxPermSize=128M
          實(shí)際運(yùn)行的結(jié)果可以通過Eclipse中“Help”-“About Eclipse SDK”窗口里面的“Configuration Details”按鈕進(jìn)行查看。
          另外需要說明的是,Eclipse壓縮包中自帶的eclipse.ini文件內(nèi)容是這樣的:
          -showsplash
          org.eclipse.platform
          --launcher.XXMaxPermSize
          256m
          -vmargs
          -Xms40m
          -Xmx256m
          其中–launcher.XXMaxPermSize(注意最前面是兩個(gè)連接線)跟-XX:MaxPermSize參數(shù)的含義基本是一樣的,我覺得唯一的區(qū)別就是前者是eclipse.exe啟動的時(shí)候設(shè)置的參數(shù),而后者是eclipse所使用的JVM中的參數(shù)。其實(shí)二者設(shè)置一個(gè)就可以了,所以這里可以把–launcher.XXMaxPermSize和下一行使用#注釋掉。

          3. 其他的啟動參數(shù)。 如果你有一個(gè)雙核的CPU,也許可以嘗試這個(gè)參數(shù):

          -XX:+UseParallelGC

          讓GC可以更快的執(zhí)行。(只是JDK 5里對GC新增加的參數(shù))

           

          補(bǔ)充:

          可以在myelipse里選中相應(yīng)的服務(wù)器比如tomcat5,展開里面的JDK子項(xiàng)頁面,來增加服務(wù)器啟動的JVM參數(shù)設(shè)置:

          -Xms128m
          -Xmx256m
          -XX:PermSize=128M
          -XX:MaxNewSize=256m
          -XX:MaxPermSize=256m

          posted @ 2010-06-26 21:37 笑看人生 閱讀(538) | 評論 (0)編輯 收藏

          Ajax中的XMLHttpRequest對象提供了兩個(gè)屬性來訪問服務(wù)器端相應(yīng)。

          • responseText:將相應(yīng)作為一個(gè)字符串返回;(系列一中已經(jīng)介紹)
          • responseXML:將相應(yīng)作為一個(gè)XML對象返回;(本系列中介紹)
          本節(jié)要介紹的內(nèi)容,很多人應(yīng)該比較熟悉,比如在網(wǎng)上注冊時(shí),在“省”列表框中選擇不同的省份,對應(yīng)的“市”列表框中出現(xiàn)該省的所有市,這個(gè)過程,不用刷新整個(gè)頁面。

          要實(shí)現(xiàn)這個(gè)功能,只須修改一下系列一中的index.jsp和AjaxServlet.java這兩個(gè)文件。

          index.jsp

          <%@ page language="java" contentType="text/html; charset=UTF-8"
              pageEncoding
          ="UTF-8"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>

          <script language="javascript">
              
          var xmlHttp;
              
          function refresh() {
                  xmlHttp 
          = createXMLHttpRequest();
                  
          var url = "AjaxServlet?province="
                          
          + document.getElementById("province").value;
                  xmlHttp.open(
          "GET", url);
                  xmlHttp.onreadystatechange 
          = handleStateChange;
                  xmlHttp.send(
          null);
              }

              
          function handleStateChange() {
                  
          if (xmlHttp.readyState == 4) {
                      
          if (xmlHttp.status == 200) {
                          updateCity();
                      }
                  }
              }

              
          function updateCity() {
                  clearCity();
                  
          var city = document.getElementById("city");
                  
          var cities = xmlHttp.responseXML.getElementsByTagName("city");        
                  
          for(var i=0;i<cities.length;i++){
                      option 
          = document.createElement("option");
                      option.appendChild(document.createTextNode(cities[i].firstChild.nodeValue));
                      city.appendChild(option);                        
                  }
              }

              
          function clearCity() {        
                  
          var city = document.getElementById("city");
                  
          while(city.childNodes.length > 0) {
                      city.removeChild(city.childNodes[
          0]);
                  }                    
              }
              
              
          function createXMLHttpRequest() {
                  
          if (window.ActiveXObject) {
                      xmlHttp 
          = new ActiveXObject("Microsoft.XMLHTTP");
                  } 
          else if (window.XMLHttpRequest) {
                      xmlHttp 
          = new XMLHttpRequest();
                  }
                  
          return xmlHttp;
              }
          </script>

          <body>
          <form action="#"><select id="province" onchange="refresh()">
              
          <option value="">Select One</option>
              
          <option value="jiangsu">Jiang Su</option>
              
          <option value="zhejiang">Zhe Jiang</option>
          </select> <br>
          <br>
          <br>
          <select id="city"></select></form>
          </body>

          </html>

          AjaxServlet.java

          package servlet;

          import java.io.IOException;
          import java.io.PrintWriter;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class AjaxServlet extends HttpServlet {

              
          private static final long serialVersionUID = 7032718233562299325L;

              @Override
              
          protected void doPost(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "POST");
              }

              @Override
              
          protected void doGet(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "GET");
              }

              
          private void processRequest(HttpServletRequest req,
                      HttpServletResponse response, String method) 
          throws IOException {
                  String province 
          = req.getParameter("province");
                  StringBuffer cities 
          = new StringBuffer("<cities>");
                  
                  
          if("jiangsu".equals(province)){
                      cities.append(
          "<city>Nanjing</city>");
                      cities.append(
          "<city>Zhenjiang</city>");
                  }
          else if("zhejiang".equals(province)){
                      cities.append(
          "<city>Hanzhou</city>");
                      cities.append(
          "<city>Wenzhou</city>");
                  }        
                  
                  PrintWriter writer 
          = response.getWriter();    
                  cities.append(
          "</cities>");
                  response.setContentType(
          "text/xml");
                  writer.write(cities.toString());
                  writer.close();
              }
          }




          posted @ 2009-11-15 09:28 笑看人生 閱讀(254) | 評論 (0)編輯 收藏

          簡單介紹一下Ajax應(yīng)用程序的基本流程:

          1. 創(chuàng)建XMLHttpRequest對象;
          2. 從頁面中獲取獲取需要的數(shù)據(jù);
          3. 建立要連接的URL;
          4. 打開與服務(wù)器的連接;
          5. 設(shè)置服務(wù)器相應(yīng)之后要調(diào)用的函數(shù),即回調(diào)函數(shù);
          6. 發(fā)送請求;
          下面以一個(gè)簡單的例子來演示一下,該例子要完成的功能是使用Ajax技術(shù)對輸入的EMail地址進(jìn)行簡單校驗(yàn),根據(jù)校驗(yàn)的結(jié)果,在頁面顯示相應(yīng)的消息。

          index.jsp

          <%@ page language="java" contentType="text/html; charset=UTF-8"
              pageEncoding
          ="UTF-8"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>

          <script language="javascript">
                
          var xmlHttp;  
               
          function sendreq(){  
                 xmlHttp 
          = createXMLHttpRequest();  //步驟1
                 
          var url = "AjaxServlet?email=" + document.getElementById("email").value;//步驟2,3
                 xmlHttp.open(
          "GET", url);  //步驟4
                 xmlHttp.onreadystatechange 
          = handleStateChange; //步驟5
                 xmlHttp.send(
          null);    //步驟6
              }  
               
          function handleStateChange(){       
                
          if(xmlHttp.readyState == 4){  
                   
          if(xmlHttp.status == 200){             
                       document.getElementById(
          "result").innerHTML=xmlHttp.responseText;  
                   }  
                 }  
               }  
               
          function createXMLHttpRequest(){  
                   
          if (window.ActiveXObject){  
                       xmlHttp 
          = new ActiveXObject("Microsoft.XMLHTTP");  
                   }  
                   
          else if (window.XMLHttpRequest){  
                       xmlHttp 
          = new XMLHttpRequest();  
                   }  
                   
          return xmlHttp;  
               }  
              
              
          </script>
          <body>
            
          <label id="result">&nbsp;</label><br>
            Email:
          <input type="text" id="email" onblur="sendreq()" value="" />  
            
          <label id="result">&nbsp;</label>
            
          </body>
          </html>

          AjaxServlet.java

          package servlet;

          import java.io.IOException;
          import java.io.PrintWriter;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class AjaxServlet extends HttpServlet {

              
          private static final long serialVersionUID = 7032718233562299325L;

              @Override
              
          protected void doPost(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "POST");
              }

              @Override
              
          protected void doGet(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "GET");
              }

              
          private void processRequest(HttpServletRequest req,
                      HttpServletResponse response, String method) 
          throws IOException {
                  String email 
          = req.getParameter("email");
                  StringBuffer validator 
          = new StringBuffer("");
                  validator.append(method);
                  response.setContentType(
          "text/html;charset=UTF-8");
                  PrintWriter writer 
          = response.getWriter();
                  
          if (email.indexOf("@"< 0) {
                      validator.append(
          ":FAILURE!");
                  }
          else{
                      validator.append(
          ":SUCCESS!");
                  }
                  writer.write(validator.toString());
                  writer.close();
              }
          }

          GET和POST請求方式區(qū)別:

          • POST方法將參數(shù)串放在請求體中發(fā)送;
          • GET方法將參數(shù)串放在URL中發(fā)送;

          如果數(shù)據(jù)處理不改變數(shù)據(jù)模型的狀態(tài),建議使用GET方式,如果需要改變數(shù)據(jù)模型的狀態(tài),建議使用POST方式;

          web.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app id="WebApp_ID" version="2.4"
              xmlns
          ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
              
          <display-name>ajax</display-name>
              
          <welcome-file-list>
                  
          <welcome-file>index.jsp</welcome-file>
              
          </welcome-file-list>
              
          <servlet>
                  
          <servlet-name>AjaxServlet</servlet-name>
                  
          <servlet-class>servlet.AjaxServlet</servlet-class>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>AjaxServlet</servlet-name>
                  
          <url-pattern>/AjaxServlet</url-pattern>
              
          </servlet-mapping>
          </web-app>


          posted @ 2009-11-01 12:31 笑看人生 閱讀(331) | 評論 (0)編輯 收藏

          最近在升級JDK版本后,啟動Eclipse時(shí)候,遇到這樣的問題:
          JVM terminated.Exit code=1.
          在網(wǎng)上找了些資料,找到了解決方法。
          只要修改eclipse.ini文件即可。修改內(nèi)容如下:
          -startup
          plugins\org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar
          --launcher.library
          plugins\org.eclipse.equinox.launcher.win32.win32.x86_1.0.100.v20080509-1800
          -showsplash
          vm
          D:\Java\jdk15011\bin\javaw.exe
          org.eclipse.platform
          --launcher.XXMaxPermSize
          256m
          -vmargs
          -Xms40m
          -Xmx256m
          其中粗體部分為新增內(nèi)容。

          posted @ 2008-07-27 21:22 笑看人生 閱讀(11649) | 評論 (4)編輯 收藏

           

          問題描述:

          最近在做Eclipse插件開發(fā)的時(shí)候,在國際化的問題上遇到這樣問題,比如我的plugin.xml文件中,通過%xxx來引用plugin_zh.properties中對應(yīng)鍵xxx的信息時(shí),在插件運(yùn)行時(shí),相應(yīng)的信息顯示不了,后來進(jìn)過研究,發(fā)現(xiàn)是由于在MANIFEST.MF少配置了一項(xiàng)造成的

          Bundle-Localization: plugin,

          而這一項(xiàng)配置我在Overview標(biāo)簽頁上沒有找到相應(yīng)設(shè)置的地方,把這個(gè)在MANIFEST.MF加上,插件在運(yùn)行時(shí),就可以顯示plugin_zh.properties中定義的消息了。

          posted @ 2008-04-23 19:44 笑看人生 閱讀(1616) | 評論 (0)編輯 收藏

          僅列出標(biāo)題
          共4頁: 上一頁 1 2 3 4 下一頁 
          主站蜘蛛池模板: 五大连池市| 太保市| 万安县| 思茅市| 中西区| 鄄城县| 长垣县| 揭东县| 文成县| 天全县| 齐齐哈尔市| 宜君县| 文山县| 陇南市| 雅江县| 梁河县| 华坪县| 平阴县| 茶陵县| 新田县| 绥滨县| 舒城县| 新疆| 卢氏县| 内乡县| 寻甸| 南开区| 湾仔区| 怀集县| 手游| 遂平县| 民丰县| 获嘉县| 仁怀市| 林口县| 武冈市| 尼勒克县| 江山市| 洛阳市| 达州市| 惠州市|