1 package net.sourceforge.phpeclipse.wiki.builder;
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.Iterator;
7 import net.sourceforge.phpeclipse.wiki.preferences.Util;
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;
26 public class AddBuilderAction implements IObjectActionDelegate {
27 private IWorkbenchPart workbenchPart;
32 public AddBuilderAction() {
37 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
39 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
40 workbenchPart = targetPart;
43 public void run(IAction action) {
44 ISelectionProvider selectionProvider = null;
45 selectionProvider = workbenchPart.getSite().getSelectionProvider();
47 StructuredSelection selection = null;
48 selection = (StructuredSelection) selectionProvider.getSelection();
51 Iterator iterator = null;
52 iterator = selection.iterator();
53 while (iterator.hasNext()) {
54 // obj => selected object in the view
55 Object obj = iterator.next();
58 if (obj instanceof IResource) {
59 IResource resource = (IResource) obj;
61 // check if it's a project resource
62 switch (resource.getType()) {
64 case IResource.PROJECT:
65 addBuilder((IProject) resource);
72 * @see IActionDelegate#selectionChanged(IAction, ISelection)
74 public void selectionChanged(IAction action, ISelection selection) {
77 private void addBuilder(IProject project) {
78 IProjectDescription desc;
80 desc = project.getDescription();
82 ICommand[] commands = desc.getBuildSpec();
83 boolean found = false;
85 for (int i = 0; i < commands.length; ++i) {
86 if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
92 //add builder to project
93 ICommand command = desc.newCommand();
94 command.setBuilderName(WikiBuilder.BUILDER_ID);
95 ICommand[] newCommands = new ICommand[commands.length + 1];
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.setWikiTextsPath(project);
105 createVelocityFile(project, "main.vm");
106 createVelocityFile(project, "export.vm");
107 createVelocityFile(project, "main.css");
109 } catch (CoreException e) {
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 file.create(is, true, null);