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