1 19 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 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 fontString; 43 private boolean isBold; 44 private boolean isItalic; 45 46 private String msgs; 47 private String msgString; 48 private String endMsgs; 49 private String endMsgString; 50 51 private Vector bulletItems; 52 private String bulletItemString; 53 54 public MessageArea () { 55 super (); 56 bulletItems = new Vector (); 57 width = WIDTH; 58 } 59 60 public MessageArea (String 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"); if (c != null) { 71 setBackground (c); 72 } 73 74 c = defs.getColor ("OptionPane.foreground"); 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 sbuf = new StringBuffer 86 ("<html><table width=\"" + width + "\"><tr>"); 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>"); } 106 107 if (isBold) { 108 sbuf.append ("</b>"); } 110 111 if (fontString != null) { 112 sbuf.append ("</font>"); } 114 115 sbuf.append ("</tr></table></html>"); super.setText (sbuf.toString ()); 117 } 118 119 private void makeMsgString () { 120 if ((msgs == null) || (msgs.length () < 1)) { 121 msgString = null; 122 } 123 msgString = msgs; 125 makeEntireMsg (); 126 } 127 128 private void makeEndMsgString () { 129 if ((endMsgs == null) || (endMsgs.length () < 1)) { 130 endMsgString = null; 131 } 132 endMsgString = endMsgs; 134 makeEntireMsg (); 135 } 136 137 private String toHex (int i) { 138 StringBuffer sbuf = new StringBuffer (); 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 indexToOffset (int idx) { 157 int middle = sizeMap.length / 2; 158 159 if (idx < middle) { 160 return ("-" + (middle-idx)); } 162 163 return ("+" + (idx - middle)); } 165 166 private String 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 192 return indexToOffset (sizeMap.length / 2); 193 } 194 195 private void makeFontString (Font f, Color c) { StringBuffer sbuf = new StringBuffer ("<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 } 229 private void makeBulletItems () { 230 if (bulletItems.isEmpty ()) { 231 bulletItemString = null; 232 } 233 234 StringBuffer sbuf = new StringBuffer (); 235 sbuf.append ("<ul>"); for (int i = 0; i < bulletItems.size (); i++) { 237 String s = (String ) bulletItems.elementAt (i); 238 sbuf.append ("<li>" + s + "</li>"); } 240 241 sbuf.append ("</ul>"); 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 s, Vector v) { 262 StringTokenizer st = new StringTokenizer (s, "\n"); 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 s) { 273 msgs = s.replace ('\n', ' '); 274 makeMsgString (); 275 } 276 277 public void setEndText (String s) { 278 endMsgs = s.replace ('\n', ' '); 279 makeEndMsgString (); 280 } 281 282 public void appendText (String s) { 283 String noNewLine = s.replace ('\n', ' '); 284 msgs = msgs.concat (noNewLine); 285 makeMsgString (); 286 } 287 288 public void setBulletItems (String s) { 289 bulletItems.clear (); 290 tokenizeString (s, bulletItems); 291 makeBulletItems (); 292 } 293 294 public void setBulletItems (java.util.List l) { 295 bulletItems.clear (); 296 bulletItems.addAll (l); 297 makeBulletItems (); 298 } 299 300 public void setBulletItems (String [] 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 s) { 309 tokenizeString (s, bulletItems); 310 makeBulletItems (); 311 } 312 313 public void appendBulletItems (String [] 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 l) { 321 bulletItems.addAll (l); 322 makeBulletItems (); 323 } 324 } 325 | Popular Tags |