📄 phrase.java
字号:
/** * Gets all the chunks in this element. * * @return an <CODE>ArrayList</CODE> */ public ArrayList getChunks() { ArrayList tmp = new ArrayList(); for (Iterator i = iterator(); i.hasNext(); ) { tmp.addAll(((Element) i.next()).getChunks()); } return tmp; } // overriding some of the ArrayList-methods /** * Adds a <CODE>Chunk</CODE>, an <CODE>Anchor</CODE> or another <CODE>Phrase</CODE> * to this <CODE>Phrase</CODE>. * * @param index index at which the specified element is to be inserted * @param o an object of type <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> * @throws ClassCastException when you try to add something that isn't a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> */ public void add(int index, Object o) { try { Element element = (Element) o; if (element.type() == Element.CHUNK) { Chunk chunk = (Chunk) element; if (!font.isStandardFont()) { chunk.setFont(font.difference(chunk.font())); } super.add(index, chunk); } else if (element.type() == Element.PHRASE || element.type() == Element.ANCHOR || element.type() == Element.ANNOTATION || element.type() == Element.TABLE || // line added by David Freels element.type() == Element.GRAPHIC) { super.add(index, element); } else { throw new ClassCastException(String.valueOf(element.type())); } } catch(ClassCastException cce) { throw new ClassCastException("Insertion of illegal Element: " + cce.getMessage()); } } /** * Adds a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or another <CODE>Phrase</CODE> * to this <CODE>Phrase</CODE>. * * @param o an object of type <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> * @return a boolean * @throws ClassCastException when you try to add something that isn't a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> */ public boolean add(Object o) { if (o instanceof String) { return super.add(new Chunk((String) o, font)); } try { Element element = (Element) o; switch(element.type()) { case Element.CHUNK: return addChunk((Chunk) o); case Element.PHRASE: case Element.PARAGRAPH: Phrase phrase = (Phrase) o; boolean success = true; Element e; for (Iterator i = phrase.iterator(); i.hasNext(); ) { e = (Element) i.next(); if (e instanceof Chunk) { success &= addChunk((Chunk)e); } else { success &= this.add(e); } } return success; case Element.ANCHOR: return super.add((Anchor) o); case Element.ANNOTATION: return super.add((Annotation) o); case Element.TABLE: // case added by David Freels return super.add((Table) o); case Element.LIST: return super.add((List) o); default: throw new ClassCastException(String.valueOf(element.type())); } } catch(ClassCastException cce) { throw new ClassCastException("Insertion of illegal Element: " + cce.getMessage()); } } /** * Adds a Chunk. * <p> * This method is a hack to solve a problem I had with phrases that were split between chunks * in the wrong place. */ private synchronized boolean addChunk(Chunk chunk) { if (!font.isStandardFont()) { chunk.setFont(font.difference(chunk.font())); } if (size() > 0 && !chunk.hasAttributes()) { try { Chunk previous = (Chunk) get(size() - 1); if (!previous.hasAttributes() && previous.font().compareTo(chunk.font()) == 0 && !"".equals(previous.content().trim()) && !"".equals(chunk.content().trim())) { previous.append(chunk.content()); return true; } } catch(ClassCastException cce) { } } return super.add(chunk); } /** * Adds a collection of <CODE>Chunk</CODE>s * to this <CODE>Phrase</CODE>. * * @param collection a collection of <CODE>Chunk</CODE>s, <CODE>Anchor</CODE>s and <CODE>Phrase</CODE>s. * @return <CODE>true</CODE> if the action succeeded, <CODE>false</CODE> if not. * @throws ClassCastException when you try to add something that isn't a <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE> */ public boolean addAll(Collection collection) { for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) { this.add(iterator.next()); } return true; } /** * Adds a <CODE>Object</CODE> to the <CODE>Paragraph</CODE>. * * @param object the object to add. */ protected void addSpecial(Object object) { super.add(object); } // methods /** * Sets the leading of this phrase. * * @param leading the new leading */ public void setLeading(float leading) { this.leading = leading; } // methods to retrieve information /** * Checks is this <CODE>Phrase</CODE> contains no or 1 empty <CODE>Chunk</CODE>. * * @return <CODE>false</CODE> if the <CODE>Phrase</CODE> * contains more than one or more non-empty<CODE>Chunk</CODE>s. */ public boolean isEmpty() { switch(size()) { case 0: return true; case 1: Element element = (Element) get(0); if (element.type() == Element.CHUNK && ((Chunk) element).isEmpty()) { return true; } return false; default: return false; } } /** * Checks you if the leading of this phrase is defined. * * @return true if the leading is defined */ public boolean leadingDefined() { if (Float.isNaN(leading)) { return false; } return true; } /** * Gets the leading of this phrase. * * @return the linespacing */ public float leading() { if (Float.isNaN(leading)) { return font.leading(1.5f); } return leading; } /** * Gets the font of the first <CODE>Chunk</CODE> that appears in this <CODE>Phrase</CODE>. * * @return a <CODE>Font</CODE> */ public Font font() { return font; } /** * Checks if a given tag corresponds with this object. * * @param tag the given tag * @return true if the tag corresponds */ public static boolean isTag(String tag) { return ElementTags.PHRASE.equals(tag); } /** * @see com.lowagie.text.MarkupAttributes#setMarkupAttribute(java.lang.String, java.lang.String) */ public void setMarkupAttribute(String name, String value) { markupAttributes = (markupAttributes == null) ? new Properties() : markupAttributes; markupAttributes.put(name, value); } /** * @see com.lowagie.text.MarkupAttributes#setMarkupAttributes(java.util.Properties) */ public void setMarkupAttributes(Properties markupAttributes) { this.markupAttributes = markupAttributes; } /** * @see com.lowagie.text.MarkupAttributes#getMarkupAttribute(java.lang.String) */ public String getMarkupAttribute(String name) { return (markupAttributes == null) ? null : String.valueOf(markupAttributes.get(name)); } /** * @see com.lowagie.text.MarkupAttributes#getMarkupAttributeNames() */ public Set getMarkupAttributeNames() { return Chunk.getKeySet(markupAttributes); } /** * @see com.lowagie.text.MarkupAttributes#getMarkupAttributes() */ public Properties getMarkupAttributes() { return markupAttributes; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -