|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * UI for property dialogs |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.view; |
|
20 |
|
21 import java.util.List; |
|
22 |
|
23 import org.eclipse.swt.SWT; |
|
24 import org.eclipse.swt.events.ModifyEvent; |
|
25 import org.eclipse.swt.events.ModifyListener; |
|
26 import org.eclipse.swt.events.SelectionEvent; |
|
27 import org.eclipse.swt.events.SelectionListener; |
|
28 import org.eclipse.swt.layout.GridLayout; |
|
29 import org.eclipse.swt.widgets.Composite; |
|
30 |
|
31 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialog; |
|
32 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogEnabler; |
|
33 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogTemplate; |
|
34 import com.nokia.tracebuilder.engine.TraceParameterPropertyDialogTemplate; |
|
35 |
|
36 /** |
|
37 * UI for property dialogs |
|
38 * |
|
39 */ |
|
40 final class PropertyDialogUI extends Composite { |
|
41 |
|
42 /** |
|
43 * Target combo-box selection listener |
|
44 * |
|
45 */ |
|
46 private final class TargetSelectionListener implements SelectionListener { |
|
47 |
|
48 /* |
|
49 * (non-Javadoc) |
|
50 * |
|
51 * @see org.eclipse.swt.events.SelectionListener# |
|
52 * widgetSelected(org.eclipse.swt.events.SelectionEvent) |
|
53 */ |
|
54 public void widgetSelected(SelectionEvent e) { |
|
55 if (changeCallback != null) { |
|
56 changeCallback.targetChanged(getTarget()); |
|
57 } |
|
58 } |
|
59 |
|
60 /* |
|
61 * (non-Javadoc) |
|
62 * |
|
63 * @see org.eclipse.swt.events.SelectionListener# |
|
64 * widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
|
65 */ |
|
66 public void widgetDefaultSelected(SelectionEvent e) { |
|
67 widgetSelected(e); |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * Target combo-box modify listener |
|
73 * |
|
74 */ |
|
75 private final class TargetFieldModifyListener implements ModifyListener { |
|
76 |
|
77 /* |
|
78 * (non-Javadoc) |
|
79 * |
|
80 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
|
81 */ |
|
82 public void modifyText(ModifyEvent e) { |
|
83 if (changeCallback != null) { |
|
84 changeCallback.targetChanged(getTarget()); |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Type combo-box selection listener |
|
91 * |
|
92 */ |
|
93 private final class TypeSelectionListener implements SelectionListener { |
|
94 |
|
95 /* |
|
96 * (non-Javadoc) |
|
97 * |
|
98 * @see org.eclipse.swt.events.SelectionListener# |
|
99 * widgetSelected(org.eclipse.swt.events.SelectionEvent) |
|
100 */ |
|
101 public void widgetSelected(SelectionEvent e) { |
|
102 if (changeCallback != null) { |
|
103 changeCallback.typeChanged(getType()); |
|
104 } |
|
105 } |
|
106 |
|
107 /* |
|
108 * (non-Javadoc) |
|
109 * |
|
110 * @see org.eclipse.swt.events.SelectionListener# |
|
111 * widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
|
112 */ |
|
113 public void widgetDefaultSelected(SelectionEvent e) { |
|
114 widgetSelected(e); |
|
115 } |
|
116 } |
|
117 |
|
118 /** |
|
119 * Text field modification listener |
|
120 * |
|
121 */ |
|
122 private final class TextFieldModifyListener implements ModifyListener { |
|
123 |
|
124 /* |
|
125 * (non-Javadoc) |
|
126 * |
|
127 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
|
128 */ |
|
129 public void modifyText(ModifyEvent e) { |
|
130 if (changeCallback != null) { |
|
131 changeCallback.fieldChanged(); |
|
132 } |
|
133 } |
|
134 } |
|
135 |
|
136 /** |
|
137 * Stores target object if combo is not created |
|
138 */ |
|
139 private String dummyTarget; |
|
140 |
|
141 /** |
|
142 * Stores ID if ID field is not created |
|
143 */ |
|
144 private int dummyID; |
|
145 |
|
146 /** |
|
147 * Stores name if name field is not created |
|
148 */ |
|
149 private String dummyName; |
|
150 |
|
151 /** |
|
152 * Stores value if value field is not created |
|
153 */ |
|
154 private String dummyValue; |
|
155 |
|
156 /** |
|
157 * Stores type if type combo is not created |
|
158 */ |
|
159 private String dummyType; |
|
160 |
|
161 /** |
|
162 * Target composite |
|
163 */ |
|
164 private PropertyDialogUITarget targetComposite; |
|
165 |
|
166 /** |
|
167 * ID composite |
|
168 */ |
|
169 private PropertyDialogUIID idComposite; |
|
170 |
|
171 /** |
|
172 * Name composite |
|
173 */ |
|
174 private PropertyDialogUIName nameComposite; |
|
175 |
|
176 /** |
|
177 * Type composite |
|
178 */ |
|
179 private PropertyDialogUIType typeComposite; |
|
180 |
|
181 /** |
|
182 * Value composite |
|
183 */ |
|
184 public PropertyDialogUIValue valueComposite; |
|
185 |
|
186 /** |
|
187 * Template composite |
|
188 */ |
|
189 public PropertyDialogUITemplate templateComposite; |
|
190 |
|
191 /** |
|
192 * Callback to dialog |
|
193 */ |
|
194 private PropertyDialogUIChangeCallback changeCallback; |
|
195 |
|
196 /** |
|
197 * Template combo box listener |
|
198 */ |
|
199 private TemplateComboSelectionListener templateListener; |
|
200 |
|
201 /** |
|
202 * Text field modification listener |
|
203 */ |
|
204 private ModifyListener textFieldModifyListener; |
|
205 |
|
206 /** |
|
207 * Ui type |
|
208 */ |
|
209 private int uiType; |
|
210 |
|
211 /** |
|
212 * Number of columns in UI |
|
213 */ |
|
214 private static final int COLUMN_COUNT = 2; // CodForChk_Dis_Magic |
|
215 |
|
216 /** |
|
217 * Creates a dialog UI with given parent component |
|
218 * |
|
219 * @param parent |
|
220 * the parent composite |
|
221 * @param uiType |
|
222 * the UI type |
|
223 * @param templates |
|
224 * the dialog templates |
|
225 */ |
|
226 PropertyDialogUI(Composite parent, int uiType, |
|
227 List<TraceObjectPropertyDialogTemplate> templates) { |
|
228 super(parent, SWT.NONE); |
|
229 initialize(uiType, templates); |
|
230 } |
|
231 |
|
232 /** |
|
233 * Initializes the member widgets |
|
234 * |
|
235 * @param uiType |
|
236 * the UI type |
|
237 * @param templates |
|
238 * the templates list |
|
239 */ |
|
240 private void initialize(int uiType, |
|
241 List<TraceObjectPropertyDialogTemplate> templates) { |
|
242 this.uiType = uiType; |
|
243 GridLayout gridLayout = new GridLayout(); |
|
244 gridLayout.numColumns = COLUMN_COUNT; |
|
245 this.setLayout(gridLayout); |
|
246 if (uiType == TraceObjectPropertyDialog.ADD_TRACE |
|
247 || uiType == TraceObjectPropertyDialog.ADD_CONSTANT |
|
248 || uiType == TraceObjectPropertyDialog.INSTRUMENTER |
|
249 || uiType == TraceObjectPropertyDialog.SELECT_COMPONENT) { |
|
250 // Target is needed when adding traces or constants |
|
251 targetComposite = new PropertyDialogUITarget(this, uiType, |
|
252 new TargetFieldModifyListener(), |
|
253 new TargetSelectionListener()); |
|
254 } |
|
255 |
|
256 if (uiType != TraceObjectPropertyDialog.SELECT_COMPONENT) { |
|
257 textFieldModifyListener = new TextFieldModifyListener(); |
|
258 nameComposite = new PropertyDialogUIName(this, uiType, |
|
259 textFieldModifyListener); |
|
260 } |
|
261 if (uiType == TraceObjectPropertyDialog.ADD_TRACE |
|
262 || uiType == TraceObjectPropertyDialog.EDIT_TRACE |
|
263 || uiType == TraceObjectPropertyDialog.INSTRUMENTER) { |
|
264 // Trace text is the only value |
|
265 valueComposite = new PropertyDialogUIValue(this, uiType, |
|
266 textFieldModifyListener); |
|
267 } |
|
268 if (uiType == TraceObjectPropertyDialog.ADD_CONSTANT |
|
269 || uiType == TraceObjectPropertyDialog.EDIT_CONSTANT) { |
|
270 // Constants have user-defined ID's. Other ID's are managed by |
|
271 // builder |
|
272 idComposite = new PropertyDialogUIID(this, uiType, |
|
273 textFieldModifyListener); |
|
274 } |
|
275 if (uiType == TraceObjectPropertyDialog.ADD_PARAMETER) { |
|
276 // Type is needed by parameters |
|
277 typeComposite = new PropertyDialogUIType(this, uiType, |
|
278 new TypeSelectionListener()); |
|
279 } |
|
280 if (uiType == TraceObjectPropertyDialog.ADD_PARAMETER |
|
281 || uiType == TraceObjectPropertyDialog.ADD_TRACE |
|
282 || uiType == TraceObjectPropertyDialog.INSTRUMENTER) { |
|
283 templateListener = new TemplateComboSelectionListener(this); |
|
284 templateComposite = new PropertyDialogUITemplate(this, uiType, |
|
285 templateListener, templates); |
|
286 } |
|
287 } |
|
288 |
|
289 /** |
|
290 * Gets the target object |
|
291 * |
|
292 * @return the target |
|
293 */ |
|
294 String getTarget() { |
|
295 String target; |
|
296 if (targetComposite != null) { |
|
297 target = targetComposite.getTarget(); |
|
298 } else { |
|
299 target = dummyTarget; |
|
300 } |
|
301 return target; |
|
302 } |
|
303 |
|
304 /** |
|
305 * Selects the target which has given name |
|
306 * |
|
307 * @param target |
|
308 * the target to be selected |
|
309 */ |
|
310 void setTarget(String target) { |
|
311 if (targetComposite != null && target != null) { |
|
312 targetComposite.setTarget(target); |
|
313 } else { |
|
314 dummyTarget = target; |
|
315 } |
|
316 } |
|
317 |
|
318 /** |
|
319 * Sets the ID field value |
|
320 * |
|
321 * @param id |
|
322 * the ID field value |
|
323 */ |
|
324 void setIDField(int id) { |
|
325 if (idComposite != null) { |
|
326 idComposite.setID(id); |
|
327 } else { |
|
328 dummyID = id; |
|
329 } |
|
330 } |
|
331 |
|
332 /** |
|
333 * Gets the ID field value |
|
334 * |
|
335 * @return the ID field value |
|
336 */ |
|
337 int getIDField() { |
|
338 int retval; |
|
339 if (idComposite != null) { |
|
340 retval = idComposite.getID(); |
|
341 } else { |
|
342 retval = dummyID; |
|
343 } |
|
344 return retval; |
|
345 } |
|
346 |
|
347 /** |
|
348 * Sets the contents of the name field |
|
349 * |
|
350 * @param name |
|
351 * the name field |
|
352 */ |
|
353 void setNameField(String name) { |
|
354 if (nameComposite != null) { |
|
355 if (name != null) { |
|
356 nameComposite.setName(name); |
|
357 } else { |
|
358 nameComposite.setName(""); //$NON-NLS-1$ |
|
359 } |
|
360 } else { |
|
361 dummyName = name; |
|
362 } |
|
363 } |
|
364 |
|
365 /** |
|
366 * Returns the contents of the name field |
|
367 * |
|
368 * @return the name field |
|
369 */ |
|
370 String getNameField() { |
|
371 String name; |
|
372 if (nameComposite != null) { |
|
373 name = nameComposite.getName(); |
|
374 } else { |
|
375 name = dummyName; |
|
376 } |
|
377 return name; |
|
378 } |
|
379 |
|
380 /** |
|
381 * Sets the contents of the value field. |
|
382 * |
|
383 * @param value |
|
384 * the field contents |
|
385 */ |
|
386 void setValueField(String value) { |
|
387 if (valueComposite != null) { |
|
388 if (value != null) { |
|
389 valueComposite.setValue(value); |
|
390 } else { |
|
391 valueComposite.setValue(""); //$NON-NLS-1$ |
|
392 } |
|
393 } else { |
|
394 dummyValue = value; |
|
395 } |
|
396 } |
|
397 |
|
398 /** |
|
399 * Gets the value field contents |
|
400 * |
|
401 * @return value field contents |
|
402 */ |
|
403 String getValueField() { |
|
404 String ret; |
|
405 if (valueComposite != null) { |
|
406 ret = valueComposite.getValue(); |
|
407 } else { |
|
408 ret = dummyValue; |
|
409 } |
|
410 return ret; |
|
411 } |
|
412 |
|
413 /** |
|
414 * Sets the type |
|
415 * |
|
416 * @param type |
|
417 * the type |
|
418 */ |
|
419 void setType(String type) { |
|
420 if (typeComposite != null) { |
|
421 typeComposite.setType(type); |
|
422 } else { |
|
423 dummyType = type; |
|
424 } |
|
425 } |
|
426 |
|
427 /** |
|
428 * Gets the type |
|
429 * |
|
430 * @return the type |
|
431 */ |
|
432 String getType() { |
|
433 String type; |
|
434 if (typeComposite != null) { |
|
435 type = typeComposite.getSelectedType(); |
|
436 } else { |
|
437 type = dummyType; |
|
438 } |
|
439 return type; |
|
440 } |
|
441 |
|
442 /** |
|
443 * Gets the UI type |
|
444 * |
|
445 * @return the UI type |
|
446 */ |
|
447 int getUiType() { |
|
448 return uiType; |
|
449 } |
|
450 |
|
451 /** |
|
452 * Gets the template at given index |
|
453 * |
|
454 * @param index |
|
455 * the index to the template |
|
456 * @return the template |
|
457 */ |
|
458 TraceObjectPropertyDialogTemplate getTemplate(int index) { |
|
459 return templateComposite.getTemplateAt(index); |
|
460 } |
|
461 |
|
462 /** |
|
463 * Called by the listener when template changes |
|
464 * |
|
465 * @param template |
|
466 * the new template |
|
467 */ |
|
468 void notifyTemplateChanged(TraceObjectPropertyDialogTemplate template) { |
|
469 if (targetComposite != null) { |
|
470 targetComposite.setEnabled(template == null |
|
471 || template.isTargetEnabled()); |
|
472 } |
|
473 if (idComposite != null) { |
|
474 idComposite.setEnabled(template == null || template.isIDEnabled()); |
|
475 } |
|
476 if (valueComposite != null) { |
|
477 valueComposite.setEnabled(template == null |
|
478 || template.isValueEnabled()); |
|
479 } |
|
480 if (nameComposite != null) { |
|
481 nameComposite.setEnabled(template == null |
|
482 || template.isNameEnabled()); |
|
483 } |
|
484 if (typeComposite != null) { |
|
485 boolean typeEnabled = !(template instanceof TraceParameterPropertyDialogTemplate) |
|
486 || ((TraceParameterPropertyDialogTemplate) template) |
|
487 .isTypeEnabled(); |
|
488 typeComposite.setEnabled(typeEnabled); |
|
489 } |
|
490 if (changeCallback != null) { |
|
491 changeCallback.templateChanged(template); |
|
492 } |
|
493 } |
|
494 |
|
495 /** |
|
496 * Selects a template |
|
497 * |
|
498 * @param template |
|
499 * the template to be selected |
|
500 */ |
|
501 void setTemplate(TraceObjectPropertyDialogTemplate template) { |
|
502 if (templateComposite != null) { |
|
503 int index = templateComposite.selectTemplate(template); |
|
504 if (templateListener != null) { |
|
505 if (index < 0) { |
|
506 templateListener.templateSelected(0); |
|
507 } else { |
|
508 templateListener.templateSelected(index); |
|
509 } |
|
510 } |
|
511 } |
|
512 } |
|
513 |
|
514 /** |
|
515 * Disables all UI elements flagged to be disabled |
|
516 * |
|
517 * @param enabler |
|
518 * the enabler interface |
|
519 */ |
|
520 void setEnabler(TraceObjectPropertyDialogEnabler enabler) { |
|
521 if (enabler != null) { |
|
522 if (nameComposite != null && !enabler.isNameEnabled()) { |
|
523 nameComposite.setEnabled(false); |
|
524 } |
|
525 if (targetComposite != null && !enabler.isTargetEnabled()) { |
|
526 targetComposite.setEnabled(false); |
|
527 } |
|
528 if (idComposite != null && !enabler.isIdEnabled()) { |
|
529 idComposite.setEnabled(false); |
|
530 } |
|
531 if (typeComposite != null && !enabler.isTypeEnabled()) { |
|
532 typeComposite.setEnabled(false); |
|
533 } |
|
534 if (valueComposite != null && !enabler.isValueEnabled()) { |
|
535 valueComposite.setEnabled(false); |
|
536 } |
|
537 } |
|
538 } |
|
539 |
|
540 /** |
|
541 * Sets the change notification callback |
|
542 * |
|
543 * @param callback |
|
544 * the callback |
|
545 */ |
|
546 void setChangeCallback(PropertyDialogUIChangeCallback callback) { |
|
547 this.changeCallback = callback; |
|
548 |
|
549 } |
|
550 |
|
551 } |