1 23 24 package com.sun.enterprise.tools.upgrade.gui; 25 26 import java.awt.*; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.text.BreakIterator ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 public class FlowLabel extends Canvas 34 { 35 36 private String labelText; 37 private Vector textVector; 38 private transient Object textVectorLock; 39 private static Font labelFont = null; 40 private static String addAttr[] = { 41 "<bold>", "<b>", "<italics>", "<i>", "<tt>", "<br>", "<p>" 42 }; 43 private static int attrBold = 3; 44 private static int attrItalics = 12; 45 private static int attrTt = 16; 46 private static int attrBreak; 47 private static int attrParagraph; 48 private static int attrFlowControlOnly; 49 private static String removeAttr[] = { 50 "</bold>", "</b>", "</italics>", "</i>", "</tt>", "</br>", "</p>" 51 }; 52 private transient Dimension currentSize; 53 private transient Object currentSizeLock; 54 private transient Vector paintVector; 55 private transient Object paintVectorLock; 56 private int horizontalMargin; 57 private int verticalMargin; 58 private transient boolean resizePerformed; 59 static final long serialVersionUID = 0xc821ccd1154af049L; 60 61 public FlowLabel() 62 { 63 labelText = ""; 64 textVector = new Vector (); 65 textVectorLock = new Object (); 66 currentSize = new Dimension(0, 0); 67 currentSizeLock = new Object (); 68 paintVector = new Vector (); 69 paintVectorLock = new Object (); 70 horizontalMargin = 10; 71 verticalMargin = 5; 72 resizePerformed = false; 73 } 74 75 public FlowLabel(String s) 76 { 77 labelText = ""; 78 textVector = new Vector (); 79 textVectorLock = new Object (); 80 currentSize = new Dimension(0, 0); 81 currentSizeLock = new Object (); 82 paintVector = new Vector (); 83 paintVectorLock = new Object (); 84 horizontalMargin = 10; 85 verticalMargin = 5; 86 resizePerformed = false; 87 labelText = s; 88 textVector = createTextVector(); 89 } 90 91 private Vector createPaintVector(Dimension dimension) 92 { 93 Vector vector = new Vector (); 94 try 95 { 96 Dimension dimension1 = new Dimension(dimension); 97 dimension1.width -= horizontalMargin * 2; 98 dimension1.height -= verticalMargin * 2; 99 int i = 0; 100 boolean flag = false; 101 Vector vector1 = new Vector (); 102 String s = new String (); 103 Enumeration enumeration = null; 104 boolean flag1 = false; 105 int i1 = 0; 106 boolean flag2 = false; 107 int k1 = 0; 108 Font font = getFont(); 109 font = new Font(font.getName(), 0, font.getSize()); 110 FontMetrics fontmetrics = getToolkit().getFontMetrics(font); 111 synchronized(textVector) 112 { 113 enumeration = textVector.elements(); 114 } 115 while(enumeration.hasMoreElements()) 116 { 117 Object obj = enumeration.nextElement(); 118 if(obj instanceof Integer ) 119 { 120 int l1 = ((Integer )obj).intValue(); 121 if((l1 & attrBreak) != 0) 122 { 123 vector.addElement(vector1); 124 vector1 = new Vector (); 125 int j = i1; 126 k1 = 0; 127 i = 0; 128 } else 129 if((l1 & attrParagraph) != 0) 130 { 131 vector.addElement(vector1); 132 vector1 = new Vector (); 133 vector.addElement(vector1); 134 vector1 = new Vector (); 135 int k = i1; 136 k1 = 0; 137 i = 0; 138 } 139 font = getNewFont(font, ((Integer )obj).intValue()); 140 fontmetrics = getToolkit().getFontMetrics(font); 141 vector1.addElement(obj); 142 } else 143 { 144 String s1 = (String )obj; 145 int l = 0; 146 for(i1 = 0; i1 < s1.length() - 1;) 147 { 148 if(k1 == 0) 149 { 150 l = skipWhitespace(s1, l); 151 i1 = getNextWordIndex(s1, i1); 152 k1++; 153 } 154 for(int j1 = getNextWordIndex(s1, i1); j1 <= s1.length() && fontmetrics.stringWidth(s1.substring(l, j1)) + i < dimension1.width;) 155 { 156 i1 = j1; 157 j1 = getNextWordIndex(s1, i1); 158 k1++; 159 } 160 161 if(i1 >= s1.length() - 1) 162 { 163 i = fontmetrics.stringWidth(s1.substring(l, i1)) + i; 164 vector1.addElement(s1.substring(l, i1)); 165 } else 166 { 167 vector1.addElement(s1.substring(l, i1)); 168 vector.addElement(vector1); 169 vector1 = new Vector (); 170 l = i1; 171 k1 = 0; 172 i = 0; 173 } 174 } 175 176 } 177 } 178 if(vector1.size() > 0) 179 { 180 vector.addElement(vector1); 181 } 182 } 183 catch(Exception _ex) 184 { 185 return createPaintVector(dimension); 186 } 187 return vector; 188 } 189 190 private Vector createTextVector() 191 { 192 Vector vector = new Vector (); 193 int i = 0; 194 int j = 0; 195 StringBuffer stringbuffer = new StringBuffer (); 196 boolean flag = false; 197 while(i < labelText.length()) 198 { 199 boolean flag1 = false; 200 while(i < labelText.length() && labelText.charAt(i) == '<') 201 { 202 for(int k = 0; k < addAttr.length; k++) 203 { 204 if(labelText.length() >= i + addAttr[k].length() && labelText.substring(i, i + addAttr[k].length()).equalsIgnoreCase(addAttr[k])) 205 { 206 j |= 1 << k; 207 i += addAttr[k].length(); 208 flag1 = true; 209 } 210 if(labelText.length() >= i + removeAttr[k].length() && labelText.substring(i, i + removeAttr[k].length()).equalsIgnoreCase(removeAttr[k])) 211 { 212 j &= 255 - (1 << k); 213 i += removeAttr[k].length(); 214 flag1 = (1 << k & attrFlowControlOnly) == 0; 215 } 216 } 217 218 if(!flag1) 219 { 220 break; 221 } 222 } 223 if(flag1) 224 { 225 int l = stringbuffer.length(); 226 if(l > 0) 227 { 228 vector.addElement(stringbuffer.toString()); 229 stringbuffer = new StringBuffer (); 230 } 231 vector.addElement(new Integer (j)); 232 j &= ~attrFlowControlOnly; 233 } else 234 { 235 if(labelText.charAt(i) == '\n') 236 { 237 if(stringbuffer.length() > 0) 238 { 239 vector.addElement(stringbuffer.toString()); 240 stringbuffer = new StringBuffer (); 241 } 242 vector.addElement(new Integer (attrBreak)); 243 i++; 244 } 245 if(i < labelText.length()) 246 { 247 stringbuffer.append(labelText.charAt(i)); 248 i++; 249 } 250 } 251 } 252 if(stringbuffer.length() > 0) 253 { 254 vector.addElement(stringbuffer.toString()); 255 } 256 return vector; 257 } 258 259 public void forceResize() 260 { 261 if(!resizePerformed) 262 { 263 resizePerformed = true; 264 invalidate(); 265 Container container; 266 for(container = getParent(); container != null && container.getParent() != null; container = container.getParent()) { } 267 container.validate(); 268 } 269 } 270 271 public String getContentString() 272 { 273 String s = ""; 274 Vector vector = createTextVector(); 275 for(Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();) 276 { 277 Object obj = enumeration.nextElement(); 278 if(obj instanceof String ) 279 { 280 s = s + obj; 281 } 282 } 283 284 return s; 285 } 286 287 public Dimension getMinimumSize() 288 { 289 return getPreferredSize(); 290 } 291 292 private Font getNewFont(Font font, int i) 293 { 294 if(font != null) 295 { 296 if(labelFont == null && (i & attrTt) != 0) 297 { 298 labelFont = font; 299 font = new Font("Monospaced", 0, font.getSize()); 300 if(font == null) 301 { 302 font = labelFont; 303 labelFont = null; 304 } 305 } else 306 if(labelFont != null && (i & attrTt) == 0) 307 { 308 font = labelFont; 309 labelFont = null; 310 } 311 font = new Font(font.getName(), ((i & attrBold) == 0 ? 0 : 1) | ((i & attrItalics) == 0 ? 0 : 2), font.getSize()); 312 } 313 return font; 314 } 315 316 private int getNextWordIndex(String s, int i) 317 { 318 int j; 319 for(j = i + 1; j < s.length() && Character.isWhitespace(s.charAt(j)); j++) { } 320 if(j < s.length()) 321 { 322 BreakIterator breakiterator = BreakIterator.getLineInstance(); 323 breakiterator.setText(s.substring(j, s.length())); 324 int k = breakiterator.first(); 325 k = breakiterator.next(); 326 if(k != -1) 327 { 328 if(s.charAt((k + j) - 1) == '.') 329 { 330 BreakIterator breakiterator1 = BreakIterator.getSentenceInstance(); 331 breakiterator1.setText(s); 332 int l; 333 for(l = breakiterator1.first(); l != -1 && l < k + j; l = breakiterator1.next()) { } 334 if(l != k + j) 335 { 336 return getNextWordIndex(s, k + j); 337 } 338 } 339 return k + j; 340 } 341 } 342 return j; 343 } 344 345 public Dimension getPreferredSize() 346 { 347 Container container = getParent(); 348 Dimension dimension = container.getSize(); 349 Dimension dimension1 = new Dimension(1, 1); 350 if(dimension.width == 0) 351 { 352 dimension = new Dimension(400, 200); 353 } 354 Insets insets = container.getInsets(); 355 try 356 { 357 Font font = getNewFont(getFont(), 0); 358 FontMetrics fontmetrics = getToolkit().getFontMetrics(font); 359 dimension1 = new Dimension(dimension.width - insets.right - insets.left, dimension.height - insets.top - insets.bottom); 360 synchronized(currentSizeLock) 361 { 362 synchronized(paintVectorLock) 363 { 364 Vector vector = createPaintVector(dimension1); 365 dimension1.height = verticalMargin * 2 + fontmetrics.getHeight() * vector.size(); 366 } 367 } 368 } 369 catch(Error _ex) 370 { 371 return getPreferredSize(); 372 } 373 return dimension1; 374 } 375 376 public void paint(Graphics g) 377 { 378 Dimension dimension = getSize(); 379 synchronized(currentSizeLock) 380 { 381 if(currentSize.width != dimension.width) 382 { 383 sizeChanged(dimension); 384 } 385 } 386 Dimension dimension1 = getPreferredSize(); 387 if(dimension.height < dimension1.height && !resizePerformed) 388 { 389 forceResize(); 390 return; 391 } 392 g.setClip(0, 0, dimension.width, dimension.height); 393 g.setColor(getForeground()); 394 Font font = getFont(); 395 font = new Font(font.getName(), 0, font.getSize()); 396 FontMetrics fontmetrics = getToolkit().getFontMetrics(font); 397 g.setFont(font); 398 int i = horizontalMargin; 399 int j = fontmetrics.getMaxAscent() + verticalMargin; 400 synchronized(paintVectorLock) 401 { 402 for(int k = 0; k < paintVector.size(); k++) 403 { 404 Vector vector = (Vector )paintVector.elementAt(k); 405 for(int l = 0; l < vector.size(); l++) 406 { 407 Object obj2 = vector.elementAt(l); 408 if(obj2 instanceof Integer ) 409 { 410 font = getNewFont(font, ((Integer )obj2).intValue()); 411 fontmetrics = getToolkit().getFontMetrics(font); 412 g.setFont(font); 413 } 414 if(obj2 instanceof String ) 415 { 416 try 417 { 418 g.drawString((String )obj2, i, j); 419 i += fontmetrics.stringWidth((String )obj2); 420 } 421 catch(Exception _ex) 422 { 423 repaint(); 424 } 425 } 426 } 427 428 i = horizontalMargin; 429 j += fontmetrics.getHeight(); 430 } 431 432 } 433 } 434 435 private void readObject(ObjectInputStream objectinputstream) 436 throws IOException 437 { 438 try 439 { 440 objectinputstream.defaultReadObject(); 441 } 442 catch(ClassNotFoundException classnotfoundexception) 443 { 444 classnotfoundexception.printStackTrace(); 445 } 446 finally 447 { 448 textVectorLock = new Object (); 449 paintVectorLock = new Object (); 450 paintVector = new Vector (); 451 currentSize = new Dimension(0, 0); 452 currentSizeLock = new Object (); 453 } 454 } 455 456 public void setHgap(int i) 457 { 458 horizontalMargin = i; 459 } 460 461 public void setText(String s) 462 { 463 labelText = s; 464 synchronized(textVectorLock) 465 { 466 textVector = createTextVector(); 467 } 468 Dimension dimension = getSize(); 469 if(dimension.width > 0 || dimension.height > 0) 470 { 471 synchronized(paintVectorLock) 472 { 473 paintVector = createPaintVector(dimension); 474 } 475 } 476 if(isShowing()) 477 { 478 invalidate(); 479 repaint(); 480 } 481 } 482 483 public void setVgap(int i) 484 { 485 verticalMargin = i; 486 } 487 488 public void sizeChanged(Dimension dimension) 489 { 490 synchronized(currentSizeLock) 491 { 492 Dimension dimension1 = currentSize; 493 if(!dimension.equals(dimension1)) 494 { 495 try 496 { 497 Graphics g = getGraphics(); 498 g.setColor(getBackground()); 499 g.fillRect(0, 0, dimension1.width + 1, dimension1.height + 1); 500 } 501 catch(Exception _ex) { } 502 } 503 if(dimension.width != dimension1.width) 504 { 505 synchronized(paintVectorLock) 506 { 507 paintVector = createPaintVector(dimension); 508 } 509 } 510 currentSize = dimension; 511 resizePerformed = false; 512 } 513 } 514 515 private int skipWhitespace(String s, int i) 516 { 517 int j; 518 for(j = i; j < s.length() && Character.isWhitespace(s.charAt(j)); j++) { } 519 return j; 520 } 521 522 static 523 { 524 attrBreak = 32; 525 attrParagraph = 64; 526 attrFlowControlOnly = attrBreak | attrParagraph; 527 } 528 } 529 | Popular Tags |