1 package net.sourceforge.phpeclipse.wiki.builder;
3 import java.util.Iterator;
5 import net.sourceforge.phpeclipse.wiki.preferences.Util;
7 import org.eclipse.core.resources.ICommand;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IProjectDescription;
10 import org.eclipse.core.resources.IResource;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.jface.action.IAction;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.ISelectionProvider;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.ui.IActionDelegate;
17 import org.eclipse.ui.IObjectActionDelegate;
18 import org.eclipse.ui.IWorkbenchPart;
20 public class AddBuilderAction implements IObjectActionDelegate {
21 private IWorkbenchPart workbenchPart;
26 public AddBuilderAction() {
31 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
33 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
34 workbenchPart = targetPart;
37 public void run(IAction action) {
38 ISelectionProvider selectionProvider = null;
39 selectionProvider = workbenchPart.getSite().getSelectionProvider();
41 StructuredSelection selection = null;
42 selection = (StructuredSelection) selectionProvider.getSelection();
45 Iterator iterator = null;
46 iterator = selection.iterator();
47 while (iterator.hasNext()) {
48 // obj => selected object in the view
49 Object obj = iterator.next();
52 if (obj instanceof IResource) {
53 IResource resource = (IResource) obj;
55 // check if it's a project resource
56 switch (resource.getType()) {
58 case IResource.PROJECT:
59 addBuilder((IProject) resource);
66 * @see IActionDelegate#selectionChanged(IAction, ISelection)
68 public void selectionChanged(IAction action, ISelection selection) {
71 private void addBuilder(IProject project) {
72 IProjectDescription desc;
74 desc = project.getDescription();
76 ICommand[] commands = desc.getBuildSpec();
77 boolean found = false;
79 for (int i = 0; i < commands.length; ++i) {
80 if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
86 //add builder to project
87 ICommand command = desc.newCommand();
88 command.setBuilderName(WikiBuilder.BUILDER_ID);
89 ICommand[] newCommands = new ICommand[commands.length + 1];
91 // Add it before other builders.
92 System.arraycopy(commands, 0, newCommands, 1, commands.length);
93 newCommands[0] = command;
94 desc.setBuildSpec(newCommands);
95 project.setDescription(desc, null);
96 // add some default wiki project settings
97 Util.setWikiTextsPath(project);
100 } catch (CoreException e) {