Source code: org/gendiapo/editor/document/GenDiapoDocument.java
1 /*
2 * @(#)GenDiapoDocument.java 1.000 01/05/30
3 */
4 package org.gendiapo.editor.document;
5
6 import java.awt.Color;
7 import java.awt.Component;
8 import java.util.*;
9 import java.io.*;
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import javax.swing.text.*;
13 import javax.swing.undo.*;
14
15 //import org.merlotxml.merlot.*; // ref a MerlotDOMNode pour BranchElement.getName()
16
17 public class GenDiapoDocument extends DefaultStyledDocument {
18
19 public static final Object EDITION_TYPE = "editionType";
20 // Attributs
21 public static final Object ATTRIBUT_MERLOTNODE = "merlotNode";
22 public static final Object ATTRIBUT_MERLOTTEXT = "merlotText";
23 public static final Object ATTRIBUT_XMLNODE = "xmlNode";
24 public static final Object ATTRIBUT_DTDATTRIBUTE = "dtdAttribute";
25 public static final Object ATTRIBUT_TYPE = "type";
26 public static final Object ATTRIBUT_STYLENAME = "styleName";
27 public static final Object ATTRIBUT_STYLEATTRIBUTES = "styleAttribute";
28 // Transaction
29 public static final Object TRANSACTION = "transaction";
30 // ViewFactory
31 public static final Object VIEWFACTORY = "viewFactory";
32 // StyleSheet
33 public static final Object STYLESHEET = "styleSheet";
34 //
35 public static final int TextCSS = -5;
36 public static final int ParagraphCSS = -4;
37 public static final int Text = -3;
38 public static final int Paragraph = -2;
39 public static final int Dummy = -1;
40
41 //public static final int BUFFER_SIZE_DEFAULT = 4096;
42
43 public GenDiapoDocument() {
44 super();
45 }
46
47 public GenDiapoDocument(Content data, StyleContext styles) {
48 super(data, styles);
49 }
50
51 public GenDiapoDocument(ElementSpec[] es) {
52 super();
53 create(es);
54 }
55
56 // Pas propre.... glyphView a la fin
57 protected AbstractElement createDefaultRoot() {
58
59 writeLock();
60
61 // Creation des attributs
62 SimpleAttributeSet node_attr = new SimpleAttributeSet();
63 node_attr.addAttribute(ATTRIBUT_TYPE, new java.lang.Integer(Dummy));
64 SimpleAttributeSet attr_attr = new SimpleAttributeSet();
65 attr_attr.addAttribute(ATTRIBUT_TYPE, new java.lang.Integer(Paragraph));
66 SimpleAttributeSet text_attr = new SimpleAttributeSet();
67 text_attr.addAttribute(ATTRIBUT_TYPE, new java.lang.Integer(Text));
68
69 // Creation des elements
70 NodeElement node = new NodeElement(null,node_attr);
71 NodeElement attr = new NodeElement(node,attr_attr);
72 TextElement content = new TextElement(attr, text_attr,0,1);
73
74 Element[] buff = new Element[1];
75
76
77 buff[0] = content;
78 attr.replace(0,0, buff);
79
80 buff[0] = attr;
81 node.replace(0, 0, buff);
82
83 writeUnlock();
84
85 return node;
86 }
87
88 //public int getLength(); // DefaultStyledDocument
89 //public void addDocumentListener(DocumentListener listener); // DefaultStyledDocument
90 //public void removeDocumentListener(DocumentListener listener); // DefaultStyledDocument
91 //public void addUndoableEditListener(UndoableEditListener listener); // AbstractDocument
92 //public void removeUndoableEditListener(UndoableEditListener listener); // AbstractDocument
93 //public Object getProperty(Object key); // AbstractDocument
94 //public void putProperty(Object key, Object value); // AbstractDocument
95
96 public void remove(int offs, int len) throws BadLocationException {
97 Element first = getAttributeElement(offs);
98 Element last = getAttributeElement(offs+len);
99 if (first == last) {
100 super.remove(offs, len);
101 }
102 }
103
104 public void removeUncheck(int offs, int len) throws BadLocationException {
105 super.remove(offs, len);
106 }
107
108 public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
109 Element elem = getAttributeElement(offset);
110 AttributeSet attr = elem.getAttributes();
111 Object oType = attr.getAttribute(GenDiapoDocument.ATTRIBUT_TYPE);
112 int type = ((Integer)oType).intValue();
113 boolean doInsert = true;
114 if (type == Dummy) {
115 doInsert = false;
116 } else if (type == org.w3c.dom.Node.ATTRIBUTE_NODE) {
117 if (str.equals("\n")) {
118 doInsert = false;
119 }
120 }
121 if (doInsert) {
122 super.insertString(offset, str, a);
123 }
124 }
125
126 //public String getText(int offset, int length) throws BadLocationException; //getText
127 //public void getText(int offset, int length, Segment txt) throws BadLocationException; //getText
128 //public Position getStartPosition(); //AbstractDocument
129 //public Position getEndPosition(); //AbstractDocument
130 //public Position createPosition(int offs) throws BadLocationException;
131 //public Element[] getRootElements();
132 //public static final String StreamDescriptionProperty = "stream";
133 //public static final String TitleProperty = "title";
134 //public Element getDefaultRootElement()
135
136 //public Element getCharacterElement(int pos)
137 //public Element getParagraphElement(int pos)
138
139 /*
140 public Element getParagraphElement(int pos) {
141 Element e = getCharacterElement(pos);
142 if(e != null)
143 return e.getParentElement();
144 return e;
145 }
146 */
147
148 public Element getAttributeElement(int pos) {
149 return getParagraphElement(pos).getParentElement();
150 }
151
152 // a modifier pour pointer tjrs sur un noeud et pas un attribut
153 public Element getNodeElement(int pos) {
154 return getAttributeElement(pos).getParentElement();
155 }
156
157 protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) {
158 return new TextElement(parent, a, p0, p1);
159 }
160
161 protected Element createBranchElement(Element parent, AttributeSet a) {
162 return new NodeElement(parent, a);
163 }
164
165 public void insert(int offset, ElementSpec[] data) throws BadLocationException {
166 super.insert(offset, data);
167 }
168
169 /*------------------------------------------------------------------------------------------------------------*/
170 public Element findElementWithAttributes(Element elem, Object key, Object value) {
171
172 // Test si elem et celui recherche
173 Object obj = (elem.getAttributes()).getAttribute(key);
174 if (obj == value) {
175 return elem;
176 }
177
178 // Test pour ses fils
179 int nb = elem.getElementCount();
180 for (int i=0; i<nb; i++) {
181 Element eChild = elem.getElement(i);
182 Element retval = findElementWithAttributes(eChild, key, value);
183 if (retval != null) {
184 return retval;
185 }
186 }
187
188 // retourne null si non trouve
189 return null;
190 }
191
192 public Element findElementWithProperty(Object key, Object value) {
193 return findElementWithAttributes(getDefaultRootElement(), key, value);
194 }
195
196 // ---------------- Element ----------------
197 // NodeElement
198 protected class NodeElement extends BranchElement {
199
200 public NodeElement(Element parent, AttributeSet a) {
201 super(parent, a);
202 }
203
204 public NodeElement() {
205 super(null, null);
206 }
207
208 public String getName() {
209 AttributeSet attr = getAttributes();
210 String name = "#node";
211
212 // ATTRIBUT_MERLOTNODE
213 Object obj = attr.getAttribute(GenDiapoDocument.ATTRIBUT_MERLOTNODE);
214 if (obj instanceof org.merlotxml.merlot.MerlotDOMNode) {
215 String info;
216
217 name = ((org.merlotxml.merlot.MerlotDOMNode) obj).getNodeName();
218
219 int type = ((java.lang.Integer)attr.getAttribute(GenDiapoDocument.ATTRIBUT_TYPE)).intValue();
220 switch (type) {
221 case GenDiapoDocument.Dummy : info = "Dummy"; break;
222 case GenDiapoDocument.Paragraph : info = "Paragraph"; break;
223 case GenDiapoDocument.Text : info = "Text"; break;
224 case GenDiapoDocument.ParagraphCSS : info = "ParagraphCSS"; break;
225 case GenDiapoDocument.TextCSS : info = "TextCSS"; break;
226 case org.w3c.dom.Node.ATTRIBUTE_NODE : info = "ATTRIBUTE_NODE"; break;
227 case org.w3c.dom.Node.CDATA_SECTION_NODE : info = "CDATA_SECTION_NODE"; break;
228 case org.w3c.dom.Node.COMMENT_NODE : info = "COMMENT_NODE"; break;
229 case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE : info = "DOCUMENT_FRAGMENT_NODE"; break;
230 case org.w3c.dom.Node.DOCUMENT_NODE : info = "DOCUMENT_NODE"; break;
231 case org.w3c.dom.Node.DOCUMENT_TYPE_NODE : info = "DOCUMENT_TYPE_NODE";break;
232 case org.w3c.dom.Node.ELEMENT_NODE : info = "ELEMENT_NODE";break;
233 case org.w3c.dom.Node.ENTITY_NODE : info = "ENTITY_NODE";break;
234 case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: info = "ENTITY_REFERENCE_NODE";break;
235 case org.w3c.dom.Node.NOTATION_NODE : info = "NOTATION_NODE";break;
236 case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE : info = "PROCESSING_INTRUCTION_NODE";break;
237 case org.w3c.dom.Node.TEXT_NODE : info = "TEXT_NODE";break;
238 default : info = "default";break;
239 }
240
241 name += "'"+info;
242 }
243
244 // Type
245 return name;
246 }
247 }
248
249 // TextElement
250 protected class TextElement extends LeafElement {
251
252 public TextElement(Element parent, AttributeSet a, int offs0, int offs1) {
253 super(parent, a, offs0, offs1);
254 }
255
256 }
257
258 }
259