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