001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.util;
029:
030: import java.text.AttributedString;
031: import java.util.ArrayList;
032: import java.util.List;
033: import java.util.Map;
034:
035: /**
036: * @author Teodor Danciu (teodord@users.sourceforge.net)
037: * @version $Id: JRStyledText.java 1229 2006-04-19 10:27:35Z teodord $
038: */
039: public class JRStyledText {
040:
041: /**
042: *
043: */
044: private StringBuffer sbuffer = new StringBuffer();
045: private List runs = new ArrayList();
046: private AttributedString attributedString = null;
047:
048: /**
049: *
050: */
051: public JRStyledText() {
052: }
053:
054: /**
055: *
056: */
057: public void append(String text) {
058: sbuffer.append(text);
059: attributedString = null;
060: }
061:
062: /**
063: *
064: */
065: public void addRun(Run run) {
066: runs.add(run);
067: attributedString = null;
068: }
069:
070: /**
071: *
072: */
073: public int length() {
074: return sbuffer.length();
075: }
076:
077: /**
078: *
079: */
080: public String getText() {
081: return sbuffer.toString();
082: }
083:
084: /**
085: *
086: */
087: public AttributedString getAttributedString() {
088: if (attributedString == null) {
089: attributedString = new AttributedString(sbuffer.toString());
090:
091: for (int i = runs.size() - 1; i >= 0; i--) {
092: Run run = (Run) runs.get(i);
093: if (run.startIndex != run.endIndex) {
094: attributedString.addAttributes(run.attributes,
095: run.startIndex, run.endIndex);
096: }
097: }
098: }
099:
100: return attributedString;
101: }
102:
103: /**
104: *
105: */
106: public List getRuns() {
107: return runs;
108: }
109:
110: /**
111: *
112: */
113: public static class Run {
114: /**
115: *
116: */
117: public Map attributes = null;
118: public int startIndex = 0;
119: public int endIndex = 0;
120:
121: /**
122: *
123: */
124: public Run(Map attributes, int startIndex, int endIndex) {
125: this.attributes = attributes;
126: this.startIndex = startIndex;
127: this.endIndex = endIndex;
128: }
129: }
130:
131: }
|