/
ActivateFoghornAction.java
88 lines (77 loc) · 2.84 KB
/
ActivateFoghornAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* (c) 2003 Jérôme Nègre
*
*/
package org.jnegre.foghorn;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
/**
* @author Jérôme Nègre (plugin@jnegre.org)
*
* (c) Copyright 2003 Jérôme Nègre - http://www.jnegre.org/
*
*/
public class ActivateFoghornAction implements IObjectActionDelegate {
IWorkbenchPart targetPart;
/**
* Constructor for ActivateFoghornAction.
*/
public ActivateFoghornAction() {
super();
}
/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.targetPart = targetPart;
}
/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
IProject project =
(IProject)((IAdaptable) ((StructuredSelection) targetPart.getSite().getSelectionProvider().getSelection())
.getFirstElement()).getAdapter(IProject.class);
try {
IProjectDescription desc = project.getDescription();
ICommand[] commands = desc.getBuildSpec();
boolean found = false;
for (int i = 0; i < commands.length; ++i) {
if (commands[i].getBuilderName().equals("org.jnegre.foghorn.builder")) {
found = true;
break;
}
}
if (!found) {
//add builder to project
ICommand command = desc.newCommand();
command.setBuilderName("org.jnegre.foghorn.builder");
ICommand[] newCommands = new ICommand[commands.length + 1];
// Add it after other builders.
System.arraycopy(commands, 0, newCommands, 0, commands.length);
newCommands[commands.length] = command;
desc.setBuildSpec(newCommands);
project.setDescription(desc, null);
}
} catch (CoreException e) {
Shell shell = new Shell();
MessageDialog.openInformation(shell, "Foghorn", "Error: " + e);
}
}
/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
}
}