KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package org.terracotta.dso.actions;
6
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.jdt.core.IJavaElement;
10 import org.eclipse.jdt.core.IJavaProject;
11 import org.eclipse.jface.action.IAction;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Menu;
15
16 import org.terracotta.dso.ConfigurationHelper;
17 import org.terracotta.dso.ProjectNature;
18 import org.terracotta.dso.TcPlugin;
19 import com.terracottatech.config.Server;
20 import com.terracottatech.config.Servers;
21
22 /**
23  * Action providing server management menuitems.
24  *
25  * @see org.eclipse.jdt.core.IProject
26  * @see BaseMenuCreator
27  * @see ManageServerAction
28  */

29
30 public class ManageServerHandler extends BaseMenuCreator
31   implements IProjectAction
32 {
33   private IJavaProject m_javaProject;
34
35   public ManageServerHandler() {
36     super();
37     TcPlugin.getDefault().registerProjectAction(this);
38   }
39
40   public void selectionChanged(IAction action, ISelection selection) {
41     super.selectionChanged(action, selection);
42     action.setEnabled((m_element = getElement(selection)) != null);
43   }
44
45   protected IJavaElement getJavaElement(ISelection selection) {
46     update(ActionUtil.locateSelectedJavaProject(selection));
47     return m_javaProject;
48   }
49
50   private void update(IJavaProject javaProject) {
51     if(javaProject != null) {
52       try {
53         if(javaProject.getProject().hasNature(ProjectNature.NATURE_ID)) {
54           m_javaProject = javaProject;
55         }
56         else {
57           m_javaProject = null;
58         }
59       } catch(CoreException ce) {/**/}
60     }
61     else {
62       m_javaProject = null;
63     }
64   }
65   
66   public void update(IProject project) {
67     update(ActionUtil.findJavaProject(project));
68     if(m_delegateAction != null) {
69       m_delegateAction.setEnabled(m_javaProject != null);
70     }
71   }
72   
73   public Menu getMenu(Control parent) {
74     Menu menu = null;
75     
76     buildMenu(menu = new Menu(parent));
77
78     return menu;
79   }
80
81   public Menu getMenu(Menu parent) {
82     Menu menu = null;
83     
84     buildMenu(menu = new Menu(parent));
85
86     return menu;
87   }
88
89   protected void fillMenu(Menu menu) {
90     if(m_javaProject != null) {
91       TcPlugin plugin = TcPlugin.getDefault();
92       IProject project = m_javaProject.getProject();
93       ConfigurationHelper configHelper = plugin.getConfigurationHelper(project);
94       Servers servers = configHelper.getServers();
95
96       if(servers == null) {
97         m_delegateAction.setEnabled(true);
98         addMenuAction(menu, new ManageServerAction(m_javaProject, "default"));
99       } else {
100         Server[] serverArray = servers.getServerArray();
101
102         m_delegateAction.setEnabled(true);
103
104         if(serverArray != null) {
105           for(int i = 0; i < serverArray.length; i++) {
106             addMenuAction(menu, new ManageServerAction(m_javaProject, serverArray[i].getName()));
107           }
108         }
109         else {
110           m_delegateAction.setEnabled(false);
111         }
112       }
113     }
114   }
115 }
116
Popular Tags