~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.dashboard/src/org/eclipse/linuxtools/systemtap/ui/dashboard/actions/StopModuleAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse, Anithra P J
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.systemtap.ui.dashboard.actions;
 
13
 
 
14
import java.util.ArrayList;
 
15
 
 
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;
 
27
 
 
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;
 
39
 
 
40
/**
 
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.
 
43
 * @author Ryan Morse
 
44
 */
 
45
public class StopModuleAction extends Action implements IViewActionDelegate, IWorkbenchWindowActionDelegate {
 
46
        public void init(IViewPart view) {
 
47
                this.view = view;
 
48
        }
 
49
        
 
50
        public void init(IWorkbenchWindow window) {}
 
51
        
 
52
        public void run(IAction act) {
 
53
                run();
 
54
        }
 
55
        
 
56
        /**
 
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.
 
60
         */
 
61
        public void run() {
 
62
                PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
 
63
                        boolean stop = false;
 
64
                        public void run() {
 
65
                                if(stop) return;
 
66
                                try {
 
67
                                        TreeNode treenode = GetSelectedModule.getNode(view);
 
68
                                        DashboardModule module = null;
 
69
                                        if (treenode.getChildCount() == 0)
 
70
                                        {
 
71
                                                module = (DashboardModule)treenode.getData();
 
72
                                                stop =  stopmodule(module);
 
73
                                        }
 
74
                                        else 
 
75
                                        { 
 
76
                                                for(int j=0; j<treenode.getChildCount(); j++) {
 
77
                                                module = (DashboardModule)treenode.getChildAt(j).getData();
 
78
                                                stop = stopmodule(module) || stop ;
 
79
                                                }
 
80
                                        }
 
81
                                } catch (Exception e) {
 
82
                                        stop = true;
 
83
                                }
 
84
                        }
 
85
                });
 
86
        }
 
87
        
 
88
        private boolean stopmodule(DashboardModule module)
 
89
        {
 
90
                IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
 
91
                ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
 
92
                GraphTreeNode graphNode;
 
93
                DashboardGraphData graphData;
 
94
                boolean stop = false;
 
95
                try{
 
96
                        if(ambv.isActive(module)) {
 
97
                                ActiveModuleTreeNode node = ambv.remove(module);
 
98
                                ActiveModuleData amd = (ActiveModuleData)node.getData();
 
99
                                
 
100
                                ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardView.ID);
 
101
                                DashboardView dv = (DashboardView)ivp;
 
102
                                
 
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;
 
109
                                        }
 
110
                                }
 
111
                                
 
112
                                if(!amd.paused) {
 
113
                                        if (amd.cmd.isRunning())
 
114
                                        amd.cmd.stop();
 
115
                                        amd.data = null;
 
116
                                }
 
117
                                dv.closeComposite(amd.module.category);
 
118
                                amd = null;
 
119
                                fireActionEvent();
 
120
                        }
 
121
 
 
122
                        setEnablement(false);
 
123
                        buildEnablementChecks();
 
124
                } catch (Exception e) {
 
125
                        stop = true;
 
126
                }       
 
127
                return stop;
 
128
        }
 
129
        
 
130
        /**
 
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.
 
134
         */
 
135
        public void selectionChanged(IAction act, ISelection select) {
 
136
                this.act = act;
 
137
                setEnablement(false);
 
138
                buildEnablementChecks();
 
139
        }
 
140
        
 
141
        /**
 
142
         * This method handles creating the checks that are used to determine if
 
143
         * the action should be enabled for use.
 
144
         */
 
145
        private void buildEnablementChecks() {
 
146
                IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardModuleBrowserView.ID);
 
147
                if(null != ivp) {
 
148
                        final DashboardModuleBrowserView dmbv = (DashboardModuleBrowserView)ivp;
 
149
                        dmbv.getViewer().addSelectionChangedListener(moduleListener);
 
150
                        
 
151
                        ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
 
152
                        final ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
 
153
                        ambv.getViewer().addSelectionChangedListener(activeModuleListener);
 
154
                        
 
155
                        RunModuleAction.addActionListener(runListener);
 
156
                }
 
157
        }
 
158
        
 
159
        /**
 
160
         * Toggles whether or not the action is enabled
 
161
         * @param enabled boolean flag representing whether the action is enabled or not
 
162
         */
 
163
        private void setEnablement(boolean enabled) {
 
164
                act.setEnabled(enabled);
 
165
        }
 
166
        
 
167
        /**
 
168
         * Removes all internal references provided by this action.  Nothing should be
 
169
         * called or referenced after the dispose method.
 
170
         */
 
171
        public void dispose() {
 
172
                try {
 
173
                        RunModuleAction.removeActionListener(runListener);
 
174
        
 
175
                        IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(DashboardModuleBrowserView.ID);
 
176
                        final DashboardModuleBrowserView dmbv = (DashboardModuleBrowserView)ivp;
 
177
                        dmbv.getViewer().removeSelectionChangedListener(moduleListener);
 
178
                        
 
179
                        ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ActiveModuleBrowserView.ID);
 
180
                        final ActiveModuleBrowserView ambv = (ActiveModuleBrowserView)ivp;
 
181
                        ambv.getViewer().removeSelectionChangedListener(activeModuleListener);
 
182
                } catch(Exception e) {}
 
183
                
 
184
                view = null;
 
185
                act = null;
 
186
        }
 
187
        
 
188
        /**
 
189
         * Adds a new listener to the button to inform others when the stop button
 
190
         * is pressed.
 
191
         * @param listener The class interested in knowing when scripts are stopped
 
192
         */
 
193
        public static void addActionListener(IActionListener listener) {
 
194
                listeners.add(listener);
 
195
        }
 
196
        
 
197
        /**
 
198
         * Removes the listener from getting stop events.
 
199
         * @param listener The class that no longer should receive stop notices
 
200
         */
 
201
        public static void removeActionListener(IActionListener listener) {
 
202
                listeners.remove(listener);
 
203
        }
 
204
        
 
205
        /**
 
206
         * This method fires the event when a module is run to every listener
 
207
         * that is registered.
 
208
         */
 
209
        private static void fireActionEvent() {
 
210
                for(int i=0; i<listeners.size(); i++)
 
211
                        listeners.get(i).handleActionEvent();
 
212
        }
 
213
        
 
214
        /**
 
215
         * Enables this action everytime a new module is run.
 
216
         */
 
217
        private final IActionListener runListener = new IActionListener() {
 
218
                public void handleActionEvent() {
 
219
                        setEnablement(true);
 
220
                }
 
221
        };
 
222
        
 
223
        /**
 
224
         * Enables this action everytime an item in the ActiveModuleBrowserView
 
225
         * is selected.
 
226
         */
 
227
        private final ISelectionChangedListener activeModuleListener = new ISelectionChangedListener() {
 
228
                public void selectionChanged(SelectionChangedEvent e) {
 
229
                        setEnablement(true);
 
230
                }
 
231
        };
 
232
        
 
233
        /**
 
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.
 
237
         */
 
238
        private final ISelectionChangedListener moduleListener = new ISelectionChangedListener() {
 
239
                public void selectionChanged(SelectionChangedEvent e) {
 
240
                        try {
 
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()))
 
246
                                        setEnablement(true);
 
247
                                else if(childcount > 0)
 
248
                                {       
 
249
                                        boolean active = false;
 
250
                                        
 
251
                                        
 
252
                                                for(int j=0; j<childcount; j++) {
 
253
                                                        if(amdv.isActive((DashboardModule)node.getChildAt(j).getData())) {
 
254
                                                                active = true;
 
255
                                                                break; }
 
256
                                                }
 
257
                                                if (active == true ) setEnablement(true);
 
258
                                        
 
259
                                }
 
260
                                else
 
261
                                        setEnablement(false);
 
262
                        } catch(Exception ex) {}
 
263
                }
 
264
        };
 
265
        
 
266
        private IViewPart view;
 
267
        private IAction act;
 
268
        private static ArrayList<IActionListener> listeners = new ArrayList<IActionListener>();
 
269
}