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