View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package net.sf.tm.plugin.ui;
34  
35  import net.sf.tm.WebContext;
36  import net.sf.tm.WebContextStatus;
37  import net.sf.tm.plugin.AutoUpdate;
38  import net.sf.tm.plugin.ContextManagerPlugin;
39  import net.sf.tm.plugin.IUpdatable;
40  import net.sf.tm.plugin.TreeNode;
41  import net.sf.tm.plugin.WebContextContentProvider;
42  import net.sf.tm.plugin.WebContextLabelProvider;
43  import net.sf.tm.plugin.actions.CopyToClipboardAction;
44  import net.sf.tm.plugin.actions.DiscardServerAction;
45  import net.sf.tm.plugin.actions.EditServerAction;
46  import net.sf.tm.plugin.actions.NewServerAction;
47  import net.sf.tm.plugin.actions.RefreshAction;
48  import net.sf.tm.plugin.actions.ReloadContextAction;
49  import net.sf.tm.plugin.actions.StartContextAction;
50  import net.sf.tm.plugin.actions.StopContextAction;
51  
52  import org.eclipse.core.resources.ResourcesPlugin;
53  import org.eclipse.jface.action.IAction;
54  import org.eclipse.jface.action.IMenuListener;
55  import org.eclipse.jface.action.IMenuManager;
56  import org.eclipse.jface.action.IToolBarManager;
57  import org.eclipse.jface.action.MenuManager;
58  import org.eclipse.jface.action.Separator;
59  import org.eclipse.jface.viewers.ISelectionChangedListener;
60  import org.eclipse.jface.viewers.IStructuredSelection;
61  import org.eclipse.jface.viewers.SelectionChangedEvent;
62  import org.eclipse.jface.viewers.TreeViewer;
63  import org.eclipse.swt.SWT;
64  import org.eclipse.swt.widgets.Composite;
65  import org.eclipse.swt.widgets.Menu;
66  import org.eclipse.ui.IActionBars;
67  import org.eclipse.ui.IWorkbenchActionConstants;
68  import org.eclipse.ui.part.DrillDownAdapter;
69  import org.eclipse.ui.part.ViewPart;
70  
71  /***
72   * Main view of the plugin. Displays a Tree with all configured servers.
73   * 
74   * @author Andreas Pataki
75   * @version $Id: WebContextView.java,v 1.8 2004/03/28 16:22:09 apataki Exp $
76   */
77  public class WebContextView extends ViewPart implements IUpdatable {
78  
79      /***
80       * Tree with servers
81       */
82      private TreeViewer viewer;
83      /***
84       * Drilldownadapter for web like navigation in the Tree
85       */
86      private DrillDownAdapter drillDownAdapter;
87  
88      /***
89       * Action instance for stoping a context
90       */
91      private IAction stopAction;
92      /***
93       * Action instance for starting a context
94       */
95      private IAction startAction;
96      /***
97       * Action instance for reloading a context
98       */
99      private IAction reloadAction;
100     /***
101      * Action instance for creating a new server
102      */
103     private IAction newAction;
104     /***
105      * Action instance for refreshing the servers
106      */
107     private IAction refreshAction;
108     /***
109      * Action instance for editing a server
110      */
111     private IAction editAction;
112     /***
113      * Action instance for discarding a server
114      */
115     private IAction discardAction;
116     /***
117      * Action instance for copying a context restart URL to the clipboard
118      */
119     private IAction copyAction;
120     /***
121      * Automatic view updater
122      */
123     private AutoUpdate updater;
124 
125     /***
126      * This is a callback that will allow us
127      * to create the viewer and initialize it.
128      */
129     public void createPartControl(Composite parent) {
130         viewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
131         drillDownAdapter = new DrillDownAdapter(viewer);
132         viewer.setContentProvider(new WebContextContentProvider());
133         viewer.setLabelProvider(new WebContextLabelProvider());
134         viewer.setInput(ResourcesPlugin.getWorkspace());
135         makeActions();
136         hookContextMenu();
137         contributeToActionBars();
138 
139         // update the action buttons when a selection change occurs
140         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
141             public void selectionChanged(SelectionChangedEvent event) {
142                 updateActionsState();
143             }
144         });
145 
146         updater =
147             new AutoUpdate(
148                 ContextManagerPlugin.getDefault().getPreferenceStore());
149         updater.start(this, getViewSite().getShell().getDisplay());
150     }
151 
152     /***
153      * @see org.eclipse.ui.IWorkbenchPart#dispose()
154      */
155     public void dispose() {
156         super.dispose();
157         updater.stop();
158     }
159 
160     /***
161      * Mehod for updating the view. It should be allways called when tree data
162      * has changed. On call the tree will get refreshed and the action status
163      * will get updated. 
164      */
165     public void update() {
166         viewer.refresh();
167         updateActionsState();
168     }
169 
170     /***
171      * Updates the status of a single context.
172      * 
173      * @param contextNode Context element
174      */
175     public void updateContextNode(TreeNode contextNode) {
176         viewer.update(contextNode, null);
177         //updateActionsState();
178     }
179 
180     /***
181      * Updates the current action status depending on the current selected 
182      * tree element. 
183      */
184     private void updateActionsState() {
185         Object selected = getSelected();
186         if ((selected instanceof TreeNode) && (selected != null)) {
187 
188             TreeNode node = (TreeNode) selected;
189 
190             if (node.getContent() instanceof WebContext) {
191                 // a web context is selected
192                 WebContext context = (WebContext) node.getContent();
193                 boolean running =
194                     (context.getStatus().equals(WebContextStatus.RUNNING));
195                 contextSelectedActionState(running);
196 
197             } else {
198                 serverSelectedActionState();
199             }
200         } else {
201             // nothing is selectd
202             defaultActionState();
203         }
204     }
205 
206     /***
207      * Updates the action status to the default.
208      */
209     private void defaultActionState() {
210         startAction.setEnabled(false);
211         reloadAction.setEnabled(false);
212         stopAction.setEnabled(false);
213         editAction.setEnabled(false);
214         discardAction.setEnabled(false);
215         newAction.setEnabled(true);
216         copyAction.setEnabled(false);
217     }
218 
219     /***
220      * Updates all actions to the state for a selected server entry.
221      */
222     private void serverSelectedActionState() {
223         newAction.setEnabled(true);
224         startAction.setEnabled(false);
225         reloadAction.setEnabled(false);
226         stopAction.setEnabled(false);
227         editAction.setEnabled(true);
228         discardAction.setEnabled(true);
229         copyAction.setEnabled(false);
230     }
231 
232     /***
233      * Updates all actions to the state for a selected context entry.
234      * 
235      * @param running if <code>true</code> the selected context is
236      *   started
237      */
238     private void contextSelectedActionState(boolean running) {
239         copyAction.setEnabled(true);
240         editAction.setEnabled(false);
241         discardAction.setEnabled(false);
242 
243         if (running) {
244             // context is running only stop makes sense
245             startAction.setEnabled(false);
246             reloadAction.setEnabled(true);
247             stopAction.setEnabled(true);
248         } else {
249             // context is stopped enable only start action
250             startAction.setEnabled(true);
251             reloadAction.setEnabled(false);
252             stopAction.setEnabled(false);
253         }
254     }
255 
256     /***
257      * Attaches the context menu to the view. For creating the menu the method
258      * {@link fillContextMenu(IMenuManager)} is called.
259      */
260     private void hookContextMenu() {
261         MenuManager menuMgr = new MenuManager("#PopupMenu");
262         menuMgr.setRemoveAllWhenShown(true);
263         menuMgr.addMenuListener(new IMenuListener() {
264             public void menuAboutToShow(IMenuManager manager) {
265                 WebContextView.this.fillContextMenu(manager);
266             }
267         });
268         Menu menu = menuMgr.createContextMenu(viewer.getControl());
269         viewer.getControl().setMenu(menu);
270         getSite().registerContextMenu(menuMgr, viewer);
271 
272     }
273 
274     private void contributeToActionBars() {
275         IActionBars bars = getViewSite().getActionBars();
276         fillLocalToolBar(bars.getToolBarManager());
277     }
278 
279     /***
280      * Creates the context menu layout.
281      * 
282      * @param manager menu manager
283      */
284     private void fillContextMenu(IMenuManager manager) {
285         manager.add(startAction);
286         manager.add(reloadAction);
287         manager.add(stopAction);
288         manager.add(new Separator());
289         manager.add(copyAction);
290         manager.add(refreshAction);
291         manager.add(new Separator());
292         manager.add(newAction);
293         manager.add(editAction);
294         manager.add(discardAction);
295         manager.add(new Separator());
296         drillDownAdapter.addNavigationActions(manager);
297         // Other plug-ins can contribute there actions here
298         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
299         manager.add(
300             new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end"));
301     }
302 
303     private void fillLocalToolBar(IToolBarManager manager) {
304         manager.add(startAction);
305         manager.add(reloadAction);
306         manager.add(stopAction);
307         manager.add(new Separator());
308         drillDownAdapter.addNavigationActions(manager);
309     }
310 
311     /***
312      * Creates all action instances.
313      */
314     private void makeActions() {
315         stopAction = new StopContextAction(this);
316         startAction = new StartContextAction(this);
317         reloadAction = new ReloadContextAction(this);
318         newAction = new NewServerAction(this);
319         refreshAction = new RefreshAction(this);
320         editAction = new EditServerAction(this);
321         discardAction = new DiscardServerAction(this);
322         copyAction = new CopyToClipboardAction(this);
323 
324        /* reloadAction.setActionDefinitionId("org.apache.tomcat.reload");
325         getViewSite().getKeyBindingService().registerAction(reloadAction);
326 
327         getViewSite().getActionBars().setGlobalActionHandler(
328             "org.apache.tomcat.reload",
329             reloadAction);*/
330 
331         defaultActionState();
332     }
333 
334     /***
335      * Passing the focus request to the viewer's control.
336      */
337     public void setFocus() {
338         viewer.getControl().setFocus();
339     }
340 
341     private String getLocalizedString(String key) {
342         return ContextManagerPlugin.getDefault().getLocalizedString(key);
343     }
344 
345     public Object getSelected() {
346         return ((IStructuredSelection) viewer.getSelection()).getFirstElement();
347     }
348 
349 }