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