Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/PreferenceDialog.d @ 46:9d7d88f13040
Fix anon classes
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Apr 2008 16:19:01 +0200 |
parents | ea8ff534f622 |
children | 46a6e0e6ccd4 |
rev | line source |
---|---|
9 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Teddy Walker <teddy.walker@googlemail.com> | |
11 * - Bug 188056 [Preferences] PreferencePages have to less indent in PreferenceDialog | |
12 * Port to the D programming language: | |
13 * Frank Benoit <benoit@tionex.de> | |
14 *******************************************************************************/ | |
15 module dwtx.jface.preference.PreferenceDialog; | |
16 | |
34 | 17 import dwtx.jface.preference.IPreferencePageContainer; |
18 import dwtx.jface.preference.IPreferencePage; | |
19 import dwtx.jface.preference.IPreferenceNode; | |
20 import dwtx.jface.preference.IPreferenceStore; | |
21 import dwtx.jface.preference.IPersistentPreferenceStore; | |
22 import dwtx.jface.preference.PreferenceManager; | |
23 import dwtx.jface.preference.PreferencePage; | |
24 import dwtx.jface.preference.PreferenceLabelProvider; | |
25 import dwtx.jface.preference.PreferenceContentProvider; | |
9 | 26 |
27 import dwt.DWT; | |
28 import dwt.custom.BusyIndicator; | |
29 import dwt.custom.ScrolledComposite; | |
30 import dwt.events.ControlAdapter; | |
31 import dwt.events.ControlEvent; | |
32 import dwt.events.DisposeEvent; | |
33 import dwt.events.DisposeListener; | |
34 import dwt.events.HelpEvent; | |
35 import dwt.events.HelpListener; | |
36 import dwt.events.SelectionAdapter; | |
37 import dwt.events.SelectionEvent; | |
38 import dwt.events.ShellAdapter; | |
39 import dwt.events.ShellEvent; | |
40 import dwt.graphics.Font; | |
41 import dwt.graphics.Point; | |
42 import dwt.graphics.Rectangle; | |
43 import dwt.layout.FormAttachment; | |
44 import dwt.layout.FormData; | |
45 import dwt.layout.FormLayout; | |
46 import dwt.layout.GridData; | |
47 import dwt.layout.GridLayout; | |
48 import dwt.widgets.Button; | |
49 import dwt.widgets.Composite; | |
50 import dwt.widgets.Control; | |
51 import dwt.widgets.Event; | |
52 import dwt.widgets.Label; | |
53 import dwt.widgets.Layout; | |
54 import dwt.widgets.Listener; | |
55 import dwt.widgets.Sash; | |
56 import dwt.widgets.Shell; | |
57 import dwt.widgets.Tree; | |
58 import dwtx.core.runtime.Assert; | |
59 import dwtx.core.runtime.ISafeRunnable; | |
60 import dwtx.core.runtime.IStatus; | |
61 import dwtx.core.runtime.ListenerList; | |
62 import dwtx.core.runtime.SafeRunner; | |
63 import dwtx.core.runtime.Status; | |
64 import dwtx.jface.dialogs.DialogMessageArea; | |
65 import dwtx.jface.dialogs.IDialogConstants; | |
66 import dwtx.jface.dialogs.IMessageProvider; | |
67 import dwtx.jface.dialogs.IPageChangeProvider; | |
68 import dwtx.jface.dialogs.IPageChangedListener; | |
69 import dwtx.jface.dialogs.MessageDialog; | |
70 import dwtx.jface.dialogs.PageChangedEvent; | |
71 import dwtx.jface.dialogs.TrayDialog; | |
72 import dwtx.jface.resource.JFaceResources; | |
73 import dwtx.jface.util.IPropertyChangeListener; | |
74 import dwtx.jface.util.Policy; | |
75 import dwtx.jface.util.PropertyChangeEvent; | |
76 import dwtx.jface.util.SafeRunnable; | |
77 import dwtx.jface.viewers.ISelection; | |
78 import dwtx.jface.viewers.ISelectionChangedListener; | |
79 import dwtx.jface.viewers.IStructuredSelection; | |
80 import dwtx.jface.viewers.SelectionChangedEvent; | |
81 import dwtx.jface.viewers.StructuredSelection; | |
82 import dwtx.jface.viewers.TreeViewer; | |
83 import dwtx.jface.viewers.ViewerFilter; | |
84 | |
34 | 85 import dwt.dwthelper.utils; |
86 import dwt.dwthelper.Runnable; | |
87 | |
9 | 88 /** |
89 * A preference dialog is a hierarchical presentation of preference pages. Each | |
90 * page is represented by a node in the tree shown on the left hand side of the | |
91 * dialog; when a node is selected, the corresponding page is shown on the right | |
92 * hand side. | |
93 */ | |
34 | 94 public class PreferenceDialog : TrayDialog, IPreferencePageContainer, IPageChangeProvider { |
9 | 95 /** |
96 * Layout for the page container. | |
97 * | |
98 */ | |
34 | 99 private class PageLayout : Layout { |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
100 public override Point computeSize(Composite composite, int wHint, int hHint, bool force) { |
9 | 101 if (wHint !is DWT.DEFAULT && hHint !is DWT.DEFAULT) { |
102 return new Point(wHint, hHint); | |
103 } | |
104 int x = minimumPageSize.x; | |
105 int y = minimumPageSize.y; | |
106 Control[] children = composite.getChildren(); | |
107 for (int i = 0; i < children.length; i++) { | |
108 Point size = children[i].computeSize(DWT.DEFAULT, DWT.DEFAULT, force); | |
109 x = Math.max(x, size.x); | |
110 y = Math.max(y, size.y); | |
111 } | |
112 | |
113 //As pages can implement thier own computeSize | |
114 //take it into account | |
115 if(currentPage !is null){ | |
116 Point size = currentPage.computeSize(); | |
117 x = Math.max(x, size.x); | |
118 y = Math.max(y, size.y); | |
119 } | |
120 | |
121 if (wHint !is DWT.DEFAULT) { | |
122 x = wHint; | |
123 } | |
124 if (hHint !is DWT.DEFAULT) { | |
125 y = hHint; | |
126 } | |
127 return new Point(x, y); | |
128 } | |
129 | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
130 public override void layout(Composite composite, bool force) { |
9 | 131 Rectangle rect = composite.getClientArea(); |
132 Control[] children = composite.getChildren(); | |
133 for (int i = 0; i < children.length; i++) { | |
134 children[i].setSize(rect.width, rect.height); | |
135 } | |
136 } | |
137 } | |
138 | |
139 //The id of the last page that was selected | |
140 private static String lastPreferenceId = null; | |
141 | |
142 //The last known tree width | |
143 private static int lastTreeWidth = 180; | |
144 | |
145 /** | |
146 * Indentifier for the error image | |
147 */ | |
34 | 148 public static const String PREF_DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR; |
9 | 149 |
150 /** | |
151 * Title area fields | |
152 */ | |
34 | 153 public static const String PREF_DLG_TITLE_IMG = "preference_dialog_title_image"; //$NON-NLS-1$ |
9 | 154 |
155 /** | |
156 * Return code used when dialog failed | |
157 */ | |
34 | 158 protected static const int FAILED = 2; |
9 | 159 |
160 /** | |
161 * The current preference page, or <code>null</code> if there is none. | |
162 */ | |
163 private IPreferencePage currentPage; | |
164 | |
165 private DialogMessageArea messageArea; | |
166 | |
167 private Point lastShellSize; | |
168 | |
169 private IPreferenceNode lastSuccessfulNode; | |
170 | |
171 /** | |
172 * The minimum page size; 400 by 400 by default. | |
173 * | |
174 * @see #setMinimumPageSize(Point) | |
175 */ | |
34 | 176 private Point minimumPageSize; |
9 | 177 |
178 /** | |
179 * The OK button. | |
180 */ | |
181 private Button okButton; | |
182 | |
183 /** | |
184 * The Composite in which a page is shown. | |
185 */ | |
186 private Composite pageContainer; | |
187 | |
188 /** | |
189 * The preference manager. | |
190 */ | |
191 private PreferenceManager preferenceManager; | |
192 | |
193 /** | |
194 * Flag for the presence of the error message. | |
195 */ | |
196 private bool showingError = false; | |
197 | |
198 /** | |
199 * Preference store, initially <code>null</code> meaning none. | |
200 * | |
201 * @see #setPreferenceStore | |
202 */ | |
203 private IPreferenceStore preferenceStore; | |
204 | |
205 private Composite titleArea; | |
206 | |
207 /** | |
208 * The tree viewer. | |
209 */ | |
210 private TreeViewer treeViewer; | |
211 | |
34 | 212 private ListenerList pageChangedListeners; |
9 | 213 |
214 /** | |
215 * Composite with a FormLayout to contain the title area | |
216 */ | |
217 Composite formTitleComposite; | |
218 | |
219 private ScrolledComposite scrolled; | |
220 | |
221 /** | |
222 * Creates a new preference dialog under the control of the given preference | |
223 * manager. | |
224 * | |
225 * @param parentShell | |
226 * the parent shell | |
227 * @param manager | |
228 * the preference manager | |
229 */ | |
34 | 230 public this(Shell parentShell, PreferenceManager manager) { |
231 minimumPageSize = new Point(400, 400); | |
232 pageChangedListeners = new ListenerList(); | |
9 | 233 super(parentShell); |
234 setShellStyle(getShellStyle() | DWT.RESIZE | DWT.MAX); | |
235 preferenceManager = manager; | |
236 } | |
237 | |
238 /* | |
239 * (non-Javadoc) | |
240 * | |
241 * @see dwtx.jface.dialogs.Dialog#buttonPressed(int) | |
242 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
243 protected override void buttonPressed(int buttonId) { |
9 | 244 switch (buttonId) { |
245 case IDialogConstants.OK_ID: { | |
246 okPressed(); | |
247 return; | |
248 } | |
249 case IDialogConstants.CANCEL_ID: { | |
250 cancelPressed(); | |
251 return; | |
252 } | |
253 case IDialogConstants.HELP_ID: { | |
254 helpPressed(); | |
255 return; | |
256 } | |
257 } | |
258 } | |
259 | |
260 /* | |
261 * (non-Javadoc) | |
262 * | |
263 * @see dwtx.jface.dialogs.Dialog#cancelPressed() | |
264 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
265 protected override void cancelPressed() { |
9 | 266 // Inform all pages that we are cancelling |
34 | 267 foreach( e; preferenceManager.getElements(PreferenceManager.PRE_ORDER)){ |
268 IPreferenceNode node = cast(IPreferenceNode) e; | |
9 | 269 if (getPage(node) !is null) { |
34 | 270 |
271 // this_: strange workaround for compiler error with dmd 1.028 in run() | |
39 | 272 SafeRunnable.run(new class(this,node) SafeRunnable { |
34 | 273 IPreferenceNode node_; |
39 | 274 PreferenceDialog this_; |
275 this(PreferenceDialog pd, IPreferenceNode b){ | |
276 this_=pd; | |
277 node_=b; | |
34 | 278 } |
9 | 279 public void run() { |
34 | 280 if (!this_.getPage(node_).performCancel()) { |
9 | 281 return; |
282 } | |
283 } | |
284 }); | |
285 } | |
286 } | |
287 setReturnCode(CANCEL); | |
288 close(); | |
289 } | |
290 | |
291 /** | |
292 * Clear the last selected node. This is so that we not chache the last | |
293 * selection in case of an error. | |
294 */ | |
295 void clearSelectedNode() { | |
296 setSelectedNodePreference(null); | |
297 } | |
298 | |
299 /* | |
300 * (non-Javadoc) | |
301 * | |
302 * @see dwtx.jface.window.Window#close() | |
303 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
304 public override bool close() { |
9 | 305 |
306 //Do this is in a SafeRunnable as it may run client code | |
34 | 307 SafeRunnable runnable = new class SafeRunnable{ |
9 | 308 /* (non-Javadoc) |
309 * @see dwtx.core.runtime.ISafeRunnable#run() | |
310 */ | |
34 | 311 public void run() { |
312 auto nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER); | |
9 | 313 for (int i = 0; i < nodes.size(); i++) { |
34 | 314 IPreferenceNode node = cast(IPreferenceNode) nodes.get(i); |
9 | 315 node.disposeResources(); |
316 } | |
317 | |
318 } | |
319 | |
320 /* (non-Javadoc) | |
321 * @see dwtx.jface.util.SafeRunnable#handleException(java.lang.Throwable) | |
322 */ | |
34 | 323 public void handleException(Exception e) { |
9 | 324 super.handleException(e); |
325 clearSelectedNode();//Do not cache a node with problems | |
326 } | |
327 }; | |
328 | |
329 SafeRunner.run(runnable); | |
330 | |
331 return super.close(); | |
332 } | |
333 | |
334 /* | |
335 * (non-Javadoc) | |
336 * | |
337 * @see dwtx.jface.window.Window#configureShell(dwt.widgets.Shell) | |
338 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
339 protected override void configureShell(Shell newShell) { |
9 | 340 super.configureShell(newShell); |
341 newShell.setText(JFaceResources.getString("PreferenceDialog.title")); //$NON-NLS-1$ | |
34 | 342 newShell.addShellListener(new class ShellAdapter { |
9 | 343 public void shellActivated(ShellEvent e) { |
344 if (lastShellSize is null) { | |
345 lastShellSize = getShell().getSize(); | |
346 } | |
347 } | |
348 | |
349 }); | |
350 | |
351 } | |
352 | |
353 /* | |
354 * (non-Javadoc) | |
355 * | |
356 * @see dwtx.jface.window.Window#constrainShellSize() | |
357 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
358 protected override void constrainShellSize() { |
9 | 359 super.constrainShellSize(); |
360 // record opening shell size | |
361 if (lastShellSize is null) { | |
362 lastShellSize = getShell().getSize(); | |
363 } | |
364 } | |
365 | |
366 /* | |
367 * (non-Javadoc) | |
368 * | |
369 * @see dwtx.jface.dialogs.Dialog#createButtonsForButtonBar(dwt.widgets.Composite) | |
370 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
371 protected override void createButtonsForButtonBar(Composite parent) { |
9 | 372 // create OK and Cancel buttons by default |
373 okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); | |
374 getShell().setDefaultButton(okButton); | |
375 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); | |
376 } | |
377 | |
378 /* | |
379 * (non-Javadoc) | |
380 * | |
381 * @see dwtx.jface.window.Window#createContents(dwt.widgets.Composite) | |
382 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
383 protected override Control createContents(Composite parent) { |
34 | 384 Control[1] control; |
46 | 385 BusyIndicator.showWhile(getShell().getDisplay(), new class(parent,control) Runnable { |
34 | 386 Composite parent_; |
46 | 387 Control[] control_; |
388 this(Composite a,Control[] b){ | |
39 | 389 parent_=a; |
46 | 390 control_=b; |
34 | 391 } |
9 | 392 public void run() { |
46 | 393 control_[0] = callSuperCreateContents(parent_); |
9 | 394 // Add the first page |
395 selectSavedItem(); | |
396 } | |
397 }); | |
398 | |
399 return control[0]; | |
400 } | |
34 | 401 private Control callSuperCreateContents( Composite c ){ |
402 return super.createContents( c ); | |
403 } | |
9 | 404 /* |
405 * (non-Javadoc) | |
406 * | |
407 * @see dwtx.jface.dialogs.Dialog#createDialogArea(dwt.widgets.Composite) | |
408 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
409 protected override Control createDialogArea(Composite parent) { |
34 | 410 final Composite composite = cast(Composite) super.createDialogArea(parent); |
411 GridLayout parentLayout = (cast(GridLayout) composite.getLayout()); | |
9 | 412 parentLayout.numColumns = 4; |
413 parentLayout.marginHeight = 0; | |
414 parentLayout.marginWidth = 0; | |
415 parentLayout.verticalSpacing = 0; | |
416 parentLayout.horizontalSpacing = 0; | |
417 | |
418 composite.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_LIST_BACKGROUND)); | |
419 | |
420 Control treeControl = createTreeAreaContents(composite); | |
421 createSash(composite,treeControl); | |
422 | |
423 Label versep = new Label(composite, DWT.SEPARATOR | DWT.VERTICAL); | |
424 GridData verGd = new GridData(GridData.FILL_VERTICAL | GridData.GRAB_VERTICAL); | |
425 | |
426 versep.setLayoutData(verGd); | |
427 versep.setLayoutData(new GridData(DWT.LEFT, DWT.FILL, false, true)); | |
428 | |
429 Composite pageAreaComposite = new Composite(composite, DWT.NONE); | |
430 pageAreaComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
431 GridLayout layout = new GridLayout(1, true); | |
432 layout.marginHeight = 0; | |
433 layout.marginWidth = 0; | |
434 layout.verticalSpacing = 0; | |
435 pageAreaComposite.setLayout(layout); | |
436 | |
437 formTitleComposite = new Composite(pageAreaComposite, DWT.NONE); | |
438 FormLayout titleLayout = new FormLayout(); | |
439 titleLayout.marginWidth = 0; | |
440 titleLayout.marginHeight = 0; | |
441 formTitleComposite.setLayout(titleLayout); | |
442 | |
443 GridData titleGridData = new GridData(GridData.FILL_HORIZONTAL); | |
444 titleGridData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; | |
445 formTitleComposite.setLayoutData(titleGridData); | |
446 | |
447 // Build the title area and separator line | |
448 Composite titleComposite = new Composite(formTitleComposite, DWT.NONE); | |
449 layout = new GridLayout(); | |
450 layout.marginBottom = 5; | |
451 layout.marginHeight = 0; | |
452 layout.marginWidth = 0; | |
453 layout.horizontalSpacing = 0; | |
454 titleComposite.setLayout(layout); | |
455 | |
456 FormData titleFormData = new FormData(); | |
457 titleFormData.top = new FormAttachment(0,0); | |
458 titleFormData.left = new FormAttachment(0,0); | |
459 titleFormData.right = new FormAttachment(100,0); | |
460 titleFormData.bottom = new FormAttachment(100,0); | |
461 | |
462 titleComposite.setLayoutData(titleFormData); | |
463 createTitleArea(titleComposite); | |
464 | |
465 Label separator = new Label(pageAreaComposite, DWT.HORIZONTAL | DWT.SEPARATOR); | |
466 | |
467 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); | |
468 | |
469 | |
470 // Build the Page container | |
471 pageContainer = createPageContainer(pageAreaComposite); | |
472 GridData pageContainerData = new GridData(GridData.FILL_BOTH); | |
473 pageContainerData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; | |
474 pageContainer.setLayoutData(pageContainerData); | |
475 // Build the separator line | |
476 Label bottomSeparator = new Label(parent, DWT.HORIZONTAL | DWT.SEPARATOR); | |
477 bottomSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); | |
478 return composite; | |
479 } | |
480 | |
481 /** | |
482 * Create the sash with right control on the right. Note | |
483 * that this method assumes GridData for the layout data | |
484 * of the rightControl. | |
485 * @param composite | |
486 * @param rightControl | |
487 * @return Sash | |
488 * | |
489 * @since 3.1 | |
490 */ | |
34 | 491 protected Sash createSash(Composite composite, Control rightControl) { |
492 Sash sash = new Sash(composite, DWT.VERTICAL); | |
9 | 493 sash.setLayoutData(new GridData(GridData.FILL_VERTICAL)); |
494 sash.setBackground(composite.getDisplay().getSystemColor(DWT.COLOR_LIST_BACKGROUND)); | |
495 // the following listener resizes the tree control based on sash deltas. | |
496 // If necessary, it will also grow/shrink the dialog. | |
39 | 497 sash.addListener(DWT.Selection, new class(composite,rightControl,sash) Listener { |
34 | 498 Composite composite_; |
499 Control rightControl_; | |
500 Sash sash_; | |
39 | 501 this(Composite a,Control b,Sash c){ |
502 composite_=a; | |
503 rightControl_=b; | |
504 sash_=c; | |
34 | 505 } |
9 | 506 /* |
507 * (non-Javadoc) | |
508 * | |
509 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event) | |
510 */ | |
511 public void handleEvent(Event event) { | |
512 if (event.detail is DWT.DRAG) { | |
513 return; | |
514 } | |
34 | 515 int shift = event.x - sash_.getBounds().x; |
516 GridData data = cast(GridData) rightControl_.getLayoutData(); | |
9 | 517 int newWidthHint = data.widthHint + shift; |
518 if (newWidthHint < 20) { | |
519 return; | |
520 } | |
521 Point computedSize = getShell().computeSize(DWT.DEFAULT, DWT.DEFAULT); | |
522 Point currentSize = getShell().getSize(); | |
523 // if the dialog wasn't of a custom size we know we can shrink | |
524 // it if necessary based on sash movement. | |
34 | 525 bool customSize = !computedSize.opEquals(currentSize); |
9 | 526 data.widthHint = newWidthHint; |
527 setLastTreeWidth(newWidthHint); | |
34 | 528 composite_.layout(true); |
9 | 529 // recompute based on new widget size |
530 computedSize = getShell().computeSize(DWT.DEFAULT, DWT.DEFAULT); | |
531 // if the dialog was of a custom size then increase it only if | |
532 // necessary. | |
533 if (customSize) { | |
534 computedSize.x = Math.max(computedSize.x, currentSize.x); | |
535 } | |
536 computedSize.y = Math.max(computedSize.y, currentSize.y); | |
34 | 537 if (computedSize.opEquals(currentSize)) { |
9 | 538 return; |
539 } | |
540 setShellSize(computedSize.x, computedSize.y); | |
541 lastShellSize = getShell().getSize(); | |
542 } | |
543 }); | |
544 return sash; | |
545 } | |
546 | |
547 /** | |
548 * Creates the inner page container. | |
549 * | |
550 * @param parent | |
551 * @return Composite | |
552 */ | |
553 protected Composite createPageContainer(Composite parent) { | |
554 | |
555 Composite outer = new Composite(parent, DWT.NONE); | |
556 | |
557 GridData outerData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | |
558 | GridData.GRAB_VERTICAL); | |
559 outerData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; | |
560 | |
561 outer.setLayout(new GridLayout()); | |
562 outer.setLayoutData(outerData); | |
563 | |
564 //Create an outer composite for spacing | |
565 scrolled = new ScrolledComposite(outer, DWT.V_SCROLL | DWT.H_SCROLL); | |
566 | |
567 scrolled.setExpandHorizontal(true); | |
568 scrolled.setExpandVertical(true); | |
569 | |
570 GridData scrolledData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | |
571 | GridData.GRAB_VERTICAL); | |
572 | |
573 scrolled.setLayoutData(scrolledData); | |
574 | |
575 Composite result = new Composite(scrolled, DWT.NONE); | |
576 | |
577 GridData resultData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | |
578 | GridData.GRAB_VERTICAL); | |
579 | |
580 result.setLayout(getPageLayout()); | |
581 result.setLayoutData(resultData); | |
582 | |
583 scrolled.setContent(result); | |
584 | |
585 return result; | |
586 } | |
587 | |
588 /** | |
589 * Return the layout for the composite that contains | |
590 * the pages. | |
591 * @return PageLayout | |
592 * | |
593 * @since 3.1 | |
594 */ | |
595 protected Layout getPageLayout() { | |
596 return new PageLayout(); | |
597 } | |
598 | |
599 /** | |
600 * Creates the wizard's title area. | |
601 * | |
602 * @param parent | |
603 * the DWT parent for the title area composite. | |
604 * @return the created title area composite. | |
605 */ | |
606 protected Composite createTitleArea(Composite parent) { | |
607 // Create the title area which will contain | |
608 // a title, message, and image. | |
609 int margins = 2; | |
610 titleArea = new Composite(parent, DWT.NONE); | |
611 FormLayout layout = new FormLayout(); | |
612 layout.marginHeight = 0; | |
613 layout.marginWidth = margins; | |
614 titleArea.setLayout(layout); | |
615 | |
616 | |
617 GridData layoutData = new GridData(GridData.FILL_HORIZONTAL); | |
618 layoutData.verticalAlignment = DWT.TOP; | |
619 titleArea.setLayoutData(layoutData); | |
620 | |
621 // Message label | |
622 messageArea = new DialogMessageArea(); | |
623 messageArea.createContents(titleArea); | |
624 | |
34 | 625 titleArea.addControlListener(new class ControlAdapter { |
9 | 626 /* (non-Javadoc) |
627 * @see dwt.events.ControlAdapter#controlResized(dwt.events.ControlEvent) | |
628 */ | |
629 public void controlResized(ControlEvent e) { | |
630 updateMessage(); | |
631 } | |
632 }); | |
633 | |
46 | 634 IPropertyChangeListener fontListener = new class IPropertyChangeListener { |
9 | 635 public void propertyChange(PropertyChangeEvent event) { |
636 if (JFaceResources.BANNER_FONT.equals(event.getProperty())) { | |
637 updateMessage(); | |
638 } | |
639 if (JFaceResources.DIALOG_FONT.equals(event.getProperty())) { | |
640 updateMessage(); | |
641 Font dialogFont = JFaceResources.getDialogFont(); | |
642 updateTreeFont(dialogFont); | |
34 | 643 Control[] children = (cast(Composite) buttonBar).getChildren(); |
9 | 644 for (int i = 0; i < children.length; i++) { |
645 children[i].setFont(dialogFont); | |
646 } | |
647 } | |
648 } | |
649 }; | |
650 | |
46 | 651 titleArea.addDisposeListener(new class(fontListener) DisposeListener { |
652 IPropertyChangeListener fontListener_; | |
653 this(IPropertyChangeListener a){ | |
654 fontListener_=a; | |
655 } | |
9 | 656 public void widgetDisposed(DisposeEvent event) { |
46 | 657 JFaceResources.getFontRegistry().removeListener(fontListener_); |
9 | 658 } |
659 }); | |
660 JFaceResources.getFontRegistry().addListener(fontListener); | |
661 messageArea.setTitleLayoutData(createMessageAreaData()); | |
662 messageArea.setMessageLayoutData(createMessageAreaData()); | |
663 return titleArea; | |
664 } | |
665 | |
666 /** | |
667 * Create the layout data for the message area. | |
668 * | |
669 * @return FormData for the message area. | |
670 */ | |
671 private FormData createMessageAreaData() { | |
672 FormData messageData = new FormData(); | |
673 messageData.top = new FormAttachment(0); | |
674 messageData.bottom = new FormAttachment(100); | |
675 messageData.right = new FormAttachment(100); | |
676 messageData.left = new FormAttachment(0); | |
677 return messageData; | |
678 } | |
679 | |
680 /** | |
681 * @param parent | |
682 * the DWT parent for the tree area controls. | |
683 * @return the new <code>Control</code>. | |
684 * @since 3.0 | |
685 */ | |
686 protected Control createTreeAreaContents(Composite parent) { | |
687 // Build the tree an put it into the composite. | |
688 treeViewer = createTreeViewer(parent); | |
689 treeViewer.setInput(getPreferenceManager()); | |
690 updateTreeFont(JFaceResources.getDialogFont()); | |
691 layoutTreeAreaControl(treeViewer.getControl()); | |
692 return treeViewer.getControl(); | |
693 } | |
694 | |
695 /** | |
696 * Create a new <code>TreeViewer</code>. | |
697 * | |
698 * @param parent | |
699 * the parent <code>Composite</code>. | |
700 * @return the <code>TreeViewer</code>. | |
701 * @since 3.0 | |
702 */ | |
703 protected TreeViewer createTreeViewer(Composite parent) { | |
704 final TreeViewer viewer = new TreeViewer(parent, DWT.NONE); | |
705 addListeners(viewer); | |
706 viewer.setLabelProvider(new PreferenceLabelProvider()); | |
707 viewer.setContentProvider(new PreferenceContentProvider()); | |
708 return viewer; | |
709 } | |
710 | |
711 /** | |
712 * Add the listeners to the tree viewer. | |
713 * @param viewer | |
714 * | |
715 * @since 3.1 | |
716 */ | |
34 | 717 protected void addListeners(TreeViewer viewer) { |
39 | 718 viewer.addPostSelectionChangedListener(new class(viewer) ISelectionChangedListener { |
34 | 719 TreeViewer viewer_; |
39 | 720 this(TreeViewer a){ |
721 viewer_=a; | |
34 | 722 } |
9 | 723 private void handleError() { |
724 try { | |
725 // remove the listener temporarily so that the events caused | |
726 // by the error handling dont further cause error handling | |
727 // to occur. | |
34 | 728 viewer_.removePostSelectionChangedListener(this); |
9 | 729 showPageFlippingAbortDialog(); |
730 selectCurrentPageAgain(); | |
731 clearSelectedNode(); | |
732 } finally { | |
34 | 733 viewer_.addPostSelectionChangedListener(this); |
9 | 734 } |
735 } | |
736 | |
737 public void selectionChanged(SelectionChangedEvent event) { | |
34 | 738 Object selection = cast(Object) getSingleSelection(event.getSelection()); |
739 if (cast(IPreferenceNode)selection ) { | |
9 | 740 if (!isCurrentPageValid()) { |
741 handleError(); | |
34 | 742 } else if (!showPage(cast(IPreferenceNode) selection)) { |
9 | 743 // Page flipping wasn't successful |
744 handleError(); | |
745 } else { | |
746 // Everything went well | |
34 | 747 lastSuccessfulNode = cast(IPreferenceNode) selection; |
9 | 748 } |
749 } | |
750 } | |
751 }); | |
39 | 752 (cast(Tree) viewer.getControl()).addSelectionListener(new class(viewer) SelectionAdapter { |
34 | 753 TreeViewer viewer_; |
39 | 754 this(TreeViewer a){ |
755 viewer_=a; | |
34 | 756 } |
757 public void widgetDefaultSelected(SelectionEvent event) { | |
758 ISelection selection = viewer_.getSelection(); | |
9 | 759 if (selection.isEmpty()) { |
760 return; | |
761 } | |
762 IPreferenceNode singleSelection = getSingleSelection(selection); | |
34 | 763 bool expanded = viewer_.getExpandedState(cast(Object)singleSelection); |
764 viewer_.setExpandedState(cast(Object)singleSelection, !expanded); | |
9 | 765 } |
766 }); | |
767 //Register help listener on the tree to use context sensitive help | |
34 | 768 viewer.getControl().addHelpListener(new class HelpListener { |
9 | 769 public void helpRequested(HelpEvent event) { |
770 // call perform help on the current page | |
771 if (currentPage !is null) { | |
772 currentPage.performHelp(); | |
773 } | |
774 } | |
775 }); | |
776 } | |
777 | |
778 /** | |
779 * Find the <code>IPreferenceNode</code> that has data the same id as the | |
780 * supplied value. | |
781 * | |
782 * @param nodeId | |
783 * the id to search for. | |
784 * @return <code>IPreferenceNode</code> or <code>null</code> if not | |
785 * found. | |
786 */ | |
787 protected IPreferenceNode findNodeMatching(String nodeId) { | |
34 | 788 foreach( e; preferenceManager.getElements(PreferenceManager.POST_ORDER)){ |
789 IPreferenceNode node = cast(IPreferenceNode) e; | |
9 | 790 if (node.getId().equals(nodeId)) { |
791 return node; | |
792 } | |
793 } | |
794 return null; | |
795 } | |
796 | |
797 /** | |
798 * Get the last known right side width. | |
799 * | |
800 * @return the width. | |
801 */ | |
802 protected int getLastRightWidth() { | |
803 return lastTreeWidth; | |
804 } | |
805 | |
806 /** | |
807 * Returns the preference mananger used by this preference dialog. | |
808 * | |
809 * @return the preference mananger | |
810 */ | |
811 public PreferenceManager getPreferenceManager() { | |
812 return preferenceManager; | |
813 } | |
814 | |
815 /* | |
816 * (non-Javadoc) | |
817 * | |
818 * @see dwtx.jface.preference.IPreferencePageContainer#getPreferenceStore() | |
819 */ | |
820 public IPreferenceStore getPreferenceStore() { | |
821 return preferenceStore; | |
822 } | |
823 | |
824 /** | |
825 * Get the name of the selected item preference | |
826 * | |
827 * @return String | |
828 */ | |
829 protected String getSelectedNodePreference() { | |
830 return lastPreferenceId; | |
831 } | |
832 | |
833 /** | |
834 * @param selection | |
835 * the <code>ISelection</code> to examine. | |
836 * @return the first element, or null if empty. | |
837 */ | |
838 protected IPreferenceNode getSingleSelection(ISelection selection) { | |
839 if (!selection.isEmpty()) { | |
34 | 840 IStructuredSelection structured = cast(IStructuredSelection) selection; |
841 if (cast(IPreferenceNode)structured.getFirstElement() ) { | |
842 return cast(IPreferenceNode) structured.getFirstElement(); | |
9 | 843 } |
844 } | |
845 return null; | |
846 } | |
847 | |
848 /** | |
849 * @return the <code>TreeViewer</code> for this dialog. | |
850 * @since 3.3 | |
851 */ | |
852 public TreeViewer getTreeViewer() { | |
853 return treeViewer; | |
854 } | |
855 | |
856 /** | |
857 * Save the values specified in the pages. | |
858 * <p> | |
859 * The default implementation of this framework method saves all pages of | |
860 * type <code>PreferencePage</code> (if their store needs saving and is a | |
861 * <code>PreferenceStore</code>). | |
862 * </p> | |
863 * <p> | |
864 * Subclasses may override. | |
865 * </p> | |
866 */ | |
867 protected void handleSave() { | |
34 | 868 foreach( e; preferenceManager.getElements(PreferenceManager.PRE_ORDER)){ |
869 IPreferenceNode node = cast(IPreferenceNode) e; | |
9 | 870 IPreferencePage page = node.getPage(); |
34 | 871 if (cast(PreferencePage)page ) { |
9 | 872 // Save now in case tbe workbench does not shutdown cleanly |
34 | 873 IPreferenceStore store = (cast(PreferencePage) page).getPreferenceStore(); |
9 | 874 if (store !is null && store.needsSaving() |
34 | 875 && cast(IPersistentPreferenceStore)store ) { |
9 | 876 try { |
34 | 877 (cast(IPersistentPreferenceStore) store).save(); |
9 | 878 } catch (IOException e) { |
879 MessageDialog | |
880 .openError( | |
881 getShell(), | |
882 JFaceResources.getString("PreferenceDialog.saveErrorTitle"), //$NON-NLS-1$ | |
883 JFaceResources | |
884 .format( | |
34 | 885 "PreferenceDialog.saveErrorMessage", [ page.getTitle(), e.msg ])); //$NON-NLS-1$ |
9 | 886 } |
887 } | |
888 } | |
889 } | |
890 } | |
891 | |
892 /** | |
893 * Notifies that the window's close button was pressed, the close menu was | |
894 * selected, or the ESCAPE key pressed. | |
895 * <p> | |
896 * The default implementation of this framework method sets the window's | |
897 * return code to <code>CANCEL</code> and closes the window using | |
898 * <code>close</code>. Subclasses may extend or reimplement. | |
899 * </p> | |
900 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
901 protected override void handleShellCloseEvent() { |
9 | 902 // handle the same as pressing cancel |
903 cancelPressed(); | |
904 } | |
905 | |
906 /** | |
907 * Notifies of the pressing of the Help button. | |
908 * <p> | |
909 * The default implementation of this framework method calls | |
910 * <code>performHelp</code> on the currently active page. | |
911 * </p> | |
912 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
913 protected /+override+/ void helpPressed() { |
9 | 914 if (currentPage !is null) { |
915 currentPage.performHelp(); | |
916 } | |
917 } | |
918 | |
919 /** | |
920 * Returns whether the current page is valid. | |
921 * | |
922 * @return <code>false</code> if the current page is not valid, or or | |
923 * <code>true</code> if the current page is valid or there is no | |
924 * current page | |
925 */ | |
926 protected bool isCurrentPageValid() { | |
927 if (currentPage is null) { | |
928 return true; | |
929 } | |
930 return currentPage.isValid(); | |
931 } | |
932 | |
933 /** | |
934 * @param control | |
935 * the <code>Control</code> to lay out. | |
936 * @since 3.0 | |
937 */ | |
938 protected void layoutTreeAreaControl(Control control) { | |
939 GridData gd = new GridData(GridData.FILL_VERTICAL); | |
940 gd.widthHint = getLastRightWidth(); | |
941 gd.verticalSpan = 1; | |
942 control.setLayoutData(gd); | |
943 } | |
944 | |
945 /** | |
946 * The preference dialog implementation of this <code>Dialog</code> | |
947 * framework method sends <code>performOk</code> to all pages of the | |
948 * preference dialog, then calls <code>handleSave</code> on this dialog to | |
949 * save any state, and then calls <code>close</code> to close this dialog. | |
950 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
951 protected override void okPressed() { |
34 | 952 SafeRunnable.run(new class SafeRunnable { |
9 | 953 private bool errorOccurred; |
954 | |
955 /* | |
956 * (non-Javadoc) | |
957 * | |
958 * @see dwtx.core.runtime.ISafeRunnable#run() | |
959 */ | |
960 public void run() { | |
961 getButton(IDialogConstants.OK_ID).setEnabled(false); | |
962 errorOccurred = false; | |
963 bool hasFailedOK = false; | |
964 try { | |
965 // Notify all the pages and give them a chance to abort | |
34 | 966 foreach( e; preferenceManager.getElements(PreferenceManager.PRE_ORDER)){ |
967 IPreferenceNode node = cast(IPreferenceNode) e; | |
9 | 968 IPreferencePage page = node.getPage(); |
969 if (page !is null) { | |
970 if (!page.performOk()){ | |
971 hasFailedOK = true; | |
972 return; | |
973 } | |
974 } | |
975 } | |
976 } catch (Exception e) { | |
977 handleException(e); | |
978 } finally { | |
979 //Don't bother closing if the OK failed | |
980 if(hasFailedOK){ | |
981 setReturnCode(FAILED); | |
982 getButton(IDialogConstants.OK_ID).setEnabled(true); | |
34 | 983 //return; |
9 | 984 } |
34 | 985 else{ |
9 | 986 |
34 | 987 if (!errorOccurred) { |
988 //Give subclasses the choice to save the state of the | |
989 //preference pages. | |
990 handleSave(); | |
991 } | |
992 setReturnCode(OK); | |
993 close(); | |
9 | 994 } |
995 } | |
996 } | |
997 | |
998 /* | |
999 * (non-Javadoc) | |
1000 * | |
1001 * @see dwtx.core.runtime.ISafeRunnable#handleException(java.lang.Throwable) | |
1002 */ | |
34 | 1003 public void handleException(Exception e) { |
9 | 1004 errorOccurred = true; |
1005 | |
1006 Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, 0, e.toString(), e)); | |
1007 | |
1008 clearSelectedNode(); | |
1009 String message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$ | |
1010 MessageDialog.openError(getShell(), JFaceResources.getString("Error"), message); //$NON-NLS-1$ | |
1011 | |
1012 } | |
1013 }); | |
1014 } | |
1015 | |
1016 /** | |
1017 * Selects the page determined by <code>lastSuccessfulNode</code> in the | |
1018 * page hierarchy. | |
1019 */ | |
1020 void selectCurrentPageAgain() { | |
1021 if (lastSuccessfulNode is null) { | |
1022 return; | |
1023 } | |
34 | 1024 getTreeViewer().setSelection(new StructuredSelection(cast(Object)lastSuccessfulNode)); |
9 | 1025 currentPage.setVisible(true); |
1026 } | |
1027 | |
1028 /** | |
1029 * Selects the saved item in the tree of preference pages. If it cannot do | |
1030 * this it saves the first one. | |
1031 */ | |
1032 protected void selectSavedItem() { | |
1033 IPreferenceNode node = findNodeMatching(getSelectedNodePreference()); | |
1034 if (node is null) { | |
1035 IPreferenceNode[] nodes = preferenceManager.getRootSubNodes(); | |
1036 ViewerFilter[] filters = getTreeViewer().getFilters(); | |
1037 for (int i = 0; i < nodes.length; i++) { | |
1038 IPreferenceNode selectedNode = nodes[i]; | |
1039 // See if it passes all filters | |
1040 for (int j = 0; j < filters.length; j++) { | |
34 | 1041 if (!filters[j].select(this.treeViewer, cast(Object)preferenceManager |
1042 .getRoot_package(), cast(Object)selectedNode)) { | |
9 | 1043 selectedNode = null; |
1044 break; | |
1045 } | |
1046 } | |
1047 // if it passes all filters select it | |
1048 if (selectedNode !is null) { | |
1049 node = selectedNode; | |
1050 break; | |
1051 } | |
1052 } | |
1053 } | |
1054 if (node !is null) { | |
34 | 1055 getTreeViewer().setSelection(new StructuredSelection(cast(Object)node), true); |
9 | 1056 // Keep focus in tree. See bugs 2692, 2621, and 6775. |
1057 getTreeViewer().getControl().setFocus(); | |
1058 } | |
1059 } | |
1060 | |
1061 /** | |
1062 * Display the given error message. The currently displayed message is saved | |
1063 * and will be redisplayed when the error message is set to | |
1064 * <code>null</code>. | |
1065 * | |
1066 * @param newErrorMessage | |
1067 * the errorMessage to display or <code>null</code> | |
1068 */ | |
1069 public void setErrorMessage(String newErrorMessage) { | |
1070 if (newErrorMessage is null) { | |
1071 messageArea.clearErrorMessage(); | |
1072 } else { | |
1073 messageArea.updateText(newErrorMessage, IMessageProvider.ERROR); | |
1074 } | |
1075 } | |
1076 | |
1077 /** | |
1078 * Save the last known tree width. | |
1079 * | |
1080 * @param width | |
1081 * the width. | |
1082 */ | |
1083 private void setLastTreeWidth(int width) { | |
1084 lastTreeWidth = width; | |
1085 } | |
1086 | |
1087 /** | |
1088 * Set the message text. If the message line currently displays an error, | |
1089 * the message is stored and will be shown after a call to clearErrorMessage | |
1090 * <p> | |
1091 * Shortcut for <code>setMessage(newMessage, NONE)</code> | |
1092 * </p> | |
1093 * | |
1094 * @param newMessage | |
1095 * the message, or <code>null</code> to clear the message | |
1096 */ | |
1097 public void setMessage(String newMessage) { | |
1098 setMessage(newMessage, IMessageProvider.NONE); | |
1099 } | |
1100 | |
1101 /** | |
1102 * Sets the message for this dialog with an indication of what type of | |
1103 * message it is. | |
1104 * <p> | |
1105 * The valid message types are one of <code>NONE</code>, | |
1106 * <code>INFORMATION</code>,<code>WARNING</code>, or | |
1107 * <code>ERROR</code>. | |
1108 * </p> | |
1109 * <p> | |
1110 * Note that for backward compatibility, a message of type | |
1111 * <code>ERROR</code> is different than an error message (set using | |
1112 * <code>setErrorMessage</code>). An error message overrides the current | |
1113 * message until the error message is cleared. This method replaces the | |
1114 * current message and does not affect the error message. | |
1115 * </p> | |
1116 * | |
1117 * @param newMessage | |
1118 * the message, or <code>null</code> to clear the message | |
1119 * @param newType | |
1120 * the message type | |
1121 * @since 2.0 | |
1122 */ | |
1123 public void setMessage(String newMessage, int newType) { | |
1124 messageArea.updateText(newMessage, newType); | |
1125 } | |
1126 | |
1127 /** | |
1128 * Sets the minimum page size. | |
1129 * | |
1130 * @param minWidth | |
1131 * the minimum page width | |
1132 * @param minHeight | |
1133 * the minimum page height | |
1134 * @see #setMinimumPageSize(Point) | |
1135 */ | |
1136 public void setMinimumPageSize(int minWidth, int minHeight) { | |
1137 minimumPageSize.x = minWidth; | |
1138 minimumPageSize.y = minHeight; | |
1139 } | |
1140 | |
1141 /** | |
1142 * Sets the minimum page size. | |
1143 * | |
1144 * @param size | |
1145 * the page size encoded as <code>new Point(width,height)</code> | |
1146 * @see #setMinimumPageSize(int,int) | |
1147 */ | |
1148 public void setMinimumPageSize(Point size) { | |
1149 minimumPageSize.x = size.x; | |
1150 minimumPageSize.y = size.y; | |
1151 } | |
1152 | |
1153 /** | |
1154 * Sets the preference store for this preference dialog. | |
1155 * | |
1156 * @param store | |
1157 * the preference store | |
1158 * @see #getPreferenceStore | |
1159 */ | |
1160 public void setPreferenceStore(IPreferenceStore store) { | |
34 | 1161 Assert.isNotNull(cast(Object)store); |
9 | 1162 preferenceStore = store; |
1163 } | |
1164 | |
1165 /** | |
1166 * Save the currently selected node. | |
1167 */ | |
1168 private void setSelectedNode() { | |
1169 String storeValue = null; | |
34 | 1170 IStructuredSelection selection = cast(IStructuredSelection) getTreeViewer().getSelection(); |
9 | 1171 if (selection.size() is 1) { |
34 | 1172 IPreferenceNode node = cast(IPreferenceNode) selection.getFirstElement(); |
9 | 1173 storeValue = node.getId(); |
1174 } | |
1175 setSelectedNodePreference(storeValue); | |
1176 } | |
1177 | |
1178 /** | |
1179 * Sets the name of the selected item preference. Public equivalent to | |
1180 * <code>setSelectedNodePreference</code>. | |
1181 * | |
1182 * @param pageId | |
1183 * The identifier for the page | |
1184 * @since 3.0 | |
1185 */ | |
1186 public void setSelectedNode(String pageId) { | |
1187 setSelectedNodePreference(pageId); | |
1188 } | |
1189 | |
1190 /** | |
1191 * Sets the name of the selected item preference. | |
1192 * | |
1193 * @param pageId | |
1194 * The identifier for the page | |
1195 */ | |
1196 protected void setSelectedNodePreference(String pageId) { | |
1197 lastPreferenceId = pageId; | |
1198 } | |
1199 | |
1200 /** | |
1201 * Changes the shell size to the given size, ensuring that it is no larger | |
1202 * than the display bounds. | |
1203 * | |
1204 * @param width | |
1205 * the shell width | |
1206 * @param height | |
1207 * the shell height | |
1208 */ | |
1209 private void setShellSize(int width, int height) { | |
1210 Rectangle preferred = getShell().getBounds(); | |
1211 preferred.width = width; | |
1212 preferred.height = height; | |
1213 getShell().setBounds(getConstrainedShellBounds(preferred)); | |
1214 } | |
1215 | |
1216 /** | |
1217 * Shows the preference page corresponding to the given preference node. | |
1218 * Does nothing if that page is already current. | |
1219 * | |
1220 * @param node | |
1221 * the preference node, or <code>null</code> if none | |
1222 * @return <code>true</code> if the page flip was successful, and | |
1223 * <code>false</code> is unsuccessful | |
1224 */ | |
1225 protected bool showPage(IPreferenceNode node) { | |
1226 if (node is null) { | |
1227 return false; | |
1228 } | |
1229 // Create the page if nessessary | |
1230 if (node.getPage() is null) { | |
1231 createPage(node); | |
1232 } | |
1233 if (node.getPage() is null) { | |
1234 return false; | |
1235 } | |
1236 IPreferencePage newPage = getPage(node); | |
1237 if (newPage is currentPage) { | |
1238 return true; | |
1239 } | |
1240 if (currentPage !is null) { | |
1241 if (!currentPage.okToLeave()) { | |
1242 return false; | |
1243 } | |
1244 } | |
1245 IPreferencePage oldPage = currentPage; | |
1246 currentPage = newPage; | |
1247 // Set the new page's container | |
1248 currentPage.setContainer(this); | |
1249 // Ensure that the page control has been created | |
1250 // (this allows lazy page control creation) | |
1251 if (currentPage.getControl() is null) { | |
34 | 1252 bool[1] failed; failed[0] = false; |
46 | 1253 SafeRunnable.run(new class(failed) ISafeRunnable { |
1254 bool[] failed_; | |
1255 this(bool[] a){ | |
1256 this.failed_=a; | |
1257 } | |
34 | 1258 public void handleException(Exception e) { |
46 | 1259 this.failed_[0] = true; |
9 | 1260 } |
1261 | |
1262 public void run() { | |
1263 createPageControl(currentPage, pageContainer); | |
1264 } | |
1265 }); | |
1266 if (failed[0]) { | |
1267 return false; | |
1268 } | |
1269 // the page is responsible for ensuring the created control is | |
1270 // accessable | |
1271 // via getControl. | |
1272 Assert.isNotNull(currentPage.getControl()); | |
1273 } | |
1274 // Force calculation of the page's description label because | |
1275 // label can be wrapped. | |
46 | 1276 Point[1] size; |
1277 Point failed = new Point(-1, -1); | |
1278 SafeRunnable.run(new class(size,failed) ISafeRunnable { | |
1279 Point[] size_; | |
1280 Point failed_; | |
1281 this(Point[] a,Point b){ | |
1282 size_=a; | |
1283 failed_=b; | |
1284 } | |
34 | 1285 public void handleException(Exception e) { |
46 | 1286 size_[0] = failed_; |
9 | 1287 } |
1288 | |
1289 public void run() { | |
46 | 1290 size_[0] = currentPage.computeSize(); |
9 | 1291 } |
1292 }); | |
34 | 1293 if (size[0].opEquals(failed)) { |
9 | 1294 return false; |
1295 } | |
1296 Point contentSize = size[0]; | |
1297 // Do we need resizing. Computation not needed if the | |
1298 // first page is inserted since computing the dialog's | |
1299 // size is done by calling dialog.open(). | |
1300 // Also prevent auto resize if the user has manually resized | |
1301 Shell shell = getShell(); | |
1302 Point shellSize = shell.getSize(); | |
1303 if (oldPage !is null) { | |
1304 Rectangle rect = pageContainer.getClientArea(); | |
1305 Point containerSize = new Point(rect.width, rect.height); | |
1306 int hdiff = contentSize.x - containerSize.x; | |
1307 int vdiff = contentSize.y - containerSize.y; | |
34 | 1308 if ((hdiff > 0 || vdiff > 0) && shellSize.opEquals(lastShellSize)) { |
9 | 1309 hdiff = Math.max(0, hdiff); |
1310 vdiff = Math.max(0, vdiff); | |
1311 setShellSize(shellSize.x + hdiff, shellSize.y + vdiff); | |
1312 lastShellSize = shell.getSize(); | |
1313 if (currentPage.getControl().getSize().x is 0) { | |
1314 currentPage.getControl().setSize(containerSize); | |
1315 } | |
1316 | |
1317 } else { | |
1318 currentPage.setSize(containerSize); | |
1319 } | |
1320 } | |
1321 | |
1322 scrolled.setMinSize(contentSize); | |
1323 // Ensure that all other pages are invisible | |
1324 // (including ones that triggered an exception during | |
1325 // their creation). | |
1326 Control[] children = pageContainer.getChildren(); | |
1327 Control currentControl = currentPage.getControl(); | |
1328 for (int i = 0; i < children.length; i++) { | |
1329 if (children[i] !is currentControl) { | |
1330 children[i].setVisible(false); | |
1331 } | |
1332 } | |
1333 // Make the new page visible | |
1334 currentPage.setVisible(true); | |
1335 if (oldPage !is null) { | |
1336 oldPage.setVisible(false); | |
1337 } | |
1338 // update the dialog controls | |
1339 update(); | |
1340 return true; | |
1341 } | |
1342 | |
1343 /** | |
1344 * Create the page for the node. | |
1345 * @param node | |
1346 * | |
1347 * @since 3.1 | |
1348 */ | |
1349 protected void createPage(IPreferenceNode node) { | |
1350 node.createPage(); | |
1351 } | |
1352 | |
1353 /** | |
1354 * Get the page for the node. | |
1355 * @param node | |
1356 * @return IPreferencePage | |
1357 * | |
1358 * @since 3.1 | |
1359 */ | |
1360 protected IPreferencePage getPage(IPreferenceNode node) { | |
1361 return node.getPage(); | |
1362 } | |
1363 | |
1364 /** | |
1365 * Shows the "Page Flipping abort" dialog. | |
1366 */ | |
1367 void showPageFlippingAbortDialog() { | |
1368 MessageDialog.openError(getShell(), JFaceResources | |
1369 .getString("AbortPageFlippingDialog.title"), //$NON-NLS-1$ | |
1370 JFaceResources.getString("AbortPageFlippingDialog.message")); //$NON-NLS-1$ | |
1371 } | |
1372 | |
1373 /** | |
1374 * Updates this dialog's controls to reflect the current page. | |
1375 */ | |
1376 protected void update() { | |
1377 // Update the title bar | |
1378 updateTitle(); | |
1379 // Update the message line | |
1380 updateMessage(); | |
1381 // Update the buttons | |
1382 updateButtons(); | |
1383 //Saved the selected node in the preferences | |
1384 setSelectedNode(); | |
34 | 1385 firePageChanged(new PageChangedEvent(this, cast(Object)getCurrentPage())); |
9 | 1386 } |
1387 | |
1388 /* | |
1389 * (non-Javadoc) | |
1390 * | |
1391 * @see dwtx.jface.preference.IPreferencePageContainer#updateButtons() | |
1392 */ | |
1393 public void updateButtons() { | |
1394 okButton.setEnabled(isCurrentPageValid()); | |
1395 } | |
1396 | |
1397 /* | |
1398 * (non-Javadoc) | |
1399 * | |
1400 * @see dwtx.jface.preference.IPreferencePageContainer#updateMessage() | |
1401 */ | |
1402 public void updateMessage() { | |
1403 String message = null; | |
1404 String errorMessage = null; | |
1405 if(currentPage !is null){ | |
1406 message = currentPage.getMessage(); | |
1407 errorMessage = currentPage.getErrorMessage(); | |
1408 } | |
1409 int messageType = IMessageProvider.NONE; | |
34 | 1410 if (message !is null && cast(IMessageProvider)currentPage ) { |
1411 messageType = (cast(IMessageProvider) currentPage).getMessageType(); | |
9 | 1412 } |
1413 | |
1414 if (errorMessage is null){ | |
1415 if (showingError) { | |
1416 // we were previously showing an error | |
1417 showingError = false; | |
1418 } | |
1419 } | |
1420 else { | |
1421 message = errorMessage; | |
1422 messageType = IMessageProvider.ERROR; | |
1423 if (!showingError) { | |
1424 // we were not previously showing an error | |
1425 showingError = true; | |
1426 } | |
1427 } | |
1428 messageArea.updateText(message,messageType); | |
1429 } | |
1430 | |
1431 /* | |
1432 * (non-Javadoc) | |
1433 * | |
1434 * @see dwtx.jface.preference.IPreferencePageContainer#updateTitle() | |
1435 */ | |
1436 public void updateTitle() { | |
1437 if(currentPage is null) { | |
1438 return; | |
1439 } | |
1440 messageArea.showTitle(currentPage.getTitle(), currentPage.getImage()); | |
1441 } | |
1442 | |
1443 /** | |
1444 * Update the tree to use the specified <code>Font</code>. | |
1445 * | |
1446 * @param dialogFont | |
1447 * the <code>Font</code> to use. | |
1448 * @since 3.0 | |
1449 */ | |
1450 protected void updateTreeFont(Font dialogFont) { | |
1451 getTreeViewer().getControl().setFont(dialogFont); | |
1452 } | |
1453 | |
1454 /** | |
1455 * Returns the currentPage. | |
1456 * @return IPreferencePage | |
1457 * @since 3.1 | |
1458 */ | |
1459 protected IPreferencePage getCurrentPage() { | |
1460 return currentPage; | |
1461 } | |
1462 | |
1463 /** | |
1464 * Sets the current page. | |
1465 * @param currentPage | |
1466 * | |
1467 * @since 3.1 | |
1468 */ | |
1469 protected void setCurrentPage(IPreferencePage currentPage) { | |
1470 this.currentPage = currentPage; | |
1471 } | |
1472 | |
1473 /** | |
1474 * Set the treeViewer. | |
1475 * @param treeViewer | |
1476 * | |
1477 * @since 3.1 | |
1478 */ | |
1479 protected void setTreeViewer(TreeViewer treeViewer) { | |
1480 this.treeViewer = treeViewer; | |
1481 } | |
1482 | |
1483 /** | |
1484 * Get the composite that is showing the page. | |
1485 * | |
1486 * @return Composite. | |
1487 * | |
1488 * @since 3.1 | |
1489 */ | |
1490 protected Composite getPageContainer() { | |
1491 return this.pageContainer; | |
1492 } | |
1493 | |
1494 /** | |
1495 * Set the composite that is showing the page. | |
1496 * @param pageContainer Composite | |
1497 * | |
1498 * @since 3.1 | |
1499 */ | |
1500 protected void setPageContainer(Composite pageContainer) { | |
1501 this.pageContainer = pageContainer; | |
1502 } | |
1503 /** | |
1504 * Create the page control for the supplied page. | |
1505 * | |
1506 * @param page - the preference page to be shown | |
1507 * @param parent - the composite to parent the page | |
1508 * | |
1509 * @since 3.1 | |
1510 */ | |
1511 protected void createPageControl(IPreferencePage page, Composite parent) { | |
1512 page.createControl(parent); | |
1513 } | |
1514 | |
1515 /** | |
1516 * @see dwtx.jface.dialogs.IPageChangeProvider#getSelectedPage() | |
1517 * | |
1518 * @since 3.1 | |
1519 */ | |
1520 public Object getSelectedPage() { | |
34 | 1521 return cast(Object)getCurrentPage(); |
9 | 1522 } |
1523 | |
1524 /** | |
1525 * @see dwtx.jface.dialogs.IPageChangeProvider#addPageChangedListener(dwtx.jface.dialogs.IPageChangedListener) | |
1526 * @since 3.1 | |
1527 */ | |
1528 public void addPageChangedListener(IPageChangedListener listener) { | |
34 | 1529 pageChangedListeners.add(cast(Object)listener); |
9 | 1530 } |
1531 | |
1532 /** | |
1533 * @see dwtx.jface.dialogs.IPageChangeProvider#removePageChangedListener(dwtx.jface.dialogs.IPageChangedListener) | |
1534 * @since 3.1 | |
1535 */ | |
1536 public void removePageChangedListener(IPageChangedListener listener) { | |
34 | 1537 pageChangedListeners.remove(cast(Object)listener); |
9 | 1538 |
1539 } | |
1540 | |
1541 /** | |
1542 * Notifies any selection changed listeners that the selected page | |
1543 * has changed. | |
1544 * Only listeners registered at the time this method is called are notified. | |
1545 * | |
1546 * @param event a selection changed event | |
1547 * | |
1548 * @see IPageChangedListener#pageChanged | |
1549 * | |
1550 * @since 3.1 | |
1551 */ | |
34 | 1552 protected void firePageChanged(PageChangedEvent event) { |
9 | 1553 Object[] listeners = pageChangedListeners.getListeners(); |
1554 for (int i = 0; i < listeners.length; i++) { | |
39 | 1555 SafeRunnable.run(new class(event,cast(IPageChangedListener) listeners[i]) SafeRunnable { |
34 | 1556 PageChangedEvent event_; |
1557 IPageChangedListener l; | |
39 | 1558 this(PageChangedEvent a,IPageChangedListener b){ |
1559 event_=a; | |
1560 l =b ; | |
34 | 1561 } |
9 | 1562 public void run() { |
34 | 1563 l.pageChanged(event_); |
9 | 1564 } |
1565 }); | |
1566 } | |
1567 } | |
1568 } |