Add predefined Velocity and CSS templates if wiki builder is assigned to a project
[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.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.Iterator;
6
7 import net.sourceforge.phpeclipse.wiki.preferences.Util;
8
9 import org.eclipse.core.resources.ICommand;
10 import org.eclipse.core.resources.IContainer;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IFolder;
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IProjectDescription;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.IActionDelegate;
23 import org.eclipse.ui.IObjectActionDelegate;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 public class AddBuilderAction implements IObjectActionDelegate {
27   private IWorkbenchPart workbenchPart;
28
29   /**
30    *  
31    */
32   public AddBuilderAction() {
33     super();
34   }
35
36   /**
37    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
38    */
39   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
40     workbenchPart = targetPart;
41   }
42
43   public void run(IAction action) {
44     ISelectionProvider selectionProvider = null;
45     selectionProvider = workbenchPart.getSite().getSelectionProvider();
46
47     StructuredSelection selection = null;
48     selection = (StructuredSelection) selectionProvider.getSelection();
49
50     //Shell shell = null;
51     Iterator iterator = null;
52     iterator = selection.iterator();
53     while (iterator.hasNext()) {
54       //  obj => selected object in the view
55       Object obj = iterator.next();
56
57       // is it a resource
58       if (obj instanceof IResource) {
59         IResource resource = (IResource) obj;
60
61         // check if it's a project resource
62         switch (resource.getType()) {
63
64         case IResource.PROJECT:
65           addBuilder((IProject) resource);
66         }
67       }
68     }
69   }
70
71   /**
72    * @see IActionDelegate#selectionChanged(IAction, ISelection)
73    */
74   public void selectionChanged(IAction action, ISelection selection) {
75   }
76
77   private void addBuilder(IProject project) {
78     IProjectDescription desc;
79     try {
80       desc = project.getDescription();
81
82       ICommand[] commands = desc.getBuildSpec();
83       boolean found = false;
84
85       for (int i = 0; i < commands.length; ++i) {
86         if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
87           found = true;
88           break;
89         }
90       }
91       if (!found) {
92         //add builder to project
93         ICommand command = desc.newCommand();
94         command.setBuilderName(WikiBuilder.BUILDER_ID);
95         ICommand[] newCommands = new ICommand[commands.length + 1];
96
97         // Add it before other builders.
98         System.arraycopy(commands, 0, newCommands, 1, commands.length);
99         newCommands[0] = command;
100         desc.setBuildSpec(newCommands);
101         project.setDescription(desc, null);
102         // add some default wiki project settings
103         Util.setWikiBuilderPreferences(project);
104
105         createVelocityFile(project, "main.vm");
106         createVelocityFile(project, "export.vm");
107         createVelocityFile(project, "main.css");
108       }
109     } catch (CoreException e) {
110       e.printStackTrace();
111     }
112   }
113
114   private void createVelocityFile(IProject project, String filename) throws CoreException {
115     InputStream is = AddBuilderAction.class.getResourceAsStream(filename);
116     final IFile file = project.getFile(new Path("wpsrc/" + filename));
117     if (!file.exists()) {
118       file.create(is, true, null);
119     }
120   }
121 }