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