package net.sourceforge.phpeclipse.wiki.builder; import java.io.InputStream; import java.util.Iterator; import net.sourceforge.phpeclipse.wiki.preferences.Util; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; public class AddBuilderAction implements IObjectActionDelegate { private IWorkbenchPart workbenchPart; /** * */ public AddBuilderAction() { super(); } /** * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { workbenchPart = targetPart; } public void run(IAction action) { ISelectionProvider selectionProvider = null; selectionProvider = workbenchPart.getSite().getSelectionProvider(); StructuredSelection selection = null; selection = (StructuredSelection) selectionProvider.getSelection(); //Shell shell = null; Iterator iterator = null; iterator = selection.iterator(); while (iterator.hasNext()) { // obj => selected object in the view Object obj = iterator.next(); // is it a resource if (obj instanceof IResource) { IResource resource = (IResource) obj; // check if it's a project resource switch (resource.getType()) { case IResource.PROJECT: addBuilder((IProject) resource); } } } } /** * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { } public static void addBuilder(IProject project) { IProjectDescription desc; try { desc = project.getDescription(); ICommand[] commands = desc.getBuildSpec(); boolean found = false; for (int i = 0; i < commands.length; ++i) { if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) { found = true; break; } } if (!found) { //add builder to project ICommand command = desc.newCommand(); command.setBuilderName(WikiBuilder.BUILDER_ID); ICommand[] newCommands = new ICommand[commands.length + 1]; // Add it before other builders. System.arraycopy(commands, 0, newCommands, 1, commands.length); newCommands[0] = command; desc.setBuildSpec(newCommands); project.setDescription(desc, null); // add some default wiki project settings Util.setWikiBuilderPreferences(project); createVelocityFile(project, "main.vm"); createVelocityFile(project, "export.vm"); createVelocityFile(project, "main.css"); } } catch (CoreException e) { e.printStackTrace(); } } private static void createVelocityFile(IProject project, String filename) throws CoreException { InputStream is = AddBuilderAction.class.getResourceAsStream(filename); final IFile file = project.getFile(new Path("wpsrc/" + filename)); if (!file.exists()) { file.create(is, true, null); } } }