1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * JFlex 1.4.3 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
4 | * All rights reserved. *
|
---|
5 | * *
|
---|
6 | * This program is free software; you can redistribute it and/or modify *
|
---|
7 | * it under the terms of the GNU General Public License. See the file *
|
---|
8 | * COPYRIGHT for more information. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License along *
|
---|
16 | * with this program; if not, write to the Free Software Foundation, Inc., *
|
---|
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
---|
18 | * *
|
---|
19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
20 |
|
---|
21 | package JFlex.gui;
|
---|
22 |
|
---|
23 | import java.awt.*;
|
---|
24 | import java.util.*;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Grid layout manager like GridLayout but with predefinable
|
---|
28 | * grid size.
|
---|
29 | *
|
---|
30 | * @author Gerwin Klein
|
---|
31 | * @version JFlex 1.4.3, $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
32 | */
|
---|
33 | public class GridPanel extends Panel implements Handles {
|
---|
34 |
|
---|
35 | private int cols;
|
---|
36 | private int rows;
|
---|
37 |
|
---|
38 | private int hgap;
|
---|
39 | private int vgap;
|
---|
40 |
|
---|
41 | private Vector constraints = new Vector();
|
---|
42 | private Insets insets = new Insets(0,0,0,0);
|
---|
43 |
|
---|
44 | public GridPanel(int cols, int rows) {
|
---|
45 | this(cols, rows, 0, 0);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public GridPanel(int cols, int rows, int hgap, int vgap) {
|
---|
49 | this.cols = cols;
|
---|
50 | this.rows = rows;
|
---|
51 | this.hgap = hgap;
|
---|
52 | this.vgap = vgap;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void doLayout() {
|
---|
56 | Dimension size = getSize();
|
---|
57 | size.height -= insets.top+insets.bottom;
|
---|
58 | size.width -= insets.left+insets.right;
|
---|
59 |
|
---|
60 | float cellWidth = size.width/cols;
|
---|
61 | float cellHeight = size.height/rows;
|
---|
62 |
|
---|
63 | for (int i = 0; i < constraints.size(); i++) {
|
---|
64 | GridPanelConstraint c = (GridPanelConstraint) constraints.elementAt(i);
|
---|
65 |
|
---|
66 | float x = cellWidth * c.x + insets.left + hgap/2;
|
---|
67 | float y = cellHeight * c.y + insets.right + vgap/2;
|
---|
68 |
|
---|
69 | float width, height;
|
---|
70 |
|
---|
71 | if (c.handle == FILL) {
|
---|
72 | width = (cellWidth-hgap) * c.width;
|
---|
73 | height = (cellHeight-vgap) * c.height;
|
---|
74 | }
|
---|
75 | else {
|
---|
76 | Dimension d = c.component.getPreferredSize();
|
---|
77 | width = d.width;
|
---|
78 | height = d.height;
|
---|
79 | }
|
---|
80 |
|
---|
81 | switch (c.handle) {
|
---|
82 | case TOP_CENTER:
|
---|
83 | x+= (cellWidth+width)/2;
|
---|
84 | break;
|
---|
85 | case TOP_RIGHT:
|
---|
86 | x+= cellWidth-width;
|
---|
87 | break;
|
---|
88 | case CENTER_LEFT:
|
---|
89 | y+= (cellHeight+height)/2;
|
---|
90 | break;
|
---|
91 | case CENTER:
|
---|
92 | x+= (cellWidth+width)/2;
|
---|
93 | y+= (cellHeight+height)/2;
|
---|
94 | break;
|
---|
95 | case CENTER_RIGHT:
|
---|
96 | y+= (cellHeight+height)/2;
|
---|
97 | x+= cellWidth-width;
|
---|
98 | break;
|
---|
99 | case BOTTOM:
|
---|
100 | y+= cellHeight-height;
|
---|
101 | break;
|
---|
102 | case BOTTOM_CENTER:
|
---|
103 | x+= (cellWidth+width)/2;
|
---|
104 | y+= cellHeight-height;
|
---|
105 | break;
|
---|
106 | case BOTTOM_RIGHT:
|
---|
107 | y+= cellHeight-height;
|
---|
108 | x+= cellWidth-width;
|
---|
109 | break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | c.component.setBounds(new Rectangle((int)x, (int)y, (int)width, (int)height));
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public Dimension getPreferredSize() {
|
---|
117 | float dy = 0;
|
---|
118 | float dx = 0;
|
---|
119 |
|
---|
120 | for (int i = 0; i < constraints.size(); i++) {
|
---|
121 | GridPanelConstraint c = (GridPanelConstraint) constraints.elementAt(i);
|
---|
122 |
|
---|
123 | Dimension d = c.component.getPreferredSize();
|
---|
124 |
|
---|
125 | dx = Math.max(dx, d.width/c.width);
|
---|
126 | dy = Math.max(dy, d.height/c.height);
|
---|
127 | }
|
---|
128 |
|
---|
129 | dx+= hgap;
|
---|
130 | dy+= vgap;
|
---|
131 |
|
---|
132 | dx*= cols;
|
---|
133 | dy*= rows;
|
---|
134 |
|
---|
135 | dx+= insets.left+insets.right;
|
---|
136 | dy+= insets.top+insets.bottom;
|
---|
137 |
|
---|
138 | return new Dimension((int)dx,(int)dy);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void setInsets(Insets insets) {
|
---|
142 | this.insets = insets;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void add(int x, int y, Component c) {
|
---|
146 | add(x,y,1,1,FILL,c);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void add(int x, int y, int handle, Component c) {
|
---|
150 | add(x,y,1,1,handle,c);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void add(int x, int y, int dx, int dy, Component c) {
|
---|
154 | add(x,y,dx,dy,FILL,c);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void add(int x, int y, int dx, int dy, int handle, Component c) {
|
---|
158 | super.add(c);
|
---|
159 | constraints.addElement(new GridPanelConstraint(x,y,dx,dy,handle,c));
|
---|
160 | }
|
---|
161 | }
|
---|