1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45:
48: public class DefaultStyledDocument extends AbstractDocument
49: implements StyledDocument
50: {
51: public class ElementBuffer
52: implements Serializable
53: {
54: private Element root;
55:
56: public ElementBuffer(Element root)
57: {
58: this.root = root;
59: }
60:
61: public Element getRootElement()
62: {
63: return root;
64: }
65: }
66:
67: public static final int BUFFER_SIZE_DEFAULT = 4096;
68:
69: protected DefaultStyledDocument.ElementBuffer buffer;
70:
71: public DefaultStyledDocument()
72: {
73: this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleContext());
74: }
75:
76: public DefaultStyledDocument(StyleContext context)
77: {
78: this(new GapContent(BUFFER_SIZE_DEFAULT), context);
79: }
80:
81: public DefaultStyledDocument(AbstractDocument.Content content,
82: StyleContext context)
83: {
84: super(content, context);
85: buffer = new ElementBuffer(createDefaultRoot());
86: setLogicalStyle(0, context.getStyle(StyleContext.DEFAULT_STYLE));
87: }
88:
89: public Style addStyle(String nm, Style parent)
90: {
91: StyleContext context = (StyleContext) getAttributeContext();
92: return context.addStyle(nm, parent);
93: }
94:
95: protected AbstractDocument.AbstractElement createDefaultRoot()
96: {
97: Element[] tmp;
98: BranchElement section = new BranchElement(null, null);
99:
100: BranchElement paragraph = new BranchElement(section, null);
101: tmp = new Element[1];
102: tmp[0] = paragraph;
103: section.replace(0, 0, tmp);
104:
105: LeafElement leaf = new LeafElement(paragraph, null, 0, 1);
106: tmp = new Element[1];
107: tmp[0] = leaf;
108: paragraph.replace(0, 0, tmp);
109:
110: return section;
111: }
112:
113: public Element getCharacterElement(int position)
114: {
115: Element element = getDefaultRootElement();
116:
117: while (! element.isLeaf())
118: {
119: int index = element.getElementIndex(position);
120: element = element.getElement(index);
121: }
122:
123: return element;
124: }
125:
126: public Color getBackground(AttributeSet attributes)
127: {
128: StyleContext context = (StyleContext) getAttributeContext();
129: return context.getBackground(attributes);
130: }
131:
132: public Element getDefaultRootElement()
133: {
134: return buffer.getRootElement();
135: }
136:
137: public Font getFont(AttributeSet attributes)
138: {
139: StyleContext context = (StyleContext) getAttributeContext();
140: return context.getFont(attributes);
141: }
142:
143: public Color getForeground(AttributeSet attributes)
144: {
145: StyleContext context = (StyleContext) getAttributeContext();
146: return context.getForeground(attributes);
147: }
148:
149: public Style getLogicalStyle(int position)
150: {
151: Element paragraph = getParagraphElement(position);
152: AttributeSet attributes = paragraph.getAttributes();
153: return (Style) attributes.getResolveParent();
154: }
155:
156: public Element getParagraphElement(int position)
157: {
158: Element element = getCharacterElement(position);
159: return element.getParentElement();
160: }
161:
162: public Style getStyle(String nm)
163: {
164: StyleContext context = (StyleContext) getAttributeContext();
165: return context.getStyle(nm);
166: }
167:
168: public void removeStyle(String nm)
169: {
170: StyleContext context = (StyleContext) getAttributeContext();
171: context.removeStyle(nm);
172: }
173:
174: public void setCharacterAttributes(int offset, int length,
175: AttributeSet attributes,
176: boolean replace)
177: {
178:
179: throw new Error("not implemented");
180: }
181:
182: public void setLogicalStyle(int position, Style style)
183: {
184: Element el = getParagraphElement(position);
185: if (el instanceof AbstractElement)
186: {
187: AbstractElement ael = (AbstractElement) el;
188: ael.setResolveParent(style);
189: }
190: else
191: throw new AssertionError("paragraph elements are expected to be"
192: + "instances of javax.swing.text.AbstractDocument.AbstractElement");
193: }
194:
195: public void setParagraphAttributes(int offset, int length,
196: AttributeSet attributes,
197: boolean replace)
198: {
199:
200: throw new Error("not implemented");
201: }
202: }