KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > util > MessageLine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.util;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.CLabel;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Composite;
20
21 /**
22  * A message line. It distinguishs between "normal" messages and errors.
23  * Setting an error message hides a currently displayed message until
24  * <code>clearErrorMessage</code> is called.
25  */

26 public class MessageLine extends CLabel {
27
28     private String JavaDoc fMessageText;
29     private String JavaDoc fErrorText;
30
31     private Color fDefaultColor;
32     private RGB fErrorRGB;
33     private Color fErrorColor;
34
35     private static RGB fgErrorRGB= new RGB(200, 0, 0);
36
37     /**
38      * Creates a new message line as a child of the given parent.
39      * Error message will be shown with in the rgb color 200,0,0.
40      */

41     public MessageLine(Composite parent) {
42         this(parent, SWT.LEFT);
43     }
44
45     /**
46      * Creates a new message line as a child of the parent and with the given SWT stylebits.
47      * Error message will be shown with in the rgb color 200,0,0.
48      */

49     public MessageLine(Composite parent, int style) {
50         super(parent, style);
51         fDefaultColor= getForeground();
52         fErrorRGB= fgErrorRGB;
53     }
54
55     /**
56      * Creates a new message line as a child of the parent and with the given SWT stylebits.
57      * Error message will be shown with in the given rgb color.
58      */

59     public MessageLine(Composite parent, int style, RGB errorRGB) {
60         super(parent, style);
61         fDefaultColor= getForeground();
62         fErrorRGB= errorRGB;
63     }
64
65     /**
66      * Clears the currently displayed error message and redisplayes
67      * the message which was active before the error message was set.
68      */

69     public void clearErrorMessage() {
70         setErrorMessage(null);
71     }
72
73     /**
74      * Clears the currently displayed message.
75      */

76     public void clearMessage() {
77         setMessage(null);
78     }
79
80     /**
81      * Get the currently displayed error text.
82      * @return The error message. If no error message is displayed <code>null</code> is returned.
83      */

84     public String JavaDoc getErrorMessage() {
85         return fErrorText;
86     }
87
88     /**
89      * Get the currently displayed message.
90      * @return The message. If no message is displayed <code>null</code> is returned.
91      */

92     public String JavaDoc getMessage() {
93         return fMessageText;
94     }
95
96     /**
97      * Sets the default error color used by all message lines.
98      * Note: a call to this method only affects newly created MessageLines not existing ones.
99      */

100     public static void setDefaultErrorColor(RGB color) {
101         fgErrorRGB= color;
102     }
103
104     /**
105      * Display the given error message. A currently displayed message
106      * is saved and will be redisplayed when the error message is cleared.
107      */

108     public void setErrorMessage(String JavaDoc message) {
109         fErrorText= message;
110
111         if (message == null) {
112             setMessage(fMessageText);
113         } else {
114             if (fErrorColor == null) {
115                 fErrorColor= new Color(getDisplay(), fErrorRGB);
116                 addDisposeListener(new DisposeListener() {
117                     public void widgetDisposed(DisposeEvent e) {
118                         getErrorColor().dispose();
119                     }
120                 });
121             }
122             setForeground(fErrorColor);
123             setText(message);
124         }
125     }
126
127     /**
128      * Set the message text. If the message line currently displays an error,
129      * the message is stored and will be shown after a call to clearErrorMessage
130      */

131     public void setMessage(String JavaDoc message) {
132         fMessageText= message;
133         if (message == null)
134             message= ""; //$NON-NLS-1$
135
if (fErrorText == null) {
136             setForeground(fDefaultColor);
137             setText(message);
138         }
139     }
140     
141     protected Color getErrorColor() {
142         return fErrorColor;
143     }
144 }
145
Popular Tags