Fix Findbugs warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / BaseAddContextHandler.java
CommitLineData
b793fbe1
BH
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 **********************************************************************/
12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
13
14import java.util.List;
15
16import org.eclipse.core.commands.ExecutionEvent;
17import org.eclipse.core.commands.ExecutionException;
18import org.eclipse.core.runtime.IProgressMonitor;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Status;
21import org.eclipse.core.runtime.jobs.Job;
22import org.eclipse.jface.window.Window;
23import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
24import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
25import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IAddContextDialog;
26import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory;
27import org.eclipse.ui.IWorkbenchWindow;
28import org.eclipse.ui.PlatformUI;
29import 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 */
37abstract 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 StringBuffer errorString = new StringBuffer();
94
95 try {
96 List<String> contextNames = dialog.getContexts();
97 addContexts(param, contextNames, monitor);
98
99 } catch (ExecutionException e) {
100 errorString.append(e.toString());
101 errorString.append('\n');
102 }
103
104 // get session configuration in all cases
105 try {
106 param.getSession().getConfigurationFromNode(monitor);
107 } catch (ExecutionException e) {
108 errorString.append(Messages.TraceControl_ListSessionFailure);
109 errorString.append(": "); //$NON-NLS-1$
110 errorString.append(e.toString());
111 }
112
113 if (errorString.length() > 0) {
114 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString.toString());
115 }
116 return Status.OK_STATUS;
117 }
118 };
119 addJob.setUser(true);
120 addJob.schedule();
121 } catch (ExecutionException e) {
122 return new Status(Status.ERROR, Activator.PLUGIN_ID, e.toString());
123 }
124
125 return Status.OK_STATUS;
126 }
127 };
128 getJob.setUser(false);
129 getJob.schedule();
130
131 } finally {
132 fLock.unlock();
133 }
134 return null;
135 }
136}
This page took 0.032463 seconds and 5 git commands to generate.