Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / GridLayout.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (["java.awt.LayoutManager"], "java.awt.GridLayout", ["java.lang.IllegalArgumentException", "java.awt.Dimension"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.hgap = 0;
5 this.vgap = 0;
6 this.rows = 0;
7 this.cols = 0;
8 Clazz.instantialize (this, arguments);
9 }, java.awt, "GridLayout", null, [java.awt.LayoutManager, java.io.Serializable]);
10 Clazz.makeConstructor (c$, 
11 function () {
12 this.construct (1, 0, 0, 0);
13 });
14 Clazz.makeConstructor (c$, 
15 function (rows, cols) {
16 this.construct (rows, cols, 0, 0);
17 }, "~N,~N");
18 Clazz.makeConstructor (c$, 
19 function (rows, cols, hgap, vgap) {
20 if ((rows == 0) && (cols == 0)) {
21 throw  new IllegalArgumentException ("rows and cols cannot both be zero");
22 }this.rows = rows;
23 this.cols = cols;
24 this.hgap = hgap;
25 this.vgap = vgap;
26 }, "~N,~N,~N,~N");
27 Clazz.defineMethod (c$, "getRows", 
28 function () {
29 return this.rows;
30 });
31 Clazz.defineMethod (c$, "setRows", 
32 function (rows) {
33 if ((rows == 0) && (this.cols == 0)) {
34 throw  new IllegalArgumentException ("rows and cols cannot both be zero");
35 }this.rows = rows;
36 }, "~N");
37 Clazz.defineMethod (c$, "getColumns", 
38 function () {
39 return this.cols;
40 });
41 Clazz.defineMethod (c$, "setColumns", 
42 function (cols) {
43 if ((cols == 0) && (this.rows == 0)) {
44 throw  new IllegalArgumentException ("rows and cols cannot both be zero");
45 }this.cols = cols;
46 }, "~N");
47 Clazz.defineMethod (c$, "getHgap", 
48 function () {
49 return this.hgap;
50 });
51 Clazz.defineMethod (c$, "setHgap", 
52 function (hgap) {
53 this.hgap = hgap;
54 }, "~N");
55 Clazz.defineMethod (c$, "getVgap", 
56 function () {
57 return this.vgap;
58 });
59 Clazz.defineMethod (c$, "setVgap", 
60 function (vgap) {
61 this.vgap = vgap;
62 }, "~N");
63 Clazz.overrideMethod (c$, "addLayoutComponent", 
64 function (name, comp) {
65 }, "~S,java.awt.Component");
66 Clazz.overrideMethod (c$, "removeLayoutComponent", 
67 function (comp) {
68 }, "java.awt.Component");
69 Clazz.overrideMethod (c$, "preferredLayoutSize", 
70 function (parent) {
71 {
72 var insets = parent.getInsets ();
73 var ncomponents = parent.getComponentCount ();
74 var nrows = this.rows;
75 var ncols = this.cols;
76 if (nrows > 0) {
77 ncols = Clazz.doubleToInt ((ncomponents + nrows - 1) / nrows);
78 } else {
79 nrows = Clazz.doubleToInt ((ncomponents + ncols - 1) / ncols);
80 }var w = 0;
81 var h = 0;
82 for (var i = 0; i < ncomponents; i++) {
83 var comp = parent.getComponent (i);
84 var d = comp.getPreferredSize ();
85 if (w < d.width) {
86 w = d.width;
87 }if (h < d.height) {
88 h = d.height;
89 }}
90 return  new java.awt.Dimension (insets.left + insets.right + ncols * w + (ncols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (nrows - 1) * this.vgap);
91 }}, "java.awt.Container");
92 Clazz.overrideMethod (c$, "minimumLayoutSize", 
93 function (parent) {
94 {
95 var insets = parent.getInsets ();
96 var ncomponents = parent.getComponentCount ();
97 var nrows = this.rows;
98 var ncols = this.cols;
99 if (nrows > 0) {
100 ncols = Clazz.doubleToInt ((ncomponents + nrows - 1) / nrows);
101 } else {
102 nrows = Clazz.doubleToInt ((ncomponents + ncols - 1) / ncols);
103 }var w = 0;
104 var h = 0;
105 for (var i = 0; i < ncomponents; i++) {
106 var comp = parent.getComponent (i);
107 var d = comp.getMinimumSize ();
108 if (w < d.width) {
109 w = d.width;
110 }if (h < d.height) {
111 h = d.height;
112 }}
113 return  new java.awt.Dimension (insets.left + insets.right + ncols * w + (ncols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (nrows - 1) * this.vgap);
114 }}, "java.awt.Container");
115 Clazz.overrideMethod (c$, "layoutContainer", 
116 function (parent) {
117 {
118 var insets = parent.getInsets ();
119 var ncomponents = parent.getComponentCount ();
120 var nrows = this.rows;
121 var ncols = this.cols;
122 var ltr = parent.getComponentOrientation ().isLeftToRight ();
123 if (ncomponents == 0) {
124 return;
125 }if (nrows > 0) {
126 ncols = Clazz.doubleToInt ((ncomponents + nrows - 1) / nrows);
127 } else {
128 nrows = Clazz.doubleToInt ((ncomponents + ncols - 1) / ncols);
129 }var totalGapsWidth = (ncols - 1) * this.hgap;
130 var widthWOInsets = parent.width - (insets.left + insets.right);
131 var widthOnComponent = Clazz.doubleToInt ((widthWOInsets - totalGapsWidth) / ncols);
132 var extraWidthAvailable = Clazz.doubleToInt ((widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2);
133 var totalGapsHeight = (nrows - 1) * this.vgap;
134 var heightWOInsets = parent.height - (insets.top + insets.bottom);
135 var heightOnComponent = Clazz.doubleToInt ((heightWOInsets - totalGapsHeight) / nrows);
136 var extraHeightAvailable = Clazz.doubleToInt ((heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2);
137 if (ltr) {
138 for (var c = 0, x = insets.left + extraWidthAvailable; c < ncols; c++, x += widthOnComponent + this.hgap) {
139 for (var r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent + this.vgap) {
140 var i = r * ncols + c;
141 if (i < ncomponents) {
142 parent.getComponent (i).setBounds (x, y, widthOnComponent, heightOnComponent);
143 }}
144 }
145 } else {
146 for (var c = 0, x = (parent.width - insets.right - widthOnComponent) - extraWidthAvailable; c < ncols; c++, x -= widthOnComponent + this.hgap) {
147 for (var r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent + this.vgap) {
148 var i = r * ncols + c;
149 if (i < ncomponents) {
150 parent.getComponent (i).setBounds (x, y, widthOnComponent, heightOnComponent);
151 }}
152 }
153 }}}, "java.awt.Container");
154 Clazz.overrideMethod (c$, "toString", 
155 function () {
156 return this.getClass ().getName () + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + "]";
157 });
158 });