|
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 * Property dialog for TraceObject |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.view; |
|
20 |
|
21 import java.util.List; |
|
22 |
|
23 import org.eclipse.jface.dialogs.IDialogConstants; |
|
24 import org.eclipse.jface.dialogs.TitleAreaDialog; |
|
25 import org.eclipse.swt.SWT; |
|
26 import org.eclipse.swt.events.SelectionEvent; |
|
27 import org.eclipse.swt.events.SelectionListener; |
|
28 import org.eclipse.swt.graphics.Point; |
|
29 import org.eclipse.swt.graphics.Rectangle; |
|
30 import org.eclipse.swt.layout.RowLayout; |
|
31 import org.eclipse.swt.widgets.Button; |
|
32 import org.eclipse.swt.widgets.Composite; |
|
33 import org.eclipse.swt.widgets.Control; |
|
34 import org.eclipse.swt.widgets.Shell; |
|
35 import org.eclipse.ui.PlatformUI; |
|
36 |
|
37 import com.nokia.tracebuilder.engine.TraceBuilderConfiguration; |
|
38 import com.nokia.tracebuilder.engine.TraceBuilderErrorMessages; |
|
39 import com.nokia.tracebuilder.engine.TraceBuilderGlobals; |
|
40 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialog; |
|
41 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogDynamicFlag; |
|
42 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogFlag; |
|
43 import com.nokia.tracebuilder.engine.TraceObjectPropertyDialogTemplate; |
|
44 import com.nokia.tracebuilder.engine.TraceParameterPropertyDialogDynamicFlag; |
|
45 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.TraceBuilderErrorCode; |
|
46 import com.nokia.tracebuilder.model.TraceBuilderException; |
|
47 |
|
48 /** |
|
49 * Property dialog for TraceObject |
|
50 * |
|
51 */ |
|
52 class PropertyDialog extends TitleAreaDialog { |
|
53 |
|
54 /** |
|
55 * Callback for dialog UI |
|
56 * |
|
57 */ |
|
58 private final class UIChangeCallback implements |
|
59 PropertyDialogUIChangeCallback { |
|
60 |
|
61 /* |
|
62 * (non-Javadoc) |
|
63 * |
|
64 * @see com.nokia.tracebuilder.view.PropertyDialogUIChangeCallback# |
|
65 * templateChanged(com.nokia.tracebuilder.engine.TraceObjectPropertyDialogTemplate) |
|
66 */ |
|
67 public void templateChanged(TraceObjectPropertyDialogTemplate template) { |
|
68 PropertyDialog.this.templateChanged(template, false); |
|
69 verifyContents(true); |
|
70 } |
|
71 |
|
72 /* |
|
73 * (non-Javadoc) |
|
74 * |
|
75 * @see com.nokia.tracebuilder.view.PropertyDialogUIChangeCallback#fieldChanged() |
|
76 */ |
|
77 public void fieldChanged() { |
|
78 verifyContents(true); |
|
79 } |
|
80 |
|
81 /* |
|
82 * (non-Javadoc) |
|
83 * |
|
84 * @see com.nokia.tracebuilder.view.PropertyDialogUIChangeCallback#targetChanged(java.lang.String) |
|
85 */ |
|
86 public void targetChanged(String target) { |
|
87 properties.setTarget(target); |
|
88 updateTitle(); |
|
89 verifyContents(true); |
|
90 } |
|
91 |
|
92 /* |
|
93 * (non-Javadoc) |
|
94 * |
|
95 * @see com.nokia.tracebuilder.view.PropertyDialogUIChangeCallback#typeChanged(java.lang.String) |
|
96 */ |
|
97 public void typeChanged(String type) { |
|
98 properties.setValue(type); |
|
99 verifyContents(true); |
|
100 PropertyDialog.this.typeChanged(type); |
|
101 } |
|
102 } |
|
103 |
|
104 /** |
|
105 * Selection listener for flag buttons |
|
106 * |
|
107 */ |
|
108 private final class FlagButtonSelectionListener implements |
|
109 SelectionListener { |
|
110 |
|
111 /** |
|
112 * The flag to be monitored |
|
113 */ |
|
114 private TraceObjectPropertyDialogFlag flag; |
|
115 |
|
116 /** |
|
117 * Constructor |
|
118 * |
|
119 * @param flag |
|
120 * the flag to be monitored |
|
121 */ |
|
122 FlagButtonSelectionListener(TraceObjectPropertyDialogFlag flag) { |
|
123 this.flag = flag; |
|
124 } |
|
125 |
|
126 /* |
|
127 * (non-Javadoc) |
|
128 * |
|
129 * @see org.eclipse.swt.events.SelectionListener# |
|
130 * widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
|
131 */ |
|
132 public void widgetDefaultSelected(SelectionEvent e) { |
|
133 } |
|
134 |
|
135 /* |
|
136 * (non-Javadoc) |
|
137 * |
|
138 * @see org.eclipse.swt.events.SelectionListener# |
|
139 * widgetSelected(org.eclipse.swt.events.SelectionEvent) |
|
140 */ |
|
141 public void widgetSelected(SelectionEvent e) { |
|
142 flag.setEnabled(((Button) e.widget).getSelection()); |
|
143 flagChanged(flag); |
|
144 } |
|
145 } |
|
146 |
|
147 /** |
|
148 * Dialog margin |
|
149 */ |
|
150 private static final int MARGIN_WIDTH = 5; // CodForChk_Dis_Magic |
|
151 |
|
152 /** |
|
153 * Dialog UI |
|
154 */ |
|
155 private PropertyDialogUI ui; |
|
156 |
|
157 /** |
|
158 * Dialog properties |
|
159 */ |
|
160 private PropertyDialogProperties properties; |
|
161 |
|
162 /** |
|
163 * Buttons for flags |
|
164 */ |
|
165 private Button[] flagButtons; |
|
166 |
|
167 /** |
|
168 * Dialog constructor |
|
169 * |
|
170 * @param shell |
|
171 * the shell where the dialog is associated to |
|
172 * @param properties |
|
173 * the dialog properties |
|
174 */ |
|
175 PropertyDialog(Shell shell, PropertyDialogProperties properties) { |
|
176 super(shell); |
|
177 this.properties = properties; |
|
178 } |
|
179 |
|
180 /* |
|
181 * (non-Javadoc) |
|
182 * |
|
183 * @see org.eclipse.jface.dialogs.TitleAreaDialog# |
|
184 * createContents(org.eclipse.swt.widgets.Composite) |
|
185 */ |
|
186 @Override |
|
187 protected Control createContents(Composite parent) { |
|
188 Control control = super.createContents(parent); |
|
189 // Registers the help context to the root control |
|
190 PlatformUI.getWorkbench().getHelpSystem().setHelp( |
|
191 control, |
|
192 TraceViewHelp.getPropertyDialogContext(properties |
|
193 .getDialogType())); |
|
194 parent.pack(); |
|
195 Point size = parent.getSize(); |
|
196 Point location = getInitialLocation(size); |
|
197 parent.setBounds(getConstrainedShellBounds(new Rectangle(location.x, |
|
198 location.y, size.x, size.y))); |
|
199 return control; |
|
200 } |
|
201 |
|
202 /* |
|
203 * (non-Javadoc) |
|
204 * |
|
205 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
|
206 */ |
|
207 @Override |
|
208 protected Control createDialogArea(Composite parent) { |
|
209 Composite composite = (Composite) super.createDialogArea(parent); |
|
210 ui = new PropertyDialogUI(composite, properties.getDialogType(), |
|
211 properties.getTemplates()); |
|
212 createFlagButtons(composite); |
|
213 loadProperties(); |
|
214 updateTitle(); |
|
215 TraceObjectPropertyDialogTemplate template = properties.getTemplate(); |
|
216 ui.setTemplate(template); |
|
217 ui.setChangeCallback(new UIChangeCallback()); |
|
218 // The dynamic flags are notified and thus each can initialize itself to |
|
219 // correct state |
|
220 templateChanged(template, true); |
|
221 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
222 if (flags != null) { |
|
223 for (int i = 0; i < flags.size(); i++) { |
|
224 flagChanged(flags.get(i)); |
|
225 } |
|
226 } |
|
227 // After all initialization has been done, the enabler is used to |
|
228 // disabler UI elements that should not be modified |
|
229 ui.setEnabler(properties.getEnabler()); |
|
230 return composite; |
|
231 } |
|
232 |
|
233 /** |
|
234 * Creates the check-boxes for flags |
|
235 * |
|
236 * @param composite |
|
237 * the owner for the check-boxes |
|
238 */ |
|
239 private void createFlagButtons(Composite composite) { |
|
240 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
241 if (flags != null && flags.size() > 0) { |
|
242 Composite flagContainer = new Composite(composite, SWT.NONE); |
|
243 RowLayout layout = new RowLayout(SWT.VERTICAL); |
|
244 layout.marginWidth = MARGIN_WIDTH; |
|
245 flagContainer.setLayout(layout); |
|
246 flagButtons = new Button[flags.size()]; |
|
247 for (int i = 0; i < flags.size(); i++) { |
|
248 createFlagButton(flags, flagContainer, i); |
|
249 } |
|
250 } |
|
251 } |
|
252 |
|
253 /** |
|
254 * Creates a flag button |
|
255 * |
|
256 * @param flags |
|
257 * the flags list |
|
258 * @param flagContainer |
|
259 * the container |
|
260 * @param i |
|
261 * the list index |
|
262 */ |
|
263 private void createFlagButton(List<TraceObjectPropertyDialogFlag> flags, |
|
264 Composite flagContainer, int i) { |
|
265 TraceObjectPropertyDialogFlag flag = flags.get(i); |
|
266 if (flag.isVisible()) { |
|
267 flagButtons[i] = new Button(flagContainer, SWT.CHECK); |
|
268 flagButtons[i].setText(flag.getText()); |
|
269 flagButtons[i] |
|
270 .addSelectionListener(new FlagButtonSelectionListener(flag)); |
|
271 flagButtons[i].setSelection(flag.isEnabled()); |
|
272 } |
|
273 } |
|
274 |
|
275 /** |
|
276 * Template change notification |
|
277 * |
|
278 * @param template |
|
279 * the new template |
|
280 * @param forceUiUpdate |
|
281 * forces UI update even if flag did not change |
|
282 */ |
|
283 void templateChanged(TraceObjectPropertyDialogTemplate template, |
|
284 boolean forceUiUpdate) { |
|
285 // When a template changes, the dynamic flags are updated |
|
286 properties.setTemplate(template); |
|
287 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
288 if (flags != null) { |
|
289 TraceObjectPropertyDialogDynamicFlag dynflag; |
|
290 for (int i = 0; i < flags.size(); i++) { |
|
291 TraceObjectPropertyDialogFlag flag = flags.get(i); |
|
292 if (flag instanceof TraceObjectPropertyDialogDynamicFlag |
|
293 && flagButtons[i] != null) { |
|
294 // Notifies all dynamic flags with the change in template |
|
295 dynflag = (TraceObjectPropertyDialogDynamicFlag) flag; |
|
296 if (dynflag.templateChanged(template) || forceUiUpdate) { |
|
297 updateDynamicFlag(i, dynflag); |
|
298 } |
|
299 } |
|
300 } |
|
301 } |
|
302 typeChanged(ui.getType()); |
|
303 } |
|
304 |
|
305 /** |
|
306 * Parameter type change notification |
|
307 * |
|
308 * @param type |
|
309 * the new type |
|
310 */ |
|
311 void typeChanged(String type) { |
|
312 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
313 if (flags != null) { |
|
314 TraceParameterPropertyDialogDynamicFlag dynflag; |
|
315 for (int i = 0; i < flags.size(); i++) { |
|
316 TraceObjectPropertyDialogFlag flag = flags.get(i); |
|
317 if (flag instanceof TraceParameterPropertyDialogDynamicFlag |
|
318 && flagButtons[i] != null) { |
|
319 // Notifies all dynamic parameter flags with the change |
|
320 dynflag = (TraceParameterPropertyDialogDynamicFlag) flag; |
|
321 if (dynflag.typeChanged(type)) { |
|
322 updateDynamicFlag(i, dynflag); |
|
323 } |
|
324 } |
|
325 } |
|
326 } |
|
327 } |
|
328 |
|
329 /** |
|
330 * Updates the flag button of a dynamic flag |
|
331 * |
|
332 * @param i |
|
333 * the flag index |
|
334 * @param dynflag |
|
335 * the flag |
|
336 */ |
|
337 private void updateDynamicFlag(int i, |
|
338 TraceObjectPropertyDialogDynamicFlag dynflag) { |
|
339 if (dynflag.isAvailable()) { |
|
340 flagButtons[i].setSelection(dynflag.isEnabled()); |
|
341 flagButtons[i].setEnabled(true); |
|
342 } else { |
|
343 flagButtons[i].setSelection(dynflag.isAlwaysEnabled()); |
|
344 flagButtons[i].setEnabled(false); |
|
345 } |
|
346 } |
|
347 |
|
348 /** |
|
349 * Flag change notification |
|
350 * |
|
351 * @param flag |
|
352 * the flag that was changed |
|
353 */ |
|
354 void flagChanged(TraceObjectPropertyDialogFlag flag) { |
|
355 // When a flag changes, other flags are updated |
|
356 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
357 if (flags != null) { |
|
358 TraceObjectPropertyDialogDynamicFlag dynflag; |
|
359 for (int i = 0; i < flags.size(); i++) { |
|
360 TraceObjectPropertyDialogFlag otherFlag = flags.get(i); |
|
361 if (otherFlag instanceof TraceObjectPropertyDialogDynamicFlag |
|
362 && otherFlag != flag && flagButtons[i] != null) { |
|
363 // Notifies all dynamic flags with the change in template |
|
364 dynflag = (TraceObjectPropertyDialogDynamicFlag) otherFlag; |
|
365 if (dynflag.flagChanged(flag)) { |
|
366 updateDynamicFlag(i, dynflag); |
|
367 } |
|
368 } |
|
369 } |
|
370 } |
|
371 } |
|
372 |
|
373 /* |
|
374 * (non-Javadoc) |
|
375 * |
|
376 * @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite) |
|
377 */ |
|
378 @Override |
|
379 protected void createButtonsForButtonBar(Composite parent) { |
|
380 super.createButtonsForButtonBar(parent); |
|
381 verifyContents(false); |
|
382 } |
|
383 |
|
384 /* |
|
385 * (non-Javadoc) |
|
386 * |
|
387 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int) |
|
388 */ |
|
389 @Override |
|
390 protected void buttonPressed(int buttonId) { |
|
391 super.buttonPressed(buttonId); |
|
392 ui = null; |
|
393 } |
|
394 |
|
395 /* |
|
396 * (non-Javadoc) |
|
397 * |
|
398 * @see org.eclipse.jface.dialogs.Dialog#okPressed() |
|
399 */ |
|
400 @Override |
|
401 protected void okPressed() { |
|
402 boolean save = saveProperties(); |
|
403 if (TraceBuilderConfiguration.ASSERTIONS_ENABLED && !save) { |
|
404 TraceBuilderGlobals.getEvents().postAssertionFailed( |
|
405 "Save dialog properties failed -> Object might be invalid", //$NON-NLS-1$ |
|
406 null); |
|
407 } |
|
408 checkFlags(); |
|
409 super.okPressed(); |
|
410 } |
|
411 |
|
412 /** |
|
413 * Loads the properties to UI |
|
414 */ |
|
415 private void loadProperties() { |
|
416 ui.setNameField(properties.getName()); |
|
417 ui.setIDField(properties.getID()); |
|
418 int dialogType = properties.getDialogType(); |
|
419 |
|
420 // In case of select component, type and name fields are not used |
|
421 if (dialogType != TraceObjectPropertyDialog.SELECT_COMPONENT) { |
|
422 if (dialogType == TraceObjectPropertyDialog.ADD_PARAMETER) { |
|
423 // In case of parameters value is mapped to the type selector |
|
424 ui.setType(properties.getValue()); |
|
425 } else { |
|
426 // Otherwise the value is mapped to the value field |
|
427 ui.setValueField(properties.getValue()); |
|
428 } |
|
429 } |
|
430 ui.setTarget(properties.getTarget()); |
|
431 } |
|
432 |
|
433 /** |
|
434 * Saves the properties from the UI to the properties object |
|
435 * |
|
436 * @return true if saved, false if failed |
|
437 */ |
|
438 private boolean saveProperties() { |
|
439 boolean retval; |
|
440 try { |
|
441 properties.setID(ui.getIDField()); |
|
442 properties.setName(ui.getNameField()); |
|
443 if (properties.getDialogType() == TraceObjectPropertyDialog.ADD_PARAMETER) { |
|
444 properties.setValue(ui.getType()); |
|
445 } else { |
|
446 properties.setValue(ui.getValueField()); |
|
447 } |
|
448 properties.setTarget(ui.getTarget()); |
|
449 retval = true; |
|
450 } catch (Exception e) { |
|
451 setErrorMessage(Messages.getString("PropertyDialog.InvalidId")); //$NON-NLS-1$ |
|
452 retval = false; |
|
453 } |
|
454 return retval; |
|
455 } |
|
456 |
|
457 /** |
|
458 * Changes the state of flags depending on the check boxes |
|
459 */ |
|
460 private void checkFlags() { |
|
461 // When user changes a template or updates a flag state, some flags may |
|
462 // be disabled in the UI. However, the enabled state of the flag is not |
|
463 // changed when that happens. This allows the checkbox to be set to |
|
464 // correct selection if it is re-enabled. |
|
465 // When OK button is pressed, the enabled state of flags needs to be |
|
466 // synchronized with the button selection state |
|
467 List<TraceObjectPropertyDialogFlag> flags = properties.getFlags(); |
|
468 if (flags != null) { |
|
469 for (int i = 0; i < flags.size(); i++) { |
|
470 if (flagButtons[i] != null) { |
|
471 flags.get(i).setEnabled(flagButtons[i].getSelection()); |
|
472 } |
|
473 } |
|
474 } |
|
475 } |
|
476 |
|
477 /** |
|
478 * Uses the verification callback to verify dialog contents |
|
479 * |
|
480 * @param setError |
|
481 * true if error needs to be set to dialog, false if not |
|
482 */ |
|
483 private void verifyContents(boolean setError) { |
|
484 Button okButton = getButton(IDialogConstants.OK_ID); |
|
485 if (okButton != null) { |
|
486 boolean enabled; |
|
487 if (saveProperties()) { |
|
488 try { |
|
489 properties.verifyContents(); |
|
490 setErrorMessage(null); |
|
491 enabled = true; |
|
492 } catch (TraceBuilderException e) { |
|
493 if (setError) { |
|
494 setErrorMessage(TraceBuilderErrorMessages |
|
495 .getErrorMessage((TraceBuilderErrorCode) e |
|
496 .getErrorCode(), e.getErrorParameters())); |
|
497 } |
|
498 enabled = false; |
|
499 } |
|
500 } else { |
|
501 enabled = false; |
|
502 } |
|
503 okButton.setEnabled(enabled); |
|
504 } |
|
505 } |
|
506 |
|
507 /** |
|
508 * Updates the title of this dialog |
|
509 */ |
|
510 private void updateTitle() { |
|
511 getShell().setText( |
|
512 TraceViewMessages.getPropertyDialogCaption(properties |
|
513 .getDialogType())); |
|
514 setTitle(TraceViewMessages.getPropertyDialogTitle(properties |
|
515 .getDialogType())); |
|
516 setMessage(TraceViewMessages.getPropertyDialogMessage(properties |
|
517 .getDialogType(), properties.getTarget())); |
|
518 } |
|
519 |
|
520 } |