1 50 51 package com.lowagie.text; 52 53 import java.util.ArrayList ; 54 import java.util.Collection ; 55 import java.util.Iterator ; 56 import java.util.Properties ; 57 58 84 85 public class Phrase extends ArrayList implements TextElementArray { 86 87 private static final long serialVersionUID = 2643594602455068231L; 89 90 92 protected float leading = Float.NaN; 93 94 95 protected Font font = new Font(); 96 97 99 102 public Phrase() { 103 this(16); 104 } 105 106 109 public Phrase(Phrase phrase) { 110 super(phrase); 111 leading = phrase.getLeading(); 112 font = phrase.getFont(); 113 } 114 115 120 public Phrase(float leading) { 121 this.leading = leading; 122 } 123 124 129 public Phrase(Chunk chunk) { 130 super.add(chunk); 131 font = chunk.getFont(); 132 } 133 134 141 public Phrase(float leading, Chunk chunk) { 142 this(leading); 143 super.add(chunk); 144 font = chunk.getFont(); 145 } 146 147 152 public Phrase(String string) { 153 this(Float.NaN, string, new Font()); 154 } 155 156 162 public Phrase(String string, Font font) { 163 this(Float.NaN, string, font); 164 this.font = font; 165 } 166 167 173 public Phrase(float leading, String string) { 174 this(leading, string, new Font()); 175 } 176 177 185 public Phrase(float leading, String string, Font font) { 186 this(leading); 187 this.font = font; 188 189 if (string != null && string.length() != 0) { 190 super.add(new Chunk(string, font)); 191 } 192 } 193 194 196 203 public boolean process(ElementListener listener) { 204 try { 205 for (Iterator i = iterator(); i.hasNext(); ) { 206 listener.add((Element) i.next()); 207 } 208 return true; 209 } 210 catch(DocumentException de) { 211 return false; 212 } 213 } 214 215 220 public int type() { 221 return Element.PHRASE; 222 } 223 224 229 public ArrayList getChunks() { 230 ArrayList tmp = new ArrayList (); 231 for (Iterator i = iterator(); i.hasNext(); ) { 232 tmp.addAll(((Element) i.next()).getChunks()); 233 } 234 return tmp; 235 } 236 237 239 247 public void add(int index, Object o) { 248 if (o == null) return; 249 try { 250 Element element = (Element) o; 251 if (element.type() == Element.CHUNK) { 252 Chunk chunk = (Chunk) element; 253 if (!font.isStandardFont()) { 254 chunk.setFont(font.difference(chunk.getFont())); 255 } 256 super.add(index, chunk); 257 } 258 else if (element.type() == Element.PHRASE || 259 element.type() == Element.ANCHOR || 260 element.type() == Element.ANNOTATION || 261 element.type() == Element.TABLE || element.type() == Element.MARKED) { 263 super.add(index, element); 264 } 265 else { 266 throw new ClassCastException (String.valueOf(element.type())); 267 } 268 } 269 catch(ClassCastException cce) { 270 throw new ClassCastException ("Insertion of illegal Element: " + cce.getMessage()); 271 } 272 } 273 274 282 public boolean add(Object o) { 283 if (o == null) return false; 284 if (o instanceof String ) { 285 return super.add(new Chunk((String ) o, font)); 286 } 287 try { 288 Element element = (Element) o; 289 switch(element.type()) { 290 case Element.CHUNK: 291 return addChunk((Chunk) o); 292 case Element.PHRASE: 293 case Element.PARAGRAPH: 294 Phrase phrase = (Phrase) o; 295 boolean success = true; 296 Element e; 297 for (Iterator i = phrase.iterator(); i.hasNext(); ) { 298 e = (Element) i.next(); 299 if (e instanceof Chunk) { 300 success &= addChunk((Chunk)e); 301 } 302 else { 303 success &= this.add(e); 304 } 305 } 306 return success; 307 case Element.MARKED: 308 case Element.ANCHOR: 309 case Element.ANNOTATION: 310 case Element.TABLE: case Element.PTABLE: case Element.LIST: 314 return super.add(o); 315 default: 316 throw new ClassCastException (String.valueOf(element.type())); 317 } 318 } 319 catch(ClassCastException cce) { 320 throw new ClassCastException ("Insertion of illegal Element: " + cce.getMessage()); 321 } 322 } 323 324 332 public boolean addAll(Collection collection) { 333 for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) { 334 this.add(iterator.next()); 335 } 336 return true; 337 } 338 339 347 protected synchronized boolean addChunk(Chunk chunk) { 348 if (!font.isStandardFont()) { 349 chunk.setFont(font.difference(chunk.getFont())); 350 } 351 if (size() > 0 && !chunk.hasAttributes()) { 352 try { 353 Chunk previous = (Chunk) get(size() - 1); 354 if (!previous.hasAttributes() && previous.getFont().compareTo(chunk.getFont()) == 0 && !"".equals(previous.getContent().trim()) && !"".equals(chunk.getContent().trim())) { 355 previous.append(chunk.getContent()); 356 return true; 357 } 358 } 359 catch(ClassCastException cce) { 360 } 361 } 362 return super.add(chunk); 363 } 364 365 370 protected void addSpecial(Object object) { 371 super.add(object); 372 } 373 374 376 381 382 public void setLeading(float leading) { 383 this.leading = leading; 384 } 385 386 390 public void setFont(Font font) { 391 this.font = font; 392 } 393 394 396 401 public float getLeading() { 402 if (Float.isNaN(leading)) { 403 return font.getCalculatedLeading(1.5f); 404 } 405 return leading; 406 } 407 408 413 public boolean hasLeading() { 414 if (Float.isNaN(leading)) { 415 return false; 416 } 417 return true; 418 } 419 420 425 public Font getFont() { 426 return font; 427 } 428 429 433 public String getContent() { 434 StringBuffer buf = new StringBuffer (); 435 for (Iterator i = getChunks().iterator(); i.hasNext(); ) { 436 buf.append(i.next().toString()); 437 } 438 return buf.toString(); 439 } 440 441 447 public boolean isEmpty() { 448 switch(size()) { 449 case 0: 450 return true; 451 case 1: 452 Element element = (Element) get(0); 453 if (element.type() == Element.CHUNK && ((Chunk) element).isEmpty()) { 454 return true; 455 } 456 return false; 457 default: 458 return false; 459 } 460 } 461 462 465 469 private Phrase(boolean dummy) { 470 } 471 472 477 public static final Phrase getInstance(String string) { 478 return getInstance(16, string, new Font()); 479 } 480 481 487 public static final Phrase getInstance(int leading, String string) { 488 return getInstance(leading, string, new Font()); 489 } 490 491 498 public static final Phrase getInstance(int leading, String string, Font font) { 499 Phrase p = new Phrase(true); 500 p.setLeading(leading); 501 p.font = font; 502 if (font.getFamily() != Font.SYMBOL && font.getFamily() != Font.ZAPFDINGBATS && font.getBaseFont() == null) { 503 int index; 504 while((index = SpecialSymbol.index(string)) > -1) { 505 if (index > 0) { 506 String firstPart = string.substring(0, index); 507 ((ArrayList )p).add(new Chunk(firstPart, font)); 508 string = string.substring(index); 509 } 510 Font symbol = new Font(Font.SYMBOL, font.getSize(), font.getStyle(), font.getColor()); 511 StringBuffer buf = new StringBuffer (); 512 buf.append(SpecialSymbol.getCorrespondingSymbol(string.charAt(0))); 513 string = string.substring(1); 514 while (SpecialSymbol.index(string) == 0) { 515 buf.append(SpecialSymbol.getCorrespondingSymbol(string.charAt(0))); 516 string = string.substring(1); 517 } 518 ((ArrayList )p).add(new Chunk(buf.toString(), symbol)); 519 } 520 } 521 if (string != null && string.length() != 0) { 522 ((ArrayList )p).add(new Chunk(string, font)); 523 } 524 return p; 525 } 526 527 529 535 public Phrase(Properties attributes) { 536 this(com.lowagie.text.factories.ElementFactory.getPhrase(attributes)); 537 } 538 544 public Font font() { 545 return getFont(); 546 } 547 553 public float leading() { 554 return getLeading(); 555 } 556 562 public boolean leadingDefined() { 563 return hasLeading(); 564 } 565 566 571 public String content() { 572 return getContent(); 573 } 574 } | Popular Tags |