1
/*******************************************************************************
2
* Copyright (c) 2006 IBM Corporation.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse, Anithra P J
10
*******************************************************************************/
12
package org.eclipse.linuxtools.systemtap.ui.dashboard.actions;
14
import java.util.ArrayList;
16
import org.eclipse.jface.action.Action;
17
import org.eclipse.jface.action.IAction;
18
import org.eclipse.jface.viewers.ISelection;
19
import org.eclipse.jface.viewers.ISelectionChangedListener;
20
import org.eclipse.jface.viewers.SelectionChangedEvent;
21
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.ui.IViewActionDelegate;
23
import org.eclipse.ui.IViewPart;
24
import org.eclipse.ui.IWorkbenchWindow;
25
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
26
import org.eclipse.ui.PlatformUI;
28
import org.eclipse.linuxtools.systemtap.ui.structures.TreeNode;
29
import org.eclipse.linuxtools.systemtap.ui.structures.listeners.IActionListener;
30
import org.eclipse.linuxtools.systemtap.ui.dashboard.actions.hidden.GetSelectedModule;
31
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.ActiveModuleData;
32
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.ActiveModuleTreeNode;
33
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardGraphData;
34
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardModule;
35
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.GraphTreeNode;
36
import org.eclipse.linuxtools.systemtap.ui.dashboard.views.ActiveModuleBrowserView;
37
import org.eclipse.linuxtools.systemtap.ui.dashboard.views.DashboardModuleBrowserView;
38
import org.eclipse.linuxtools.systemtap.ui.dashboard.views.DashboardView;
41
* This action is used to stop the selected dashboard module. The graphs are all
42
* closed, the process is killed, and the item is removed from tha ActiveModuleBrowserView.
45
public class StopModuleAction extends Action implements IViewActionDelegate, IWorkbenchWindowActionDelegate {
46
public void init(IViewPart view) {
50
public void init(IWorkbenchWindow window) {}
52
public void run(IAction act) {
57
* This is the main method of the class. It handles stopping of the module.
58
* All of the graphs are closed, the process is killed, and the item
59
* is removed from the ActiveModuleBrowserView.
62
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
67
TreeNode treenode = GetSelectedModule.getNode(view);
68
DashboardModule module = null;
69
if (treenode.getChildCount() == 0)
71
module = (DashboardModule)treenode.getData();
72
stop = stopmodule(module);
76
for(int j=0; j<treenode.getChildCount(); j++) {
77
module = (DashboardModule)treenode.getChildAt(j).getData();
78
stop = stopmodule(module) || stop ;
81
} catch (Exception e) {
88
private boolean stopmodule(DashboardModule module)
90
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
91
ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
92
GraphTreeNode graphNode;
93
DashboardGraphData graphData;
96
if(ambv.isActive(module)) {
97
ActiveModuleTreeNode node = ambv.remove(module);
98
ActiveModuleData amd = (ActiveModuleData)node.getData();
100
ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardView.ID);
101
DashboardView dv = (DashboardView)ivp;
103
for(int i=0; i<node.getChildCount(); i++) {
104
graphNode = (GraphTreeNode)node.getChildAt(i);
105
graphData = (DashboardGraphData)graphNode.getData();
106
if(null != graphData.adapter) {
107
dv.removeGraph(graphData.adapter, module.getcategory());
108
graphData.adapter = null;
113
if (amd.cmd.isRunning())
117
dv.closeComposite(amd.module.category);
122
setEnablement(false);
123
buildEnablementChecks();
124
} catch (Exception e) {
131
* This updates the enablement of the action based on the newly selected item
132
* @param act The action that called this method
133
* @param select The newly selected item.
135
public void selectionChanged(IAction act, ISelection select) {
137
setEnablement(false);
138
buildEnablementChecks();
142
* This method handles creating the checks that are used to determine if
143
* the action should be enabled for use.
145
private void buildEnablementChecks() {
146
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardModuleBrowserView.ID);
148
final DashboardModuleBrowserView dmbv = (DashboardModuleBrowserView)ivp;
149
dmbv.getViewer().addSelectionChangedListener(moduleListener);
151
ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
152
final ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
153
ambv.getViewer().addSelectionChangedListener(activeModuleListener);
155
RunModuleAction.addActionListener(runListener);
160
* Toggles whether or not the action is enabled
161
* @param enabled boolean flag representing whether the action is enabled or not
163
private void setEnablement(boolean enabled) {
164
act.setEnabled(enabled);
168
* Removes all internal references provided by this action. Nothing should be
169
* called or referenced after the dispose method.
171
public void dispose() {
173
RunModuleAction.removeActionListener(runListener);
175
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardModuleBrowserView.ID);
176
final DashboardModuleBrowserView dmbv = (DashboardModuleBrowserView)ivp;
177
dmbv.getViewer().removeSelectionChangedListener(moduleListener);
179
ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
180
final ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
181
ambv.getViewer().removeSelectionChangedListener(activeModuleListener);
182
} catch(Exception e) {}
189
* Adds a new listener to the button to inform others when the stop button
191
* @param listener The class interested in knowing when scripts are stopped
193
public static void addActionListener(IActionListener listener) {
194
listeners.add(listener);
198
* Removes the listener from getting stop events.
199
* @param listener The class that no longer should receive stop notices
201
public static void removeActionListener(IActionListener listener) {
202
listeners.remove(listener);
206
* This method fires the event when a module is run to every listener
207
* that is registered.
209
private static void fireActionEvent() {
210
for(int i=0; i<listeners.size(); i++)
211
listeners.get(i).handleActionEvent();
215
* Enables this action everytime a new module is run.
217
private final IActionListener runListener = new IActionListener() {
218
public void handleActionEvent() {
224
* Enables this action everytime an item in the ActiveModuleBrowserView
227
private final ISelectionChangedListener activeModuleListener = new ISelectionChangedListener() {
228
public void selectionChanged(SelectionChangedEvent e) {
234
* This method checks to see if the newly selected item in the
235
* DashboardModuleBrowserView is running or not. It will then set the
236
* enablement based on whether or not it is running.
238
private final ISelectionChangedListener moduleListener = new ISelectionChangedListener() {
239
public void selectionChanged(SelectionChangedEvent e) {
241
TreeNode node = (TreeNode)((StructuredSelection)(e.getSelection())).getFirstElement();
242
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
243
ActiveModuleBrowserView amdv = (ActiveModuleBrowserView)ivp;
244
int childcount = node.getChildCount();
245
if(0 == childcount && amdv.isActive((DashboardModule)node.getData()))
247
else if(childcount > 0)
249
boolean active = false;
252
for(int j=0; j<childcount; j++) {
253
if(amdv.isActive((DashboardModule)node.getChildAt(j).getData())) {
257
if (active == true ) setEnablement(true);
261
setEnablement(false);
262
} catch(Exception ex) {}
266
private IViewPart view;
268
private static ArrayList<IActionListener> listeners = new ArrayList<IActionListener>();