KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > actions > BaseAction


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.actions;
5
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.jdt.core.ICompilationUnit;
8 import org.eclipse.jdt.core.IJavaElement;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.Event;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.ui.IWorkbench;
15 import org.eclipse.ui.PlatformUI;
16
17 import org.terracotta.dso.ConfigurationHelper;
18 import org.terracotta.dso.TcPlugin;
19
20 /**
21  * Base type for all popup actions which operate on Jave Project elements.
22  *
23  * @see org.eclipse.core.resources.IProject
24  * @see org.eclipse.jdt.core.ICompilationUnit
25  * @see org.eclipse.jdt.core.IJavaElement
26  */

27
28 public class BaseAction extends Action {
29   protected IJavaElement m_element;
30   
31   public BaseAction(String JavaDoc label) {
32     super(label);
33   }
34   
35   public BaseAction(String JavaDoc label, int type) {
36     super(label, type);
37   }
38   
39   public void setJavaElement(IJavaElement element) {
40     m_element = element;
41   }
42   
43   public IJavaElement getJavaElement() {
44     return m_element;
45   }
46   
47   public ICompilationUnit getCompilationUnit() {
48     if(m_element != null) {
49       return (ICompilationUnit)m_element.getAncestor(IJavaElement.COMPILATION_UNIT);
50     }
51     
52     return null;
53   }
54   
55   public void performAction() {
56     // do nothing
57
}
58   
59   public void performAction(Event event) {
60     performAction();
61   }
62   
63   protected void inspectCompilationUnit() {
64     ICompilationUnit cu = getCompilationUnit();
65     
66     if(cu != null) {
67       TcPlugin.getDefault().inspect(cu);
68     }
69   }
70   
71   public void runWithEvent(final Event event) {
72     IWorkbench workbench = PlatformUI.getWorkbench();
73     Display display = workbench.getDisplay();
74     
75     display.asyncExec(new Runnable JavaDoc () {
76       public void run() {
77         if(getJavaElement() != null) {
78           TcPlugin plugin = TcPlugin.getDefault();
79           IProject project = getProject();
80           
81           if(plugin.getConfiguration(project) == TcPlugin.BAD_CONFIG) {
82             Shell shell = Display.getDefault().getActiveShell();
83             String JavaDoc title = "Terracotta Plugin";
84             String JavaDoc msg = "The configuration source is not parsable and cannot be\n used until these errors are resolved.";
85             
86             MessageDialog.openWarning(shell, title, msg);
87             try {
88               plugin.openConfigurationEditor(project);
89             } catch(Exception JavaDoc e) {
90               // TODO:
91
}
92           } else {
93             performAction(event);
94           }
95         }
96       }
97     });
98   }
99
100   protected ConfigurationHelper getConfigHelper() {
101     ConfigurationHelper helper = null;
102     IProject project = getProject();
103     
104     if(project != null) {
105       helper = TcPlugin.getDefault().getConfigurationHelper(project);
106     }
107     
108     return helper;
109   }
110   
111   protected IProject getProject() {
112     return m_element != null ? m_element.getJavaProject().getProject() : null;
113   }
114 }
115
Popular Tags