Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / dialogs / ConfigureMarkersDialog.java
CommitLineData
e8d771d5
BH
1/*******************************************************************************
2 * Copyright (c) 2011 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 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
12 *
13 *******************************************************************************/
14package org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs;
15
16import java.util.HashMap;
17import java.util.Map;
18import java.util.concurrent.TimeUnit;
19
6c13869b
FC
20import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TargetResource;
21import org.eclipse.linuxtools.lttng.core.tracecontrol.service.ILttControllerService;
e8d771d5
BH
22import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
23import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
24import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
25import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
26import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
27import org.eclipse.rse.ui.SystemBasePlugin;
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.graphics.Point;
30import org.eclipse.swt.layout.FillLayout;
31import org.eclipse.swt.layout.GridData;
32import org.eclipse.swt.layout.GridLayout;
33import org.eclipse.swt.widgets.Button;
34import org.eclipse.swt.widgets.Composite;
35import org.eclipse.swt.widgets.Dialog;
36import org.eclipse.swt.widgets.Display;
37import org.eclipse.swt.widgets.Event;
38import org.eclipse.swt.widgets.Label;
39import org.eclipse.swt.widgets.Listener;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.swt.widgets.Table;
42import org.eclipse.swt.widgets.TableColumn;
43import org.eclipse.swt.widgets.TableItem;
44import org.eclipse.tm.tcf.protocol.IToken;
45import org.eclipse.tm.tcf.util.TCFTask;
46
47/**
48 * <b><u>ConfigureMarkersDialog</u></b>
49 * <p>
50 * Dialog box to configure markers for a given target resource.
51 * </p>
52 */
53public class ConfigureMarkersDialog extends Dialog {
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
59 private String[] fMarkersList;
60 private TargetResource fTarget;
61 private Boolean fOkClicked;
62 Map<String, Boolean> fMap;
63 private TableItem[] fTableLines;
64 private Boolean[] fInitialMarkersStates;
65
66 private TraceSubSystem fSubSystem;
67
68 // ------------------------------------------------------------------------
69 // Constructors
70 // ------------------------------------------------------------------------
71
72 /**
73 * Constructor
74 *
75 * @param parent The parent shell
76 * @param subSystem The trace SubSystem
77 */
78 public ConfigureMarkersDialog(Shell parent, TraceSubSystem subSystem) {
79 super(parent);
80 fOkClicked = false;
81 fSubSystem = subSystem;
82 }
83
84 /**
85 * Constructor
86 *
87 * @param parent The parent shell
88 * @param style The dialog box style
89 * @param subSystem The trace SubSystem
90
91 */
92 public ConfigureMarkersDialog(Shell parent, int style, TraceSubSystem subSystem) {
93 super(parent, style);
94 fOkClicked = false;
95 fSubSystem = subSystem;
96 }
97
98 // ------------------------------------------------------------------------
99 // Operations
100 // ------------------------------------------------------------------------
101 public Map<String, Boolean> open(TargetResource aTarget) {
102 fTarget = aTarget;
103
104 Shell parent = getParent();
105 final Display display = parent.getDisplay();
106 final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL | SWT.RESIZE);
107 shell.setText(Messages.ConfigureMarkersDialog_Title);
108 shell.setImage(LTTngUiPlugin.getDefault().getImage(LTTngUiPlugin.ICON_ID_CONFIG_MARKERS));
109 shell.setLayout(new FillLayout());
110 Composite composite = new Composite(shell, SWT.NONE);
111 composite.setLayout(new GridLayout());
112
113 try {
114
115 final ILttControllerService service = fSubSystem.getControllerService();
116
117 // Create future task
118 fMarkersList = new TCFTask<String[]>() {
119 @Override
120 public void run() {
121
122 // Get markers using Lttng controller service proxy
123 service.getMarkers(fTarget.getParent().getName(), fTarget.getName(), new ILttControllerService.DoneGetMarkers() {
124
125 @Override
126 public void doneGetMarkers(IToken token, Exception error, String[] str) {
127 if (error != null) {
128 // Notify with error
129 error(error);
130 return;
131 }
132
133 // Notify about success
134 done(str);
135 }
136 });
137 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
138 } catch (Exception e) {
139 SystemMessageException sysExp;
140 if (e instanceof SystemMessageException) {
141 sysExp = (SystemMessageException)e;
142 } else {
143 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
144 }
145 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorGetMarkers + " (" + //$NON-NLS-1$
146 Messages.Lttng_Resource_Target + ": " + fTarget.getName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
147 return null;
148 }
149
150 for (int i = 0; i < fMarkersList.length; i++) {
151 fMarkersList[i] = fMarkersList[i].trim();
152 }
153 final Table table = new Table(composite, SWT.BORDER | SWT.CHECK);
154 TableColumn tc0 = new TableColumn(table, SWT.LEFT | SWT.CENTER);
155 TableColumn tc1 = new TableColumn(table, SWT.LEFT);
156 TableColumn tc2 = new TableColumn(table, SWT.LEFT);
157 TableColumn tc3 = new TableColumn(table, SWT.LEFT);
158 TableColumn tc4 = new TableColumn(table, SWT.LEFT);
159 TableColumn tc5 = new TableColumn(table, SWT.LEFT);
160 TableColumn tc6 = new TableColumn(table, SWT.LEFT);
161 tc1.setText(Messages.ConfigureMarkersDialog_NameColumn);
162 tc2.setText(Messages.ConfigureMarkersDialog_Location);
163 tc3.setText(Messages.ConfigureMarkersDialog_Format);
164 tc4.setText(Messages.ConfigureMarkersDialog_EventId);
165 tc5.setText(Messages.ConfigureMarkersDialog_Call);
166 tc6.setText(Messages.ConfigureMarkersDialog_Probe_Single);
167 tc0.setWidth(25);
168 tc1.setWidth(100);
169 tc2.setWidth(100);
170 tc3.setWidth(170);
171 tc4.pack();
172 tc5.setWidth(100);
173 tc6.setWidth(100);
174 table.setHeaderVisible(true);
175 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
176 fTableLines = new TableItem[fMarkersList.length];
177 fInitialMarkersStates = new Boolean[fMarkersList.length];
178
179 for (int i = 0; i < fMarkersList.length; i++) {
180 fTableLines[i] = new TableItem(table, SWT.NONE);
181 String markerInfoResult = null;
182 final String currentMarker = fMarkersList[i];
183
184 try {
185
186 final ILttControllerService service = fSubSystem.getControllerService();
187
188 // Create future task
189 markerInfoResult = new TCFTask<String>() {
190 @Override
191 public void run() {
192
193 // Get marker info using Lttng controller service proxy
194 service.getMarkerInfo(fTarget.getParent().getName(), fTarget.getName(), currentMarker, new ILttControllerService.DoneGetMarkerInfo() {
195
196 @Override
197 public void doneGetMarkerInfo(IToken token, Exception error, String str) {
198 if (error != null) {
199 // Notify with error
200 error(error);
201 return;
202 }
203
204 // Notify about success
205 done(str);
206 }
207 });
208 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
209 } catch (Exception e) {
210 SystemMessageException sysExp;
211 if (e instanceof SystemMessageException) {
212 sysExp = (SystemMessageException)e;
213 } else {
214 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
215 }
216 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorGetMarkerInfo + " (" + //$NON-NLS-1$
217 Messages.Lttng_Resource_Target + ": " + fTarget.getName() + ", " + //$NON-NLS-1$ //$NON-NLS-2$
218 Messages.Lttng_Resource_Marker + ": " + currentMarker + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
219 return null;
220 }
221
222 String markerInfos = markerInfoResult.substring(1, markerInfoResult.length() - 1);
223 // System.out.println("markerInfos " + markerInfos);
224
225 // HACK!!!
226 // BAD : payload CAN contain comma!!!
227 /*
228 * String[] infosList = markerInfos.split(","); for(int j=0 ; j<infosList.length ; j++) { infosList[j] = infosList[j].trim(); String[] tempTable = infosList[j].split("="); infosList[j] = tempTable[1]; System.out.print(infosList[j] + " ");
229 * } System.out.println("");
230 */
231
232 // QUICK FIX :
233 int nbOfEqualsHack = 0;
234 for (int x = 0; x < markerInfos.length(); x++) {
235 if (markerInfos.charAt(x) == '=') {
236 nbOfEqualsHack++;
237 }
238 }
239
240 if (nbOfEqualsHack > 0) {
241 String[] infosList = new String[nbOfEqualsHack];
242 @SuppressWarnings("unused")
243 String name = ""; //$NON-NLS-1$
244 String value = ""; //$NON-NLS-1$
245
246 int prevPos = 0;
247 int curPos = 0;
248 int eqPos = 0;
249 int nbDone = 0;
250
251 while ((curPos < markerInfos.length()) && (eqPos >= 0)) {
252 eqPos = markerInfos.indexOf("=", curPos); //$NON-NLS-1$
253
254 if (eqPos >= 0) {
255 name = markerInfos.substring(curPos, eqPos - 1);
256
257 prevPos = markerInfos.lastIndexOf(",", eqPos); //$NON-NLS-1$
258 } else {
259 prevPos = markerInfos.length() - 1;
260 }
261
262 if (prevPos >= 0) {
263 value = markerInfos.substring(curPos, prevPos);
264
265 infosList[nbDone] = value;
266 nbDone++;
267 }
268 curPos = eqPos + 1;
269 }
270
271 fTableLines[i].setText(new String[] { null, fMarkersList[i], infosList[3], infosList[4], infosList[2], infosList[0], infosList[5] });
272
273 if (infosList[1].compareTo("1") == 0) { //$NON-NLS-1$
274 fTableLines[i].setChecked(true);
275 fInitialMarkersStates[i] = true;
276 } else {
277 fTableLines[i].setForeground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
278 fInitialMarkersStates[i] = false;
279 }
280 }
281 }
282
283 table.addListener(SWT.Selection, new Listener() {
284 @Override
285 public void handleEvent(Event event) {
286 if (event.detail == SWT.CHECK) {
287 TableItem ti = (TableItem) event.item;
288 if (!ti.getChecked()) {
289 ti.setForeground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
290 } else {
291 ti.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
292 }
293 }
294 }
295 });
296
297 final Composite buttonComposite = new Composite(composite, SWT.NONE);
298 buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
299 GridLayout gl = new GridLayout(4, false);
300 gl.verticalSpacing = 10;
301 buttonComposite.setLayout(gl);
302
303 Label shadow_sep_h = new Label(buttonComposite, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
304 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
305 shadow_sep_h.setLayoutData(gd);
306
307 Button selectAllButton = new Button(buttonComposite, SWT.PUSH);
308 selectAllButton.setText(Messages.ConfigureMarkersDialog_Select_All);
309 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
310 gd.widthHint = 100;
311 selectAllButton.setLayoutData(gd);
312 selectAllButton.addListener(SWT.Selection, new Listener() {
313 @Override
314 public void handleEvent(Event e) {
315 for (int i = 0; i < fTableLines.length; i++) {
316 fTableLines[i].setChecked(true);
317 fTableLines[i].setForeground(display.getSystemColor(SWT.COLOR_BLACK));
318 }
319 }
320 });
321
322 Button deselectAllButton = new Button(buttonComposite, SWT.PUSH);
323 deselectAllButton.setText(Messages.ConfigureMarkersDialog_Deselect_All);
324 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
325 gd.widthHint = 100;
326 deselectAllButton.setLayoutData(gd);
327 deselectAllButton.addListener(SWT.Selection, new Listener() {
328 @Override
329 public void handleEvent(Event e) {
330 for (int i = 0; i < fTableLines.length; i++) {
331 fTableLines[i].setChecked(false);
332 fTableLines[i].setForeground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
333 }
334 }
335 });
336
337 Button cancelButton = new Button(buttonComposite, SWT.PUSH);
338 cancelButton.setText(Messages.ConfigureMarkersDialog_Cancel);
339 gd = new GridData();
340 gd = new GridData(SWT.RIGHT, SWT.CENTER, true, false);
341 gd.widthHint = 100;
342 cancelButton.setLayoutData(gd);
343 cancelButton.addListener(SWT.Selection, new Listener() {
344 @Override
345 public void handleEvent(Event e) {
346 fOkClicked = Boolean.valueOf(false);
347 shell.dispose();
348 }
349 });
350
351 Button okButton = new Button(buttonComposite, SWT.PUSH);
352 okButton.setText(Messages.ConfigureMarkersDialog_Ok);
353 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
354 gd.widthHint = 100;
355 okButton.setLayoutData(gd);
356 okButton.addListener(SWT.Selection, new Listener() {
357 @Override
358 public void handleEvent(Event e) {
359 fOkClicked = Boolean.valueOf(true);
360 fMap = new HashMap<String, Boolean>();
361 for (int k = 0; k < fTableLines.length; k++) {
362 Boolean isChecked = fTableLines[k].getChecked();
363 if (isChecked.booleanValue() != fInitialMarkersStates[k].booleanValue()) {
364 if (isChecked) {
365 fMap.put(fMarkersList[k], Boolean.valueOf(true));
366 } else {
367 fMap.put(fMarkersList[k], Boolean.valueOf(false));
368 }
369 }
370 }
371 shell.dispose();
372 }
373 });
374
375 shell.addListener(SWT.Traverse, new Listener() {
376 @Override
377 public void handleEvent(Event event) {
378 if (event.detail == SWT.TRAVERSE_ESCAPE) {
379 event.doit = false;
380 }
381 }
382 });
383
384 TableItem[] items = table.getItems();
385 for (int i = 0; i < items.length; i++) {
386 if ((i % 2) != 0) {
387 items[i].setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND));
388 }
389 }
390
391 Point minSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
392 shell.setMinimumSize(shell.computeSize(minSize.x, minSize.y).x, 200);
393 shell.setSize(shell.computeSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300));
394 shell.open();
395
396 while (!shell.isDisposed()) {
397 if (!display.readAndDispatch()) {
398 display.sleep();
399 }
400 }
401 if (!fOkClicked) {
402 return null;
403 }
404
405 return fMap;
406 }
407}
This page took 0.038848 seconds and 5 git commands to generate.