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