comparison org.eclipse.jface/src/org/eclipse/jface/dialogs/DialogPage.d @ 12:bc29606a740c

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