comparison dwtx/jface/dialogs/DialogMessageArea.d @ 19:2b36428a5ce4

DialogSettings
author Frank Benoit <benoit@tionex.de>
date Thu, 03 Apr 2008 00:25:45 +0200
parents
children c3583c6ec027
comparison
equal deleted inserted replaced
18:7615869f89e6 19:2b36428a5ce4
1 /*******************************************************************************
2 * Copyright (c) 2004, 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.DialogMessageArea;
14
15 import dwtx.jface.dialogs.Dialog;
16 import dwtx.jface.dialogs.IMessageProvider;
17
18 import dwt.DWT;
19 import dwt.custom.CLabel;
20 import dwt.graphics.Image;
21 import dwt.layout.GridData;
22 import dwt.layout.GridLayout;
23 import dwt.widgets.Composite;
24 import dwt.widgets.Label;
25 import dwt.widgets.Text;
26 import dwtx.jface.resource.JFaceResources;
27
28 import dwt.dwthelper.utils;
29
30 /**
31 * The DialogMessageArea is a resusable component for adding an accessible
32 * message area to a dialog.
33 *
34 * When the message is normal a CLabel is used but an errors replaces the
35 * message area with a non editable text that can take focus for use by screen
36 * readers.
37 *
38 * @since 3.0
39 */
40 public class DialogMessageArea : Object {
41 private Text messageText;
42
43 private Label messageImageLabel;
44
45 private Composite messageComposite;
46
47 private String lastMessageText;
48
49 private int lastMessageType;
50
51 private CLabel titleLabel;
52
53 /**
54 * Create a new instance of the receiver.
55 */
56 public this() {
57 //No initial behaviour
58 }
59
60 /**
61 * Create the contents for the receiver.
62 *
63 * @param parent
64 * the Composite that the children will be created in
65 */
66 public void createContents(Composite parent) {
67
68 // Message label
69 titleLabel = new CLabel(parent, DWT.NONE);
70 titleLabel.setFont(JFaceResources.getBannerFont());
71 messageComposite = new Composite(parent, DWT.NONE);
72 GridLayout messageLayout = new GridLayout();
73 messageLayout.numColumns = 2;
74 messageLayout.marginWidth = 0;
75 messageLayout.marginHeight = 0;
76 messageLayout.makeColumnsEqualWidth = false;
77 messageComposite.setLayout(messageLayout);
78 messageImageLabel = new Label(messageComposite, DWT.NONE);
79 messageImageLabel.setImage(JFaceResources
80 .getImage(Dialog.DLG_IMG_MESSAGE_INFO));
81 messageImageLabel.setLayoutData(new GridData(
82 GridData.VERTICAL_ALIGN_CENTER));
83
84 messageText = new Text(messageComposite, DWT.NONE);
85 messageText.setEditable(false);
86
87 GridData textData = new GridData(GridData.GRAB_HORIZONTAL
88 | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
89 messageText.setLayoutData(textData);
90
91 }
92
93 /**
94 * Set the layoutData for the title area. In most cases this will be a copy
95 * of the layoutData used in setMessageLayoutData.
96 *
97 * @param layoutData
98 * the layoutData for the title
99 * @see #setMessageLayoutData(Object)
100 */
101 public void setTitleLayoutData(Object layoutData) {
102 titleLabel.setLayoutData(layoutData);
103 }
104
105 /**
106 * Set the layoutData for the messageArea. In most cases this will be a copy
107 * of the layoutData used in setTitleLayoutData.
108 *
109 * @param layoutData
110 * the layoutData for the message area composite.
111 * @see #setTitleLayoutData(Object)
112 */
113 public void setMessageLayoutData(Object layoutData) {
114 messageComposite.setLayoutData(layoutData);
115 }
116
117 /**
118 * Show the title.
119 *
120 * @param titleMessage
121 * String for the titke
122 * @param titleImage
123 * Image or <code>null</code>
124 */
125 public void showTitle(String titleMessage, Image titleImage) {
126 titleLabel.setImage(titleImage);
127 titleLabel.setText(titleMessage);
128 restoreTitle();
129 return;
130 }
131
132 /**
133 * Enable the title and disable the message text and image.
134 */
135 public void restoreTitle() {
136 titleLabel.setVisible(true);
137 messageComposite.setVisible(false);
138 lastMessageText = null;
139 lastMessageType = IMessageProvider.NONE;
140 }
141
142 /**
143 * Show the new message in the message text and update the image. Base the
144 * background color on whether or not there are errors.
145 *
146 * @param newMessage
147 * The new value for the message
148 * @param newType
149 * One of the IMessageProvider constants. If newType is
150 * IMessageProvider.NONE show the title.
151 * @see IMessageProvider
152 */
153 public void updateText(String newMessage, int newType) {
154 Image newImage = null;
155 switch (newType) {
156 case IMessageProvider.NONE:
157 if (newMessage is null) {
158 restoreTitle();
159 } else {
160 showTitle(newMessage, null);
161 }
162 return;
163 case IMessageProvider.INFORMATION:
164 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
165 break;
166 case IMessageProvider.WARNING:
167 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
168 break;
169 case IMessageProvider.ERROR:
170 newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
171
172 break;
173 }
174 messageComposite.setVisible(true);
175 titleLabel.setVisible(false);
176 // Any more updates required?
177 // If the message text equals the tooltip (i.e. non-shortened text is the same)
178 // and shortened text is the same (i.e. not a resize)
179 // and the image is the same then nothing to do
180 String shortText = Dialog.shortenText(newMessage,messageText);
181 if (newMessage.equals(messageText.getToolTipText())
182 && newImage is messageImageLabel.getImage()
183 && shortText.equals(messageText.getText())) {
184 return;
185 }
186 messageImageLabel.setImage(newImage);
187 messageText.setText(Dialog.shortenText(newMessage,messageText));
188 messageText.setToolTipText(newMessage);
189 lastMessageText = newMessage;
190
191 }
192
193
194 /**
195 * Clear the error message. Restore the previously displayed message if
196 * there is one, if not restore the title label.
197 *
198 */
199 public void clearErrorMessage() {
200 if (lastMessageText is null) {
201 restoreTitle();
202 } else {
203 updateText(lastMessageText, lastMessageType);
204 }
205 }
206 }