KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > MessageRegion


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

11 package org.eclipse.ui.texteditor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Rectangle;
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 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IMessageProvider;
26 import org.eclipse.jface.resource.JFaceColors;
27 import org.eclipse.jface.resource.JFaceResources;
28
29
30 /**
31  * The MessageRegion is the optional area to
32  * show messages in the page.
33  * <p>
34  * XXX: Copied from org.eclipse.jface.preference.PreferencePage.MessageRegion
35  * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=84061
36  * </p>
37  *
38  * @since 3.1
39  */

40 class MessageRegion {
41
42     private Text messageText;
43
44     private Label messageImageLabel;
45
46     private Composite messageComposite;
47
48     private String JavaDoc lastMessageText = "";//$NON-NLS-1$
49

50     private int lastMessageType;
51
52     /**
53      * Create a new instance of the receiver.
54      */

55     public MessageRegion() {
56         //No initial behavior
57
}
58
59     /**
60      * Create the contents for the receiver.
61      *
62      * @param parent
63      * the Composite that the children will be created in
64      */

65     public void createContents(Composite parent) {
66         messageComposite = new Composite(parent, SWT.NONE);
67         GridLayout messageLayout = new GridLayout();
68         messageLayout.numColumns = 2;
69         messageLayout.marginWidth = 0;
70         messageLayout.marginHeight = 0;
71         messageLayout.makeColumnsEqualWidth = false;
72         messageComposite.setLayout(messageLayout);
73         messageImageLabel = new Label(messageComposite, SWT.NONE);
74
75         GridData imageData = new GridData(GridData.VERTICAL_ALIGN_CENTER);
76         Image sizingImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
77         Rectangle imageBounds;
78         if(sizingImage == null)
79             imageBounds = new Rectangle(0,0,IDialogConstants.VERTICAL_MARGIN * 2,IDialogConstants.VERTICAL_MARGIN * 2);
80         else
81             imageBounds = sizingImage.getBounds();
82         imageData.heightHint = imageBounds.height + IDialogConstants.VERTICAL_SPACING;
83         imageData.widthHint = imageBounds.width + IDialogConstants.HORIZONTAL_SPACING;
84         messageImageLabel.setLayoutData(imageData);
85
86         messageText = new Text(messageComposite, SWT.NONE);
87         messageText.setEditable(false);
88         messageText.setBackground(parent.getDisplay().getSystemColor(
89                 SWT.COLOR_WIDGET_BACKGROUND));
90
91         GridData textData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL
92                 | GridData.VERTICAL_ALIGN_CENTER);
93         messageText.setLayoutData(textData);
94         hideRegion();
95
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      */

105     public void setMessageLayoutData(Object JavaDoc layoutData) {
106         messageComposite.setLayoutData(layoutData);
107     }
108
109     /**
110      * Show the new message in the message text and update the image. Base the
111      * background color on whether or not there are errors.
112      *
113      * @param newMessage
114      * The new value for the message
115      * @param newType
116      * One of the IMessageProvider constants. If newType is
117      * IMessageProvider.NONE show the title.
118      * @see IMessageProvider
119      */

120     public void updateText(String JavaDoc newMessage, int newType) {
121         Image newImage = null;
122         boolean showingError = false;
123         switch (newType) {
124         case IMessageProvider.NONE:
125             hideRegion();
126             return;
127         case IMessageProvider.INFORMATION:
128             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
129             break;
130         case IMessageProvider.WARNING:
131             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
132             break;
133         case IMessageProvider.ERROR:
134             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
135             showingError = true;
136             break;
137         }
138
139         if(newMessage == null){//No message so clear the area
140
hideRegion();
141             return;
142         }
143         showRegion();
144         // Any more updates required
145
if (newMessage.equals(messageText.getText())
146                 && newImage == messageImageLabel.getImage())
147             return;
148         messageImageLabel.setImage(newImage);
149         messageText.setText(newMessage);
150         if (showingError)
151             setMessageColors(JFaceColors.getErrorBackground(messageComposite.getDisplay()));
152         else {
153             lastMessageText = newMessage;
154             setMessageColors(JFaceColors.getBannerBackground(messageComposite.getDisplay()));
155         }
156
157     }
158
159     /**
160      * Show and enable the widgets in the message region
161      */

162     private void showRegion() {
163         messageComposite.setVisible(true);
164     }
165
166     /**
167      * Hide the message region and clear out the caches.
168      */

169     private void hideRegion() {
170         messageComposite.setVisible(false);
171         lastMessageText = null;
172         lastMessageType = IMessageProvider.NONE;
173     }
174
175     /**
176      * Set the colors of the message area.
177      *
178      * @param color
179      * The color to be use in the message area.
180      */

181     private void setMessageColors(Color color) {
182         messageText.setBackground(color);
183         messageComposite.setBackground(color);
184         messageImageLabel.setBackground(color);
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         updateText(lastMessageText, lastMessageType);
194     }
195 }
196
Popular Tags