001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.graphics.Color;
014: import org.eclipse.swt.graphics.Image;
015: import org.eclipse.swt.graphics.Rectangle;
016: import org.eclipse.swt.layout.GridData;
017: import org.eclipse.swt.layout.GridLayout;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Label;
020: import org.eclipse.swt.widgets.Text;
021:
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.IDialogConstants;
024: import org.eclipse.jface.dialogs.IMessageProvider;
025: import org.eclipse.jface.resource.JFaceColors;
026: import org.eclipse.jface.resource.JFaceResources;
027:
028: /**
029: * The MessageRegion is the optional area to
030: * show messages in the page.
031: * <p>
032: * XXX: Copied from org.eclipse.jface.preference.PreferencePage.MessageRegion
033: * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=84061
034: * </p>
035: *
036: * @since 3.1
037: */
038: class MessageRegion {
039:
040: private Text messageText;
041:
042: private Label messageImageLabel;
043:
044: private Composite messageComposite;
045:
046: private String lastMessageText = "";//$NON-NLS-1$
047:
048: private int lastMessageType;
049:
050: /**
051: * Create a new instance of the receiver.
052: */
053: public MessageRegion() {
054: //No initial behavior
055: }
056:
057: /**
058: * Create the contents for the receiver.
059: *
060: * @param parent
061: * the Composite that the children will be created in
062: */
063: public void createContents(Composite parent) {
064: messageComposite = new Composite(parent, SWT.NONE);
065: GridLayout messageLayout = new GridLayout();
066: messageLayout.numColumns = 2;
067: messageLayout.marginWidth = 0;
068: messageLayout.marginHeight = 0;
069: messageLayout.makeColumnsEqualWidth = false;
070: messageComposite.setLayout(messageLayout);
071: messageImageLabel = new Label(messageComposite, SWT.NONE);
072:
073: GridData imageData = new GridData(
074: GridData.VERTICAL_ALIGN_CENTER);
075: Image sizingImage = JFaceResources
076: .getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
077: Rectangle imageBounds;
078: if (sizingImage == null)
079: imageBounds = new Rectangle(0, 0,
080: IDialogConstants.VERTICAL_MARGIN * 2,
081: IDialogConstants.VERTICAL_MARGIN * 2);
082: else
083: imageBounds = sizingImage.getBounds();
084: imageData.heightHint = imageBounds.height
085: + IDialogConstants.VERTICAL_SPACING;
086: imageData.widthHint = imageBounds.width
087: + IDialogConstants.HORIZONTAL_SPACING;
088: messageImageLabel.setLayoutData(imageData);
089:
090: messageText = new Text(messageComposite, SWT.NONE);
091: messageText.setEditable(false);
092: messageText.setBackground(parent.getDisplay().getSystemColor(
093: SWT.COLOR_WIDGET_BACKGROUND));
094:
095: GridData textData = new GridData(GridData.GRAB_HORIZONTAL
096: | GridData.FILL_HORIZONTAL
097: | GridData.VERTICAL_ALIGN_CENTER);
098: messageText.setLayoutData(textData);
099: hideRegion();
100:
101: }
102:
103: /**
104: * Set the layoutData for the messageArea. In most cases this will be a copy
105: * of the layoutData used in setTitleLayoutData.
106: *
107: * @param layoutData
108: * the layoutData for the message area composite.
109: */
110: public void setMessageLayoutData(Object layoutData) {
111: messageComposite.setLayoutData(layoutData);
112: }
113:
114: /**
115: * Show the new message in the message text and update the image. Base the
116: * background color on whether or not there are errors.
117: *
118: * @param newMessage
119: * The new value for the message
120: * @param newType
121: * One of the IMessageProvider constants. If newType is
122: * IMessageProvider.NONE show the title.
123: * @see IMessageProvider
124: */
125: public void updateText(String newMessage, int newType) {
126: Image newImage = null;
127: boolean showingError = false;
128: switch (newType) {
129: case IMessageProvider.NONE:
130: hideRegion();
131: return;
132: case IMessageProvider.INFORMATION:
133: newImage = JFaceResources
134: .getImage(Dialog.DLG_IMG_MESSAGE_INFO);
135: break;
136: case IMessageProvider.WARNING:
137: newImage = JFaceResources
138: .getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
139: break;
140: case IMessageProvider.ERROR:
141: newImage = JFaceResources
142: .getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
143: showingError = true;
144: break;
145: }
146:
147: if (newMessage == null) {//No message so clear the area
148: hideRegion();
149: return;
150: }
151: showRegion();
152: // Any more updates required
153: if (newMessage.equals(messageText.getText())
154: && newImage == messageImageLabel.getImage())
155: return;
156: messageImageLabel.setImage(newImage);
157: messageText.setText(newMessage);
158: if (showingError)
159: setMessageColors(JFaceColors
160: .getErrorBackground(messageComposite.getDisplay()));
161: else {
162: lastMessageText = newMessage;
163: setMessageColors(JFaceColors
164: .getBannerBackground(messageComposite.getDisplay()));
165: }
166:
167: }
168:
169: /**
170: * Show and enable the widgets in the message region
171: */
172: private void showRegion() {
173: messageComposite.setVisible(true);
174: }
175:
176: /**
177: * Hide the message region and clear out the caches.
178: */
179: private void hideRegion() {
180: messageComposite.setVisible(false);
181: lastMessageText = null;
182: lastMessageType = IMessageProvider.NONE;
183: }
184:
185: /**
186: * Set the colors of the message area.
187: *
188: * @param color
189: * The color to be use in the message area.
190: */
191: private void setMessageColors(Color color) {
192: messageText.setBackground(color);
193: messageComposite.setBackground(color);
194: messageImageLabel.setBackground(color);
195: }
196:
197: /**
198: * Clear the error message. Restore the previously displayed message if
199: * there is one, if not restore the title label.
200: *
201: */
202: public void clearErrorMessage() {
203: updateText(lastMessageText, lastMessageType);
204: }
205: }
|