1 19 24 package org.netbeans.modules.j2ee.sun.ide.editors.ui; 25 26 import java.awt.*; 27 import java.awt.font.*; 28 import java.text.*; 29 import java.util.*; 30 import javax.accessibility.*; 31 import javax.swing.*; 32 33 38 39 public class MultiLineField extends JComponent implements Accessible { 40 41 44 private static final int CHARS_DISPLAYED = 80; 45 46 49 private static final int LINES_DISPLAYED = 4; 50 51 54 private static final int LINE_SPACING = 2; 55 56 59 private static final int DEFAULT_WIDTH = 400; 60 61 64 private static final int MIN_WIDTH = 50; 65 66 69 private static final String SPACE = " "; 71 74 private static final Insets INSETS = new Insets (0, 5, 5, 5); 75 76 79 protected static final Color ERROR_COLOR = Color.red; 80 81 private String [] stringsToDisplay; 82 private AttributedString [] attributedStrings; 83 private Object lockObject; 84 85 protected FontMetrics fontMetrics; 86 protected int totalWidth; 87 protected Dimension preferredSize; 88 89 private Insets insets; 90 private int numberLines; 91 private int lineHeight; 92 private int charHeight; 93 private int lineSpacing; 94 private boolean useMonospacedFont; 95 96 private boolean formatText; 97 98 public MultiLineField () { 99 super (); 100 lockObject = new Object (); 101 setText(new String [0]); 102 init (LINES_DISPLAYED); 103 } 104 105 public MultiLineField (String text) { 106 super (); 107 108 lockObject = new Object (); 109 setText (text); 110 init (stringsToDisplay.length); 111 } 112 113 public MultiLineField (String text, boolean format) { 114 this (text); 115 116 formatText = format; 117 } 118 119 public MultiLineField (String [] strs) { 120 super (); 121 122 lockObject = new Object (); 123 setText (strs); 124 init (stringsToDisplay.length); 125 } 126 127 private void init (int nLines) { 128 formatText = false; 129 insets = INSETS; 130 numberLines = nLines; 131 useMonospacedFont = false; 132 133 134 preferredSize = new Dimension (100, 20); 135 } 136 137 public void setInsets (Insets i) { 138 insets = i; 139 } 140 141 public void useMonospacedFont (boolean b) { 142 useMonospacedFont = b; 143 } 144 145 public void addNotify () { 146 super.addNotify (); 147 148 int ptSize = getFont ().getSize (); 149 if (useMonospacedFont) { 150 Font font = new Font ("Monospaced", Font.PLAIN, ptSize); setFont (font); 152 } 153 fontMetrics = getFontMetrics (getFont ()); 154 155 159 160 int width = 0; 161 if (stringsToDisplay == null) { 162 163 width = fontMetrics.charWidth ('v') * CHARS_DISPLAYED; 164 } else { 165 if (formatText) { 166 width = DEFAULT_WIDTH; 167 } else { 168 for (int i = 0; i < stringsToDisplay.length; i++) { 169 int lineWidth = fontMetrics.stringWidth 170 (stringsToDisplay [i]); 171 if (lineWidth > width) { 172 width = lineWidth; 173 } 174 } 175 } 176 } 177 178 charHeight = fontMetrics.getMaxAscent () + fontMetrics.getMaxDescent (); 179 180 184 lineSpacing = 0; 185 if (ptSize > 12) { 186 lineSpacing = LINE_SPACING + ((ptSize - 4) / 4); 187 } else { 188 lineSpacing = LINE_SPACING; 189 } 190 191 totalWidth = insets.left + insets.right + width; 192 lineHeight = charHeight + lineSpacing; 193 if (formatText) { 194 reformat (); 195 } 196 197 int totalHeight = insets.top + insets.bottom + 198 (charHeight * numberLines) + 199 (lineSpacing * (numberLines - 1)); 200 201 preferredSize = new Dimension (totalWidth, totalHeight); 202 } 203 204 private void reformat () { 205 if ((stringsToDisplay == null) || (stringsToDisplay.length < 1)) { 206 return; 207 } 208 209 String entireString = stringsToDisplay [0]; 210 for (int i = 1; i < stringsToDisplay.length; i++) { 211 entireString = entireString.concat (SPACE + stringsToDisplay [i]); 212 } 213 214 reformat (entireString); 215 } 216 217 private void reformat (String s) { 218 if (totalWidth < MIN_WIDTH) { 219 return; 220 } 221 222 int lineWidth = totalWidth - (insets.left + insets.right); 223 224 StringTokenizer st = new StringTokenizer (s, SPACE); 225 Vector v = new Vector (); 226 String prevString = st.nextToken (); 227 int curWidth; 228 String curString; 229 String curToken; 230 231 while (st.hasMoreTokens ()) { 232 curToken = st.nextToken (); 233 curString = prevString + SPACE + curToken; 234 curWidth = fontMetrics.stringWidth (curString); 235 if (curWidth > lineWidth) { 236 v.addElement (prevString); 237 prevString = curToken; 238 } else { 239 prevString = curString; 240 } 241 } 242 243 v.addElement (prevString); 244 245 numberLines = v.size (); 246 stringsToDisplay = new String [numberLines]; 247 v.copyInto (stringsToDisplay); 248 } 249 250 public Dimension getPreferredSize () { 251 return preferredSize; 252 } 253 254 public Dimension getMinimumSize () { 255 return preferredSize; 256 } 257 258 public void setSize (Dimension d) { 259 if (d.width != totalWidth) { 260 totalWidth = d.width; 261 if (formatText) { 262 reformat (); 263 } 264 } 265 266 super.setSize (d); 267 } 268 269 public void setBounds (Rectangle r) { 270 if (r.width != totalWidth) { 271 totalWidth = r.width; 272 if (formatText) { 273 reformat (); 274 } 275 } 276 277 super.setBounds (r); 278 } 279 280 public void setBounds (int x, int y, int width, int height) { 281 if (width != totalWidth) { 282 totalWidth = width; 283 if (formatText) { 284 reformat (); 285 } 286 } 287 288 super.setBounds (x, y, width, height); 289 } 290 291 public void paint (Graphics g) { 292 Dimension size = getSize (); 293 if (size.width != totalWidth) { 294 totalWidth = size.width; 295 } 296 297 g.clearRect (0, 0, size.width, size.height); 298 299 304 synchronized (lockObject) { 305 int offset = insets.top + fontMetrics.getMaxAscent (); 306 int blankLines = (numberLines - stringsToDisplay.length) / 2; 307 if (blankLines > 0) { 308 offset += (lineHeight * blankLines); 309 } 310 311 for (int i = 0; i < stringsToDisplay.length; i++) { 312 if (attributedStrings != null) { 313 attributedStrings [i].addAttribute (TextAttribute.FONT, 314 getFont ()); 315 g.drawString (attributedStrings [i].getIterator (), 316 insets.left, offset); 317 } else { 318 g.drawString (stringsToDisplay [i], insets.left, offset); 319 } 320 offset += lineHeight; 321 } 322 } 323 } 324 325 public void setText (AttributedString [] strs) { 326 synchronized (lockObject) { 327 attributedStrings = strs; 328 } 329 repaint(); 330 } 331 332 public void setText (String [] strs) { 333 synchronized (lockObject) { 334 stringsToDisplay = strs; 335 } 336 repaint(); 337 } 338 339 public String [] getText () { 340 String [] retStrings = null; 341 synchronized (lockObject) { 342 if ((stringsToDisplay != null) && (stringsToDisplay.length > 0)) { 343 retStrings = new String [stringsToDisplay.length]; 344 System.arraycopy (stringsToDisplay, 0, retStrings, 0, 345 stringsToDisplay.length); 346 } 347 } 348 349 return retStrings; 350 } 351 352 public void setText (String s) { 353 synchronized (lockObject) { 354 attributedStrings = null; 355 if (s == null) { 356 stringsToDisplay = new String [1]; 357 stringsToDisplay [0] = SPACE; 358 } else { 359 if (formatText) { 360 if (fontMetrics != null) { 361 reformat (s); 362 } else { 363 stringsToDisplay = new String [1]; 364 stringsToDisplay [0] = s; 365 } 366 } else { 367 StringTokenizer st = new StringTokenizer (s, "\n"); int nTokens = st.countTokens (); 369 stringsToDisplay = new String [nTokens]; 370 for (int i = 0; i < nTokens; i++) { 371 stringsToDisplay [i] = st.nextToken (); 372 } 373 } 374 } 375 } 376 } 377 378 public void setError (boolean val) { 379 if (val) { 380 if (stringsToDisplay == null) { 381 return; 382 } 383 attributedStrings = new AttributedString [stringsToDisplay.length]; 384 for (int i = 0; i < stringsToDisplay.length; i++) { 385 attributedStrings [i] = 386 new AttributedString (stringsToDisplay [i]); 387 attributedStrings [i].addAttribute (TextAttribute.FOREGROUND, 388 ERROR_COLOR); 389 } 390 } else { 391 attributedStrings = null; 392 } 393 } 394 395 public AccessibleContext getAccessibleContext() { 396 if (accessibleContext == null) { 397 accessibleContext = new AccessibleMultiLineField(); 398 } 399 return accessibleContext; 400 } 401 402 protected class AccessibleMultiLineField extends AccessibleJComponent { 403 404 public AccessibleRole getAccessibleRole () { 405 return AccessibleRole.LABEL; 406 } 407 } 408 } 409 | Popular Tags |