Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / CreateSessionHandler.java
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.lttng.ui.views.control.handlers;
13
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;
23 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
24 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
25 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
26 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.CreateSessionDialog;
27 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateSessionDialog;
28 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionGroup;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32
33 /**
34 * <b><u>CreateSessionHandler</u></b>
35 * <p>
36 * Command handler implementation to create a trace session.
37 * </p>
38 */
39 public class CreateSessionHandler extends BaseControlViewHandler {
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44 /**
45 * The trace session group the command is to be executed on.
46 */
47 private TraceSessionGroup fSessionGroup = null;
48
49 // ------------------------------------------------------------------------
50 // Operations
51 // ------------------------------------------------------------------------
52 /*
53 * (non-Javadoc)
54 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
55 */
56 @Override
57 public Object execute(ExecutionEvent event) throws ExecutionException {
58 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
59 if (window == null) {
60 return false;
61 }
62
63 // Open dialog box for the node name and address
64 ICreateSessionDialog dialog = new CreateSessionDialog(window.getShell(), fSessionGroup);
65
66 if (dialog.open() != Window.OK) {
67 return null;
68 }
69
70 final String sessionName = dialog.getSessionName();
71 final String sessionPath = dialog.isDefaultSessionPath() ? null : dialog.getSessionPath();
72
73 Job job = new Job(Messages.TraceControl_CreateSessionJob) {
74 @Override
75 protected IStatus run(IProgressMonitor monitor) {
76 try {
77 fSessionGroup.createSession(sessionName, sessionPath, monitor);
78 } catch (ExecutionException e) {
79 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString());
80 }
81 return Status.OK_STATUS;
82 }
83 };
84 job.setUser(true);
85 job.schedule();
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
97 // Get workbench page for the Control View
98 IWorkbenchPage page = getWorkbenchPage();
99 if (page == null) {
100 return false;
101 }
102
103 fSessionGroup = null;
104
105 // Check if the session group project is selected
106 ISelection selection = page.getSelection(ControlView.ID);
107 if (selection instanceof StructuredSelection) {
108 Object element = ((StructuredSelection) selection).getFirstElement();
109 fSessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null;
110 }
111 return fSessionGroup != null;
112 }
113 }
This page took 0.032945 seconds and 6 git commands to generate.