WTP插件开发中的preset学习笔记(by quqi99)
作者:张华 发表于:2010-04-27
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.common.project.facet.core.IDynamicPreset;
import org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy;
import org.eclipse.wst.common.project.facet.core.IPresetFactory;
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.PresetDefinition;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponent;
/**
* WTP插件中的preset
* @version 0.10 2010-4-27
* @author Zhang Hua
*/
public class MyPresetFactory implements IPresetFactory {
/**
* <extension point="org.eclipse.wst.common.project.facet.core.presets">
* <static-preset id="my.preset.v8">
* <label>My preset</label>
* <facet id="my.facet" verstion="8"/>
* <facet id="jst.web" verstion="2.3"/>
* </static-preset>
* <dynamic-preset id="my.preset.v8">
* <factory class="test.MyPresetFactory"/>
* </dynamic-preset>
* </extension>
*/
public PresetDefinition createPreset(String presetId, Map<String, Object> context)
throws CoreException {
final IFacetedProjectWorkingCopy copy = (IFacetedProjectWorkingCopy)context.get(IDynamicPreset.CONTEXT_KEY_FACETED_PROJECT);
String facetRuntimeTypeID = null;
String facetRuntimeVersion = null;
IRuntime runtime = null;
Set<IRuntime> set = copy.getTargetableRuntimes();
if(set!=null && set.size()>0){
runtime = (IRuntime)set.toArray()[0];
if(runtime.getRuntimeComponents().size() > 0){
IRuntimeComponent runtimeComponent = runtime.getRuntimeComponents().get(0);
facetRuntimeTypeID = runtimeComponent.getRuntimeComponentType().getId();
facetRuntimeVersion = runtimeComponent.getRuntimeComponentVersion().getVersionString();
}
}
if("my.runtime.appserv".equals(facetRuntimeTypeID) && "8.0".equals(facetRuntimeVersion)){
IProjectFacet myfacet = ProjectFacetsManager.getProjectFacet("my.facet");
IProjectFacetVersion facetVer = myfacet.getDefaultVersion();
Set<IProjectFacetVersion> facets = new HashSet<IProjectFacetVersion>();
facets.addAll(copy.getProjectFacets());
facets.add(facetVer);
copy.setProjectFacets(facets);
return new PresetDefinition("My App preset v8.0","My App preset v8.0",facets);
}
return null;
}
}