KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > PreferenceMessageArea


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.jface.preference;
13
14 import org.eclipse.jface.dialogs.ControlAnimator;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.DialogMessageArea;
17 import org.eclipse.jface.dialogs.IMessageProvider;
18 import org.eclipse.jface.dialogs.ImageAndMessageArea;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.util.Policy;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CLabel;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.FormAttachment;
25 import org.eclipse.swt.layout.FormData;
26 import org.eclipse.swt.widgets.Composite;
27
28 /**
29  * The PreferenceMessageArea is a subclass of DialogMessageArea that provides a
30  * component for displaying a normal title message and an animated message area
31  * for displaying errors and warnings.
32  *
33  * @since 3.2
34  */

35 class PreferenceMessageArea extends DialogMessageArea {
36     
37     private String JavaDoc lastMessageText;
38
39     private int lastMessageType;
40
41     private CLabel titleLabel;
42
43     private ImageAndMessageArea messageArea;
44     
45     private PreferenceDialog preferenceDialog;
46     
47     private static int titleLabelOffset = -1;
48     
49     private ControlAnimator animator;
50
51     
52
53     /**
54      * Creates a new instance of the receiver and passes along the
55      * PreferenceDialog that creates it.
56      *
57      * @param preferenceDialog The PreferenceDialog that creates
58      * the message area.
59      */

60     public PreferenceMessageArea(PreferenceDialog preferenceDialog) {
61         this.preferenceDialog = preferenceDialog;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.dialogs.DialogMessageArea#createContents(org.eclipse.swt.widgets.Composite)
66      */

67     public void createContents(Composite parent) {
68            
69         // Message label
70
titleLabel = new CLabel(parent, SWT.NONE);
71         titleLabel.setFont(JFaceResources.getBannerFont());
72         
73         messageArea = new ImageAndMessageArea(preferenceDialog.formTitleComposite, SWT.WRAP);
74         messageArea.setFont(JFaceResources.getDialogFont());
75         messageArea.setVisible(false);
76         
77         animator = Policy.getAnimatorFactory().createAnimator(messageArea);
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.jface.dialogs.DialogMessageArea#setMessageLayoutData(java.lang.Object)
82      */

83     public void setMessageLayoutData(Object JavaDoc layoutData) {
84         ((FormData) layoutData).top = null;
85         ((FormData) layoutData).right = new FormAttachment(100,-getTitleLabelOffset());
86          messageArea.setLayoutData(layoutData);
87     }
88
89
90     /**
91      * Calculates the width difference between the title label and the parent composite
92      * of the message area. Required for when the workbench adds a toolbar to the
93      * preference dialog.
94      *
95      * @return the width difference if the calculation yields a value > 0, otherwise it
96      * will return 0.Subsequent calls will immediately return the width difference
97      * if it has already been calculated and is > 0.
98      */

99     private int getTitleLabelOffset() {
100         if (titleLabelOffset == -1) {
101             int offsetCalc = preferenceDialog.formTitleComposite.getBounds().width -
102                              titleLabel.getBounds().width;
103             if (offsetCalc > 0)
104                 return titleLabelOffset = offsetCalc;
105             
106             return 0;
107         }
108         return titleLabelOffset;
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jface.dialogs.DialogMessageArea#updateText(java.lang.String, int)
113      */

114     public void updateText(String JavaDoc newMessage, int newType) {
115         Image newImage = null;
116         switch (newType) {
117         case IMessageProvider.NONE:
118             if (newMessage == null) {
119                 restoreTitle();
120             } else {
121                 showTitle(newMessage, null);
122             }
123             return;
124         case IMessageProvider.INFORMATION:
125             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
126             break;
127         case IMessageProvider.WARNING:
128             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
129             break;
130         case IMessageProvider.ERROR:
131             newImage = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
132
133             break;
134         }
135
136         messageArea.moveAbove(null);
137         messageArea.setImage(newImage);
138         
139         messageArea.setText(newMessage);
140         messageArea.setToolTipText(newMessage);
141         lastMessageText = newMessage;
142         
143         int bottom = messageArea.getParent().getBounds().height;
144         if (!messageArea.isVisible() && messageArea.getBounds().y != bottom) {
145             messageArea.setBounds(messageArea.getBounds().x,
146                                   bottom,
147                                   titleLabel.getBounds().width,
148                                   messageArea.computeSize(SWT.DEFAULT,SWT.DEFAULT).y);
149         }
150         animator.setVisible(true);
151         setMessageLayoutData(messageArea.getLayoutData());
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.jface.dialogs.DialogMessageArea#restoreTitle()
156      */

157     public void restoreTitle() {
158         titleLabel.setVisible(true);
159         animator.setVisible(false);
160         lastMessageText = null;
161         lastMessageType = IMessageProvider.NONE;
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.jface.dialogs.DialogMessageArea#clearErrorMessage()
166      */

167     public void clearErrorMessage() {
168         if (lastMessageText == null) {
169             restoreTitle();
170         } else {
171             updateText(lastMessageText, lastMessageType);
172         }
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.jface.dialogs.DialogMessageArea#setTitleLayoutData(java.lang.Object)
177      */

178     public void setTitleLayoutData(Object JavaDoc layoutData) {
179         titleLabel.setLayoutData(layoutData);
180     }
181
182     /* (non-Javadoc)
183      * @see org.eclipse.jface.dialogs.DialogMessageArea#showTitle(java.lang.String, org.eclipse.swt.graphics.Image)
184      */

185     public void showTitle(String JavaDoc titleMessage, Image titleImage) {
186         titleLabel.setImage(titleImage);
187         titleLabel.setText(titleMessage);
188         restoreTitle();
189         return;
190     }
191 }
192
Popular Tags