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.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
25 import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
26 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
27 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.CreateSessionDialog;
28 import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.ICreateSessionDialog;
29 import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionGroup;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import 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 */
41 public 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 return null;
70 }
71
72 final String sessionName = dialog.getSessionName();
73 final String sessionPath = dialog.isDefaultSessionPath() ? null : dialog.getSessionPath();
74
75 Job job = new Job(Messages.TraceControl_CreateSessionJob) {
76 @Override
77 protected IStatus run(IProgressMonitor monitor) {
78 try {
79 fSessionGroup.createSession(sessionName, sessionPath, monitor);
80 } catch (ExecutionException e) {
81 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, e.toString());
82 }
83 return Status.OK_STATUS;
84 }
85 };
86 job.setUser(true);
87 job.schedule();
88
89 return null;
90 }
91
92 /*
93 * (non-Javadoc)
94 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
95 */
96 @Override
97 public boolean isEnabled() {
98 fSessionGroup = null;
99
100 // Check if we are closing down
101 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
102 if (window == null) {
103 return false;
104 }
105
106 // Check if we are in the Project View
107 IWorkbenchPage page = window.getActivePage();
108 if (page == null) {
109 return false;
110 }
111
112 IWorkbenchPart part = page.getActivePart();
113 if (!(part instanceof ControlView)) {
114 return false;
115 }
116
117 // Check if the session group project is selected
118 ISelection selection = page.getSelection(ControlView.ID);
119 if (selection instanceof StructuredSelection) {
120 Object element = ((StructuredSelection) selection).getFirstElement();
121 fSessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null;
122 }
123 return fSessionGroup != null;
124 }
125 }
This page took 0.033033 seconds and 6 git commands to generate.