如何在新项目创建向导中创建文件

时间:2016-01-20 07:39:35

标签: eclipse eclipse-plugin

在我目前的项目中,我正在尝试创建项目(eclipse插件项目),其中包含四个扩展名为.mydsl的文件(例如-vocab.mydsl,arch.mydsl&等)并且想要将该项目导出为Eclipse插件。为此,我创建了项目(使用eclipse插件项目)并在eclipse中导出为Plug-ins和Fragments。我附上源代码以便更清晰。

package customplugin.wizards;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;

public class BuilderNewWizard extends Wizard implements INewWizard {

private WizardNewProjectCreationPage _pageOne;

public BuilderNewWizard() {
// Here I am adding Title of New Wizard
    setWindowTitle("IoT Suite");
}

@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
}

@Override
public boolean performFinish() {
    return true;
}

@Override
public void addPages() {
    super.addPages();
 // Here I am creating One Page with title and other attributes..
    _pageOne = new WizardNewProjectCreationPage("From Scratch Project Wizard");
    _pageOne.setTitle("IoT Suite Configuration");
    _pageOne.setDescription("Create IoTSuite Configuration");
    addPage(_pageOne);
  }
}

因为无法上传图像所以附加图像的链接(在项目向导中显示名称IoT的项目)。 Click Here for Image 现在问题是如何添加动作以便我可以将文件添加到该项目?当我单击Finish时, Project未导入Eclipse Package Explorer窗口。我在performFinish方法中遗漏了什么?

1 个答案:

答案 0 :(得分:0)

最后,我找出解决方案完美运行。请参阅以下代码,其中doFinish方法,使用我们要创建的文件名调用openContentStream方法。在openContentStream方法中,我们使用预定义内容创建所需文件。如果我们不想要任何预定义的内容而不是设置content=null

private void doFinish(String containerName, String fileName, String title,
        IProgressMonitor monitor) throws CoreException {

    monitor.beginTask("Creating " + fileName, 2);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource resource = root.findMember(new Path(containerName));

    if (!resource.exists() || !(resource instanceof IContainer)) {
        throwCoreException("Container \"" + containerName
                + "\" does not exist.");
    }
    IContainer container = (IContainer) resource;

    final IFile file = container.getFile(new Path(fileName));
    try {

        InputStream stream = openContentStream(name);

        try {
            if (file.exists()) {
                file.setContents(stream, true, true, monitor);
            } else {
                file.create(stream, true, monitor);
            }
        } finally {
            stream.close();
        }

    } catch (IOException e) {
    }
    monitor.worked(1);
    monitor.setTaskName("Opening file for editing...");
    getShell().getDisplay().asyncExec(new Runnable() {
        public void run() {
            IWorkbenchPage page = PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getActivePage();
            try {
                IDE.openEditor(page, file, true);
            } catch (PartInitException e) {
            }
        }
    });
    monitor.worked(1);
}

/**
 * Initialize the file contents to contents of the given resource.
 */
public static InputStream openContentStream(String name) throws CoreException {
    if(name.equals("vocab"))
    {
        contents=null;
         contents = "structs:                                                                                   "+
                 "\n/*  TempStruct                                                                              "+
                 "\n        tempValue: double;                                                                  "+
                 "\n        unitOfMeasurement : String;   */                                "+
                 "\nresources:                                                                                 "+
                 "\n    sensors:                                                                                "+
                 "\n        periodicSensors:                                                                    "+
                 "\n        /*  TemperatureSensor                                                               "+
                 "\n                generate tempMeasurement:TempStruct;                                        "+
                 "\n                sample period 1000 for 100000;    */                               "+
                 "\n        eventDrivenSensors:                                                                 "+
                 "\n        /*  SmokeDetector                                                                   "+
                 "\n                generate smokeMeasurement:SmokeStruct;                                      "+
                 "\n                onCondition smokeValue > 650 PPM ;     */                                     "+
                 "\n        requestBasedSensors:                                                                "+
                 "\n        /*  YahooWeatherService                                                             "+
                 "\n                generate weatherMeasurement:TempStruct accessed-by locationID : String; */    "+
                 "\n        tags:                                                                               "+
                 "\n        /*  BadgeReader                                                                     "+
                 "\n                generate badgeDetected: BadgeStruct;    */                                    "+
                 "\n        actuators:                                                                          "+
                 "\n        /*  Heater                                                               "+
                 "\n                action Off();                                                                          "+
                 "\n                action SetTemp(setTemp:TempStruct);     */                                    "+
                 "\n        storages:                                                                           "+
                 "\n        /*  ProfileDB                                                                       "+
                 "\n                generate profile:TempStruct accessed-by badgeID:String;  */                 "  ;    

    }