01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package org.terracotta.dso.actions;
05:
06: import org.eclipse.core.resources.IProject;
07: import org.eclipse.jdt.core.IJavaProject;
08: import org.eclipse.jface.action.IAction;
09: import org.eclipse.jface.viewers.ISelection;
10: import org.eclipse.jface.viewers.IStructuredSelection;
11: import org.eclipse.ui.IActionDelegate;
12: import org.eclipse.ui.IWorkbenchWindow;
13: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
14:
15: import org.terracotta.dso.ProjectNature;
16: import org.terracotta.dso.TcPlugin;
17:
18: public class WorkbenchNatureAction implements IActionDelegate,
19: IWorkbenchWindowActionDelegate, IProjectAction {
20: private IJavaProject m_javaProject;
21: private boolean m_addNature;
22: private IAction m_action;
23:
24: public WorkbenchNatureAction() {
25: super ();
26: TcPlugin.getDefault().registerProjectAction(this );
27: }
28:
29: public void run(IAction action) {
30: TcPlugin plugin = TcPlugin.getDefault();
31:
32: if (m_addNature) {
33: plugin.addTerracottaNature(m_javaProject);
34: } else {
35: plugin.removeTerracottaNature(m_javaProject);
36: }
37:
38: update(m_javaProject);
39: }
40:
41: public void selectionChanged(IAction action, ISelection selection) {
42: m_action = action;
43:
44: if (m_javaProject == null
45: || selection instanceof IStructuredSelection) {
46: update(ActionUtil.locateSelectedJavaProject(selection));
47: } else {
48: action.setEnabled(true);
49: }
50: }
51:
52: private void update(IJavaProject javaProject) {
53: if ((m_javaProject = javaProject) != null) {
54: String text;
55:
56: try {
57: IProject project = javaProject.getProject();
58:
59: if (!project.isOpen()) {
60: m_action.setText("Add Terracotta Nature...");
61: m_action.setEnabled(false);
62: return;
63: }
64:
65: if (project.hasNature(ProjectNature.NATURE_ID)) {
66: m_addNature = false;
67: text = "Remove Terracotta Nature";
68: } else {
69: m_addNature = true;
70: text = "Add Terracotta Nature...";
71: }
72: } catch (Exception ce) {
73: ce.printStackTrace();
74: m_action.setEnabled(false);
75: return;
76: }
77:
78: m_action.setText(text);
79: m_action.setEnabled(true);
80: } else {
81: m_action.setEnabled(false);
82: }
83: }
84:
85: public void update(IProject project) {
86: update(ActionUtil.findJavaProject(project));
87: }
88:
89: public void dispose() {
90: /**/
91: }
92:
93: public void init(IWorkbenchWindow window) {
94: /**/
95: }
96: }
|