国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計(jì)軟件教程 > Eclipse WTP Projects Facets實(shí)戰(zhàn)指南(2)

Eclipse WTP Projects Facets實(shí)戰(zhàn)指南(2)
2010-01-14 23:06:05  作者:  來源:

Eclipse WTP Projects Facets實(shí)戰(zhàn)指南(2)

原創(chuàng)作者: macrochen   閱讀:388次   評(píng)論:0條   更新時(shí)間:2007-11-21    

修飾工作
給facet在選擇列表中添加圖標(biāo)
格式如下:

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.ui.images">  
  2.   <image facet="{string}" path="{string}"/> (0 or more)   
  3.   <image category="{string}" path="{string}"/> (0 or more)   
  4. extension>  


設(shè)置如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.ui.images">  
  2.   <image facet="formgen.core" path="icons/formgen-core.gif"/>  
  3.   <image facet="formgen.ext" path="icons/formgen-ext.gif"/>  
  4.   <image category="formgen.category" path="icons/formgen-cat.gif"/>  
  5. extension>  

效果如圖

 

添加向?qū)ы?br /> 有時(shí)候我們可能需要根據(jù)用戶的輸入來執(zhí)行facet, 通過添加wizard page來接收用戶的輸入, 用戶在facet選擇列表頁做出選擇之后,將出現(xiàn)我們添加的wizard Page
下面通過給我們的servlet指定url pattern的例子來進(jìn)行說明
plugin.xml設(shè)置格式如下:

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <action>  
  3.     <config-factory class="class:org.eclipse.wst.common.project.facet.core.IActionConfigFactory"/>  
  4.   action>  
  5. extension>  
  6.   
  7. <extension point="org.eclipse.wst.common.project.facet.ui.wizardPages">  
  8.   <wizard-pages action="{string}"> (0 or more)   
  9.     <page class="{class:org.eclipse.wst.common.project.facet.ui.IFacetWizardPage}"/> (1 or more)   
  10.   wizard-pages>  
  11. extension>    

 
這里需要說明的是為了讓我們定制的wizard page和action delegate之間進(jìn)行通信, 需要使用一個(gè)javabean或者是map來封裝wizard page中輸入的數(shù)據(jù), 而且必須在action配置中提供一個(gè)action factory,該對(duì)象將由factory創(chuàng)建, wizard page填充數(shù)據(jù), action delegate負(fù)責(zé)從該對(duì)象讀取數(shù)據(jù)

除外,wizard page將通過指定的action id來引用action, 這也是我們前面推薦指定action id而不是系統(tǒng)生成的原因

配置如下:

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <project-facet-version facet="formgen.core" version="1.0">  
  3.     <action type="INSTALL" id="formgen.core.install">  
  4.       <config-factory class="com.formgen.eclipse.FormGenCoreFacetInstallConfig$Factory"/>  
  5.     action>  
  6.   project-facet-version>  
  7. extension>  
  8.   
  9. <extension point="org.eclipse.wst.common.project.facet.ui.wizardPages">  
  10.   <wizard-pages action="formgen.core.install">  
  11.     <page class="com.formgen.eclipse.FormGenCoreFacetInstallPage"/>  
  12.   wizard-pages>  
  13. extension>  

 

代碼如下:

java 代碼
 
  1. package com.formgen.eclipse;   
  2.   
  3. import org.eclipse.wst.common.project.facet.core.IActionConfigFactory;   
  4.   
  5. public final class FormGenCoreFacetInstallConfig   
  6. {   
  7.     private String urlPattern = "*.form";   
  8.   
  9.     public String getUrlPattern()   
  10.     {   
  11.         return this.urlPattern;   
  12.     }   
  13.   
  14.     public void setUrlPattern( final String urlPattern )   
  15.     {   
  16.         this.urlPattern = urlPattern;   
  17.     }   
  18.   
  19.     public static final class Factory implements IActionConfigFactory   
  20.     {   
  21.         public Object create()   
  22.         {   
  23.             return new FormGenCoreFacetInstallConfig();   
  24.         }   
  25.     }   
  26. }   
  27.   


 

java 代碼
 
  1. package com.formgen.eclipse;   
  2.   
  3. import org.eclipse.swt.SWT;   
  4. import org.eclipse.swt.layout.GridData;   
  5. import org.eclipse.swt.layout.GridLayout;   
  6. import org.eclipse.swt.widgets.Composite;   
  7. import org.eclipse.swt.widgets.Label;   
  8. import org.eclipse.swt.widgets.Text;   
  9. import org.eclipse.wst.common.project.facet.ui.AbstractFacetWizardPage;   
  10.   
  11. public final class FormGenCoreFacetInstallPage extends AbstractFacetWizardPage   
  12. {   
  13.     private FormGenCoreFacetInstallConfig config;   
  14.     private Text urlPatternTextField;   
  15.   
  16.     public FormGenCoreFacetInstallPage()   
  17.     {   
  18.         super"formgen.core.facet.install.page" );   
  19.   
  20.         setTitle( "FormGen Core" );   
  21.         setDescription( "Configure the FormGen servlet." );   
  22.     }   
  23.   
  24.     public void createControl( final Composite parent )   
  25.     {   
  26.         final Composite composite = new Composite( parent, SWT.NONE );   
  27.         composite.setLayout( new GridLayout( 1false ) );   
  28.   
  29.         final Label label = new Label( composite, SWT.NONE );   
  30.         label.setLayoutData( gdhfill() );   
  31.         label.setText( "URL Pattern:" );   
  32.   
  33.         this.urlPatternTextField = new Text( composite, SWT.BORDER );   
  34.         this.urlPatternTextField.setLayoutData( gdhfill() );   
  35.         this.urlPatternTextField.setText( this.config.getUrlPattern() );   
  36.   
  37.         setControl( composite );   
  38.     }   
  39.   
  40.     public void setConfig( final Object config )   
  41.     {   
  42.         this.config = (FormGenCoreFacetInstallConfig) config;   
  43.     }   
  44.   
  45.     public void transferStateToConfig()   
  46.     {   
  47.         this.config.setUrlPattern( this.urlPatternTextField.getText() );   
  48.     }   
  49.   
  50.     private static GridData gdhfill()   
  51.     {   
  52.         return new GridData( GridData.FILL_HORIZONTAL );   
  53.     }   
  54. }   
  55.   


 

java 代碼
 
  1. package com.formgen.eclipse;   
  2.   
  3. import org.eclipse.core.resources.IFolder;   
  4. import org.eclipse.core.resources.IProject;   
  5. import org.eclipse.core.runtime.CoreException;   
  6. import org.eclipse.core.runtime.IProgressMonitor;   
  7. import org.eclipse.core.runtime.Path;   
  8. import org.eclipse.wst.common.project.facet.core.IDelegate;   
  9. import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;   
  10.   
  11. public final class FormGenCoreFacetInstallDelegate implements IDelegate   
  12. {   
  13.     public void execute( final IProject pj,   
  14.                          final IProjectFacetVersion fv,   
  15.                          final Object config,   
  16.                          final IProgressMonitor monitor )   
  17.   
  18.         throws CoreException   
  19.   
  20.     {   
  21.         monitor.beginTask( ""2 );   
  22.   
  23.         try  
  24.         {   
  25.             final FormGenCoreFacetInstallConfig cfg   
  26.                 = (FormGenCoreFacetInstallConfig) config;   
  27.   
  28.             final IFolder webInfLib = Utils.getWebInfLibDir( pj );   
  29.   
  30.             Utils.copyFromPlugin( new Path( "libs/formgen-core.jar" ),   
  31.                                   webInfLib.getFile( "formgen-core.jar" ) );   
  32.   
  33.             monitor.worked( 1 );   
  34.   
  35.             Utils.registerFormGenServlet( pj, cfg.getUrlPattern() );   
  36.   
  37.             monitor.worked( 1 );   
  38.         }   
  39.         finally  
  40.         {   
  41.             monitor.done();   
  42.         }   
  43.     }   
  44. }   
  45.   

效果如圖

定義Preset
為了將創(chuàng)建某個(gè)工程所需要的facet集中在一個(gè)用一個(gè)名字代替,這就需要使用preset(或稱configuration),其擴(kuò)展點(diǎn)格式如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <preset id="{string}">  
  3.     <label>{string}</label>  
  4.     <description>{string}</description> (optional)   
  5.     <facet id="{string}" version="{string}"/> (1 or more)   
  6.   </preset>  
  7. </extension>  

為了讓preset能應(yīng)用于指定的facet project, preset必須包含所有的fixed facet, 所謂的fixed facet指的是那些要?jiǎng)?chuàng)建的工程所必須的而不能刪除的facet.
設(shè)置如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <preset id="formgen.preset">  
  3.     <label>FormGen Web Project</label>  
  4.     <description>Creates a web project with FormGen functionality.</description>  
  5.     <facet id="jst.java" version="5.0"/>  
  6.     <facet id="jst.web" version="2.2"/>  
  7.     <facet id="formgen.core" version="1.0"/>  
  8.     <facet id="formgen.ext" version="1.0"/>  
  9.   </preset>  
  10. </extension>  

效果如圖

指定runtime映射
在創(chuàng)建project的時(shí)候,有時(shí)候需要指定項(xiàng)目依賴的runtime,我們也可以通過擴(kuò)展點(diǎn)來檢查當(dāng)前的facet所需要的runtime是否指定.
配置格式如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.runtimes">  
  2.   <supported> (0 or more)   
  3.     <runtime-component any="{boolean}"/> (optional)   
  4.     <runtime-component id="{string}"version="{version.expr}"/> (0 or more)   
  5.     <facet id="{string}"version="{version.expr}"/> (1 or more)   
  6.   </supported>  
  7. </extension>  


下面是不依賴其他組件的配置:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.runtimes">  
  2.   <supported>  
  3.     <runtime-component any="true"/>  
  4.     <facet id="formgen.core"/>  
  5.     <facet id="formgen.ext"/>  
  6.   </supported>  
  7. </extension>  


下面是依賴tomcat的配置
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.runtimes">  
  2.   <supported>  
  3.     <runtime-component id="org.eclipse.jst.server.tomcat" version="[5.0"/>  
  4.     <facet id="formgen.core"/>  
  5.     <facet id="formgen.ext"/>  
  6.   </supported>  
  7. </extension>  

總結(jié)

通過上面的步驟, 我們創(chuàng)建了一個(gè)完整的project facet, 這里面包括指定約束, 實(shí)現(xiàn)action, 對(duì)facet分組, 創(chuàng)建wizard page. 更多的信息可看下面的參考附錄
附錄A 定制Version Comparetor
就是對(duì)版本進(jìn)行比較的策略進(jìn)行定制, 如果要使用自己的版本比較方式, 那么可以通過擴(kuò)展點(diǎn)來處理,格式如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <project-facet>  
  3.     <version-comparator class="{class:java.util.Comparator<String>}"/>  
  4.   </project-facet>  
  5. </extension>  


可以直接從接口從頭實(shí)現(xiàn),也可以繼承org.eclipse.wst.common.project.facet.core.DefaultVersionComparator來進(jìn)行定制
代碼如下:
 

java 代碼
 
  1. /**  
  2.  * Returns the string containing the separator characters that should be  
  3.  * used when breaking the version string into segments. The default  
  4.  * implementation returns ".". Subclasses can override this method.  
  5.  *   
  6.  * @return the separator characters  
  7.  */  
  8.        
  9. protected String getSeparators();   
  10.        
  11. /**  
  12.  * Parses a segment of the version string. The default implementation parses  
  13.  * the first segment as an integer (leading zeroes are ignored) and the  
  14.  * rest of the segments as decimals (leading zeroes are kept). Subclasses   
  15.  * can override this method to provide custom parsing for any number of   
  16.  * segments.  
  17.  *   
  18.  * @param version the full version string  
  19.  * @param segment the version segment  
  20.  * @param position the position of the segment in the version string  
  21.  * @return the parsed representation of the segment as a {@see Comparable}  
  22.  * @throws VersionFormatException if encountered an error while parsing  
  23.  */  
  24.        
  25. protected Comparable parse( final String version,   
  26.                             final String segment,   
  27.                             final int position )   
  28.        
  29.     throws VersionFormatException;   

附錄B Version Expression
為了一次指定多個(gè)版本,就需要使用版本表達(dá)式(version expression), 在很多地方我都用到了版本表達(dá)式, 版本表達(dá)式如果有多個(gè),則使用逗號(hào)分隔,這里的逗號(hào)表示或的意思, 范圍通過括號(hào)加破折號(hào)來制定,如[x-z), 中括號(hào)表示包含, 小括號(hào)表示不包含
下面是一些例子:
1.2
1.2,1.5,3.2
[1.2-3.2]
[3.7-5.0)
[3.7
5.0)
1.2,[3.0-4.5),[7.3
附錄C Event Handler
project fact可以為某些事件注冊(cè)listener, 這些時(shí)間包括:
PRE_INSTALL
POST_INSTALL
PRE_UNINSTALL
POST_UNINSTALL
PRE_VERSION_CHANGE
POST_VERSION_CHANGE
RUNTIME_CHANGED
事件處理器的聲明和action的聲明非常類似,但是他們也有一些區(qū)別,主要體現(xiàn)在:
與action不同, 事件處理器無法由用戶直接觸發(fā),因此無法將事件處理器與wizard page關(guān)聯(lián),也無法為其提供封裝數(shù)據(jù)的配置對(duì)象
可以給一個(gè)事件設(shè)置多個(gè)事件處理器,但是他們的調(diào)用順序是不確定的
事件處理器的配置格式如下:
 

xml 代碼
 
  1. <extension point="org.eclipse.wst.common.project.facet.core.facets">  
  2.   <event-handler facet="{string}" version="{version.expr}" type="{event.type}">  
  3.     <delegate class="{class:org.eclipse.wst.common.project.facet.core.IDelegate}"/>  
  4.   </action>  
  5. </extension>  


與action的配置很類似,也實(shí)現(xiàn)了IDelegate接口, 而且event-handler也可以嵌入到project-facet-version元素內(nèi)部,此時(shí)的facet和version屬性將被忽略.

PRE_* and POST_* 事件處理器用來取得配置對(duì)象,然后傳遞給IDelegate的execute方法使用, RUNTIME_CHANGED 事件處理器是用來取得IRuntimeChangedEvent實(shí)例
 

java 代碼
 
  1. package org.eclipse.wst.common.project.facet.core;   
  2.   
  3. import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;   
  4.   
  5. /**  
  6.  * Describes the runtime changed event to the RUNTIME_CHANGED event handlers.  
  7.  */  
  8.   
  9. public interface IRuntimeChangedEvent   
  10. {   
  11.     /**  
  12.      * Returns the runtime previously associated with the project.  
  13.      *   
  14.      * @return the runtime previously associated with the project, or null  
  15.      */  
  16.        
  17.     IRuntime getOldRuntime();   
  18.        
  19.     /**  
  20.      * Returns the runtime that's now associated with the project.  
  21.      *   
  22.      * @return the runtime that's now associated with the project, or null  
  23.      */  
  24.        
  25.     IRuntime getNewRuntime();   
  26. }   
  27.   

附錄D Property Tester
Property Tester是用來被org.eclipse.core.expressions 包中的擴(kuò)展點(diǎn)測(cè)試使用的,它常用來決定某些菜單或者屬性頁選項(xiàng)是否可用.屬性名為org.eclipse.wst.common.project.facet.core.projectFacet,值為facet id或者facet id 加冒號(hào)再加版本表達(dá)式的組合
下面的這個(gè)例子用來控制project的屬性頁是否可用
 

xml 代碼
 
  1. <extension point="org.eclipse.ui.propertyPages">  
  2.   <page    
  3.     adaptable="true"  
  4.     objectClass="org.eclipse.core.resources.IProject"  
  5.     name="FormGen Properties"  
  6.     class="com.formgen.eclipse.FormGenPropertiesPage"  
  7.     id="org.eclipse.jst.j2ee.internal.J2EEDependenciesPage">  
  8.     <enabledWhen>  
  9.       <test    
  10.         forcePluginActivation="true"  
  11.         property="org.eclipse.wst.common.project.facet.core.projectFacet"  
  12.         value="formgen.core"/>  
  13.     </enabledWhen>             
  14.   </page>  
  15. </extension>  


附錄E Wizard Content
有時(shí)候我們可能根據(jù)其他facet所對(duì)應(yīng)的wizard page的用戶輸入來調(diào)整action的行為, 而IWizardContet接口就是為該目的而設(shè)計(jì)的, wizard page的setWizardContext方法被調(diào)用的時(shí)候會(huì)取得該接口的handler, 如果你的代碼對(duì)IWziardContenxt造成依賴,可能需要注意以下事項(xiàng):
1.fact需要的內(nèi)容可能在執(zhí)行前已經(jīng)被install了, 此時(shí)在wizard context中將無法找到要install的配置,所以在需要做一些判斷
2.如果某些情況下wizard page不被調(diào)用的話,要給配置對(duì)象設(shè)置默認(rèn)值
下面是IWizardContext接口的代碼:
 

java 代碼
 
  1. package org.eclipse.wst.common.project.facet.ui;   
  2.   
  3. import java.util.Set;   
  4. &nb
    安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢