Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / FlowLayout.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (["java.awt.LayoutManager"], "java.awt.FlowLayout", ["java.awt.Dimension"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.align = 0;
5 this.newAlign = 0;
6 this.hgap = 0;
7 this.vgap = 0;
8 this.alignOnBaseline = false;
9 Clazz.instantialize (this, arguments);
10 }, java.awt, "FlowLayout", null, [java.awt.LayoutManager, java.io.Serializable]);
11 Clazz.makeConstructor (c$, 
12 function () {
13 this.construct (1, 5, 5);
14 });
15 Clazz.makeConstructor (c$, 
16 function (align) {
17 this.construct (align, 5, 5);
18 }, "~N");
19 Clazz.makeConstructor (c$, 
20 function (align, hgap, vgap) {
21 this.hgap = hgap;
22 this.vgap = vgap;
23 this.setAlignment (align);
24 }, "~N,~N,~N");
25 Clazz.defineMethod (c$, "getAlignment", 
26 function () {
27 return this.newAlign;
28 });
29 Clazz.defineMethod (c$, "setAlignment", 
30 function (align) {
31 this.newAlign = align;
32 switch (align) {
33 case 3:
34 this.align = 0;
35 break;
36 case 4:
37 this.align = 2;
38 break;
39 default:
40 this.align = align;
41 break;
42 }
43 }, "~N");
44 Clazz.defineMethod (c$, "getHgap", 
45 function () {
46 return this.hgap;
47 });
48 Clazz.defineMethod (c$, "setHgap", 
49 function (hgap) {
50 this.hgap = hgap;
51 }, "~N");
52 Clazz.defineMethod (c$, "getVgap", 
53 function () {
54 return this.vgap;
55 });
56 Clazz.defineMethod (c$, "setVgap", 
57 function (vgap) {
58 this.vgap = vgap;
59 }, "~N");
60 Clazz.defineMethod (c$, "setAlignOnBaseline", 
61 function (alignOnBaseline) {
62 this.alignOnBaseline = alignOnBaseline;
63 }, "~B");
64 Clazz.defineMethod (c$, "getAlignOnBaseline", 
65 function () {
66 return this.alignOnBaseline;
67 });
68 Clazz.overrideMethod (c$, "addLayoutComponent", 
69 function (name, comp) {
70 }, "~S,java.awt.Component");
71 Clazz.overrideMethod (c$, "removeLayoutComponent", 
72 function (comp) {
73 }, "java.awt.Component");
74 Clazz.overrideMethod (c$, "preferredLayoutSize", 
75 function (target) {
76 {
77 var dim =  new java.awt.Dimension (0, 0);
78 var nmembers = target.getComponentCount ();
79 var firstVisibleComponent = true;
80 var useBaseline = this.getAlignOnBaseline ();
81 var maxAscent = 0;
82 var maxDescent = 0;
83 for (var i = 0; i < nmembers; i++) {
84 var m = target.getComponent (i);
85 if (m.isVisible ()) {
86 var d = m.getPreferredSize ();
87 dim.height = Math.max (dim.height, d.height);
88 if (firstVisibleComponent) {
89 firstVisibleComponent = false;
90 } else {
91 dim.width += this.hgap;
92 }dim.width += d.width;
93 if (useBaseline) {
94 var baseline = m.getBaseline (d.width, d.height);
95 if (baseline >= 0) {
96 maxAscent = Math.max (maxAscent, baseline);
97 maxDescent = Math.max (maxDescent, d.height - baseline);
98 }}}}
99 if (useBaseline) {
100 dim.height = Math.max (maxAscent + maxDescent, dim.height);
101 }var insets = target.getInsets ();
102 dim.width += insets.left + insets.right + this.hgap * 2;
103 dim.height += insets.top + insets.bottom + this.vgap * 2;
104 return dim;
105 }}, "java.awt.Container");
106 Clazz.overrideMethod (c$, "minimumLayoutSize", 
107 function (target) {
108 {
109 var useBaseline = this.getAlignOnBaseline ();
110 var dim =  new java.awt.Dimension (0, 0);
111 var nmembers = target.getComponentCount ();
112 var maxAscent = 0;
113 var maxDescent = 0;
114 var firstVisibleComponent = true;
115 for (var i = 0; i < nmembers; i++) {
116 var m = target.getComponent (i);
117 if (m.visible) {
118 var d = m.getMinimumSize ();
119 dim.height = Math.max (dim.height, d.height);
120 if (firstVisibleComponent) {
121 firstVisibleComponent = false;
122 } else {
123 dim.width += this.hgap;
124 }dim.width += d.width;
125 if (useBaseline) {
126 var baseline = m.getBaseline (d.width, d.height);
127 if (baseline >= 0) {
128 maxAscent = Math.max (maxAscent, baseline);
129 maxDescent = Math.max (maxDescent, dim.height - baseline);
130 }}}}
131 if (useBaseline) {
132 dim.height = Math.max (maxAscent + maxDescent, dim.height);
133 }var insets = target.getInsets ();
134 dim.width += insets.left + insets.right + this.hgap * 2;
135 dim.height += insets.top + insets.bottom + this.vgap * 2;
136 return dim;
137 }}, "java.awt.Container");
138 Clazz.defineMethod (c$, "moveComponents", 
139  function (target, x, y, width, height, rowStart, rowEnd, ltr, useBaseline, ascent, descent) {
140 switch (this.newAlign) {
141 case 0:
142 x += ltr ? 0 : width;
143 break;
144 case 1:
145 x += Clazz.doubleToInt (width / 2);
146 break;
147 case 2:
148 x += ltr ? width : 0;
149 break;
150 case 3:
151 break;
152 case 4:
153 x += width;
154 break;
155 }
156 var maxAscent = 0;
157 var nonbaselineHeight = 0;
158 var baselineOffset = 0;
159 if (useBaseline) {
160 var maxDescent = 0;
161 for (var i = rowStart; i < rowEnd; i++) {
162 var m = target.getComponent (i);
163 if (m.visible) {
164 if (ascent[i] >= 0) {
165 maxAscent = Math.max (maxAscent, ascent[i]);
166 maxDescent = Math.max (maxDescent, descent[i]);
167 } else {
168 nonbaselineHeight = Math.max (m.getHeight (), nonbaselineHeight);
169 }}}
170 height = Math.max (maxAscent + maxDescent, nonbaselineHeight);
171 baselineOffset = Clazz.doubleToInt ((height - maxAscent - maxDescent) / 2);
172 }for (var i = rowStart; i < rowEnd; i++) {
173 var m = target.getComponent (i);
174 if (m.isVisible ()) {
175 var cy;
176 if (useBaseline && ascent[i] >= 0) {
177 cy = y + baselineOffset + maxAscent - ascent[i];
178 } else {
179 cy = y + Clazz.doubleToInt ((height - m.height) / 2);
180 }if (ltr) {
181 m.setLocation (x, cy);
182 } else {
183 m.setLocation (target.width - x - m.width, cy);
184 }x += m.width + this.hgap;
185 }}
186 return height;
187 }, "java.awt.Container,~N,~N,~N,~N,~N,~N,~B,~B,~A,~A");
188 Clazz.overrideMethod (c$, "layoutContainer", 
189 function (target) {
190 System.out.println ("FlowLayout laying out " + target);
191 {
192 var insets = target.getInsets ();
193 var maxwidth = target.width - (insets.left + insets.right + this.hgap * 2);
194 var nmembers = target.getComponentCount ();
195 var x = 0;
196 var y = insets.top + this.vgap;
197 var rowh = 0;
198 var start = 0;
199 var ltr = target.getComponentOrientation ().isLeftToRight ();
200 var useBaseline = this.getAlignOnBaseline ();
201 var ascent = null;
202 var descent = null;
203 if (useBaseline) {
204 ascent =  Clazz.newIntArray (nmembers, 0);
205 descent =  Clazz.newIntArray (nmembers, 0);
206 }for (var i = 0; i < nmembers; i++) {
207 var m = target.getComponent (i);
208 if (m.isVisible ()) {
209 var d = m.getPreferredSize ();
210 m.setSize (d.width, d.height);
211 if (useBaseline) {
212 var baseline = m.getBaseline (d.width, d.height);
213 if (baseline >= 0) {
214 ascent[i] = baseline;
215 descent[i] = d.height - baseline;
216 } else {
217 ascent[i] = -1;
218 }}if ((x == 0) || ((x + d.width) <= maxwidth)) {
219 if (x > 0) {
220 x += this.hgap;
221 }x += d.width;
222 rowh = Math.max (rowh, d.height);
223 } else {
224 rowh = this.moveComponents (target, insets.left + this.hgap, y, maxwidth - x, rowh, start, i, ltr, useBaseline, ascent, descent);
225 x = d.width;
226 y += this.vgap + rowh;
227 rowh = d.height;
228 start = i;
229 }}}
230 this.moveComponents (target, insets.left + this.hgap, y, maxwidth - x, rowh, start, nmembers, ltr, useBaseline, ascent, descent);
231 }}, "java.awt.Container");
232 Clazz.overrideMethod (c$, "toString", 
233 function () {
234 var str = "";
235 switch (this.align) {
236 case 0:
237 str = ",align=left";
238 break;
239 case 1:
240 str = ",align=center";
241 break;
242 case 2:
243 str = ",align=right";
244 break;
245 case 3:
246 str = ",align=leading";
247 break;
248 case 4:
249 str = ",align=trailing";
250 break;
251 }
252 return this.getClass ().getName () + "[hgap=" + this.hgap + ",vgap=" + this.vgap + str + "]";
253 });
254 Clazz.defineStatics (c$,
255 "LEFT", 0,
256 "CENTER", 1,
257 "RIGHT", 2,
258 "LEADING", 3,
259 "TRAILING", 4);
260 });