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