5493acb32df28eb146744b7d10b330d3cc81fe83
[jalviewjs.git] / site / swingjs / j2s / javax / swing / text / AbstractWriter.js
1 Clazz.declarePackage ("javax.swing.text");
2 Clazz.load (null, "javax.swing.text.AbstractWriter", ["java.lang.Character", "javax.swing.text.ElementIterator", "$.Segment"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.it = null;
5 this.out = null;
6 this.indentLevel = 0;
7 this.indentSpace = 2;
8 this.doc = null;
9 this.maxLineLength = 100;
10 this.currLength = 0;
11 this.startOffset = 0;
12 this.endOffset = 0;
13 this.offsetIndent = 0;
14 this.lineSeparator = null;
15 this.canWrapLines = false;
16 this.$isLineEmpty = false;
17 this.indentChars = null;
18 this.tempChars = null;
19 this.newlineChars = null;
20 this.segment = null;
21 Clazz.instantialize (this, arguments);
22 }, javax.swing.text, "AbstractWriter");
23 Clazz.makeConstructor (c$, 
24 function (w, doc) {
25 this.construct (w, doc, 0, doc.getLength ());
26 }, "java.io.Writer,javax.swing.text.Document");
27 Clazz.makeConstructor (c$, 
28 function (w, doc, pos, len) {
29 this.doc = doc;
30 this.it =  new javax.swing.text.ElementIterator (doc.getDefaultRootElement ());
31 this.out = w;
32 this.startOffset = pos;
33 this.endOffset = pos + len;
34 var docNewline = doc.getProperty ("__EndOfLine__");
35 if (Clazz.instanceOf (docNewline, String)) {
36 this.setLineSeparator (docNewline);
37 } else {
38 var newline = null;
39 try {
40 newline = System.getProperty ("line.separator");
41 } catch (se) {
42 if (Clazz.exceptionOf (se, SecurityException)) {
43 } else {
44 throw se;
45 }
46 }
47 if (newline == null) {
48 newline = "\n";
49 }this.setLineSeparator (newline);
50 }this.canWrapLines = true;
51 }, "java.io.Writer,javax.swing.text.Document,~N,~N");
52 Clazz.makeConstructor (c$, 
53 function (w, root) {
54 this.construct (w, root, 0, root.getEndOffset ());
55 }, "java.io.Writer,javax.swing.text.Element");
56 Clazz.makeConstructor (c$, 
57 function (w, root, pos, len) {
58 this.doc = root.getDocument ();
59 this.it =  new javax.swing.text.ElementIterator (root);
60 this.out = w;
61 this.startOffset = pos;
62 this.endOffset = pos + len;
63 this.canWrapLines = true;
64 }, "java.io.Writer,javax.swing.text.Element,~N,~N");
65 Clazz.defineMethod (c$, "getStartOffset", 
66 function () {
67 return this.startOffset;
68 });
69 Clazz.defineMethod (c$, "getEndOffset", 
70 function () {
71 return this.endOffset;
72 });
73 Clazz.defineMethod (c$, "getElementIterator", 
74 function () {
75 return this.it;
76 });
77 Clazz.defineMethod (c$, "getWriter", 
78 function () {
79 return this.out;
80 });
81 Clazz.defineMethod (c$, "getDocument", 
82 function () {
83 return this.doc;
84 });
85 Clazz.defineMethod (c$, "inRange", 
86 function (next) {
87 var startOffset = this.getStartOffset ();
88 var endOffset = this.getEndOffset ();
89 if ((next.getStartOffset () >= startOffset && next.getStartOffset () < endOffset) || (startOffset >= next.getStartOffset () && startOffset < next.getEndOffset ())) {
90 return true;
91 }return false;
92 }, "javax.swing.text.Element");
93 Clazz.defineMethod (c$, "getText", 
94 function (elem) {
95 return this.doc.getText (elem.getStartOffset (), elem.getEndOffset () - elem.getStartOffset ());
96 }, "javax.swing.text.Element");
97 Clazz.defineMethod (c$, "text", 
98 function (elem) {
99 var start = Math.max (this.getStartOffset (), elem.getStartOffset ());
100 var end = Math.min (this.getEndOffset (), elem.getEndOffset ());
101 if (start < end) {
102 if (this.segment == null) {
103 this.segment =  new javax.swing.text.Segment ();
104 }this.getDocument ().getText (start, end - start, this.segment);
105 if (this.segment.count > 0) {
106 this.write (this.segment.array, this.segment.offset, this.segment.count);
107 }}}, "javax.swing.text.Element");
108 Clazz.defineMethod (c$, "setLineLength", 
109 function (l) {
110 this.maxLineLength = l;
111 }, "~N");
112 Clazz.defineMethod (c$, "getLineLength", 
113 function () {
114 return this.maxLineLength;
115 });
116 Clazz.defineMethod (c$, "setCurrentLineLength", 
117 function (length) {
118 this.currLength = length;
119 this.$isLineEmpty = (this.currLength == 0);
120 }, "~N");
121 Clazz.defineMethod (c$, "getCurrentLineLength", 
122 function () {
123 return this.currLength;
124 });
125 Clazz.defineMethod (c$, "isLineEmpty", 
126 function () {
127 return this.$isLineEmpty;
128 });
129 Clazz.defineMethod (c$, "setCanWrapLines", 
130 function (newValue) {
131 this.canWrapLines = newValue;
132 }, "~B");
133 Clazz.defineMethod (c$, "getCanWrapLines", 
134 function () {
135 return this.canWrapLines;
136 });
137 Clazz.defineMethod (c$, "setIndentSpace", 
138 function (space) {
139 this.indentSpace = space;
140 }, "~N");
141 Clazz.defineMethod (c$, "getIndentSpace", 
142 function () {
143 return this.indentSpace;
144 });
145 Clazz.defineMethod (c$, "setLineSeparator", 
146 function (value) {
147 this.lineSeparator = value;
148 }, "~S");
149 Clazz.defineMethod (c$, "getLineSeparator", 
150 function () {
151 return this.lineSeparator;
152 });
153 Clazz.defineMethod (c$, "incrIndent", 
154 function () {
155 if (this.offsetIndent > 0) {
156 this.offsetIndent++;
157 } else {
158 if (++this.indentLevel * this.getIndentSpace () >= this.getLineLength ()) {
159 this.offsetIndent++;
160 --this.indentLevel;
161 }}});
162 Clazz.defineMethod (c$, "decrIndent", 
163 function () {
164 if (this.offsetIndent > 0) {
165 --this.offsetIndent;
166 } else {
167 this.indentLevel--;
168 }});
169 Clazz.defineMethod (c$, "getIndentLevel", 
170 function () {
171 return this.indentLevel;
172 });
173 Clazz.defineMethod (c$, "indent", 
174 function () {
175 var max = this.getIndentLevel () * this.getIndentSpace ();
176 if (this.indentChars == null || max > this.indentChars.length) {
177 this.indentChars =  Clazz.newCharArray (max, '\0');
178 for (var counter = 0; counter < max; counter++) {
179 this.indentChars[counter] = ' ';
180 }
181 }var length = this.getCurrentLineLength ();
182 var wasEmpty = this.isLineEmpty ();
183 this.output (this.indentChars, 0, max);
184 if (wasEmpty && length == 0) {
185 this.$isLineEmpty = true;
186 }});
187 Clazz.defineMethod (c$, "write", 
188 function (ch) {
189 if (this.tempChars == null) {
190 this.tempChars =  Clazz.newCharArray (128, '\0');
191 }this.tempChars[0] = ch;
192 this.write (this.tempChars, 0, 1);
193 }, "~S");
194 Clazz.defineMethod (c$, "write", 
195 function (content) {
196 if (content == null) {
197 return;
198 }var size = content.length;
199 if (this.tempChars == null || this.tempChars.length < size) {
200 this.tempChars =  Clazz.newCharArray (size, '\0');
201 }content.getChars (0, size, this.tempChars, 0);
202 this.write (this.tempChars, 0, size);
203 }, "~S");
204 Clazz.defineMethod (c$, "writeLineSeparator", 
205 function () {
206 var newline = this.getLineSeparator ();
207 var length = newline.length;
208 if (this.newlineChars == null || this.newlineChars.length < length) {
209 this.newlineChars =  Clazz.newCharArray (length, '\0');
210 }newline.getChars (0, length, this.newlineChars, 0);
211 this.output (this.newlineChars, 0, length);
212 this.setCurrentLineLength (0);
213 });
214 Clazz.defineMethod (c$, "write", 
215 function (chars, startIndex, length) {
216 if (!this.getCanWrapLines ()) {
217 var lastIndex = startIndex;
218 var endIndex = startIndex + length;
219 var newlineIndex = this.indexOf (chars, '\u000a', startIndex, endIndex);
220 while (newlineIndex != -1) {
221 if (newlineIndex > lastIndex) {
222 this.output (chars, lastIndex, newlineIndex - lastIndex);
223 }this.writeLineSeparator ();
224 lastIndex = newlineIndex + 1;
225 newlineIndex = this.indexOf (chars, '\n', lastIndex, endIndex);
226 }
227 if (lastIndex < endIndex) {
228 this.output (chars, lastIndex, endIndex - lastIndex);
229 }} else {
230 var lastIndex = startIndex;
231 var endIndex = startIndex + length;
232 var lineLength = this.getCurrentLineLength ();
233 var maxLength = this.getLineLength ();
234 while (lastIndex < endIndex) {
235 var newlineIndex = this.indexOf (chars, '\u000a', lastIndex, endIndex);
236 var needsNewline = false;
237 var forceNewLine = false;
238 lineLength = this.getCurrentLineLength ();
239 if (newlineIndex != -1 && (lineLength + (newlineIndex - lastIndex)) < maxLength) {
240 if (newlineIndex > lastIndex) {
241 this.output (chars, lastIndex, newlineIndex - lastIndex);
242 }lastIndex = newlineIndex + 1;
243 forceNewLine = true;
244 } else if (newlineIndex == -1 && (lineLength + (endIndex - lastIndex)) < maxLength) {
245 if (endIndex > lastIndex) {
246 this.output (chars, lastIndex, endIndex - lastIndex);
247 }lastIndex = endIndex;
248 } else {
249 var breakPoint = -1;
250 var maxBreak = Math.min (endIndex - lastIndex, maxLength - lineLength - 1);
251 var counter = 0;
252 while (counter < maxBreak) {
253 if (Character.isWhitespace (chars[counter + lastIndex])) {
254 breakPoint = counter;
255 }counter++;
256 }
257 if (breakPoint != -1) {
258 breakPoint += lastIndex + 1;
259 this.output (chars, lastIndex, breakPoint - lastIndex);
260 lastIndex = breakPoint;
261 needsNewline = true;
262 } else {
263 counter = Math.max (0, maxBreak);
264 maxBreak = endIndex - lastIndex;
265 while (counter < maxBreak) {
266 if (Character.isWhitespace (chars[counter + lastIndex])) {
267 breakPoint = counter;
268 break;
269 }counter++;
270 }
271 if (breakPoint == -1) {
272 this.output (chars, lastIndex, endIndex - lastIndex);
273 breakPoint = endIndex;
274 } else {
275 breakPoint += lastIndex;
276 if (chars[breakPoint] == '\u000a') {
277 this.output (chars, lastIndex, breakPoint++ - lastIndex);
278 forceNewLine = true;
279 } else {
280 this.output (chars, lastIndex, ++breakPoint - lastIndex);
281 needsNewline = true;
282 }}lastIndex = breakPoint;
283 }}if (forceNewLine || needsNewline || lastIndex < endIndex) {
284 this.writeLineSeparator ();
285 if (lastIndex < endIndex || !forceNewLine) {
286 this.indent ();
287 }}}
288 }}, "~A,~N,~N");
289 Clazz.defineMethod (c$, "writeAttributes", 
290 function (attr) {
291 var names = attr.getAttributeNames ();
292 while (names.hasMoreElements ()) {
293 var name = names.nextElement ();
294 this.write (" " + name + "=" + attr.getAttribute (name));
295 }
296 }, "javax.swing.text.AttributeSet");
297 Clazz.defineMethod (c$, "output", 
298 function (content, start, length) {
299 this.getWriter ().write (content, start, length);
300 this.setCurrentLineLength (this.getCurrentLineLength () + length);
301 }, "~A,~N,~N");
302 Clazz.defineMethod (c$, "indexOf", 
303  function (chars, sChar, startIndex, endIndex) {
304 while (startIndex < endIndex) {
305 if (chars[startIndex] == sChar) {
306 return startIndex;
307 }startIndex++;
308 }
309 return -1;
310 }, "~A,~S,~N,~N");
311 Clazz.defineStatics (c$,
312 "NEWLINE", '\n');
313 });