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

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng2.ui/src/org/eclipse/linuxtools/internal/lttng2/ui/views/control/handlers/ChangeChannelStateHandler.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) 2012 Ericsson
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials are
 
5
 * made available under the terms of the Eclipse Public License v1.0 which
 
6
 * accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 * 
 
9
 * Contributors: 
 
10
 *   Bernd Hufmann - Initial API and implementation
 
11
 **********************************************************************/
 
12
package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
 
13
 
 
14
import java.util.ArrayList;
 
15
import java.util.Iterator;
 
16
import java.util.List;
 
17
 
 
18
import org.eclipse.core.commands.ExecutionEvent;
 
19
import org.eclipse.core.commands.ExecutionException;
 
20
import org.eclipse.core.runtime.IProgressMonitor;
 
21
import org.eclipse.core.runtime.IStatus;
 
22
import org.eclipse.core.runtime.Status;
 
23
import org.eclipse.core.runtime.jobs.Job;
 
24
import org.eclipse.jface.viewers.ISelection;
 
25
import org.eclipse.jface.viewers.StructuredSelection;
 
26
import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceEnablement;
 
27
import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
 
28
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
 
29
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
 
30
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent;
 
31
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceDomainComponent;
 
32
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
 
33
import org.eclipse.ui.IWorkbenchPage;
 
34
import org.eclipse.ui.IWorkbenchWindow;
 
35
import org.eclipse.ui.PlatformUI;
 
36
 
 
37
/**
 
38
 * <p>
 
39
 * Abstract command handler implementation to enable or disabling a trace channel.
 
40
 * </p>
 
41
 * 
 
42
 * @author Bernd Hufmann
 
43
 */
 
44
abstract public class ChangeChannelStateHandler extends BaseControlViewHandler {
 
45
 
 
46
    // ------------------------------------------------------------------------
 
47
    // Attributes
 
48
    // ------------------------------------------------------------------------
 
49
    /**
 
50
     * The command execution parameter.
 
51
     */
 
52
    protected Parameter fParam;
 
53
 
 
54
    // ------------------------------------------------------------------------
 
55
    // Accessors
 
56
    // ------------------------------------------------------------------------
 
57
    /**
 
58
     * @return the new state to set
 
59
     */
 
60
    abstract protected TraceEnablement getNewState(); 
 
61
 
 
62
    // ------------------------------------------------------------------------
 
63
    // Operations
 
64
    // ------------------------------------------------------------------------
 
65
    /**
 
66
     * Changes the state of the given channels.
 
67
     * @param domain - the domain of the channels.
 
68
     * @param channelNames - a list of channel names 
 
69
     * @param monitor - a progress monitor
 
70
     * @throws ExecutionException
 
71
     */
 
72
    abstract protected void changeState(TraceDomainComponent domain, List<String> channelNames, IProgressMonitor monitor) throws ExecutionException; 
 
73
 
 
74
    /*
 
75
     * (non-Javadoc)
 
76
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
 
77
     */
 
78
    @Override
 
79
    public Object execute(ExecutionEvent event) throws ExecutionException {
 
80
 
 
81
        fLock.lock();
 
82
        try {
 
83
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 
84
 
 
85
            if (window == null) {
 
86
                return false;
 
87
            }
 
88
 
 
89
            final Parameter param = new Parameter(fParam);
 
90
 
 
91
            Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
 
92
                @Override
 
93
                protected IStatus run(IProgressMonitor monitor) {
 
94
                    Exception error = null;
 
95
 
 
96
                    TraceSessionComponent session = null;
 
97
 
 
98
                    try {
 
99
                        TraceDomainComponent kernelDomain = param.getKernelDomain();
 
100
                        List<TraceChannelComponent> kernelChannels = param.getKernelChannels();
 
101
 
 
102
                        if (kernelDomain != null) {
 
103
                            session = (TraceSessionComponent)kernelDomain.getParent();
 
104
                            List<String> channelNames = new ArrayList<String>();
 
105
                            for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) {
 
106
                                // Enable all selected channels which are disabled
 
107
                                TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
 
108
                                channelNames.add(channel.getName());
 
109
                            }
 
110
 
 
111
                            changeState(kernelDomain, channelNames, monitor);
 
112
 
 
113
                            for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) {
 
114
                                // Enable all selected channels which are disabled
 
115
                                TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
 
116
                                channel.setState(getNewState());
 
117
                            }
 
118
                        }
 
119
 
 
120
                        TraceDomainComponent ustDomain = param.getUstDomain();
 
121
                        List<TraceChannelComponent> ustChannels = param.getUstChannels();
 
122
                        if (ustDomain != null) {
 
123
                            if (session == null) {
 
124
                                session = (TraceSessionComponent)ustDomain.getParent();
 
125
                            }
 
126
 
 
127
                            List<String> channelNames = new ArrayList<String>();
 
128
                            for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) {
 
129
                                // Enable all selected channels which are disabled
 
130
                                TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
 
131
                                channelNames.add(channel.getName());
 
132
                            }
 
133
 
 
134
                            changeState(ustDomain, channelNames, monitor);
 
135
 
 
136
                            for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) {
 
137
                                // Enable all selected channels which are disabled
 
138
                                TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
 
139
                                channel.setState(getNewState());
 
140
                            }
 
141
                        }
 
142
                    } catch (ExecutionException e) {
 
143
                        error = e;
 
144
                    }
 
145
 
 
146
                    // In all cases notify listeners  
 
147
                    session.fireComponentChanged(session);
 
148
 
 
149
                    if (error != null) {
 
150
                        return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeChannelStateFailure, error);
 
151
                    }
 
152
 
 
153
                    return Status.OK_STATUS;
 
154
                }
 
155
            };
 
156
            job.setUser(true);
 
157
            job.schedule();
 
158
        } finally {
 
159
            fLock.unlock();
 
160
        }
 
161
 
 
162
        return null;
 
163
    }
 
164
 
 
165
    /*
 
166
     * (non-Javadoc)
 
167
     * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
 
168
     */
 
169
    @Override
 
170
    public boolean isEnabled() {
 
171
 
 
172
        // Get workbench page for the Control View
 
173
        IWorkbenchPage page = getWorkbenchPage();
 
174
        if (page == null) {
 
175
            return false;
 
176
        }
 
177
 
 
178
        TraceDomainComponent kernelDomain = null;
 
179
        TraceDomainComponent ustDomain = null;
 
180
        List<TraceChannelComponent> kernelChannels = new ArrayList<TraceChannelComponent>();
 
181
        List<TraceChannelComponent> ustChannels = new ArrayList<TraceChannelComponent>();
 
182
        
 
183
        // Check if one or more session are selected
 
184
        ISelection selection = page.getSelection(ControlView.ID);
 
185
        if (selection instanceof StructuredSelection) {
 
186
            StructuredSelection structered = ((StructuredSelection) selection);
 
187
            String sessionName = null;
 
188
            for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
 
189
                Object element = (Object) iterator.next();
 
190
                 
 
191
                if (element instanceof TraceChannelComponent) {
 
192
                    
 
193
                    // Add only TraceChannelComponents that are disabled
 
194
                    TraceChannelComponent channel = (TraceChannelComponent) element;
 
195
                    if (sessionName == null) {
 
196
                        sessionName = String.valueOf(channel.getSessionName());
 
197
                    }
 
198
 
 
199
                    // Enable command only for channels of same session
 
200
                    if (!sessionName.equals(channel.getSessionName())) {
 
201
                        kernelChannels.clear();
 
202
                        ustChannels.clear();
 
203
                        break;
 
204
                    }
 
205
 
 
206
                    if ((channel.getState() != getNewState())) {
 
207
                        if (channel.isKernel()) {
 
208
                            kernelChannels.add(channel);
 
209
                            if (kernelDomain == null) {
 
210
                                kernelDomain = (TraceDomainComponent) channel.getParent();
 
211
                            }
 
212
                        } else {
 
213
                            ustChannels.add(channel);
 
214
                            if (ustDomain == null) {
 
215
                                ustDomain = (TraceDomainComponent) channel.getParent();
 
216
                            }
 
217
                        }
 
218
                    }
 
219
                }
 
220
            }
 
221
        }
 
222
        
 
223
        boolean isEnabled = (!kernelChannels.isEmpty() || !ustChannels.isEmpty());
 
224
        fLock.lock();
 
225
        try {
 
226
            if (isEnabled) {
 
227
                fParam = new Parameter(kernelDomain, ustDomain, kernelChannels, ustChannels);
 
228
            }
 
229
        } finally {
 
230
            fLock.unlock();
 
231
        }
 
232
        
 
233
        return isEnabled;
 
234
    }
 
235
    
 
236
    /**
 
237
     *  Class containing parameter for the command execution. 
 
238
     */
 
239
    static protected class Parameter {
 
240
        /**
 
241
         * Kernel domain component reference.
 
242
         */
 
243
        final protected TraceDomainComponent fKernelDomain;
 
244
        /**
 
245
         * UST domain component reference.
 
246
         */
 
247
        final protected TraceDomainComponent fUstDomain;
 
248
        /**
 
249
         * The list of kernel channel components the command is to be executed on. 
 
250
         */
 
251
        final protected List<TraceChannelComponent> fKernelChannels;
 
252
        /**
 
253
         * The list of UST channel components the command is to be executed on. 
 
254
         */
 
255
        final protected List<TraceChannelComponent> fUstChannels;
 
256
        
 
257
        /**
 
258
         * Constructor
 
259
         * @param kernelDomain - a kernel domain component
 
260
         * @param ustDomain - a UST domain component
 
261
         * @param kernelChannels - list of available kernel channels
 
262
         * @param ustChannels - list of available UST channels
 
263
         */
 
264
        public Parameter(TraceDomainComponent kernelDomain, TraceDomainComponent ustDomain, List<TraceChannelComponent> kernelChannels, List<TraceChannelComponent> ustChannels) {
 
265
            fKernelDomain = kernelDomain;
 
266
            fUstDomain = ustDomain;
 
267
            fKernelChannels = new ArrayList<TraceChannelComponent>();
 
268
            fKernelChannels.addAll(kernelChannels);
 
269
            fUstChannels = new ArrayList<TraceChannelComponent>();
 
270
            fUstChannels.addAll(ustChannels);
 
271
        }
 
272
        
 
273
        /**
 
274
         * Copy constructor
 
275
         * @param other a parameter to copy
 
276
         */
 
277
        public Parameter(Parameter other) {
 
278
            this(other.fKernelDomain, other.fUstDomain, other.fKernelChannels, other.fUstChannels);
 
279
        }
 
280
        
 
281
        /**
 
282
         * @return the kernel domain component.
 
283
         */
 
284
        public TraceDomainComponent getKernelDomain() {
 
285
            return fKernelDomain;
 
286
        }
 
287
        
 
288
        /**
 
289
         * @return the UST domain component.
 
290
         */
 
291
        public TraceDomainComponent getUstDomain() {
 
292
            return fUstDomain;
 
293
        }   
 
294
        
 
295
        /**
 
296
         * @return the list of kernel channel components.
 
297
         */
 
298
        public List<TraceChannelComponent> getKernelChannels() {
 
299
            return fKernelChannels;
 
300
        }
 
301
        
 
302
        /**
 
303
         * @return the list of UST channel components.
 
304
         */
 
305
        public List<TraceChannelComponent> getUstChannels() {
 
306
            return fUstChannels;
 
307
        }
 
308
    }
 
309
}