1。创建新建普通工程向导
1.1 新建向导页
import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; 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; /** * 测试页 * @author admin * */ public class TestPage extends WizardPage{ private String projectName; public TestPage(String pageName) { super(pageName); } @Override public void createControl(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new GridLayout(2, false)); Label label = new Label(composite,SWT.None); label.setText("工程名称"); final Text text = new Text(composite,SWT.BORDER); GridData gd = new GridData(GridData.FILL_HORIZONTAL); text.setLayoutData(gd); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { String textStr = text.getText(); if(null != textStr && !textStr.trim().equals("") ){ setPageComplete(true); setProjectName(textStr); } } }); setPageComplete(false);//是否允许下一步 setControl(composite);//如果不加这句,不显示内容 Dialog.applyDialogFont(composite); } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } }
1.2 创建向导
import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.INewWizard; import org.eclipse.ui.IWorkbench; import com.daosh.field.ui.wizard.project.TestPage; public class TestWizard extends Wizard implements INewWizard, IRunnableWithProgress{ private TestPage testPage; public TestWizard(){ super(); setNeedsProgressMonitor(true); } /** * 初始化 */ @Override public void init(IWorkbench workbench, IStructuredSelection selection) { } @Override public void addPages() { testPage = new TestPage("test"); addPage(testPage);//增加创建的页面到向导中 } /** * finish 执行的方法 */ @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IProject project = root.getProject("test");//工程名称随便写。具体的需要自定义对话框。然后输入。 project.create(null); project.open(null); } catch (CoreException e) { SysOut.error(e); LogUtil.logError(e); } } /** * 点击finish 执行的方法 */ @Override public boolean performFinish() { if(this.getContainer().getCurrentPage() != testPage){//如果不等于test页面,则不允许finish 执行方法 return false; } try { getContainer().run(false, true, this); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return true; } /** * 是否可以finish */ @Override public boolean canFinish() { if(this.getContainer().getCurrentPage() != testPage){ return false; } return super.canFinish(); } }
1.3 配置mf文件
增加org.eclipse.ui.newWizards 扩展点。
-- 新建category 。该category最终会在new - project 中可以查看
-- 新建wizard 。输入之前创建好的向导
重启即可。
2. 创建web工程。
2.1创建web 工程依赖wst的相关jar。如果没有。自行安装。
2.2 依赖的jar,缺一不可:
2.3。这个方法调用的是wst。可以动态扩展界面的个数等。。调用方法和普通的工程调用的方法一致
import java.io.File; import java.lang.reflect.InvocationTargetException; import java.util.Map; import org.eclipse.core.runtime.Path; import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jst.common.project.facet.IJavaFacetInstallDataModelProperties; import org.eclipse.jst.common.project.facet.core.JavaFacetInstallConfig; import org.eclipse.jst.j2ee.internal.web.archive.operations.WebFacetProjectCreationDataModelProvider; import org.eclipse.jst.j2ee.web.project.facet.IWebFacetInstallDataModelProperties; import org.eclipse.jst.servlet.ui.project.facet.WebProjectWizard; import org.eclipse.wst.common.componentcore.datamodel.properties.IFacetProjectCreationDataModelProperties; import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; import org.eclipse.wst.common.frameworks.internal.datamodel.DataModelImpl; import com.daosh.field.ui.wizard.project.TestWebPage; public class TestWebWizard extends WebProjectWizard{ private TestWebPage testPage; private final String SOURCE_PATH = "WEB-INF/classes";// 默认存放class的路径 public TestWebWizard(){ super(); setWindowTitle("web向导");//重写对话框标题 } /** * 创建第一页 */ @Override protected IWizardPage createFirstPage() { testPage = new TestWebPage(model,"test"); return testPage; } /** * 获取所有的page。 */ @Override public IWizardPage[] getPages() { IWizardPage[] pages = super.getPages(); return pages; } /** * 结束需要执行的方法 */ @Override protected void postPerformFinish() throws InvocationTargetException { } @SuppressWarnings({ "unchecked", "restriction" }) @Override public boolean performFinish() { testPage.setPageComplete(false); Map<String, DataModelImpl> map = (Map<String, DataModelImpl>) this.model.getProperty(IFacetProjectCreationDataModelProperties.FACET_DM_MAP); if (map != null) { // 获得web文件夹名称 DataModelImpl webImpl = (DataModelImpl) map.get("jst.web"); String webContentName = webImpl.getStringProperty(IWebFacetInstallDataModelProperties.CONFIG_FOLDER); // 获得source folder设置,并改变设置 DataModelImpl impl = (DataModelImpl) map.get("jst.java"); if (impl != null) { JavaFacetInstallConfig config = (JavaFacetInstallConfig) impl.getProperty(IJavaFacetInstallDataModelProperties.JAVA_FACET_INSTALL_CONFIG); config.setDefaultOutputFolder(new Path(webContentName+ File.separator + SOURCE_PATH)); impl.setProperty(IJavaFacetInstallDataModelProperties.JAVA_FACET_INSTALL_CONFIG,config); map.put("jst.java", impl); } } this.model.setProperty(IFacetProjectCreationDataModelProperties.FACET_DM_MAP, map); return super.performFinish(); } /** * 创建数据模型 */ @SuppressWarnings("restriction") protected IDataModel createDataModel() { return DataModelFactory.createDataModel(new WebFacetProjectCreationDataModelProvider()); } public boolean needsProgressMonitor() { return true; } /** * 是否需要上一步下一步按钮 */ public boolean needsPreviousAndNextButtons() { return false; } }
2.4 web向导的第一个页面
import java.util.Iterator; import java.util.Set; import java.util.SortedSet; import org.eclipse.jst.servlet.ui.project.facet.WebProjectFirstPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; import org.eclipse.wst.common.frameworks.internal.operations.IProjectCreationPropertiesNew; import org.eclipse.wst.common.frameworks.internal.ui.WTPCommonUIResourceHandler; import org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy; import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion; import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager; import org.eclipse.wst.common.project.facet.ui.ModifyFacetedProjectWizard; /** * 新建工程第一个页面设置和预加载设置 * @author admin * */ @SuppressWarnings("restriction") public class TestWebPage extends WebProjectFirstPage { protected Text projectNameField; public TestWebPage(IDataModel model, String pageName) { super(model, pageName); } public IProjectFacetVersion getPrimaryFacetVersion() { primaryProjectFacet = ProjectFacetsManager.getProjectFacet(getModuleTypeID()); IProjectFacetVersion fv = this.primaryProjectFacet.getVersion("2.5"); return fv; } protected Composite createTopLevelComposite(Composite parent) { Composite top = new Composite(parent, SWT.NONE); top.setLayout(new GridLayout()); top.setLayoutData(new GridData(GridData.FILL_BOTH)); createProjectGroup(top); final IProjectFacetVersion fv = getPrimaryFacetVersion(); IFacetedProjectWorkingCopy fpjwc = (IFacetedProjectWorkingCopy) this.model .getProperty(FACETED_PROJECT_WORKING_COPY); final Set<IProjectFacetVersion> facets = getFacetConfiguration(fv); fpjwc.setProjectFacets(facets); return top; } /** * 设置facet版本 * */ public void setFacetVersion() { primaryProjectFacet = ProjectFacetsManager .getProjectFacet(getModuleTypeID()); IFacetedProjectWorkingCopy fpjwc = ((ModifyFacetedProjectWizard) getWizard()) .getFacetedProjectWorkingCopy(); SortedSet<IProjectFacetVersion> initialVersions = fpjwc .getAvailableVersions(primaryProjectFacet); String[] items = new String[initialVersions.size() - 1]; int i = 0; int selectedVersionIndex = -1; for (Iterator<IProjectFacetVersion> iterator = initialVersions .iterator(); iterator.hasNext(); i++) { if (i == 4) { break; } items[i] = iterator.next().getVersionString(); if (selectedVersionIndex == -1 && items[i].equals("2.5")) { selectedVersionIndex = i; } } primaryVersionCombo.clearSelection(); primaryVersionCombo.setItems(items); primaryVersionCombo.select(selectedVersionIndex); } @Override protected void createProjectGroup(Composite parent) { Font font = parent.getFont(); Composite projectGroup = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); layout.numColumns = 2; projectGroup.setLayout(layout); projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); Group group = new Group(projectGroup, SWT.None); group.setText(""); group.setLayout(new GridLayout(2, false)); GridData gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = 2; group.setLayoutData(gd); // new project label Label projectLabel = new Label(group, SWT.NONE); projectLabel.setFont(font); projectLabel.setText(WTPCommonUIResourceHandler.Name_); // new project name entry field projectNameField = new Text(group, SWT.BORDER); GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_BOTH); projectNameField.setLayoutData(data); projectNameField.setFont(font); synchHelper.synchText(projectNameField, IProjectCreationPropertiesNew.PROJECT_NAME, new Control[] { projectLabel }); projectNameField.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { if (e.getSource().equals(projectNameField)) { } } }); } public String getProjectNameField() { return projectNameField.getText(); } }
3. 代码调用向导方法。
TestWizard testWizard = new TestWizard(); WizardDialog dialog = new WizardDialog(WorkbenchUtil.getShell(),testWizard ); dialog.open();