lttng: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / ExecuteCommandScriptHandler.java
1 /**********************************************************************
2 * Copyright (c) 2014 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.tracecompass.internal.lttng2.control.ui.views.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.tracecompass.internal.lttng2.control.ui.Activator;
24 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
25 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.ISelectCommandScriptDialog;
26 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
27 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
28 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionGroup;
29 import org.eclipse.ui.IWorkbenchPage;
30
31 /**
32 * <p>
33 * Command handler implementation to execute commands of a command script.
34 * </p>
35 *
36 * @author Bernd Hufmann
37 */
38 public class ExecuteCommandScriptHandler extends BaseControlViewHandler {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
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 @Override
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55
56 fLock.lock();
57 try {
58 final TraceSessionGroup sessionGroup = fSessionGroup;
59
60 // Open dialog box for the node name and address
61 final ISelectCommandScriptDialog dialog = TraceControlDialogFactory.getInstance().getCommandScriptDialog();
62
63 if (dialog.open() != Window.OK) {
64 return null;
65 }
66
67 Job job = new Job(Messages.TraceControl_ExecuteScriptJob) {
68 @Override
69 protected IStatus run(IProgressMonitor monitor) {
70 try {
71 sessionGroup.executeCommands(monitor, dialog.getCommands());
72 } catch (ExecutionException e) {
73 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_CreateSessionFailure, e);
74 }
75 return Status.OK_STATUS;
76 }
77 };
78 job.setUser(true);
79 job.schedule();
80 } finally {
81 fLock.unlock();
82 }
83 return null;
84 }
85
86 @Override
87 public boolean isEnabled() {
88
89 // Get workbench page for the Control View
90 IWorkbenchPage page = getWorkbenchPage();
91 if (page == null) {
92 return false;
93 }
94
95 TraceSessionGroup sessionGroup = null;
96
97 // Check if the session group project is selected
98 ISelection selection = page.getSelection(ControlView.ID);
99 if (selection instanceof StructuredSelection) {
100 Object element = ((StructuredSelection) selection).getFirstElement();
101 sessionGroup = (element instanceof TraceSessionGroup) ? (TraceSessionGroup) element : null;
102 }
103
104 boolean isEnabled = sessionGroup != null;
105 fLock.lock();
106 try {
107 fSessionGroup = null;
108 if(isEnabled) {
109 fSessionGroup = sessionGroup;
110 }
111 } finally {
112 fLock.unlock();
113 }
114 return isEnabled;
115 }
116 }
This page took 0.033668 seconds and 5 git commands to generate.