Commit | Line | Data |
---|---|---|
fbd124dd BH |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010, 2011 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation\r | |
11 | * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code\r | |
12 | * Bernd Hufmann - Adapted to new messages file, fixed warnings\r | |
13 | *******************************************************************************/\r | |
14 | package org.eclipse.linuxtools.lttng.ui.views.latency.dialogs;\r | |
15 | \r | |
16 | import java.util.Vector;\r | |
17 | \r | |
5945cec9 FC |
18 | import org.eclipse.linuxtools.internal.lttng.core.latency.analyzer.EventMatcher;\r |
19 | import org.eclipse.linuxtools.internal.lttng.core.util.EventsPair;\r | |
fbd124dd BH |
20 | import org.eclipse.linuxtools.lttng.ui.views.latency.Messages;\r |
21 | import org.eclipse.swt.SWT;\r | |
22 | import org.eclipse.swt.layout.GridData;\r | |
23 | import org.eclipse.swt.layout.GridLayout;\r | |
24 | import org.eclipse.swt.widgets.Button;\r | |
25 | import org.eclipse.swt.widgets.Composite;\r | |
26 | import org.eclipse.swt.widgets.Control;\r | |
27 | import org.eclipse.swt.widgets.Event;\r | |
28 | import org.eclipse.swt.widgets.Listener;\r | |
29 | import org.eclipse.swt.widgets.Shell;\r | |
30 | import org.eclipse.swt.widgets.Table;\r | |
31 | import org.eclipse.swt.widgets.TableColumn;\r | |
32 | import org.eclipse.swt.widgets.TableItem;\r | |
33 | \r | |
34 | /**\r | |
35 | * <b><u>AddDialog</u></b>\r | |
36 | * <p>\r | |
37 | * Add dialog, lets the user add custom start/end event pairs.\r | |
38 | * \r | |
39 | * @author Philippe Sawicki\r | |
40 | */\r | |
41 | public class AddDialog extends AbstractDialog {\r | |
42 | \r | |
43 | // ------------------------------------------------------------------------\r | |
44 | // Attributes\r | |
45 | // ------------------------------------------------------------------------\r | |
46 | \r | |
47 | /**\r | |
48 | * The dialog's start table.\r | |
49 | */\r | |
50 | protected Table fStartTable;\r | |
51 | \r | |
52 | /**\r | |
53 | * The dialog's end table.\r | |
54 | */\r | |
55 | protected Table fEndTable;\r | |
56 | \r | |
57 | /**\r | |
58 | * The dialog's list table.\r | |
59 | */\r | |
60 | protected Table fListTable;\r | |
61 | \r | |
62 | /**\r | |
63 | * Start table columns.\r | |
64 | */\r | |
65 | protected TableColumn[] fStartColumns;\r | |
66 | \r | |
67 | /**\r | |
68 | * End table columns.\r | |
69 | */\r | |
70 | protected TableColumn[] fEndColumns;\r | |
71 | \r | |
72 | /**\r | |
73 | * List table columns.\r | |
74 | */\r | |
75 | protected TableColumn[] fListColumns;\r | |
76 | \r | |
77 | /**\r | |
78 | * Start table column names (header titles).\r | |
79 | */\r | |
c6f55e56 | 80 | static final String[] START_COLUMN_NAMES = { "", Messages.LatencyView_Dialogs_AddEvents_Columns_Start }; //$NON-NLS-1$\r |
fbd124dd BH |
81 | \r |
82 | /**\r | |
83 | * End table column names (header titles).\r | |
84 | */\r | |
c6f55e56 | 85 | static final String[] END_COLUMN_NAMES = { "", Messages.LatencyView_Dialogs_AddEvents_Columns_End }; //$NON-NLS-1$\r |
fbd124dd BH |
86 | \r |
87 | /**\r | |
88 | * List table column names (header titles).\r | |
89 | */\r | |
c6f55e56 | 90 | static final String[] LIST_COLUMN_NAMES = {\r |
fbd124dd BH |
91 | "#", //$NON-NLS-1$\r |
92 | Messages.LatencyView_Dialogs_AddEvents_Columns_List_Trigger,\r | |
93 | Messages.LatencyView_Dialogs_AddEvents_Columns_List_End };\r | |
94 | \r | |
95 | /**\r | |
96 | * Column widths.\r | |
97 | */\r | |
c6f55e56 | 98 | static final int[] COLUMN_WIDTHS = { 25, 250, 250 };\r |
fbd124dd BH |
99 | \r |
100 | /**\r | |
101 | * Possible event types.\r | |
102 | */\r | |
103 | protected Vector<String> fEventTypes = new Vector<String>();\r | |
104 | \r | |
105 | /**\r | |
106 | * Start event types.\r | |
107 | */\r | |
108 | protected Vector<String> fEventStartTypes;\r | |
109 | \r | |
110 | /**\r | |
111 | * End event types.\r | |
112 | */\r | |
113 | protected Vector<String> fEventEndTypes;\r | |
114 | \r | |
115 | /**\r | |
116 | * Selected start type.\r | |
117 | */\r | |
118 | protected String fStartType;\r | |
119 | \r | |
120 | /**\r | |
121 | * Selected end type.\r | |
122 | */\r | |
123 | protected String fEndType;\r | |
124 | \r | |
125 | // ------------------------------------------------------------------------\r | |
126 | // Constructors\r | |
127 | // ------------------------------------------------------------------------\r | |
128 | \r | |
129 | /**\r | |
130 | * Constructor.\r | |
131 | * @param parentShell\r | |
132 | * The parent shell.\r | |
133 | * @param title\r | |
134 | * The dialog's window title.\r | |
135 | * @param message\r | |
136 | * The dialog's window message.\r | |
137 | */\r | |
138 | public AddDialog(Shell parentShell, String title, String message) {\r | |
139 | super(parentShell, title, message);\r | |
140 | \r | |
141 | // Get the possible events from the list\r | |
142 | fEventTypes = EventMatcher.getInstance().getTypeList();\r | |
143 | \r | |
144 | // Get the list of start and end types from the EventMatcher\r | |
145 | EventsPair pair = getMatchPairs();\r | |
146 | fEventStartTypes = pair.getFirst();\r | |
147 | fEventEndTypes = pair.getSecond();\r | |
148 | }\r | |
149 | \r | |
150 | // ------------------------------------------------------------------------\r | |
151 | // Operations\r | |
152 | // ------------------------------------------------------------------------\r | |
153 | \r | |
154 | /**\r | |
155 | * Creates the start table's columns (i.e. the table header).\r | |
156 | */\r | |
157 | protected void createStartColumns() {\r | |
158 | fStartColumns = new TableColumn[START_COLUMN_NAMES.length];\r | |
159 | for (int i = 0; i < START_COLUMN_NAMES.length; i++) {\r | |
160 | fStartColumns[i] = new TableColumn(fStartTable, SWT.LEFT);\r | |
161 | fStartColumns[i].setText(START_COLUMN_NAMES[i]);\r | |
162 | fStartColumns[i].setWidth(COLUMN_WIDTHS[i]);\r | |
163 | }\r | |
164 | }\r | |
165 | \r | |
166 | /**\r | |
167 | * Creates the end table's columns (i.e. the table header).\r | |
168 | */\r | |
169 | protected void createEndColumns() {\r | |
170 | fEndColumns = new TableColumn[END_COLUMN_NAMES.length];\r | |
171 | for (int i = 0; i < END_COLUMN_NAMES.length; i++) {\r | |
172 | fEndColumns[i] = new TableColumn(fEndTable, SWT.LEFT);\r | |
173 | fEndColumns[i].setText(END_COLUMN_NAMES[i]);\r | |
174 | fEndColumns[i].setWidth(COLUMN_WIDTHS[i]);\r | |
175 | }\r | |
176 | }\r | |
177 | \r | |
178 | /**\r | |
179 | * Creates the list table's columns (i.e. the table header).\r | |
180 | */\r | |
181 | protected void createListColumns() {\r | |
182 | fListColumns = new TableColumn[LIST_COLUMN_NAMES.length];\r | |
183 | for (int i = 0; i < LIST_COLUMN_NAMES.length; i++) {\r | |
184 | fListColumns[i] = new TableColumn(fListTable, SWT.LEFT);\r | |
185 | fListColumns[i].setText(LIST_COLUMN_NAMES[i]);\r | |
186 | fListColumns[i].setWidth(COLUMN_WIDTHS[i]);\r | |
187 | }\r | |
188 | }\r | |
189 | \r | |
190 | /**\r | |
191 | * Creates the start column list.\r | |
192 | * @param parent\r | |
193 | * The parent composite.\r | |
194 | */\r | |
195 | protected void createStartColumn(Composite parent) {\r | |
196 | final int style = SWT.SINGLE | SWT.CHECK | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL;\r | |
197 | GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);\r | |
198 | fStartTable = new Table(parent, style);\r | |
199 | fStartTable.setLayoutData(layoutData);\r | |
200 | \r | |
201 | // Some cosmetic enhancements\r | |
202 | fStartTable.setHeaderVisible(true);\r | |
203 | fStartTable.setLinesVisible(true);\r | |
204 | \r | |
205 | createStartColumns();\r | |
206 | \r | |
207 | for (int i = 0; i < fEventTypes.size(); i++) {\r | |
208 | TableItem item = new TableItem(fStartTable, SWT.RIGHT);\r | |
209 | \r | |
210 | String[] columns = { fEventTypes.get(i), fEventTypes.get(i) };\r | |
211 | \r | |
212 | item.setText(columns);\r | |
213 | }\r | |
214 | \r | |
215 | fStartTable.setItemCount(fEventTypes.size());\r | |
216 | \r | |
217 | fStartTable.addListener(SWT.Selection, new Listener() {\r | |
218 | @Override\r | |
219 | public void handleEvent(Event event) {\r | |
220 | if (event.detail == SWT.CHECK) {\r | |
221 | TableItem[] items = fStartTable.getItems();\r | |
222 | for (TableItem item : items) {\r | |
223 | if (item != event.item) {\r | |
224 | item.setChecked(false);\r | |
225 | }\r | |
226 | }\r | |
227 | }\r | |
228 | }\r | |
229 | });\r | |
230 | }\r | |
231 | \r | |
232 | /**\r | |
233 | * Creates the end column list.\r | |
234 | * @param parent\r | |
235 | * The parent composite.\r | |
236 | */\r | |
237 | protected void createEndColumn(Composite parent) {\r | |
238 | final int style = SWT.SINGLE | SWT.CHECK | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL;\r | |
239 | GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);\r | |
240 | fEndTable = new Table(parent, style);\r | |
241 | fEndTable.setLayoutData(layoutData);\r | |
242 | \r | |
243 | // Some cosmetic enhancements\r | |
244 | fEndTable.setHeaderVisible(true);\r | |
245 | fEndTable.setLinesVisible(true);\r | |
246 | \r | |
247 | createEndColumns();\r | |
248 | \r | |
249 | for (int i = 0; i < fEventTypes.size(); i++) {\r | |
250 | TableItem item = new TableItem(fEndTable, SWT.RIGHT);\r | |
251 | \r | |
252 | String[] columns = { fEventTypes.get(i), fEventTypes.get(i) };\r | |
253 | \r | |
254 | item.setText(columns);\r | |
255 | }\r | |
256 | \r | |
257 | fEndTable.setItemCount(fEventTypes.size());\r | |
258 | \r | |
259 | fEndTable.addListener(SWT.Selection, new Listener() {\r | |
260 | @Override\r | |
261 | public void handleEvent(Event event) {\r | |
262 | if (event.detail == SWT.CHECK) {\r | |
263 | TableItem[] items = fEndTable.getItems();\r | |
264 | for (TableItem item : items) {\r | |
265 | if (item != event.item) {\r | |
266 | item.setChecked(false);\r | |
267 | }\r | |
268 | }\r | |
269 | }\r | |
270 | }\r | |
271 | });\r | |
272 | }\r | |
273 | \r | |
274 | /**\r | |
275 | * Creates the list column for already existing event pairs.\r | |
276 | * @param parent\r | |
277 | * The parent composite.\r | |
278 | */\r | |
279 | protected void createListColumn(Composite parent) {\r | |
280 | final int style = SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL;\r | |
281 | GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);\r | |
282 | layoutData.horizontalSpan = 2;\r | |
283 | fListTable = new Table(parent, style);\r | |
284 | fListTable.setLayoutData(layoutData);\r | |
285 | \r | |
286 | // Some cosmetic enhancements\r | |
287 | fListTable.setHeaderVisible(true);\r | |
288 | fListTable.setLinesVisible(true);\r | |
289 | \r | |
290 | createListColumns();\r | |
291 | \r | |
292 | for (int i = 0; i < fEventStartTypes.size(); i++) {\r | |
293 | TableItem item = new TableItem(fListTable, SWT.RIGHT);\r | |
294 | \r | |
295 | String max = String.valueOf(fEventStartTypes.size());\r | |
296 | String number = formatListNumber(i + 1, max.length());\r | |
297 | \r | |
298 | String[] columns = { number, fEventStartTypes.get(i), fEventEndTypes.get(i) };\r | |
299 | \r | |
300 | item.setText(columns);\r | |
301 | }\r | |
302 | \r | |
303 | fListTable.setItemCount(103);\r | |
304 | fListTable.remove(fEventTypes.size(), 103 - 1);\r | |
305 | }\r | |
306 | \r | |
307 | /*\r | |
308 | * (non-Javadoc)\r | |
309 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.dialogs.AbstractDialog#createDialogArea(org.eclipse.swt.widgets.Composite)\r | |
310 | */\r | |
311 | @Override\r | |
312 | protected Control createDialogArea(Composite parent) {\r | |
313 | GridLayout layout = new GridLayout(2, true);\r | |
314 | parent.setLayout(layout);\r | |
315 | \r | |
316 | createStartColumn(parent);\r | |
317 | createEndColumn(parent);\r | |
318 | createListColumn(parent);\r | |
319 | \r | |
320 | return parent;\r | |
321 | }\r | |
322 | \r | |
323 | /*\r | |
324 | * (non-Javadoc)\r | |
325 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.dialogs.AbstractDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)\r | |
326 | */\r | |
327 | @Override\r | |
328 | protected void createButtonsForButtonBar(Composite parent) {\r | |
329 | GridData gridData = new GridData();\r | |
330 | gridData.verticalAlignment = GridData.FILL;\r | |
331 | gridData.horizontalSpan = 1;\r | |
332 | gridData.grabExcessHorizontalSpace = true;\r | |
333 | gridData.grabExcessVerticalSpace = true;\r | |
334 | gridData.horizontalAlignment = SWT.RIGHT;\r | |
335 | \r | |
336 | parent.setLayoutData(gridData);\r | |
337 | \r | |
338 | // Create the "Add" button\r | |
339 | Button addButton = createButton(parent, ADD, Messages.LatencyView_Dialogs_AddEvents_Buttons_Add, false);\r | |
340 | addButton.addListener(SWT.Selection, new Listener() {\r | |
341 | @Override\r | |
342 | public void handleEvent(Event event) {\r | |
343 | if (isValidInput()) {\r | |
344 | // Add the event pair to the EventMatcher and save the pairs\r | |
345 | EventMatcher.getInstance().addMatch(fStartType, fEndType);\r | |
346 | fEventStartTypes.add(fStartType);\r | |
347 | fEventEndTypes.add(fEndType);\r | |
348 | saveMatchPairs(fEventStartTypes, fEventEndTypes);\r | |
349 | \r | |
350 | EventsPair pairs = EventMatcher.getInstance().getEvents();\r | |
351 | fEventStartTypes = pairs.getFirst();\r | |
352 | fEventEndTypes = pairs.getSecond();\r | |
353 | \r | |
354 | fListTable.removeAll();\r | |
355 | \r | |
356 | for (int i = 0; i < fEventStartTypes.size(); i++) {\r | |
357 | TableItem item = new TableItem(fListTable, SWT.RIGHT);\r | |
358 | \r | |
359 | String max = String.valueOf(fEventStartTypes.size());\r | |
360 | String number = formatListNumber(i + 1, max.length());\r | |
361 | \r | |
362 | String[] columns = { number, fEventStartTypes.get(i), fEventEndTypes.get(i) };\r | |
363 | \r | |
364 | item.setText(columns);\r | |
365 | }\r | |
366 | \r | |
367 | saveMatchPairs(fEventStartTypes, fEventEndTypes);\r | |
368 | }\r | |
369 | \r | |
370 | fRedrawGraphs = true;\r | |
371 | }\r | |
372 | });\r | |
373 | \r | |
374 | // Create the "Close" button\r | |
375 | Button closeButton = createButton(parent, CANCEL, Messages.LatencyView_Dialogs_AddEvents_Buttons_Close, false);\r | |
376 | closeButton.addListener(SWT.Selection, new Listener() {\r | |
377 | @Override\r | |
378 | public void handleEvent(Event event) {\r | |
379 | setReturnCode(CANCEL);\r | |
380 | \r | |
381 | if (fRedrawGraphs == true)\r | |
382 | redrawGraphs();\r | |
383 | \r | |
384 | close();\r | |
385 | }\r | |
386 | });\r | |
387 | }\r | |
388 | \r | |
389 | /**\r | |
390 | * Validate the list before adding event pairs.\r | |
391 | * @return "true" if the input is valid, "false" otherwise.\r | |
392 | */\r | |
393 | protected boolean isValidInput() {\r | |
394 | // Remove the previous error message\r | |
395 | setErrorMessage(null);\r | |
396 | \r | |
397 | boolean valid = true;\r | |
398 | \r | |
399 | // Check if an item from the start list is selected\r | |
400 | TableItem[] items = fStartTable.getItems();\r | |
401 | fStartType = null;\r | |
402 | boolean startHasSelectedItem = false;\r | |
403 | for (int i = 0; i < items.length && !startHasSelectedItem; i++) {\r | |
404 | if (items[i].getChecked() == true) {\r | |
405 | fStartType = items[i].getText();\r | |
406 | startHasSelectedItem = true;\r | |
407 | }\r | |
408 | }\r | |
409 | \r | |
410 | // Check if an item from the end list is selected\r | |
411 | items = fEndTable.getItems();\r | |
412 | fEndType = null;\r | |
413 | boolean endHasSelectedItem = false;\r | |
414 | for (int i = 0; i < items.length && !endHasSelectedItem; i++) {\r | |
415 | if (items[i].getChecked() == true) {\r | |
416 | fEndType = items[i].getText();\r | |
417 | endHasSelectedItem = true;\r | |
418 | }\r | |
419 | }\r | |
420 | \r | |
421 | // Print error message if needed.\r | |
422 | if (!startHasSelectedItem && !endHasSelectedItem) {\r | |
423 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_NoSelection);\r | |
424 | valid = false;\r | |
425 | } else if (!startHasSelectedItem) {\r | |
426 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_StartNotSelected);\r | |
427 | valid = false;\r | |
428 | } else if (!endHasSelectedItem) {\r | |
429 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_EndNotSelected);\r | |
430 | valid = false;\r | |
431 | }\r | |
432 | \r | |
433 | // Check if the same item is selected in both lists\r | |
434 | if (startHasSelectedItem && endHasSelectedItem) {\r | |
435 | if (fStartType.equalsIgnoreCase(fEndType)) {\r | |
436 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_SameSelected);\r | |
437 | valid = false;\r | |
438 | }\r | |
439 | }\r | |
440 | \r | |
441 | // Check if the selected item is already in the list\r | |
442 | if (startHasSelectedItem && endHasSelectedItem) {\r | |
443 | EventsPair pairs = getMatchPairs();\r | |
444 | Vector<String> startEvents = pairs.getFirst();\r | |
445 | Vector<String> endEvents = pairs.getSecond();\r | |
446 | \r | |
447 | boolean startAlreadyUsed = false;\r | |
448 | boolean endAlreadyUsed = false;\r | |
449 | boolean startAsEndAlreadyUsed = false;\r | |
450 | boolean endAsStartAlreadyUsed = false;\r | |
451 | \r | |
452 | if (startEvents.contains(fStartType)) {\r | |
453 | startAlreadyUsed = true;\r | |
454 | }\r | |
455 | if (endEvents.contains(fEndType)) {\r | |
456 | endAlreadyUsed = true;\r | |
457 | }\r | |
458 | if (startEvents.contains(fEndType)) {\r | |
459 | endAsStartAlreadyUsed = true;\r | |
460 | }\r | |
461 | if (endEvents.contains(fStartType)) {\r | |
462 | startAsEndAlreadyUsed = true;\r | |
463 | }\r | |
464 | \r | |
465 | if (startAlreadyUsed && endAlreadyUsed) {\r | |
466 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_AlreadyMatched);\r | |
467 | valid = false;\r | |
468 | } else if (startAlreadyUsed) {\r | |
469 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_StartAlreadyMatched);\r | |
470 | valid = false;\r | |
471 | } else if (endAlreadyUsed) {\r | |
472 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_EndAlreadyMatched);\r | |
473 | valid = false;\r | |
474 | }\r | |
475 | \r | |
476 | if (startAsEndAlreadyUsed) {\r | |
477 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_StartAsEnd);\r | |
478 | valid = false;\r | |
479 | }\r | |
480 | if (endAsStartAlreadyUsed) {\r | |
481 | setErrorMessage(Messages.LatencyView_Dialogs_AddEvents_Errors_EndAsStart);\r | |
482 | valid = false;\r | |
483 | }\r | |
484 | }\r | |
485 | \r | |
486 | return valid;\r | |
487 | }\r | |
488 | } |