1 package net.sourceforge.phpeclipse.wiki.builder;
3 import java.io.InputStream;
4 import java.util.Iterator;
6 import net.sourceforge.phpeclipse.wiki.preferences.Util;
8 import org.eclipse.core.resources.ICommand;
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IProjectDescription;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionProvider;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.ui.IActionDelegate;
20 import org.eclipse.ui.IObjectActionDelegate;
21 import org.eclipse.ui.IWorkbenchPart;
23 public class AddBuilderAction implements IObjectActionDelegate {
24 private IWorkbenchPart workbenchPart;
29 public AddBuilderAction() {
34 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
36 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
37 workbenchPart = targetPart;
40 public void run(IAction action) {
41 ISelectionProvider selectionProvider = null;
42 selectionProvider = workbenchPart.getSite().getSelectionProvider();
44 StructuredSelection selection = null;
45 selection = (StructuredSelection) selectionProvider.getSelection();
48 Iterator iterator = null;
49 iterator = selection.iterator();
50 while (iterator.hasNext()) {
51 // obj => selected object in the view
52 Object obj = iterator.next();
55 if (obj instanceof IResource) {
56 IResource resource = (IResource) obj;
58 // check if it's a project resource
59 switch (resource.getType()) {
61 case IResource.PROJECT:
62 addBuilder((IProject) resource);
69 * @see IActionDelegate#selectionChanged(IAction, ISelection)
71 public void selectionChanged(IAction action, ISelection selection) {
74 public static void addBuilder(IProject project) {
75 IProjectDescription desc;
77 desc = project.getDescription();
79 ICommand[] commands = desc.getBuildSpec();
80 boolean found = false;
82 for (int i = 0; i < commands.length; ++i) {
83 if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
89 //add builder to project
90 ICommand command = desc.newCommand();
91 command.setBuilderName(WikiBuilder.BUILDER_ID);
92 ICommand[] newCommands = new ICommand[commands.length + 1];
94 // Add it before other builders.
95 System.arraycopy(commands, 0, newCommands, 1, commands.length);
96 newCommands[0] = command;
97 desc.setBuildSpec(newCommands);
98 project.setDescription(desc, null);
99 // add some default wiki project settings
100 Util.setWikiBuilderPreferences(project);
102 createVelocityFile(project, "main.vm");
103 createVelocityFile(project, "export.vm");
104 createVelocityFile(project, "main.css");
106 } catch (CoreException e) {
111 private static void createVelocityFile(IProject project, String filename) throws CoreException {
112 InputStream is = AddBuilderAction.class.getResourceAsStream(filename);
113 final IFile file = project.getFile(new Path("wpsrc/" + filename));
114 if (!file.exists()) {
115 file.create(is, true, null);