Eclipse WTP Projects Facets實(shí)戰(zhàn)指南(2)
修飾工作
給facet在選擇列表中添加圖標(biāo)
格式如下:
- <extension point="org.eclipse.wst.common.project.facet.ui.images">
- <image facet="{string}" path="{string}"/> (0 or more)
- <image category="{string}" path="{string}"/> (0 or more)
- extension>
設(shè)置如下:
- <extension point="org.eclipse.wst.common.project.facet.ui.images">
- <image facet="formgen.core" path="icons/formgen-core.gif"/>
- <image facet="formgen.ext" path="icons/formgen-ext.gif"/>
- <image category="formgen.category" path="icons/formgen-cat.gif"/>
- extension>
效果如圖
添加向?qū)ы?br />
有時(shí)候我們可能需要根據(jù)用戶的輸入來執(zhí)行facet, 通過添加wizard page來接收用戶的輸入, 用戶在facet選擇列表頁做出選擇之后,將出現(xiàn)我們添加的wizard Page
下面通過給我們的servlet指定url pattern的例子來進(jìn)行說明
plugin.xml設(shè)置格式如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <action>
- <config-factory class="class:org.eclipse.wst.common.project.facet.core.IActionConfigFactory"/>
- action>
- extension>
- <extension point="org.eclipse.wst.common.project.facet.ui.wizardPages">
- <wizard-pages action="{string}"> (0 or more)
- <page class="{class:org.eclipse.wst.common.project.facet.ui.IFacetWizardPage}"/> (1 or more)
- wizard-pages>
- 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)生成的原因
配置如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <project-facet-version facet="formgen.core" version="1.0">
- <action type="INSTALL" id="formgen.core.install">
- <config-factory class="com.formgen.eclipse.FormGenCoreFacetInstallConfig$Factory"/>
- action>
- project-facet-version>
- extension>
- <extension point="org.eclipse.wst.common.project.facet.ui.wizardPages">
- <wizard-pages action="formgen.core.install">
- <page class="com.formgen.eclipse.FormGenCoreFacetInstallPage"/>
- wizard-pages>
- extension>
代碼如下:
- package com.formgen.eclipse;
- import org.eclipse.wst.common.project.facet.core.IActionConfigFactory;
- public final class FormGenCoreFacetInstallConfig
- {
- private String urlPattern = "*.form";
- public String getUrlPattern()
- {
- return this.urlPattern;
- }
- public void setUrlPattern( final String urlPattern )
- {
- this.urlPattern = urlPattern;
- }
- public static final class Factory implements IActionConfigFactory
- {
- public Object create()
- {
- return new FormGenCoreFacetInstallConfig();
- }
- }
- }
- package com.formgen.eclipse;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Text;
- import org.eclipse.wst.common.project.facet.ui.AbstractFacetWizardPage;
- public final class FormGenCoreFacetInstallPage extends AbstractFacetWizardPage
- {
- private FormGenCoreFacetInstallConfig config;
- private Text urlPatternTextField;
- public FormGenCoreFacetInstallPage()
- {
- super( "formgen.core.facet.install.page" );
- setTitle( "FormGen Core" );
- setDescription( "Configure the FormGen servlet." );
- }
- public void createControl( final Composite parent )
- {
- final Composite composite = new Composite( parent, SWT.NONE );
- composite.setLayout( new GridLayout( 1, false ) );
- final Label label = new Label( composite, SWT.NONE );
- label.setLayoutData( gdhfill() );
- label.setText( "URL Pattern:" );
- this.urlPatternTextField = new Text( composite, SWT.BORDER );
- this.urlPatternTextField.setLayoutData( gdhfill() );
- this.urlPatternTextField.setText( this.config.getUrlPattern() );
- setControl( composite );
- }
- public void setConfig( final Object config )
- {
- this.config = (FormGenCoreFacetInstallConfig) config;
- }
- public void transferStateToConfig()
- {
- this.config.setUrlPattern( this.urlPatternTextField.getText() );
- }
- private static GridData gdhfill()
- {
- return new GridData( GridData.FILL_HORIZONTAL );
- }
- }
- package com.formgen.eclipse;
- import org.eclipse.core.resources.IFolder;
- import org.eclipse.core.resources.IProject;
- import org.eclipse.core.runtime.CoreException;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.core.runtime.Path;
- import org.eclipse.wst.common.project.facet.core.IDelegate;
- import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
- public final class FormGenCoreFacetInstallDelegate implements IDelegate
- {
- public void execute( final IProject pj,
- final IProjectFacetVersion fv,
- final Object config,
- final IProgressMonitor monitor )
- throws CoreException
- {
- monitor.beginTask( "", 2 );
- try
- {
- final FormGenCoreFacetInstallConfig cfg
- = (FormGenCoreFacetInstallConfig) config;
- final IFolder webInfLib = Utils.getWebInfLibDir( pj );
- Utils.copyFromPlugin( new Path( "libs/formgen-core.jar" ),
- webInfLib.getFile( "formgen-core.jar" ) );
- monitor.worked( 1 );
- Utils.registerFormGenServlet( pj, cfg.getUrlPattern() );
- monitor.worked( 1 );
- }
- finally
- {
- monitor.done();
- }
- }
- }
效果如圖
定義Preset
為了將創(chuàng)建某個(gè)工程所需要的facet集中在一個(gè)用一個(gè)名字代替,這就需要使用preset(或稱configuration),其擴(kuò)展點(diǎn)格式如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <preset id="{string}">
- <label>{string}</label>
- <description>{string}</description> (optional)
- <facet id="{string}" version="{string}"/> (1 or more)
- </preset>
- </extension>
為了讓preset能應(yīng)用于指定的facet project, preset必須包含所有的fixed facet, 所謂的fixed facet指的是那些要?jiǎng)?chuàng)建的工程所必須的而不能刪除的facet.
設(shè)置如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <preset id="formgen.preset">
- <label>FormGen Web Project</label>
- <description>Creates a web project with FormGen functionality.</description>
- <facet id="jst.java" version="5.0"/>
- <facet id="jst.web" version="2.2"/>
- <facet id="formgen.core" version="1.0"/>
- <facet id="formgen.ext" version="1.0"/>
- </preset>
- </extension>
效果如圖
指定runtime映射
在創(chuàng)建project的時(shí)候,有時(shí)候需要指定項(xiàng)目依賴的runtime,我們也可以通過擴(kuò)展點(diǎn)來檢查當(dāng)前的facet所需要的runtime是否指定.
配置格式如下:
- <extension point="org.eclipse.wst.common.project.facet.core.runtimes">
- <supported> (0 or more)
- <runtime-component any="{boolean}"/> (optional)
- <runtime-component id="{string}"/ version="{version.expr}"/> (0 or more)
- <facet id="{string}"/ version="{version.expr}"/> (1 or more)
- </supported>
- </extension>
下面是不依賴其他組件的配置:
- <extension point="org.eclipse.wst.common.project.facet.core.runtimes">
- <supported>
- <runtime-component any="true"/>
- <facet id="formgen.core"/>
- <facet id="formgen.ext"/>
- </supported>
- </extension>
下面是依賴tomcat的配置
- <extension point="org.eclipse.wst.common.project.facet.core.runtimes">
- <supported>
- <runtime-component id="org.eclipse.jst.server.tomcat" version="[5.0"/>
- <facet id="formgen.core"/>
- <facet id="formgen.ext"/>
- </supported>
- </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)來處理,格式如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <project-facet>
- <version-comparator class="{class:java.util.Comparator<String>}"/>
- </project-facet>
- </extension>
可以直接從接口從頭實(shí)現(xiàn),也可以繼承org.eclipse.wst.common.project.facet.core.DefaultVersionComparator來進(jìn)行定制
代碼如下:
- /**
- * Returns the string containing the separator characters that should be
- * used when breaking the version string into segments. The default
- * implementation returns ".". Subclasses can override this method.
- *
- * @return the separator characters
- */
- protected String getSeparators();
- /**
- * Parses a segment of the version string. The default implementation parses
- * the first segment as an integer (leading zeroes are ignored) and the
- * rest of the segments as decimals (leading zeroes are kept). Subclasses
- * can override this method to provide custom parsing for any number of
- * segments.
- *
- * @param version the full version string
- * @param segment the version segment
- * @param position the position of the segment in the version string
- * @return the parsed representation of the segment as a {@see Comparable}
- * @throws VersionFormatException if encountered an error while parsing
- */
- protected Comparable parse( final String version,
- final String segment,
- final int position )
- 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)用順序是不確定的
事件處理器的配置格式如下:
- <extension point="org.eclipse.wst.common.project.facet.core.facets">
- <event-handler facet="{string}" version="{version.expr}" type="{event.type}">
- <delegate class="{class:org.eclipse.wst.common.project.facet.core.IDelegate}"/>
- </action>
- </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í)例
- package org.eclipse.wst.common.project.facet.core;
- import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;
- /**
- * Describes the runtime changed event to the RUNTIME_CHANGED event handlers.
- */
- public interface IRuntimeChangedEvent
- {
- /**
- * Returns the runtime previously associated with the project.
- *
- * @return the runtime previously associated with the project, or null
- */
- IRuntime getOldRuntime();
- /**
- * Returns the runtime that's now associated with the project.
- *
- * @return the runtime that's now associated with the project, or null
- */
- IRuntime getNewRuntime();
- }
附錄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的屬性頁是否可用
- <extension point="org.eclipse.ui.propertyPages">
- <page
- adaptable="true"
- objectClass="org.eclipse.core.resources.IProject"
- name="FormGen Properties"
- class="com.formgen.eclipse.FormGenPropertiesPage"
- id="org.eclipse.jst.j2ee.internal.J2EEDependenciesPage">
- <enabledWhen>
- <test
- forcePluginActivation="true"
- property="org.eclipse.wst.common.project.facet.core.projectFacet"
- value="formgen.core"/>
- </enabledWhen>
- </page>
- </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接口的代碼:
- package org.eclipse.wst.common.project.facet.ui;
- import java.util.Set;
- &nb
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】