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.core.runtime.CoreException;
008: import org.eclipse.core.runtime.IProgressMonitor;
009: import org.eclipse.jdt.core.IJavaProject;
010: import org.eclipse.jface.dialogs.MessageDialog;
011: import org.eclipse.jface.operation.IRunnableWithProgress;
012: import org.eclipse.jface.resource.ImageDescriptor;
013: import org.eclipse.swt.widgets.Shell;
014: import org.eclipse.ui.IWorkbench;
015: import org.eclipse.ui.IWorkbenchWindow;
016: import org.eclipse.ui.PlatformUI;
017: import org.eclipse.ui.plugin.AbstractUIPlugin;
018: import org.terracotta.dso.ServerInfo;
019: import org.terracotta.dso.ServerTracker;
020: import org.terracotta.dso.TcPlugin;
021:
022: import java.lang.reflect.InvocationTargetException;
023:
024: /**
025: * Manage the named server.
026: */
027:
028: public class ManageServerAction extends BaseAction implements
029: IRunnableWithProgress {
030: private String m_name;
031: private static ImageDescriptor m_startImage;
032: private static ImageDescriptor m_stopImage;
033:
034: static {
035: String pluginId = TcPlugin.getPluginId();
036: String startPath = "/images/eclipse/start.gif";
037: String stopPath = "/images/eclipse/stop.gif";
038:
039: m_startImage = AbstractUIPlugin.imageDescriptorFromPlugin(
040: pluginId, startPath);
041: m_stopImage = AbstractUIPlugin.imageDescriptorFromPlugin(
042: pluginId, stopPath);
043: }
044:
045: public ManageServerAction(IJavaProject javaProject, String name) {
046: super (name);
047: setJavaElement(javaProject);
048: m_name = name;
049:
050: ServerTracker serverTracker = ServerTracker.getDefault();
051: ServerInfo serverInfo = serverTracker.getServerInfo(
052: javaProject, name);
053: ImageDescriptor imageDesc = serverInfo != null ? m_stopImage
054: : m_startImage;
055: setImageDescriptor(imageDesc);
056: setEnabled(serverInfo == null || serverInfo.isStarted());
057: if (serverInfo != null && serverInfo.isStarting()) {
058: setText(name + " (Starting...)");
059: }
060: }
061:
062: public void performAction() {
063: IWorkbench workbench = PlatformUI.getWorkbench();
064:
065: if (!workbench.saveAllEditors(true)) {
066: return;
067: }
068:
069: TcPlugin plugin = TcPlugin.getDefault();
070: IProject project = ((IJavaProject) getJavaElement())
071: .getProject();
072:
073: try {
074: if (!plugin.continueWithConfigProblems(project)) {
075: return;
076: }
077: } catch (CoreException ce) {
078: Shell shell = new Shell();
079:
080: MessageDialog.openInformation(shell, "Terracotta",
081: "Error starting Terracotta Server:\n"
082: + ActionUtil.getStatusMessages(ce));
083: }
084:
085: try {
086: IWorkbenchWindow window = workbench
087: .getActiveWorkbenchWindow();
088: window.run(true, false, this );
089: } catch (Exception e) {
090: Shell shell = new Shell();
091:
092: MessageDialog.openInformation(shell, "Terracotta",
093: "Error starting Terracotta Server:\n"
094: + ActionUtil.getStatusMessages(e));
095: }
096: }
097:
098: public void run(IProgressMonitor monitor)
099: throws InvocationTargetException, InterruptedException {
100: if (monitor != null && monitor.isCanceled())
101: throw new InterruptedException("Cancelled server '"
102: + m_name + "'");
103:
104: IJavaProject javaProject = (IJavaProject) getJavaElement();
105: ServerTracker tracker = ServerTracker.getDefault();
106:
107: if (tracker.isRunning(javaProject, m_name)) {
108: tracker.stopServer(javaProject, m_name, monitor);
109: } else {
110: tracker.startServer(javaProject, m_name, monitor);
111: }
112: }
113: }
|