KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > editors > ui > MessageArea


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * MessageArea.java -- synopsis.
21  *
22  */

23 package org.netbeans.modules.j2ee.sun.ide.editors.ui;
24
25 import java.awt.*;
26 import java.util.*;
27 import javax.swing.*;
28
29 /**
30  * A JEditorPane used for displaying messages in html format.
31  *
32  * @author Joe Warzecha
33  */

34
35 public class MessageArea extends JLabel {
36
37     private static final int WIDTH = 600;
38     private static final int [] sizeMap = {8, 10, 12, 14, 18, 24, 36};
39     private static final char [] hexVals = {'A', 'B', 'C', 'D', 'E', 'F'};
40
41     private int width;
42     private String JavaDoc fontString;
43     private boolean isBold;
44     private boolean isItalic;
45
46     private String JavaDoc msgs;
47     private String JavaDoc msgString;
48     private String JavaDoc endMsgs;
49     private String JavaDoc endMsgString;
50
51     private Vector bulletItems;
52     private String JavaDoc bulletItemString;
53
54     public MessageArea () {
55     super ();
56     bulletItems = new Vector ();
57     width = WIDTH;
58     }
59
60     public MessageArea (String JavaDoc text) {
61     this ();
62     setText (text);
63     }
64     
65     public void addNotify () {
66     super.addNotify ();
67
68     UIDefaults defs = UIManager.getDefaults ();
69     Color c = defs.getColor ("OptionPane.background");// NOI18N
70
if (c != null) {
71         setBackground (c);
72     }
73
74     c = defs.getColor ("OptionPane.foreground"); // NOI18N
75
if (c != null) {
76         setForeground (c);
77     }
78
79     if (fontString == null) {
80         makeFontString (getFont (), getForeground ());
81     }
82     }
83
84     private void makeEntireMsg () {
85     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc
86                 ("<html><table width=\"" + width + "\"><tr>"); // NOI18N
87
if (fontString != null) {
88         sbuf.append (fontString);
89     }
90     
91     if (msgString != null) {
92         sbuf.append (msgString);
93     }
94
95     if (bulletItemString != null) {
96         sbuf.append (bulletItemString);
97     }
98
99     if (endMsgString != null) {
100         sbuf.append (endMsgString);
101     }
102
103     if (isItalic) {
104         sbuf.append ("</i>"); // NOI18N
105
}
106
107     if (isBold) {
108         sbuf.append ("</b>"); // NOI18N
109
}
110
111     if (fontString != null) {
112         sbuf.append ("</font>"); // NOI18N
113
}
114
115     sbuf.append ("</tr></table></html>"); // NOI18N
116
super.setText (sbuf.toString ());
117     }
118
119     private void makeMsgString () {
120     if ((msgs == null) || (msgs.length () < 1)) {
121         msgString = null;
122     }
123     //msgString = "<p>" + msgs + "</p>";
124
msgString = msgs;
125     makeEntireMsg ();
126     }
127
128     private void makeEndMsgString () {
129     if ((endMsgs == null) || (endMsgs.length () < 1)) {
130         endMsgString = null;
131     }
132     //endMsgString = "<p>" + endMsgs + "</p>";
133
endMsgString = endMsgs;
134     makeEntireMsg ();
135     }
136
137     private String JavaDoc toHex (int i) {
138     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc ();
139         int val = i / 16;
140         if (val > 10) {
141             sbuf.append (hexVals [val - 10]);
142         } else {
143             sbuf.append (val);
144         }
145  
146         val = i % 16;
147         if (val > 10) {
148             sbuf.append (hexVals [val - 10]);
149         } else {
150             sbuf.append (val);
151         }
152
153         return sbuf.toString ();
154     }
155  
156     private String JavaDoc indexToOffset (int idx) {
157         int middle = sizeMap.length / 2;
158
159         if (idx < middle) {
160             return ("-" + (middle-idx)); //NOI18N
161
}
162
163         return ("+" + (idx - middle)); //NOI18N
164
}
165
166     private String JavaDoc closestSize (int ptSize) {
167     if (ptSize < sizeMap [0]) {
168        return indexToOffset (0);
169     }
170
171     if (ptSize >= sizeMap [sizeMap.length - 1]) {
172         return indexToOffset (sizeMap.length - 1);
173     }
174
175     for (int i = 0; i < sizeMap.length - 1; i++) {
176         if (ptSize == sizeMap [i]) {
177         return indexToOffset (i);
178         }
179
180         if ((ptSize > sizeMap [i]) && (ptSize < sizeMap [i + 1])) {
181         int diff1 = ptSize - sizeMap [i];
182         int diff2 = sizeMap [i + 1] - ptSize;
183         if (diff1 < diff2) {
184             return indexToOffset (i);
185         }
186
187         return indexToOffset (i + 1);
188         }
189     }
190
191     /* How'd we get here? return middle value */
192     return indexToOffset (sizeMap.length / 2);
193     }
194
195     private void makeFontString (Font f, Color c) { // BEGIN_NOI18N
196
StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc ("<font");
197     if (f != null) {
198         sbuf.append (" face=");
199         sbuf.append (f.getName ());
200         sbuf.append (" size=");
201         sbuf.append (closestSize (f.getSize ()));
202     }
203
204     if (c != null) {
205         sbuf.append (" color=\"#");
206         sbuf.append (toHex (c.getRed ()));
207         sbuf.append (toHex (c.getGreen ()));
208         sbuf.append (toHex (c.getBlue ()));
209         sbuf.append ("\"");
210     }
211     
212     sbuf.append (">");
213     if (f != null) {
214         if ((f.getStyle () & Font.BOLD) == Font.BOLD) {
215         sbuf.append ("<b>");
216         isBold = true;
217         }
218         
219         if ((f.getStyle () & Font.ITALIC) == Font.ITALIC) {
220         sbuf.append ("<i>");
221         isItalic = true;
222         }
223     }
224
225     fontString = sbuf.toString ();
226     makeEntireMsg ();
227     } // END_NOI18N
228

229     private void makeBulletItems () {
230     if (bulletItems.isEmpty ()) {
231         bulletItemString = null;
232     }
233
234     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc ();
235     sbuf.append ("<ul>"); // NOI18N
236
for (int i = 0; i < bulletItems.size (); i++) {
237         String JavaDoc s = (String JavaDoc) bulletItems.elementAt (i);
238         sbuf.append ("<li>" + s + "</li>"); // NOI18N
239
}
240     
241     sbuf.append ("</ul>"); // NOI18N
242
bulletItemString = sbuf.toString ();
243     makeEntireMsg ();
244     }
245
246     public void setWidth (int wid) {
247     width = wid;
248     makeEntireMsg ();
249     }
250
251     public void setFont (Font f) {
252     super.setFont (f);
253     makeFontString (f, getForeground ());
254     }
255
256     public void setForeground (Color c) {
257     super.setForeground (c);
258     makeFontString (getFont (), c);
259     }
260
261     private void tokenizeString (String JavaDoc s, Vector v) {
262     StringTokenizer st = new StringTokenizer (s, "\n"); // NOI18N
263
if (st.countTokens () > 1) {
264         while (st.hasMoreTokens ()) {
265         v.add (st.nextToken ());
266         }
267     } else {
268         v.add (s);
269     }
270     }
271
272     public void setText (String JavaDoc s) {
273     msgs = s.replace ('\n', ' ');
274     makeMsgString ();
275     }
276
277     public void setEndText (String JavaDoc s) {
278     endMsgs = s.replace ('\n', ' ');
279     makeEndMsgString ();
280     }
281
282     public void appendText (String JavaDoc s) {
283     String JavaDoc noNewLine = s.replace ('\n', ' ');
284     msgs = msgs.concat (noNewLine);
285     makeMsgString ();
286     }
287
288     public void setBulletItems (String JavaDoc s) {
289     bulletItems.clear ();
290     tokenizeString (s, bulletItems);
291     makeBulletItems ();
292     }
293
294     public void setBulletItems (java.util.List JavaDoc l) {
295     bulletItems.clear ();
296     bulletItems.addAll (l);
297     makeBulletItems ();
298     }
299
300     public void setBulletItems (String JavaDoc [] s) {
301     bulletItems.clear ();
302     for (int i = 0; i < s.length; i++) {
303         bulletItems.add (s [i]);
304     }
305     makeBulletItems ();
306     }
307
308     public void appendBulletItem (String JavaDoc s) {
309     tokenizeString (s, bulletItems);
310     makeBulletItems ();
311     }
312
313     public void appendBulletItems (String JavaDoc [] s) {
314     for (int i = 0; i < s.length; i++) {
315         bulletItems.add (s [i]);
316     }
317     makeBulletItems ();
318     }
319
320     public void appendBulletItems (java.util.List JavaDoc l) {
321     bulletItems.addAll (l);
322     makeBulletItems ();
323     }
324 }
325
Popular Tags