001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.actions;
006:
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.core.runtime.CoreException;
009: import org.eclipse.jdt.core.IJavaElement;
010: import org.eclipse.jdt.core.IJavaProject;
011: import org.eclipse.jface.action.IAction;
012: import org.eclipse.jface.viewers.ISelection;
013: import org.eclipse.swt.widgets.Control;
014: import org.eclipse.swt.widgets.Menu;
015:
016: import org.terracotta.dso.ConfigurationHelper;
017: import org.terracotta.dso.ProjectNature;
018: import org.terracotta.dso.TcPlugin;
019:
020: import com.tc.config.schema.dynamic.ParameterSubstituter;
021: import com.terracottatech.config.Server;
022: import com.terracottatech.config.Servers;
023:
024: /**
025: * Action providing server management menuitems.
026: *
027: * @see org.eclipse.jdt.core.IProject
028: * @see BaseMenuCreator
029: * @see ManageServerAction
030: */
031:
032: public class ManageServerHandler extends BaseMenuCreator implements
033: IProjectAction {
034: private IJavaProject m_javaProject;
035:
036: public ManageServerHandler() {
037: super ();
038: TcPlugin.getDefault().registerProjectAction(this );
039: }
040:
041: public void selectionChanged(IAction action, ISelection selection) {
042: super .selectionChanged(action, selection);
043: action.setEnabled((m_element = getElement(selection)) != null);
044: }
045:
046: protected IJavaElement getJavaElement(ISelection selection) {
047: update(ActionUtil.locateSelectedJavaProject(selection));
048: return m_javaProject;
049: }
050:
051: private void update(IJavaProject javaProject) {
052: if (javaProject != null) {
053: try {
054: if (javaProject.getProject().hasNature(
055: ProjectNature.NATURE_ID)) {
056: m_javaProject = javaProject;
057: } else {
058: m_javaProject = null;
059: }
060: } catch (CoreException ce) {/**/
061: }
062: } else {
063: m_javaProject = null;
064: }
065: }
066:
067: public void update(IProject project) {
068: update(ActionUtil.findJavaProject(project));
069: if (m_delegateAction != null) {
070: m_delegateAction.setEnabled(m_javaProject != null);
071: }
072: }
073:
074: public Menu getMenu(Control parent) {
075: Menu menu = null;
076:
077: buildMenu(menu = new Menu(parent));
078:
079: return menu;
080: }
081:
082: public Menu getMenu(Menu parent) {
083: Menu menu = null;
084:
085: buildMenu(menu = new Menu(parent));
086:
087: return menu;
088: }
089:
090: protected void fillMenu(Menu menu) {
091: if (m_javaProject != null) {
092: TcPlugin plugin = TcPlugin.getDefault();
093: IProject project = m_javaProject.getProject();
094: ConfigurationHelper configHelper = plugin
095: .getConfigurationHelper(project);
096: Servers servers = configHelper.getServers();
097:
098: if (servers == null) {
099: m_delegateAction.setEnabled(true);
100: addMenuAction(menu, new ManageServerAction(
101: m_javaProject, "default"));
102: } else {
103: Server[] serverArray = servers.getServerArray();
104:
105: m_delegateAction.setEnabled(true);
106:
107: if (serverArray != null) {
108: for (int i = 0; i < serverArray.length; i++) {
109: Server server = serverArray[i];
110: String name;
111:
112: if (server.isSetName()) {
113: name = server.getName();
114: } else {
115: int dsoPort = server.isSetDsoPort() ? server
116: .getDsoPort()
117: : 9510;
118: name = server.getHost() + ":" + dsoPort;
119: }
120: name = ParameterSubstituter.substitute(name);
121: addMenuAction(menu, new ManageServerAction(
122: m_javaProject, name));
123: }
124: } else {
125: m_delegateAction.setEnabled(false);
126: }
127: }
128: }
129: }
130: }
|