KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > WarningsLabel


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.LayoutManager JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.swing.BoxLayout JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33
34 /**
35  * Component for displaying labels of warning text of which some items
36  * of text can be highlighted.
37  *
38  * To highlight items of text in a string they should be surrounded with
39  * curly braces {} for example;
40  *
41  * <pre>
42  * "This is a string with two {items of highlightable} text in {it}."
43  * </pre>
44  *
45  * @author Matthew Large
46  * @version $Revision: 1.1 $
47  *
48  */

49 public class WarningsLabel extends JPanel JavaDoc {
50
51     private ArrayList JavaDoc m_aLabels = new ArrayList JavaDoc(5);
52     private HashMap JavaDoc m_variables = new HashMap JavaDoc(3);
53
54
55     /**
56      * Constructs a new warnings label component.
57      *
58      * @param sText Text to display
59      */

60     public WarningsLabel(String JavaDoc sText) {
61         super();
62         setup(sText);
63     }
64
65     /**
66      * @param arg0
67      */

68     private WarningsLabel(boolean arg0) {
69         super(arg0);
70     }
71
72     /**
73      * @param arg0
74      */

75     private WarningsLabel(LayoutManager JavaDoc arg0) {
76         super(arg0);
77     }
78
79     /**
80      * @param arg0
81      * @param arg1
82      */

83     private WarningsLabel(LayoutManager JavaDoc arg0, boolean arg1) {
84         super(arg0, arg1);
85     }
86     
87     /**
88      * Initialises the component
89      *
90      * @param sText Text to display
91      */

92     private void setup(String JavaDoc sText) {
93         
94         BoxLayout JavaDoc layout = new BoxLayout JavaDoc(this, BoxLayout.LINE_AXIS);
95         this.setLayout(layout);
96         
97         boolean bInVar = false;
98         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(sText, "{}", true);
99         while(sTok.hasMoreElements()) {
100             String JavaDoc sTemp = (String JavaDoc)sTok.nextElement();
101             if(sTemp.equals("{")) {
102                 bInVar=true;
103             } else if(sTemp.equals("}")) {
104                 bInVar=false;
105             } else {
106                 JLabel JavaDoc label = new JLabel JavaDoc(sTemp);
107                 
108                 String JavaDoc fontName = "Dialog";
109                 int fontSize = 11;
110                 Font JavaDoc font = new Font JavaDoc(fontName, Font.PLAIN, fontSize);
111                 label.setFont(font);
112                 
113                 this.m_aLabels.add(label);
114                 if(bInVar) {
115                     this.m_variables.put(sTemp, label);
116                 }
117
118                 this.add(label);
119             }
120         }
121     }
122     
123     /**
124      * Sets whether an item of text is highlighted or not. Defaults to red.
125      *
126      * @param sVarText Text to set highlight on
127      * @param bHighLighted true if text is to be highlighted
128      */

129     public void setHighlight(String JavaDoc sVarText, boolean bHighLighted) {
130         this.setHighlight(sVarText, bHighLighted, Color.RED);
131     }
132     
133     /**
134      * Sets whether an item of text is highlighted or not and the color to
135      * highlight with.
136      *
137      * @param sVarText Text to set highlight on
138      * @param bHighLighted true if text is to be highlighted
139      * @param color Color to highlight with
140      */

141     public void setHighlight(String JavaDoc sVarText, boolean bHighLighted, Color JavaDoc color) {
142         JLabel JavaDoc label = (JLabel JavaDoc)this.m_variables.get(sVarText);
143         if(label!=null) {
144             if(bHighLighted) {
145                 label.setForeground(color);
146             } else {
147                 label.setForeground(Color.BLACK);
148             }
149         }
150     }
151     
152     /**
153      * Sets whether all items of highlightable text are highlighted
154      * or not. Defaults to red.
155      *
156      * @param bHighLighted true if all items of highlightable text are to be highlighted
157      */

158     public void setAllHighlights(boolean bHighLighted) {
159         this.setAllHighlights(bHighLighted, Color.RED);
160     }
161     
162     /**
163      * Sets whether all items of highlightable text are highlighted
164      * or not. Defaults to red.
165      *
166      * @param bHighLighted true if all items of highlightable text are to be highlighted
167      * @param color Color to highlight with
168      */

169     public void setAllHighlights(boolean bHighLighted, Color JavaDoc color) {
170         Iterator JavaDoc itor = this.m_variables.values().iterator();
171         while(itor.hasNext()) {
172             JLabel JavaDoc label = (JLabel JavaDoc)itor.next();
173             if(label!=null) {
174                 if(bHighLighted) {
175                     label.setForeground(color);
176                 } else {
177                     label.setForeground(Color.BLACK);
178                 }
179             }
180         }
181     }
182
183     /* (non-Javadoc)
184      * @see java.awt.Component#getPreferredSize()
185      */

186     public Dimension JavaDoc getPreferredSize() {
187         int nX = 0;
188         int nY = 0;
189         
190         Iterator JavaDoc itor = this.m_aLabels.iterator();
191         while(itor.hasNext()) {
192             JLabel JavaDoc label = (JLabel JavaDoc)itor.next();
193             Dimension JavaDoc dim = label.getPreferredSize();
194             nX = nX + dim.width;
195             nY = nY + dim.height;
196         }
197         return new Dimension JavaDoc(nX, 13);
198     }
199
200     /* (non-Javadoc)
201      * @see java.awt.Component#setFont(java.awt.Font)
202      */

203     public void setFont(Font JavaDoc font) {
204         super.setFont(font);
205         if(this.m_aLabels!=null) {
206             Iterator JavaDoc itor = this.m_aLabels.iterator();
207             while(itor.hasNext()) {
208                 JLabel JavaDoc label = (JLabel JavaDoc)itor.next();
209                 label.setFont(font);
210             }
211         }
212     }
213
214 }
215
Popular Tags