lttng: Rename lttng2 feature/plugins to lttng2.control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / handlers / BaseAddContextHandler.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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.control.ui.views.handlers;
13
14 import java.util.List;
15
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
24 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.IAddContextDialog;
25 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
26 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.progress.UIJob;
30
31 /**
32 * <p>
33 * Base command handler implementation to add contexts.
34 * </p>
35 *
36 * @author Bernd Hufmann
37 */
38 public abstract class BaseAddContextHandler extends BaseControlViewHandler {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 /**
45 * The command execution parameter.
46 */
47 protected CommandParameter fParam = null;
48
49 // ------------------------------------------------------------------------
50 // Operations
51 // ------------------------------------------------------------------------
52
53 /**
54 * Adds contexts to channel(s) and/or event(s)
55 *
56 * @param param
57 * - a parameter instance with data for the command execution
58 * @param contextNames
59 * - list contexts to add
60 * @param monitor
61 * - a progress monitor
62 * @throws ExecutionException
63 * If something goes wrong
64 */
65 public abstract void addContexts(CommandParameter param, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException;
66
67 @Override
68 public Object execute(ExecutionEvent event) throws ExecutionException {
69
70 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
71
72 if (window == null) {
73 return false;
74 }
75 fLock.lock();
76 try {
77 // Make a copy for thread safety
78 final CommandParameter param = fParam.clone();
79
80 UIJob getJob = new UIJob(Messages.TraceControl_GetContextJob) {
81 @Override
82 public IStatus runInUIThread(IProgressMonitor monitor) {
83
84 try {
85 final List<String> availableContexts = param.getSession().getContextList(monitor);
86 final IAddContextDialog dialog = TraceControlDialogFactory.getInstance().getAddContextDialog();
87 dialog.setAvalibleContexts(availableContexts);
88
89 if ((dialog.open() != Window.OK) || (dialog.getContexts().isEmpty())) {
90 return Status.OK_STATUS;
91 }
92
93 Job addJob = new Job(Messages.TraceControl_AddContextJob) {
94 @Override
95 protected IStatus run(IProgressMonitor monitor2) {
96 Exception error = null;
97
98 try {
99 List<String> contextNames = dialog.getContexts();
100 addContexts(param, contextNames, monitor2);
101
102 } catch (ExecutionException e) {
103 error = e;
104 }
105
106 // get session configuration in all cases
107 refresh(param);
108
109 if (error != null) {
110 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_AddContextFailure, error);
111 }
112 return Status.OK_STATUS;
113 }
114 };
115 addJob.setUser(true);
116 addJob.schedule();
117 } catch (ExecutionException e) {
118 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_GetContextFailure, e);
119 }
120
121 return Status.OK_STATUS;
122 }
123 };
124 getJob.setUser(false);
125 getJob.schedule();
126
127 } finally {
128 fLock.unlock();
129 }
130 return null;
131 }
132 }
This page took 0.035559 seconds and 5 git commands to generate.