initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / builder / AddBuilderAction.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/AddBuilderAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/AddBuilderAction.java
new file mode 100644 (file)
index 0000000..ef06dc8
--- /dev/null
@@ -0,0 +1,104 @@
+package net.sourceforge.phpeclipse.wiki.builder;
+
+import java.util.Iterator;
+
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.resources.ICommand;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IActionDelegate;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+
+public class AddBuilderAction implements IObjectActionDelegate {
+  private IWorkbenchPart workbenchPart;
+
+  /**
+   *  
+   */
+  public AddBuilderAction() {
+    super();
+  }
+
+  /**
+   * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
+   */
+  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+    workbenchPart = targetPart;
+  }
+
+  public void run(IAction action) {
+    ISelectionProvider selectionProvider = null;
+    selectionProvider = workbenchPart.getSite().getSelectionProvider();
+
+    StructuredSelection selection = null;
+    selection = (StructuredSelection) selectionProvider.getSelection();
+
+    //Shell shell = null;
+    Iterator iterator = null;
+    iterator = selection.iterator();
+    while (iterator.hasNext()) {
+      //  obj => selected object in the view
+      Object obj = iterator.next();
+
+      // is it a resource
+      if (obj instanceof IResource) {
+        IResource resource = (IResource) obj;
+
+        // check if it's a project resource
+        switch (resource.getType()) {
+
+        case IResource.PROJECT:
+          addBuilder((IProject) resource);
+        }
+      }
+    }
+  }
+
+  /**
+   * @see IActionDelegate#selectionChanged(IAction, ISelection)
+   */
+  public void selectionChanged(IAction action, ISelection selection) {
+  }
+
+  private void addBuilder(IProject project) {
+    IProjectDescription desc;
+    try {
+      desc = project.getDescription();
+
+      ICommand[] commands = desc.getBuildSpec();
+      boolean found = false;
+
+      for (int i = 0; i < commands.length; ++i) {
+        if (commands[i].getBuilderName().equals(WikiBuilder.BUILDER_ID)) {
+          found = true;
+          break;
+        }
+      }
+      if (!found) {
+        //add builder to project
+        ICommand command = desc.newCommand();
+        command.setBuilderName(WikiBuilder.BUILDER_ID);
+        ICommand[] newCommands = new ICommand[commands.length + 1];
+
+        // Add it before other builders.
+        System.arraycopy(commands, 0, newCommands, 1, commands.length);
+        newCommands[0] = command;
+        desc.setBuildSpec(newCommands);
+        project.setDescription(desc, null);
+        // add some default wiki project settings
+        Util.setWikiTextsPath(project);
+
+      }
+    } catch (CoreException e) {
+      e.printStackTrace();
+    }
+  }
+}
\ No newline at end of file