001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.actions;
005:
006: import org.eclipse.core.resources.IProject;
007: import org.eclipse.jdt.core.ICompilationUnit;
008: import org.eclipse.jdt.core.IJavaElement;
009: import org.eclipse.jface.action.Action;
010: import org.eclipse.jface.dialogs.MessageDialog;
011: import org.eclipse.swt.widgets.Display;
012: import org.eclipse.swt.widgets.Event;
013: import org.eclipse.swt.widgets.Shell;
014: import org.eclipse.ui.IWorkbench;
015: import org.eclipse.ui.PlatformUI;
016:
017: import org.terracotta.dso.ConfigurationHelper;
018: import org.terracotta.dso.TcPlugin;
019:
020: /**
021: * Base type for all popup actions which operate on Jave Project elements.
022: *
023: * @see org.eclipse.core.resources.IProject
024: * @see org.eclipse.jdt.core.ICompilationUnit
025: * @see org.eclipse.jdt.core.IJavaElement
026: */
027:
028: public class BaseAction extends Action {
029: protected IJavaElement m_element;
030:
031: public BaseAction(String label) {
032: super (label);
033: }
034:
035: public BaseAction(String label, int type) {
036: super (label, type);
037: }
038:
039: public void setJavaElement(IJavaElement element) {
040: m_element = element;
041: }
042:
043: public IJavaElement getJavaElement() {
044: return m_element;
045: }
046:
047: public ICompilationUnit getCompilationUnit() {
048: if (m_element != null) {
049: return (ICompilationUnit) m_element
050: .getAncestor(IJavaElement.COMPILATION_UNIT);
051: }
052:
053: return null;
054: }
055:
056: public void performAction() {
057: // do nothing
058: }
059:
060: public void performAction(Event event) {
061: performAction();
062: }
063:
064: protected void inspectCompilationUnit() {
065: ICompilationUnit cu = getCompilationUnit();
066:
067: if (cu != null) {
068: TcPlugin.getDefault().inspect(cu);
069: }
070: }
071:
072: public void runWithEvent(final Event event) {
073: IWorkbench workbench = PlatformUI.getWorkbench();
074: Display display = workbench.getDisplay();
075:
076: display.asyncExec(new Runnable() {
077: public void run() {
078: if (getJavaElement() != null) {
079: TcPlugin plugin = TcPlugin.getDefault();
080: IProject project = getProject();
081:
082: if (plugin.getConfiguration(project) == TcPlugin.BAD_CONFIG) {
083: Shell shell = Display.getDefault()
084: .getActiveShell();
085: String title = "Terracotta Plugin";
086: String msg = "The configuration source is not parsable and cannot be\n used until these errors are resolved.";
087:
088: MessageDialog.openWarning(shell, title, msg);
089: try {
090: plugin.openConfigurationEditor(project);
091: } catch (Exception e) {
092: // TODO:
093: }
094: } else {
095: performAction(event);
096: }
097: }
098: }
099: });
100: }
101:
102: protected ConfigurationHelper getConfigHelper() {
103: ConfigurationHelper helper = null;
104: IProject project = getProject();
105:
106: if (project != null) {
107: helper = TcPlugin.getDefault().getConfigurationHelper(
108: project);
109: }
110:
111: return helper;
112: }
113:
114: protected IProject getProject() {
115: return m_element != null ? m_element.getJavaProject()
116: .getProject() : null;
117: }
118: }
|