58e42b7c44dc5a7b0efa13983c45f1a0cdcc69cc
[jalviewjs.git] / site / swingjs / j2s / javax / swing / text / PlainView.js
1 Clazz.declarePackage ("javax.swing.text");
2 Clazz.load (["javax.swing.text.TabExpander", "$.View"], "javax.swing.text.PlainView", ["java.lang.IllegalArgumentException", "java.awt.Rectangle", "javax.swing.event.DocumentEvent", "javax.swing.text.Position", "$.Segment", "$.SegmentCache", "$.Utilities"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.metrics = null;
5 this.longLine = null;
6 this.font = null;
7 this.lineBuffer = null;
8 this.tabSize = 0;
9 this.tabBase = 0;
10 this.sel0 = 0;
11 this.sel1 = 0;
12 this.unselected = null;
13 this.selected = null;
14 this.firstLineOffset = 0;
15 Clazz.instantialize (this, arguments);
16 }, javax.swing.text, "PlainView", javax.swing.text.View, javax.swing.text.TabExpander);
17 Clazz.defineMethod (c$, "getTabSize", 
18 function () {
19 var i = this.getDocument ().getProperty ("tabSize");
20 var size = (i != null) ? i.intValue () : 8;
21 return size;
22 });
23 Clazz.defineMethod (c$, "getLineBuffer", 
24 function () {
25 if (this.lineBuffer == null) {
26 this.lineBuffer =  new javax.swing.text.Segment ();
27 }return this.lineBuffer;
28 });
29 Clazz.defineMethod (c$, "updateMetrics", 
30 function () {
31 var host = this.getContainer ();
32 var f = host.getFont ();
33 if (this.font !== f) {
34 this.calculateLongestLine ();
35 this.tabSize = this.getTabSize () * this.metrics.charWidth ('m');
36 }});
37 Clazz.overrideMethod (c$, "getPreferredSpan", 
38 function (axis) {
39 this.updateMetrics ();
40 switch (axis) {
41 case 0:
42 return this.getLineWidth (this.longLine);
43 case 1:
44 return this.getElement ().getElementCount () * this.metrics.getHeight ();
45 default:
46 throw  new IllegalArgumentException ("Invalid axis: " + axis);
47 }
48 }, "~N");
49 Clazz.overrideMethod (c$, "paint", 
50 function (g, a) {
51 }, "java.awt.Graphics,java.awt.Shape");
52 Clazz.defineMethod (c$, "adjustPaintRegion", 
53 function (a) {
54 return a;
55 }, "java.awt.Shape");
56 Clazz.defineMethod (c$, "modelToView", 
57 function (pos, a, b) {
58 var doc = this.getDocument ();
59 var map = this.getElement ();
60 var lineIndex = map.getElementIndex (pos);
61 if (lineIndex < 0) {
62 return this.lineToRect (a, 0);
63 }var lineArea = this.lineToRect (a, lineIndex);
64 this.tabBase = lineArea.x;
65 var line = map.getElement (lineIndex);
66 var p0 = line.getStartOffset ();
67 var s = javax.swing.text.SegmentCache.getSharedSegment ();
68 doc.getText (p0, pos - p0, s);
69 var xOffs = javax.swing.text.Utilities.getTabbedTextWidth (s, this.metrics, this.tabBase, this, p0);
70 javax.swing.text.SegmentCache.releaseSharedSegment (s);
71 lineArea.x += xOffs;
72 lineArea.width = 1;
73 lineArea.height = this.metrics.getHeight ();
74 return lineArea;
75 }, "~N,java.awt.Shape,javax.swing.text.Position.Bias");
76 Clazz.defineMethod (c$, "viewToModel", 
77 function (fx, fy, a, bias) {
78 bias[0] = javax.swing.text.Position.Bias.Forward;
79 var alloc = a.getBounds ();
80 var doc = this.getDocument ();
81 var x = Clazz.floatToInt (fx);
82 var y = Clazz.floatToInt (fy);
83 if (y < alloc.y) {
84 return this.getStartOffset ();
85 } else if (y > alloc.y + alloc.height) {
86 return this.getEndOffset () - 1;
87 } else {
88 var map = doc.getDefaultRootElement ();
89 var fontHeight = this.metrics.getHeight ();
90 var lineIndex = (fontHeight > 0 ? Math.abs (Clazz.doubleToInt ((y - alloc.y) / fontHeight)) : map.getElementCount () - 1);
91 if (lineIndex >= map.getElementCount ()) {
92 return this.getEndOffset () - 1;
93 }var line = map.getElement (lineIndex);
94 if (lineIndex == 0) {
95 alloc.x += this.firstLineOffset;
96 alloc.width -= this.firstLineOffset;
97 }if (x < alloc.x) {
98 return line.getStartOffset ();
99 } else if (x > alloc.x + alloc.width) {
100 return line.getEndOffset () - 1;
101 } else {
102 try {
103 var p0 = line.getStartOffset ();
104 var p1 = line.getEndOffset () - 1;
105 var s = javax.swing.text.SegmentCache.getSharedSegment ();
106 doc.getText (p0, p1 - p0, s);
107 this.tabBase = alloc.x;
108 var offs = p0 + javax.swing.text.Utilities.getTabbedTextOffset (s, this.metrics, this.tabBase, x, this, p0);
109 javax.swing.text.SegmentCache.releaseSharedSegment (s);
110 return offs;
111 } catch (e) {
112 if (Clazz.exceptionOf (e, javax.swing.text.BadLocationException)) {
113 return -1;
114 } else {
115 throw e;
116 }
117 }
118 }}}, "~N,~N,java.awt.Shape,~A");
119 Clazz.overrideMethod (c$, "insertUpdate", 
120 function (changes, a, f) {
121 this.updateDamage (changes, a, f);
122 }, "javax.swing.event.DocumentEvent,java.awt.Shape,javax.swing.text.ViewFactory");
123 Clazz.overrideMethod (c$, "removeUpdate", 
124 function (changes, a, f) {
125 this.updateDamage (changes, a, f);
126 }, "javax.swing.event.DocumentEvent,java.awt.Shape,javax.swing.text.ViewFactory");
127 Clazz.overrideMethod (c$, "changedUpdate", 
128 function (changes, a, f) {
129 this.updateDamage (changes, a, f);
130 }, "javax.swing.event.DocumentEvent,java.awt.Shape,javax.swing.text.ViewFactory");
131 Clazz.defineMethod (c$, "setSize", 
132 function (width, height) {
133 Clazz.superCall (this, javax.swing.text.PlainView, "setSize", [width, height]);
134 this.updateMetrics ();
135 }, "~N,~N");
136 Clazz.overrideMethod (c$, "nextTabStop", 
137 function (x, tabOffset) {
138 if (this.tabSize == 0) {
139 return x;
140 }var ntabs = Clazz.doubleToInt (((Clazz.floatToInt (x)) - this.tabBase) / this.tabSize);
141 return this.tabBase + ((ntabs + 1) * this.tabSize);
142 }, "~N,~N");
143 Clazz.defineMethod (c$, "updateDamage", 
144 function (changes, a, f) {
145 var host = this.getContainer ();
146 this.updateMetrics ();
147 var elem = this.getElement ();
148 var ec = changes.getChange (elem);
149 var added = (ec != null) ? ec.getChildrenAdded () : null;
150 var removed = (ec != null) ? ec.getChildrenRemoved () : null;
151 if (((added != null) && (added.length > 0)) || ((removed != null) && (removed.length > 0))) {
152 if (added != null) {
153 var currWide = this.getLineWidth (this.longLine);
154 for (var i = 0; i < added.length; i++) {
155 var w = this.getLineWidth (added[i]);
156 if (w > currWide) {
157 currWide = w;
158 this.longLine = added[i];
159 }}
160 }if (removed != null) {
161 for (var i = 0; i < removed.length; i++) {
162 if (removed[i] === this.longLine) {
163 this.calculateLongestLine ();
164 break;
165 }}
166 }this.preferenceChanged (null, true, true);
167 host.repaint ();
168 } else {
169 var map = this.getElement ();
170 var line = map.getElementIndex (changes.getOffset ());
171 this.damageLineRange (line, line, a, host);
172 if (changes.getType () === javax.swing.event.DocumentEvent.EventType.INSERT) {
173 var w = this.getLineWidth (this.longLine);
174 var e = map.getElement (line);
175 if (e === this.longLine) {
176 this.preferenceChanged (null, true, false);
177 } else if (this.getLineWidth (e) > w) {
178 this.longLine = e;
179 this.preferenceChanged (null, true, false);
180 }} else if (changes.getType () === javax.swing.event.DocumentEvent.EventType.REMOVE) {
181 if (map.getElement (line) === this.longLine) {
182 this.calculateLongestLine ();
183 this.preferenceChanged (null, true, false);
184 }}}}, "javax.swing.event.DocumentEvent,java.awt.Shape,javax.swing.text.ViewFactory");
185 Clazz.defineMethod (c$, "damageLineRange", 
186 function (line0, line1, a, host) {
187 if (a != null) {
188 var area0 = this.lineToRect (a, line0);
189 var area1 = this.lineToRect (a, line1);
190 if ((area0 != null) && (area1 != null)) {
191 var damage = area0.union (area1);
192 host.repaint (damage.x, damage.y, damage.width, damage.height);
193 } else {
194 host.repaint ();
195 }}}, "~N,~N,java.awt.Shape,java.awt.Component");
196 Clazz.defineMethod (c$, "lineToRect", 
197 function (a, line) {
198 var r = null;
199 this.updateMetrics ();
200 if (this.metrics != null) {
201 var alloc = a.getBounds ();
202 if (line == 0) {
203 alloc.x += this.firstLineOffset;
204 alloc.width -= this.firstLineOffset;
205 }r =  new java.awt.Rectangle (alloc.x, alloc.y + (line * this.metrics.getHeight ()), alloc.width, this.metrics.getHeight ());
206 }return r;
207 }, "java.awt.Shape,~N");
208 Clazz.defineMethod (c$, "calculateLongestLine", 
209  function () {
210 var c = this.getContainer ();
211 this.font = c.getFont ();
212 this.metrics = c.getFontMetrics (this.font);
213 var lines = this.getElement ();
214 var n = lines.getElementCount ();
215 var maxWidth = -1;
216 for (var i = 0; i < n; i++) {
217 var line = lines.getElement (i);
218 var w = this.getLineWidth (line);
219 if (w > maxWidth) {
220 maxWidth = w;
221 this.longLine = line;
222 }}
223 });
224 Clazz.defineMethod (c$, "getLineWidth", 
225  function (line) {
226 if (line == null) {
227 return 0;
228 }var p0 = line.getStartOffset ();
229 var p1 = line.getEndOffset ();
230 var w;
231 var s = javax.swing.text.SegmentCache.getSharedSegment ();
232 try {
233 line.getDocument ().getText (p0, p1 - p0, s);
234 w = javax.swing.text.Utilities.getTabbedTextWidth (s, this.metrics, this.tabBase, this, p0);
235 } catch (ble) {
236 if (Clazz.exceptionOf (ble, javax.swing.text.BadLocationException)) {
237 w = 0;
238 } else {
239 throw ble;
240 }
241 }
242 javax.swing.text.SegmentCache.releaseSharedSegment (s);
243 return w;
244 }, "javax.swing.text.Element");
245 });