JAL-1807 Bob
[jalviewjs.git] / site / j2s / swingjs / JSAbstractDocument.js
1 Clazz.declarePackage ("swingjs");
2 Clazz.load (["javax.swing.text.DocumentFilter", "$.Element", "$.JSMinimalAbstractDocument"], "swingjs.JSAbstractDocument", ["java.lang.IllegalStateException", "java.util.HashMap", "JU.AU", "javax.swing.event.DocumentEvent", "$.DocumentListener", "$.EventListenerList", "javax.swing.text.BadLocationException", "swingjs.JSDocumentEvent"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.props = null;
5 this.root = null;
6 this.positions = null;
7 this.listenerList = null;
8 this.notifyingListeners = false;
9 this.filterBypass = null;
10 this.me = null;
11 this.sb = null;
12 this.tempChar = null;
13 this.filter = null;
14 if (!Clazz.isClassDefined ("swingjs.JSAbstractDocument.DefaultFilterBypass")) {
15 swingjs.JSAbstractDocument.$JSAbstractDocument$DefaultFilterBypass$ ();
16 }
17 if (!Clazz.isClassDefined ("swingjs.JSAbstractDocument.JSElement")) {
18 swingjs.JSAbstractDocument.$JSAbstractDocument$JSElement$ ();
19 }
20 Clazz.instantialize (this, arguments);
21 }, swingjs, "JSAbstractDocument", null, javax.swing.text.JSMinimalAbstractDocument);
22 Clazz.makeConstructor (c$, 
23 function () {
24 this.me = this;
25 this.props =  new java.util.HashMap ();
26 });
27 Clazz.overrideMethod (c$, "getRootElements", 
28 function () {
29 return  Clazz.newArray (-1, [this.root, null]);
30 });
31 Clazz.defineMethod (c$, "checkLoc", 
32 function (start, end) {
33 if (start < 0 || end > this.getLength ()) throw  new javax.swing.text.BadLocationException ("JSAbstractDocument: out of range", (start < 0 ? start : end));
34 }, "~N,~N");
35 Clazz.defineMethod (c$, "fixPositions", 
36 function (offset, length, isInsert) {
37 if (this.positions == null || this.positions.isEmpty ()) return;
38 if (isInsert) {
39 for (var i, $i = this.positions.keySet ().iterator (); $i.hasNext () && ((i = $i.next ()) || true);) {
40 var pos = i.intValue ();
41 if (pos > offset) this.positions.get (i).pos += length;
42 }
43 return;
44 }for (var i, $i = this.positions.keySet ().iterator (); $i.hasNext () && ((i = $i.next ()) || true);) {
45 var pos = i.intValue ();
46 if (pos <= offset) continue;
47 if (pos >= offset + length) this.positions.get (i).pos -= length;
48  else this.positions.get (i).pos = offset;
49 }
50 }, "~N,~N,~B");
51 Clazz.defineMethod (c$, "getFilterBypass", 
52  function () {
53 if (this.filterBypass == null) {
54 this.filterBypass = Clazz.innerTypeInstance (swingjs.JSAbstractDocument.DefaultFilterBypass, this, null);
55 }return this.filterBypass;
56 });
57 Clazz.overrideMethod (c$, "remove", 
58 function (offs, len) {
59 var filter = this.getDocumentFilter ();
60 if (filter == null) this.handleRemove (offs, len);
61  else filter.remove (this.getFilterBypass (), offs, len);
62 }, "~N,~N");
63 Clazz.overrideMethod (c$, "insertString", 
64 function (offset, str, a) {
65 var filter = this.getDocumentFilter ();
66 if (filter == null) this.handleInsertString (offset, str, a);
67  else filter.insertString (this.getFilterBypass (), offset, str, a);
68 }, "~N,~S,javax.swing.text.AttributeSet");
69 Clazz.overrideMethod (c$, "replace", 
70 function (offset, length, text, attrs) {
71 if (length == 0 && (text == null || text.length == 0)) return;
72 var filter = this.getDocumentFilter ();
73 if (filter != null) {
74 filter.replace (this.getFilterBypass (), offset, length, text, attrs);
75 } else {
76 if (length > 0) this.remove (offset, length);
77 if (text != null && text.length > 0) this.insertString (offset, text, attrs);
78 }}, "~N,~N,~S,javax.swing.text.AttributeSet");
79 Clazz.defineMethod (c$, "taint", 
80  function () {
81 this.tempChar = null;
82 });
83 Clazz.defineMethod (c$, "setLines", 
84  function () {
85 this.root = Clazz.innerTypeInstance (swingjs.JSAbstractDocument.JSElement, this, null);
86 var s = this.sb.toString ();
87 if (s.lastIndexOf ('\n') != s.length - 1) s += "\n";
88 var ilast = 0;
89 for (var i = 0; i < s.length; i++) {
90 if (s.charAt (i) != '\n') continue;
91 var e = Clazz.innerTypeInstance (swingjs.JSAbstractDocument.JSElement, this, null);
92 e.start = ilast;
93 e.end = i;
94 ilast = i + 1;
95 this.root.addChild (e);
96 }
97 });
98 Clazz.defineMethod (c$, "handleInsertString", 
99 function (offs, str, a) {
100 if ((str == null) || (str.length == 0)) {
101 return;
102 }this.checkLoc (offs, offs);
103 this.taint ();
104 this.sb.insert (offs, str);
105 this.fixPositions (offs, str.length, true);
106 if (str.indexOf ('\n') >= 0) this.setLines ();
107 var e =  new swingjs.JSDocumentEvent (this, offs, str.length, javax.swing.event.DocumentEvent.EventType.INSERT);
108 this.fireInsertUpdate (e);
109 }, "~N,~S,javax.swing.text.AttributeSet");
110 Clazz.defineMethod (c$, "handleRemove", 
111 function (offs, len) {
112 this.checkLoc (offs, offs + len);
113 this.taint ();
114 var str = this.sb.substring2 (offs, offs + len);
115 this.sb.replace (offs, offs + len, "");
116 this.fixPositions (offs, offs + len, false);
117 if (str.indexOf ('\n') >= 0) this.setLines ();
118 if (len > 0) {
119 var chng =  new swingjs.JSDocumentEvent (this, offs, len, javax.swing.event.DocumentEvent.EventType.REMOVE);
120 this.fireRemoveUpdate (chng);
121 }}, "~N,~N");
122 Clazz.defineMethod (c$, "fireInsertUpdate", 
123 function (e) {
124 if (this.listenerList == null) return;
125 this.checkAlreadyNotifying ();
126 this.notifyingListeners = true;
127 try {
128 var listeners = this.listenerList.getListenerList ();
129 for (var i = listeners.length - 2; i >= 0; i -= 2) {
130 if (listeners[i] === javax.swing.event.DocumentListener) {
131 (listeners[i + 1]).insertUpdate (e);
132 }}
133 } finally {
134 this.notifyingListeners = false;
135 }
136 }, "javax.swing.event.DocumentEvent");
137 Clazz.defineMethod (c$, "fireChangedUpdate", 
138 function (e) {
139 if (this.listenerList == null) return;
140 this.checkAlreadyNotifying ();
141 this.notifyingListeners = true;
142 try {
143 var listeners = this.listenerList.getListenerList ();
144 for (var i = listeners.length - 2; i >= 0; i -= 2) {
145 if (listeners[i] === javax.swing.event.DocumentListener) {
146 (listeners[i + 1]).changedUpdate (e);
147 }}
148 } finally {
149 this.notifyingListeners = false;
150 }
151 }, "javax.swing.event.DocumentEvent");
152 Clazz.defineMethod (c$, "fireRemoveUpdate", 
153 function (e) {
154 if (this.listenerList == null) return;
155 this.checkAlreadyNotifying ();
156 this.notifyingListeners = true;
157 try {
158 var listeners = this.listenerList.getListenerList ();
159 for (var i = listeners.length - 2; i >= 0; i -= 2) {
160 if (listeners[i] === javax.swing.event.DocumentListener) {
161 (listeners[i + 1]).removeUpdate (e);
162 }}
163 } finally {
164 this.notifyingListeners = false;
165 }
166 }, "javax.swing.event.DocumentEvent");
167 Clazz.defineMethod (c$, "checkAlreadyNotifying", 
168  function () {
169 if (this.notifyingListeners) throw  new IllegalStateException ("One of the document listeners modifed the document. This is not allowed.");
170 });
171 Clazz.overrideMethod (c$, "addDocumentListener", 
172 function (listener) {
173 if (this.listenerList == null) this.listenerList =  new javax.swing.event.EventListenerList ();
174 this.listenerList.add (javax.swing.event.DocumentListener, listener);
175 }, "javax.swing.event.DocumentListener");
176 Clazz.overrideMethod (c$, "removeDocumentListener", 
177 function (listener) {
178 if (this.listenerList != null) this.listenerList.remove (javax.swing.event.DocumentListener, listener);
179 }, "javax.swing.event.DocumentListener");
180 Clazz.overrideMethod (c$, "addUndoableEditListener", 
181 function (listener) {
182 }, "javax.swing.event.UndoableEditListener");
183 Clazz.overrideMethod (c$, "removeUndoableEditListener", 
184 function (listener) {
185 }, "javax.swing.event.UndoableEditListener");
186 Clazz.overrideMethod (c$, "getProperty", 
187 function (key) {
188 return this.props.get (key);
189 }, "~O");
190 Clazz.overrideMethod (c$, "putProperty", 
191 function (key, value) {
192 this.props.put (key, value);
193 }, "~O,~O");
194 Clazz.overrideMethod (c$, "getAsynchronousLoadPriority", 
195 function () {
196 return -1;
197 });
198 Clazz.overrideMethod (c$, "setDocumentFilter", 
199 function (filter) {
200 this.filter = filter;
201 }, "javax.swing.text.DocumentFilter");
202 Clazz.defineMethod (c$, "getDocumentFilter", 
203 function () {
204 return this.filter;
205 });
206 c$.$JSAbstractDocument$DefaultFilterBypass$ = function () {
207 Clazz.pu$h(self.c$);
208 c$ = Clazz.decorateAsClass (function () {
209 Clazz.prepareCallback (this, arguments);
210 Clazz.instantialize (this, arguments);
211 }, swingjs.JSAbstractDocument, "DefaultFilterBypass", javax.swing.text.DocumentFilter.FilterBypass);
212 Clazz.overrideMethod (c$, "getDocument", 
213 function () {
214 return this.b$["swingjs.JSAbstractDocument"].me;
215 });
216 Clazz.overrideMethod (c$, "remove", 
217 function (a, b) {
218 this.b$["swingjs.JSAbstractDocument"].handleRemove (a, b);
219 }, "~N,~N");
220 Clazz.overrideMethod (c$, "insertString", 
221 function (a, b, c) {
222 this.b$["swingjs.JSAbstractDocument"].handleInsertString (a, b, c);
223 }, "~N,~S,javax.swing.text.AttributeSet");
224 Clazz.overrideMethod (c$, "replace", 
225 function (a, b, c, d) {
226 this.b$["swingjs.JSAbstractDocument"].handleRemove (a, b);
227 this.b$["swingjs.JSAbstractDocument"].handleInsertString (a, c, d);
228 }, "~N,~N,~S,javax.swing.text.AttributeSet");
229 c$ = Clazz.p0p ();
230 };
231 c$.$JSAbstractDocument$JSElement$ = function () {
232 Clazz.pu$h(self.c$);
233 c$ = Clazz.decorateAsClass (function () {
234 Clazz.prepareCallback (this, arguments);
235 this.parent = null;
236 this.attributeSet = null;
237 this.start = 0;
238 this.end = 0;
239 this.nchildren = 0;
240 this.children = null;
241 this.lastIndex = 0;
242 Clazz.instantialize (this, arguments);
243 }, swingjs.JSAbstractDocument, "JSElement", null, javax.swing.text.Element);
244 Clazz.makeConstructor (c$, 
245 function () {
246 this.children = null;
247 this.nchildren = 0;
248 this.lastIndex = -1;
249 });
250 Clazz.defineMethod (c$, "addChild", 
251 function (a) {
252 if (this.children == null) this.children =  new Array (10);
253  else if (this.nchildren == this.children.length) this.children = JU.AU.doubleLength (this.children);
254 this.children[this.nchildren++] = a;
255 }, "swingjs.JSAbstractDocument.JSElement");
256 Clazz.overrideMethod (c$, "getDocument", 
257 function () {
258 return this.b$["swingjs.JSAbstractDocument"].me;
259 });
260 Clazz.overrideMethod (c$, "getParentElement", 
261 function () {
262 return this.parent;
263 });
264 Clazz.overrideMethod (c$, "getName", 
265 function () {
266 return this.getName ();
267 });
268 Clazz.overrideMethod (c$, "getAttributes", 
269 function () {
270 return this.attributeSet;
271 });
272 Clazz.defineMethod (c$, "getStartOffset", 
273 function () {
274 return this.start;
275 });
276 Clazz.defineMethod (c$, "getEndOffset", 
277 function () {
278 return this.end;
279 });
280 Clazz.overrideMethod (c$, "getElementIndex", 
281 function (a) {
282 var b;
283 var c = 0;
284 var d = this.nchildren - 1;
285 var e = 0;
286 var f = this.getStartOffset ();
287 var g;
288 if (this.nchildren == 0) {
289 return 0;
290 }if (a >= this.getEndOffset ()) {
291 return this.nchildren - 1;
292 }if ((this.lastIndex >= c) && (this.lastIndex <= d)) {
293 var h = this.children[this.lastIndex];
294 f = h.getStartOffset ();
295 g = h.getEndOffset ();
296 if ((a >= f) && (a < g)) {
297 return this.lastIndex;
298 }if (a < f) {
299 d = this.lastIndex;
300 } else {
301 c = this.lastIndex;
302 }}while (c <= d) {
303 e = c + (Clazz.doubleToInt ((d - c) / 2));
304 var h = this.children[e];
305 f = h.getStartOffset ();
306 g = h.getEndOffset ();
307 if ((a >= f) && (a < g)) {
308 b = e;
309 this.lastIndex = b;
310 return b;
311 } else if (a < f) {
312 d = e - 1;
313 } else {
314 c = e + 1;
315 }}
316 if (a < f) {
317 b = e;
318 } else {
319 b = e + 1;
320 }this.lastIndex = b;
321 return b;
322 }, "~N");
323 Clazz.overrideMethod (c$, "getElementCount", 
324 function () {
325 return this.nchildren;
326 });
327 Clazz.overrideMethod (c$, "getElement", 
328 function (a) {
329 return (a >= this.nchildren ? null : this.children[a]);
330 }, "~N");
331 Clazz.overrideMethod (c$, "isLeaf", 
332 function () {
333 return (this.parent != null);
334 });
335 c$ = Clazz.p0p ();
336 };
337 Clazz.defineStatics (c$,
338 "ParagraphElementName", "paragraph",
339 "ContentElementName", "content");
340 });