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 | /** | |
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) { | |
f455db37 | 93 | Exception error = null; |
b793fbe1 BH |
94 | |
95 | try { | |
96 | List<String> contextNames = dialog.getContexts(); | |
97 | addContexts(param, contextNames, monitor); | |
98 | ||
99 | } catch (ExecutionException e) { | |
f455db37 | 100 | error = e; |
b793fbe1 BH |
101 | } |
102 | ||
103 | // get session configuration in all cases | |
f455db37 BH |
104 | refresh(param); |
105 | ||
106 | if (error != null) { | |
107 | return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_AddContextFailure, error); | |
b793fbe1 BH |
108 | } |
109 | return Status.OK_STATUS; | |
110 | } | |
111 | }; | |
112 | addJob.setUser(true); | |
113 | addJob.schedule(); | |
114 | } catch (ExecutionException e) { | |
f455db37 | 115 | return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_GetContextFailure, e); |
b793fbe1 BH |
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 | } |