Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / internal / lttng / ui / views / control / handlers / CreateChannelOnSessionHandler.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 **********************************************************************/
31a6a4e4 12package org.eclipse.linuxtools.internal.lttng.ui.views.control.handlers;
bbb3538a
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
bbb3538a
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.core.runtime.jobs.Job;
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.jface.viewers.StructuredSelection;
26import org.eclipse.jface.window.Window;
31a6a4e4
BH
27import org.eclipse.linuxtools.internal.lttng.ui.Activator;
28import org.eclipse.linuxtools.internal.lttng.ui.views.control.ControlView;
29import org.eclipse.linuxtools.internal.lttng.ui.views.control.Messages;
30import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.ICreateChannelOnSessionDialog;
31import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.TraceControlDialogFactory;
32import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceSessionState;
33import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceDomainComponent;
34import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceSessionComponent;
bbb3538a 35import org.eclipse.ui.IWorkbenchPage;
bbb3538a
BH
36
37/**
38 * <b><u>CreateChannelOnSessionHandler</u></b>
39 * <p>
40 * Command handler implementation to create a trace channel for unknown domain
41 * (on session level).
42 * </p>
43 */
498704b3 44public class CreateChannelOnSessionHandler extends BaseControlViewHandler {
bbb3538a
BH
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49 /**
50 * The session component the command is to be executed on.
51 */
52 private TraceSessionComponent fSession = null;
53
54 // ------------------------------------------------------------------------
55 // Operations
56 // ------------------------------------------------------------------------
57 /*
58 * (non-Javadoc)
59 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
60 */
61 @Override
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63
d132bcc7 64 final ICreateChannelOnSessionDialog dialog = TraceControlDialogFactory.getInstance().getCreateChannelOnSessionDialog();
bbb3538a 65
6503ae0f
BH
66 if (dialog.open() != Window.OK) {
67 return null;
68 }
bbb3538a 69
6503ae0f
BH
70 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
71 @Override
72 protected IStatus run(IProgressMonitor monitor) {
73 String errorString = null;
74
75 List<String> channelNames = new ArrayList<String>();
76 TraceDomainComponent newDomain = new TraceDomainComponent("dummy", fSession); //$NON-NLS-1$
77 channelNames.add(dialog.getChannelInfo().getName());
78 newDomain.setIsKernel(dialog.isKernel());
79
80 try {
81 newDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
82 } catch (ExecutionException e) {
83 if (errorString == null) {
84 errorString = new String();
bbb3538a 85 }
6503ae0f
BH
86 errorString += e.toString() + "\n"; //$NON-NLS-1$
87 }
88
89 // get session configuration in all cases
90 try {
91 fSession.getConfigurationFromNode(monitor);
92 } catch (ExecutionException e) {
93 if (errorString == null) {
94 errorString = new String();
bbb3538a 95 }
6503ae0f
BH
96 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
97 }
98
99 if (errorString != null) {
31a6a4e4 100 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString);
6503ae0f
BH
101 }
102 return Status.OK_STATUS;
103 }
104 };
105 job.setUser(true);
106 job.schedule();
bbb3538a
BH
107
108 return null;
109 }
110
111 /*
112 * (non-Javadoc)
113 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
114 */
115 @Override
116 public boolean isEnabled() {
498704b3
BH
117 // Get workbench page for the Control View
118 IWorkbenchPage page = getWorkbenchPage();
bbb3538a
BH
119 if (page == null) {
120 return false;
121 }
122
498704b3 123 fSession = null;
bbb3538a
BH
124
125 // Check if one session is selected
126 ISelection selection = page.getSelection(ControlView.ID);
127 if (selection instanceof StructuredSelection) {
128 StructuredSelection structered = ((StructuredSelection) selection);
129 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
130 Object element = (Object) iterator.next();
131 if (element instanceof TraceSessionComponent) {
132 // Add only TraceSessionComponents that are inactive and not destroyed
133 TraceSessionComponent session = (TraceSessionComponent) element;
134 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
135 fSession = session;
136 }
137 }
138 }
139 }
140 return fSession != null;
141 }
142}
This page took 0.053238 seconds and 5 git commands to generate.