Commit | Line | Data |
---|---|---|
bbb3538a 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 | **********************************************************************/ | |
115b4a01 | 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
bbb3538a | 13 | |
bbb3538a BH |
14 | import org.eclipse.core.commands.ExecutionEvent; |
15 | import org.eclipse.core.commands.ExecutionException; | |
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | import org.eclipse.core.runtime.IStatus; | |
18 | import org.eclipse.core.runtime.Status; | |
19 | import org.eclipse.core.runtime.jobs.Job; | |
20 | import org.eclipse.jface.viewers.ISelection; | |
21 | import org.eclipse.jface.viewers.StructuredSelection; | |
22 | import org.eclipse.jface.window.Window; | |
115b4a01 BH |
23 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
24 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
115b4a01 BH |
25 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ICreateSessionDialog; |
26 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; | |
9315aeee | 27 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
115b4a01 | 28 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionGroup; |
bbb3538a | 29 | import org.eclipse.ui.IWorkbenchPage; |
bbb3538a BH |
30 | |
31 | /** | |
bbb3538a BH |
32 | * <p> |
33 | * Command handler implementation to create a trace session. | |
34 | * </p> | |
dbd4432d BH |
35 | * |
36 | * @author Bernd Hufmann | |
bbb3538a | 37 | */ |
498704b3 | 38 | public class CreateSessionHandler extends BaseControlViewHandler { |
bbb3538a BH |
39 | |
40 | // ------------------------------------------------------------------------ | |
41 | // Attributes | |
42 | // ------------------------------------------------------------------------ | |
43 | /** | |
44 | * The trace session group the command is to be executed on. | |
45 | */ | |
46 | private TraceSessionGroup fSessionGroup = null; | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Operations | |
50 | // ------------------------------------------------------------------------ | |
51 | /* | |
52 | * (non-Javadoc) | |
53 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
54 | */ | |
55 | @Override | |
56 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
bbb3538a | 57 | |
c56972bb BH |
58 | fLock.lock(); |
59 | try { | |
60 | final TraceSessionGroup sessionGroup = fSessionGroup; | |
61 | ||
62 | // Open dialog box for the node name and address | |
63 | ICreateSessionDialog dialog = TraceControlDialogFactory.getInstance().getCreateSessionDialog(); | |
64 | dialog.setTraceSessionGroup(sessionGroup); | |
bbb3538a | 65 | |
c56972bb BH |
66 | if (dialog.open() != Window.OK) { |
67 | return null; | |
68 | } | |
6503ae0f | 69 | |
c56972bb BH |
70 | final String sessionName = dialog.getSessionName(); |
71 | final String sessionPath = dialog.isDefaultSessionPath() ? null : dialog.getSessionPath(); | |
6503ae0f | 72 | |
c56972bb BH |
73 | Job job = new Job(Messages.TraceControl_CreateSessionJob) { |
74 | @Override | |
75 | protected IStatus run(IProgressMonitor monitor) { | |
76 | try { | |
77 | sessionGroup.createSession(sessionName, sessionPath, monitor); | |
78 | } catch (ExecutionException e) { | |
f455db37 | 79 | return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_CreateSessionFailure, e); |
c56972bb BH |
80 | } |
81 | return Status.OK_STATUS; | |
82 | } | |
83 | }; | |
84 | job.setUser(true); | |
85 | job.schedule(); | |
86 | } finally { | |
87 | fLock.unlock(); | |
88 | } | |
bbb3538a BH |
89 | return null; |
90 | } | |
91 | ||
92 | /* | |
93 | * (non-Javadoc) | |
94 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
95 | */ | |
96 | @Override | |
97 | public boolean isEnabled() { | |
498704b3 BH |
98 | |
99 | // Get workbench page for the Control View | |
100 | IWorkbenchPage page = getWorkbenchPage(); | |
bbb3538a BH |
101 | if (page == null) { |
102 | return false; | |
103 | } | |
104 | ||
c56972bb | 105 | TraceSessionGroup sessionGroup = null; |
bbb3538a BH |
106 | |
107 | // Check if the session group project is selected | |
108 | ISelection selection = page.getSelection(ControlView.ID); | |
109 | if (selection instanceof StructuredSelection) { | |
110 | Object element = ((StructuredSelection) selection).getFirstElement(); | |
c56972bb BH |
111 | sessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null; |
112 | } | |
113 | ||
114 | boolean isEnabled = sessionGroup != null; | |
115 | fLock.lock(); | |
116 | try { | |
117 | fSessionGroup = null; | |
118 | if(isEnabled) { | |
119 | fSessionGroup = sessionGroup; | |
120 | } | |
121 | } finally { | |
122 | fLock.unlock(); | |
bbb3538a | 123 | } |
c56972bb | 124 | return isEnabled; |
bbb3538a BH |
125 | } |
126 | } |