20
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.dialogs.DialogPage;
|
|
14
|
|
15 import dwtx.jface.dialogs.Dialog;
|
|
16 import dwtx.jface.dialogs.IDialogConstants;
|
|
17 import dwtx.jface.dialogs.IDialogPage;
|
|
18 import dwtx.jface.dialogs.IMessageProvider;
|
|
19
|
|
20 import dwt.DWT;
|
|
21 import dwt.graphics.Font;
|
|
22 import dwt.graphics.FontMetrics;
|
|
23 import dwt.graphics.GC;
|
|
24 import dwt.graphics.Image;
|
|
25 import dwt.graphics.Point;
|
|
26 import dwt.layout.GridData;
|
|
27 import dwt.widgets.Button;
|
|
28 import dwt.widgets.Control;
|
|
29 import dwt.widgets.Shell;
|
|
30 import dwtx.jface.resource.ImageDescriptor;
|
|
31 import dwtx.jface.resource.JFaceResources;
|
|
32
|
|
33 import dwt.dwthelper.utils;
|
|
34
|
|
35 /**
|
|
36 * Abstract base implementation of a dialog page. All dialog pages are
|
|
37 * subclasses of this one.
|
|
38 */
|
|
39 public abstract class DialogPage : IDialogPage, IMessageProvider {
|
|
40 /**
|
|
41 * The control for this dialog page.
|
|
42 */
|
|
43 private Control control;
|
|
44
|
|
45 /**
|
|
46 * Optional title; <code>null</code> if none.
|
|
47 *
|
|
48 * @see #setTitle
|
|
49 */
|
|
50 private String title = null;
|
|
51
|
|
52 /**
|
|
53 * Optional description; <code>null</code> if none.
|
|
54 *
|
|
55 * @see #setDescription
|
|
56 */
|
|
57 private String description = null;
|
|
58
|
|
59 /**
|
|
60 * Cached image; <code>null</code> if none.
|
|
61 *
|
|
62 * @see #setImageDescriptor(ImageDescriptor)
|
|
63 */
|
|
64 private Image image = null;
|
|
65
|
|
66 /**
|
|
67 * Optional image; <code>null</code> if none.
|
|
68 *
|
|
69 * @see #setImageDescriptor(ImageDescriptor)
|
|
70 */
|
|
71 private ImageDescriptor imageDescriptor = null;
|
|
72
|
|
73 /**
|
|
74 * The current message; <code>null</code> if none.
|
|
75 */
|
|
76 private String message = null;
|
|
77
|
|
78 /**
|
|
79 * The current message type; default value <code>NONE</code>.
|
|
80 */
|
|
81 private int messageType = NONE;
|
|
82
|
|
83 /**
|
|
84 * The current error message; <code>null</code> if none.
|
|
85 */
|
|
86 private String errorMessage = null;
|
|
87
|
|
88 /**
|
|
89 * Font metrics to use for determining pixel sizes.
|
|
90 */
|
|
91 private FontMetrics fontMetrics;
|
|
92
|
|
93 /**
|
|
94 * Creates a new empty dialog page.
|
|
95 */
|
|
96 protected this() {
|
|
97 //No initial behaviour
|
|
98 }
|
|
99
|
|
100 /**
|
|
101 * Creates a new dialog page with the given title.
|
|
102 *
|
|
103 * @param title
|
|
104 * the title of this dialog page, or <code>null</code> if none
|
|
105 */
|
|
106 protected this(String title) {
|
|
107 this.title = title;
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Creates a new dialog page with the given title and image.
|
|
112 *
|
|
113 * @param title
|
|
114 * the title of this dialog page, or <code>null</code> if none
|
|
115 * @param image
|
|
116 * the image for this dialog page, or <code>null</code> if none
|
|
117 */
|
|
118 protected this(String title, ImageDescriptor image) {
|
|
119 this(title);
|
|
120 imageDescriptor = image;
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Returns the number of pixels corresponding to the height of the given
|
|
125 * number of characters.
|
|
126 * <p>
|
|
127 * This method may only be called after <code>initializeDialogUnits</code>
|
|
128 * has been called.
|
|
129 * </p>
|
|
130 * <p>
|
|
131 * Clients may call this framework method, but should not override it.
|
|
132 * </p>
|
|
133 *
|
|
134 * @param chars
|
|
135 * the number of characters
|
|
136 * @return the number of pixels
|
|
137 */
|
|
138 protected int convertHeightInCharsToPixels(int chars) {
|
|
139 // test for failure to initialize for backward compatibility
|
|
140 if (fontMetrics is null) {
|
|
141 return 0;
|
|
142 }
|
|
143 return Dialog.convertHeightInCharsToPixels(fontMetrics, chars);
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * Returns the number of pixels corresponding to the given number of
|
|
148 * horizontal dialog units.
|
|
149 * <p>
|
|
150 * This method may only be called after <code>initializeDialogUnits</code>
|
|
151 * has been called.
|
|
152 * </p>
|
|
153 * <p>
|
|
154 * Clients may call this framework method, but should not override it.
|
|
155 * </p>
|
|
156 *
|
|
157 * @param dlus
|
|
158 * the number of horizontal dialog units
|
|
159 * @return the number of pixels
|
|
160 */
|
|
161 protected int convertHorizontalDLUsToPixels(int dlus) {
|
|
162 // test for failure to initialize for backward compatibility
|
|
163 if (fontMetrics is null) {
|
|
164 return 0;
|
|
165 }
|
|
166 return Dialog.convertHorizontalDLUsToPixels(fontMetrics, dlus);
|
|
167 }
|
|
168
|
|
169 /**
|
|
170 * Returns the number of pixels corresponding to the given number of
|
|
171 * vertical dialog units.
|
|
172 * <p>
|
|
173 * This method may only be called after <code>initializeDialogUnits</code>
|
|
174 * has been called.
|
|
175 * </p>
|
|
176 * <p>
|
|
177 * Clients may call this framework method, but should not override it.
|
|
178 * </p>
|
|
179 *
|
|
180 * @param dlus
|
|
181 * the number of vertical dialog units
|
|
182 * @return the number of pixels
|
|
183 */
|
|
184 protected int convertVerticalDLUsToPixels(int dlus) {
|
|
185 // test for failure to initialize for backward compatibility
|
|
186 if (fontMetrics is null) {
|
|
187 return 0;
|
|
188 }
|
|
189 return Dialog.convertVerticalDLUsToPixels(fontMetrics, dlus);
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Returns the number of pixels corresponding to the width of the given
|
|
194 * number of characters.
|
|
195 * <p>
|
|
196 * This method may only be called after <code>initializeDialogUnits</code>
|
|
197 * has been called.
|
|
198 * </p>
|
|
199 * <p>
|
|
200 * Clients may call this framework method, but should not override it.
|
|
201 * </p>
|
|
202 *
|
|
203 * @param chars
|
|
204 * the number of characters
|
|
205 * @return the number of pixels
|
|
206 */
|
|
207 protected int convertWidthInCharsToPixels(int chars) {
|
|
208 // test for failure to initialize for backward compatibility
|
|
209 if (fontMetrics is null) {
|
|
210 return 0;
|
|
211 }
|
|
212 return Dialog.convertWidthInCharsToPixels(fontMetrics, chars);
|
|
213 }
|
|
214
|
|
215 /**
|
|
216 * The <code>DialogPage</code> implementation of an
|
|
217 * <code>IDialogPage</code> method does nothing. Subclasses may extend.
|
|
218 */
|
|
219 public void dispose() {
|
|
220 // deallocate DWT resources
|
|
221 if (image !is null) {
|
|
222 image.dispose();
|
|
223 image = null;
|
|
224 }
|
|
225 }
|
|
226
|
|
227 /**
|
|
228 * Returns the top level control for this dialog page.
|
|
229 *
|
|
230 * @return the top level control
|
|
231 */
|
|
232 public Control getControl() {
|
|
233 return control;
|
|
234 }
|
|
235
|
|
236 /*
|
|
237 * (non-Javadoc) Method declared on IDialogPage.
|
|
238 */
|
|
239 public String getDescription() {
|
|
240 return description;
|
|
241 }
|
|
242
|
|
243 /**
|
|
244 * Returns the symbolic font name used by dialog pages.
|
|
245 *
|
|
246 * @return the symbolic font name
|
|
247 */
|
|
248 protected String getDialogFontName() {
|
|
249 return JFaceResources.DIALOG_FONT;
|
|
250 }
|
|
251
|
|
252 /*
|
|
253 * (non-Javadoc) Method declared on IDialogPage.
|
|
254 */
|
|
255 public String getErrorMessage() {
|
|
256 return errorMessage;
|
|
257 }
|
|
258
|
|
259 /**
|
|
260 * Returns the default font to use for this dialog page.
|
|
261 *
|
|
262 * @return the font
|
|
263 */
|
|
264 protected Font getFont() {
|
|
265 return JFaceResources.getFontRegistry().get(getDialogFontName());
|
|
266 }
|
|
267
|
|
268 /*
|
|
269 * (non-Javadoc) Method declared on IDialogPage.
|
|
270 */
|
|
271 public Image getImage() {
|
|
272 if (image is null) {
|
|
273 if (imageDescriptor !is null) {
|
|
274 image = imageDescriptor.createImage();
|
|
275 }
|
|
276 }
|
|
277 return image;
|
|
278 }
|
|
279
|
|
280 /*
|
|
281 * (non-Javadoc) Method declared on IDialogPage.
|
|
282 */
|
|
283 public String getMessage() {
|
|
284 return message;
|
|
285 }
|
|
286
|
|
287 /*
|
|
288 * (non-Javadoc) Method declared on IMessageProvider.
|
|
289 */
|
|
290 public int getMessageType() {
|
|
291 return messageType;
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * Returns this dialog page's shell. Convenience method for
|
|
296 * <code>getControl().getShell()</code>. This method may only be called
|
|
297 * after the page's control has been created.
|
|
298 *
|
|
299 * @return the shell
|
|
300 */
|
|
301 public Shell getShell() {
|
|
302 return getControl().getShell();
|
|
303 }
|
|
304
|
|
305 /*
|
|
306 * (non-Javadoc) Method declared on IDialogPage.
|
|
307 */
|
|
308 public String getTitle() {
|
|
309 return title;
|
|
310 }
|
|
311
|
|
312 /**
|
|
313 * Returns the tool tip text for the widget with the given id.
|
|
314 * <p>
|
|
315 * The default implementation of this framework method does nothing and
|
|
316 * returns <code>null</code>. Subclasses may override.
|
|
317 * </p>
|
|
318 *
|
|
319 * @param widgetId
|
|
320 * the id of the widget for which hover help is requested
|
|
321 * @return the tool tip text, or <code>null</code> if none
|
|
322 * @deprecated
|
|
323 */
|
|
324 protected final String getToolTipText(int widgetId) {
|
|
325 // return nothing by default
|
|
326 return null;
|
|
327 }
|
|
328
|
|
329 /**
|
|
330 * Initializes the computation of horizontal and vertical dialog units based
|
|
331 * on the size of current font.
|
|
332 * <p>
|
|
333 * This method must be called before any of the dialog unit based conversion
|
|
334 * methods are called.
|
|
335 * </p>
|
|
336 *
|
|
337 * @param testControl
|
|
338 * a control from which to obtain the current font
|
|
339 */
|
|
340 protected void initializeDialogUnits(Control testControl) {
|
|
341 // Compute and store a font metric
|
|
342 GC gc = new GC(testControl);
|
|
343 gc.setFont(JFaceResources.getDialogFont());
|
|
344 fontMetrics = gc.getFontMetrics();
|
|
345 gc.dispose();
|
|
346 }
|
|
347
|
|
348 /**
|
|
349 * Sets the <code>GridData</code> on the specified button to be one that
|
|
350 * is spaced for the current dialog page units. The method
|
|
351 * <code>initializeDialogUnits</code> must be called once before calling
|
|
352 * this method for the first time.
|
|
353 *
|
|
354 * @param button
|
|
355 * the button to set the <code>GridData</code>
|
|
356 * @return the <code>GridData</code> set on the specified button
|
|
357 */
|
|
358 protected GridData setButtonLayoutData(Button button) {
|
|
359 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
|
|
360 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
|
|
361 Point minSize = button.computeSize(DWT.DEFAULT, DWT.DEFAULT, true);
|
|
362 data.widthHint = Math.max(widthHint, minSize.x);
|
|
363 button.setLayoutData(data);
|
|
364 return data;
|
|
365 }
|
|
366
|
|
367 /**
|
|
368 * Tests whether this page's UI content has already been created.
|
|
369 *
|
|
370 * @return <code>true</code> if the control has been created, and
|
|
371 * <code>false</code> if not
|
|
372 */
|
|
373 protected bool isControlCreated() {
|
|
374 return control !is null;
|
|
375 }
|
|
376
|
|
377 /**
|
|
378 * This default implementation of an <code>IDialogPage</code> method does
|
|
379 * nothing. Subclasses should override to take some action in response to a
|
|
380 * help request.
|
|
381 */
|
|
382 public void performHelp() {
|
|
383 //No default help
|
|
384 }
|
|
385
|
|
386 /**
|
|
387 * Set the control for the receiver.
|
|
388 * @param newControl
|
|
389 */
|
|
390 protected void setControl(Control newControl) {
|
|
391 control = newControl;
|
|
392 }
|
|
393
|
|
394 /*
|
|
395 * (non-Javadoc) Method declared on IDialogPage.
|
|
396 */
|
|
397 public void setDescription(String description) {
|
|
398 this.description = description;
|
|
399 }
|
|
400
|
|
401 /**
|
|
402 * Sets or clears the error message for this page.
|
|
403 *
|
|
404 * @param newMessage
|
|
405 * the message, or <code>null</code> to clear the error message
|
|
406 */
|
|
407 public void setErrorMessage(String newMessage) {
|
|
408 errorMessage = newMessage;
|
|
409 }
|
|
410
|
|
411 /*
|
|
412 * (non-Javadoc) Method declared on IDialogPage.
|
|
413 */
|
|
414 public void setImageDescriptor(ImageDescriptor desc) {
|
|
415 imageDescriptor = desc;
|
|
416 if (image !is null) {
|
|
417 image.dispose();
|
|
418 image = null;
|
|
419 }
|
|
420 }
|
|
421
|
|
422 /**
|
|
423 * Sets or clears the message for this page.
|
|
424 * <p>
|
|
425 * This is a shortcut for <code>setMessage(newMesasge, NONE)</code>
|
|
426 * </p>
|
|
427 *
|
|
428 * @param newMessage
|
|
429 * the message, or <code>null</code> to clear the message
|
|
430 */
|
|
431 public void setMessage(String newMessage) {
|
|
432 setMessage(newMessage, NONE);
|
|
433 }
|
|
434
|
|
435 /**
|
|
436 * Sets the message for this page with an indication of what type of message
|
|
437 * it is.
|
|
438 * <p>
|
|
439 * The valid message types are one of <code>NONE</code>,
|
|
440 * <code>INFORMATION</code>,<code>WARNING</code>, or
|
|
441 * <code>ERROR</code>.
|
|
442 * </p>
|
|
443 * <p>
|
|
444 * Note that for backward compatibility, a message of type
|
|
445 * <code>ERROR</code> is different than an error message (set using
|
|
446 * <code>setErrorMessage</code>). An error message overrides the current
|
|
447 * message until the error message is cleared. This method replaces the
|
|
448 * current message and does not affect the error message.
|
|
449 * </p>
|
|
450 *
|
|
451 * @param newMessage
|
|
452 * the message, or <code>null</code> to clear the message
|
|
453 * @param newType
|
|
454 * the message type
|
|
455 * @since 2.0
|
|
456 */
|
|
457 public void setMessage(String newMessage, int newType) {
|
|
458 message = newMessage;
|
|
459 messageType = newType;
|
|
460 }
|
|
461
|
|
462 /**
|
|
463 * The <code>DialogPage</code> implementation of this
|
|
464 * <code>IDialogPage</code> method remembers the title in an internal
|
|
465 * state variable. Subclasses may extend.
|
|
466 */
|
|
467 public void setTitle(String title) {
|
|
468 this.title = title;
|
|
469 }
|
|
470
|
|
471 /**
|
|
472 * The <code>DialogPage</code> implementation of this
|
|
473 * <code>IDialogPage</code> method sets the control to the given
|
|
474 * visibility state. Subclasses may extend.
|
|
475 */
|
|
476 public void setVisible(bool visible) {
|
|
477 control.setVisible(visible);
|
|
478 }
|
|
479 }
|