Version 1.1.7
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / builder / AddBuilderAction.java
1 package net.sourceforge.phpeclipse.wiki.builder;
2
3 import java.io.InputStream;
4 import java.util.Iterator;
5
6 import net.sourceforge.phpeclipse.wiki.preferences.Util;
7
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;
22
23 public class AddBuilderAction implements IObjectActionDelegate {
24   private IWorkbenchPart workbenchPart;
25
26   /**
27    *  
28    */
29   public AddBuilderAction() {
30     super();
31   }
32
33   /**
34    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
35    */
36   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
37     workbenchPart = targetPart;
38   }
39
40   public void run(IAction action) {
41     ISelectionProvider selectionProvider = null;
42     selectionProvider = workbenchPart.getSite().getSelectionProvider();
43
44     StructuredSelection selection = null;
45     selection = (StructuredSelection) selectionProvider.getSelection();
46
47     //Shell shell = null;
48     Iterator iterator = null;
49     iterator = selection.iterator();
50     while (iterator.hasNext()) {
51       //  obj => selected object in the view
52       Object obj = iterator.next();
53
54       // is it a resource
55       if (obj instanceof IResource) {
56         IResource resource = (IResource) obj;
57
58         // check if it's a project resource
59         switch (resource.getType()) {
60
61         case IResource.PROJECT:
62           addBuilder((IProject) resource);
63         }
64       }
65     }
66   }
67
68   /**
69    * @see IActionDelegate#selectionChanged(IAction, ISelection)
70    */
71   public void selectionChanged(IAction action, ISelection selection) {
72   }
73
74   public static void addBuilder(IProject project) {
75     IProjectDescription desc;
76     try {
77       desc = project.getDescription();
78
79       ICommand[] commands = desc.getBuildSpec();
80       boolean found = false;
81
82       for (int i = 0; i < commands.length; ++i) {
83         if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
84           found = true;
85           break;
86         }
87       }
88       if (!found) {
89         //add builder to project
90         ICommand command = desc.newCommand();
91         command.setBuilderName(WikiBuilder.BUILDER_ID);
92         ICommand[] newCommands = new ICommand[commands.length + 1];
93
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);
101
102         createVelocityFile(project, "main.vm");
103         createVelocityFile(project, "export.vm");
104         createVelocityFile(project, "main.css");
105       }
106     } catch (CoreException e) {
107       e.printStackTrace();
108     }
109   }
110
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);
116     }
117   }
118 }