KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > dialogs > DialogMessageArea


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  *******************************************************************************/

11 package org.eclipse.jface.dialogs;
12
13 import org.eclipse.jface.resource.JFaceResources;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CLabel;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Text;
22
23 /**
24  * The DialogMessageArea is a resusable component for adding an accessible
25  * message area to a dialog.
26  *
27  * When the message is normal a CLabel is used but an errors replaces the
28  * message area with a non editable text that can take focus for use by screen
29  * readers.
30  *
31  * @since 3.0
32  */

33 public class DialogMessageArea extends Object JavaDoc {
34     private Text messageText;
35
36     private Label messageImageLabel;
37
38     private Composite messageComposite;
39
40     private String JavaDoc lastMessageText;
41
42     private int lastMessageType;
43
44     private CLabel titleLabel;
45
46     /**
47      * Create a new instance of the receiver.
48      */

49     public DialogMessageArea() {
50         //No initial behaviour
51
}
52
53     /**
54      * Create the contents for the receiver.
55      *
56      * @param parent
57      * the Composite that the children will be created in
58      */

59     public void createContents(Composite parent) {
60        
61         // Message label
62
titleLabel = new CLabel(parent, SWT.NONE);
63         titleLabel.setFont(JFaceResources.getBannerFont());
64         messageComposite = new Composite(parent, SWT.NONE);
65         GridLayout messageLayout = new GridLayout();
66         messageLayout.numColumns = 2;
67         messageLayout.marginWidth = 0;
68         messageLayout.marginHeight = 0;
69         messageLayout.makeColumnsEqualWidth = false;
70         messageComposite.setLayout(messageLayout);
71         messageImageLabel = new Label(messageComposite, SWT.NONE);
72         messageImageLabel.setImage(JFaceResources
73                 .getImage(Dialog.DLG_IMG_MESSAGE_INFO));
74         messageImageLabel.setLayoutData(new GridData(
75                 GridData.VERTICAL_ALIGN_CENTER));
76   
77         messageText = new Text(messageComposite, SWT.NONE);
78         messageText.setEditable(false);
79   
80         GridData textData = new GridData(GridData.GRAB_HORIZONTAL
81                 | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
82         messageText.setLayoutData(textData);
83   
84     }
85
86     /**
87      * Set the layoutData for the title area. In most cases this will be a copy
88      * of the layoutData used in setMessageLayoutData.
89      *
90      * @param layoutData
91      * the layoutData for the title
92      * @see #setMessageLayoutData(Object)
93      */

94     public void setTitleLayoutData(Object JavaDoc layoutData) {
95         titleLabel.setLayoutData(layoutData);
96     }
97
98     /**
99      * Set the layoutData for the messageArea. In most cases this will be a copy
100      * of the layoutData used in setTitleLayoutData.
101      *
102      * @param layoutData
103      * the layoutData for the message area composite.
104      * @see #setTitleLayoutData(Object)
105      */

106     public void setMessageLayoutData(Object JavaDoc layoutData) {
107         messageComposite.setLayoutData(layoutData);
108     }
109
110     /**
111      * Show the title.
112      *
113      * @param titleMessage
114      * String for the titke
115      * @param titleImage
116      * Image or <code>null</code>
117      */

118     public void showTitle(String JavaDoc titleMessage, Image titleImage) {
119         titleLabel.setImage(titleImage);
120         titleLabel.setText(titleMessage);
121         restoreTitle();
122         return;
123     }
124
125     /**
126      * Enable the title and disable the message text and image.
127      */

128     public void restoreTitle() {
129         titleLabel.setVisible(true);
130         messageComposite.setVisible(false);
131         lastMessageText = null;
132         lastMessageType = IMessageProvider.NONE;
133     }
134
135     /**
136      * Show the new message in the message text and update the image. Base the
137      * background color on whether or not there are errors.
138      *
139      * @param newMessage
140      * The new value for the message
141      * @param newType
142      * One of the IMessageProvider constants. If newType is
143      * IMessageProvider.NONE show the title.
144      * @see IMessageProvider
145      */

146     public void updateText(String JavaDoc newMessage, int newType) {
147         Image newImage = null;
148         switch (newType) {
149         case IMessageProvider.NONE:
150             if (newMessage == null) {
151                 restoreTitle();
152             } else {
153                 showTitle(newMessage, null);
154             }
155             return;
156         case IMessageProvider.INFORMATION:
157             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
158             break;
159         case IMessageProvider.WARNING:
160             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
161             break;
162         case IMessageProvider.ERROR:
163             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
164
165             break;
166         }
167         messageComposite.setVisible(true);
168         titleLabel.setVisible(false);
169         // Any more updates required?
170
// If the message text equals the tooltip (i.e. non-shortened text is the same)
171
// and shortened text is the same (i.e. not a resize)
172
// and the image is the same then nothing to do
173
String JavaDoc shortText = Dialog.shortenText(newMessage,messageText);
174         if (newMessage.equals(messageText.getToolTipText())
175                 && newImage == messageImageLabel.getImage()
176                     && shortText.equals(messageText.getText())) {
177             return;
178         }
179         messageImageLabel.setImage(newImage);
180         messageText.setText(Dialog.shortenText(newMessage,messageText));
181         messageText.setToolTipText(newMessage);
182         lastMessageText = newMessage;
183  
184     }
185
186
187     /**
188      * Clear the error message. Restore the previously displayed message if
189      * there is one, if not restore the title label.
190      *
191      */

192     public void clearErrorMessage() {
193         if (lastMessageText == null) {
194             restoreTitle();
195         } else {
196             updateText(lastMessageText, lastMessageType);
197         }
198     }
199 }
200
Popular Tags