Internalize API for trace control and move control to lttng2
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / CreateChannelOnDomainHandler.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.internal.lttng2.ui.views.control.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
28 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
29 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
30 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ICreateChannelDialog;
31 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory;
32 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceSessionState;
33 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceDomainComponent;
34 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
35 import org.eclipse.ui.IWorkbenchPage;
36
37 /**
38 * <b><u>CreateChannelOnDomainHandler</u></b>
39 * <p>
40 * Command handler implementation to create a trace channel for known domain.
41 * </p>
42 */
43 public class CreateChannelOnDomainHandler extends BaseControlViewHandler {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
49 * The the domain component the command is to be executed on.
50 */
51 private TraceDomainComponent fDomain;
52
53 // ------------------------------------------------------------------------
54 // Operations
55 // ------------------------------------------------------------------------
56 /*
57 * (non-Javadoc)
58 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59 */
60 @Override
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62
63 // Get channel information from user
64 final ICreateChannelDialog dialog = TraceControlDialogFactory.getInstance().getCreateChannelDialog();
65 dialog.setDomainComponent(fDomain);
66
67 if (dialog.open() != Window.OK) {
68 return null;
69 }
70
71 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
72 @Override
73 protected IStatus run(IProgressMonitor monitor) {
74 String errorString = null;
75
76 List<String> channelNames = new ArrayList<String>();
77 channelNames.add(dialog.getChannelInfo().getName());
78
79 try {
80 fDomain.enableChannels(channelNames, dialog.getChannelInfo(), monitor);
81 } catch (ExecutionException e) {
82 if (errorString == null) {
83 errorString = new String();
84 }
85 errorString += e.toString() + "\n"; //$NON-NLS-1$
86 }
87
88 // get session configuration in all cases
89 try {
90 fDomain.getConfigurationFromNode(monitor);
91 } catch (ExecutionException e) {
92 if (errorString == null) {
93 errorString = new String();
94 }
95 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
96 }
97
98 if (errorString != null) {
99 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString);
100 }
101 return Status.OK_STATUS;
102 }
103 };
104
105 job.setUser(true);
106 job.schedule();
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() {
117
118 // Get workbench page for the Control View
119 IWorkbenchPage page = getWorkbenchPage();
120 if (page == null) {
121 return false;
122 }
123 fDomain = null;
124
125 // Check if one domain 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 TraceDomainComponent) {
132 TraceDomainComponent domain = (TraceDomainComponent) element;
133 TraceSessionComponent session = (TraceSessionComponent) domain.getParent();
134 // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed
135 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
136 fDomain = domain;
137 }
138 }
139 }
140 }
141 return fDomain != null;
142 }
143 }
This page took 0.033807 seconds and 5 git commands to generate.