実用的な XML: プロジェクトを作成する

Eclipse に新規ウィザードを追加する

記事に戻る

リスト 6. createProject() メソッド
protected void createProject(IProgressMonitor monitor)
{
   monitor.beginTask(Resources.getString("eclipse.creatingproject"),50);
   try
   {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
      monitor.subTask(Resources.getString("eclipse.creatingdirectories"));
IProject project = root.getProject(namePage.getProjectName());
      IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
      if(!Platform.getLocation().equals(namePage.getLocationPath()))
         description.setLocation(namePage.getLocationPath());
      description.setNatureIds(new String[] { PluginConstants.NATURE_ID });
      ICommand command = description.newCommand();
      command.setBuilderName(PluginConstants.BUILDER_ID);
      description.setBuildSpec(new ICommand[] { command });
      project.create(description,monitor);
      monitor.worked(10);
project.open(monitor);
      project.setPersistentProperty(PluginConstants.SOURCE_PROPERTY_NAME,"src");
      project.setPersistentProperty(PluginConstants.RULES_PROPERTY_NAME,"rules");
      project.setPersistentProperty(PluginConstants.PUBLISH_PROPERTY_NAME,"publish");
      project.setPersistentProperty(PluginConstants.BUILD_PROPERTY_NAME,"false");
      monitor.worked(10);
      IPath projectPath = project.getFullPath(),
            srcPath = projectPath.append("src"),
            rulesPath = projectPath.append("rules"),
            publishPath = projectPath.append("publish");
      IFolder srcFolder = root.getFolder(srcPath),
              rulesFolder = root.getFolder(rulesPath),
              publishFolder = root.getFolder(publishPath);
      createFolderHelper(srcFolder,monitor);
      createFolderHelper(rulesFolder,monitor);
      createFolderHelper(publishFolder,monitor);
      monitor.worked(10);
      monitor.subTask(Resources.getString("eclipse.creatingfiles"));
      IPath indexPath = srcPath.append("index.xml"),
            defaultPath = rulesPath.append("default.xsl");
      IFile indexFile = root.getFile(indexPath),
            defaultFile = root.getFile(defaultPath);
      Class clasz = getClass();
      InputStream indexIS = clasz.getResourceAsStream("/org/ananas/xm/eclipse/resources/index.xml"),
          defaultIS = clasz.getResourceAsStream("/org/ananas/xm/eclipse/resources/default.xsl");
      indexFile.create(indexIS,false,new SubProgressMonitor(monitor,10));
      defaultFile.create(defaultIS,false,new SubProgressMonitor(monitor,10));
   }
   catch(CoreException x)
   {
      reportError(x);
   }
   finally
   {
      monitor.done();
   }
}