author | Stephen Talley <stephen.talley@oracle.com> |
Mon, 28 Mar 2011 10:53:34 -0400 | |
changeset 685 | 767674b0a2fb |
parent 219 | 57841c113efe |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
23 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 24 |
*/ |
25 |
||
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
26 |
package org.opensolaris.os.vp.util.swing.layout; |
0 | 27 |
|
28 |
import java.awt.*; |
|
29 |
import java.util.*; |
|
30 |
import java.util.List; |
|
31 |
import javax.swing.*; |
|
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
32 |
import org.opensolaris.os.vp.util.misc.CollectionUtil; |
0 | 33 |
|
34 |
public class TableLayout extends AbstractTableLayout<HasAnchors> { |
|
35 |
// |
|
36 |
// Static methods |
|
37 |
// |
|
38 |
||
39 |
public static final HorizontalAnchor DEFAULT_HORIZONTAL_ANCHOR = |
|
40 |
HorizontalAnchor.FILL; |
|
41 |
||
42 |
public static final VerticalAnchor DEFAULT_VERTICAL_ANCHOR = |
|
43 |
VerticalAnchor.FILL; |
|
44 |
||
45 |
public static final HasAnchors DEFAULT_CONSTRAINT = |
|
46 |
new SimpleHasAnchors(HorizontalAnchor.FILL, VerticalAnchor.FILL); |
|
47 |
||
48 |
// |
|
49 |
// Instance data |
|
50 |
// |
|
51 |
||
52 |
private List<AbstractTableConstraint> colConstraints = |
|
53 |
new ArrayList<AbstractTableConstraint>(); |
|
54 |
private List<AbstractTableConstraint> rowConstraints = |
|
55 |
new ArrayList<AbstractTableConstraint>(); |
|
56 |
||
57 |
// |
|
58 |
// Constructors |
|
59 |
// |
|
60 |
||
61 |
public TableLayout(AbstractTableConstraint[] rowConstraints, |
|
62 |
AbstractTableConstraint[] colConstraints) { |
|
63 |
||
64 |
super(DEFAULT_HORIZONTAL_ANCHOR, DEFAULT_VERTICAL_ANCHOR, |
|
65 |
DEFAULT_CONSTRAINT); |
|
66 |
||
67 |
CollectionUtil.addAll(this.rowConstraints, rowConstraints); |
|
68 |
CollectionUtil.addAll(this.colConstraints, colConstraints); |
|
69 |
} |
|
70 |
||
71 |
public TableLayout(int rows, int cols, int hGap, int vGap) { |
|
72 |
this(createTableConstraints(rows, vGap), |
|
73 |
createTableConstraints(cols, hGap)); |
|
74 |
} |
|
75 |
||
76 |
public TableLayout(int rows, int cols) { |
|
77 |
this(rows, cols, 0, 0); |
|
78 |
} |
|
79 |
||
80 |
// |
|
81 |
// ConstrainedLayout methods |
|
82 |
// |
|
83 |
||
84 |
@Override |
|
85 |
public HasAnchors cloneConstraint(HasAnchors constraint) { |
|
86 |
return new SimpleHasAnchors( |
|
87 |
constraint.getHorizontalAnchor(), constraint.getVerticalAnchor()); |
|
88 |
} |
|
89 |
||
90 |
// |
|
91 |
// AbstractTableLayout methods |
|
92 |
// |
|
93 |
||
94 |
@Override |
|
17
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
95 |
public AbstractTableConstraint getColumnConstraint( |
0 | 96 |
Container container, int col) { |
97 |
||
98 |
return colConstraints.get(col); |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
17
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
102 |
public AbstractTableConstraint getRowConstraint( |
0 | 103 |
Container container, int row) { |
104 |
||
105 |
int size = rowConstraints.size(); |
|
106 |
||
107 |
if (row < size) { |
|
108 |
return rowConstraints.get(row); |
|
109 |
} |
|
110 |
||
111 |
// Not enough rows were specified |
|
112 |
return rowConstraints.get(size - 1); |
|
113 |
} |
|
114 |
||
115 |
@Override |
|
116 |
protected int getColumnCount(Container container) { |
|
117 |
return colConstraints.size(); |
|
118 |
} |
|
119 |
||
120 |
// |
|
121 |
// TableLayout methods |
|
122 |
// |
|
123 |
||
124 |
public void addRows(AbstractTableConstraint... constraints) { |
|
125 |
CollectionUtil.addAll(this.rowConstraints, constraints); |
|
126 |
} |
|
127 |
||
128 |
public void addRow() { |
|
129 |
AbstractTableConstraint constraint = |
|
130 |
rowConstraints.get(rowConstraints.size() - 1); |
|
131 |
rowConstraints.add(constraint); |
|
132 |
} |
|
133 |
||
134 |
// |
|
135 |
// Static methods |
|
136 |
// |
|
137 |
||
138 |
private static SimpleTableConstraint[] createTableConstraints( |
|
139 |
int count, int gap) { |
|
140 |
||
141 |
if (count <= 0) { |
|
142 |
throw new IllegalArgumentException( |
|
143 |
"invalid number of rows/columns: " + count); |
|
144 |
} |
|
145 |
||
146 |
SimpleTableConstraint constraint = new SimpleTableConstraint(gap); |
|
147 |
||
148 |
SimpleTableConstraint[] constraints = new SimpleTableConstraint[count]; |
|
149 |
for (int i = 0; i < constraints.length; i++) { |
|
150 |
constraints[i] = constraint; |
|
151 |
} |
|
152 |
||
153 |
return constraints; |
|
154 |
} |
|
155 |
||
156 |
public static void main(String[] args) { |
|
157 |
int rows = 6; |
|
158 |
int cols = 6; |
|
159 |
int gap = 5; |
|
160 |
// TableLayout layout = new TableLayout(rows, cols, gap, gap); |
|
161 |
||
162 |
AbstractTableConstraint[] rowConstraints = |
|
163 |
new AbstractTableConstraint[rows]; |
|
164 |
for (int row = 0; row < rows; row++) { |
|
165 |
rowConstraints[row] = new SimpleTableConstraint(gap * row); |
|
166 |
rowConstraints[row].setLayoutIfInvisible(false); |
|
167 |
} |
|
168 |
||
169 |
AbstractTableConstraint[] colConstraints = |
|
170 |
new AbstractTableConstraint[cols]; |
|
171 |
for (int col = 0; col < cols; col++) { |
|
172 |
colConstraints[col] = new SimpleTableConstraint(gap * col); |
|
173 |
colConstraints[col].setLayoutIfInvisible(false); |
|
174 |
} |
|
175 |
||
176 |
TableLayout layout = new TableLayout(rowConstraints, colConstraints); |
|
177 |
layout.setVerticalAnchor(VerticalAnchor.FILL); |
|
178 |
layout.setHorizontalAnchor(HorizontalAnchor.FILL); |
|
179 |
||
180 |
JPanel p = new JPanel(layout); |
|
181 |
||
182 |
SimpleHasAnchors constraint = new SimpleHasAnchors( |
|
183 |
HorizontalAnchor.FILL, VerticalAnchor.FILL); |
|
184 |
||
185 |
for (int row = 0; row < rows; row++) { |
|
186 |
for (int col = 0; col < cols; col++) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
187 |
StringBuilder b = new StringBuilder("<html>"); |
0 | 188 |
for (int i = 0; i <= row; i++) { |
189 |
for (int j = 0; j <= col; j++) { |
|
190 |
b.append('m'); |
|
191 |
} |
|
192 |
b.append("<br/>"); |
|
193 |
} |
|
194 |
JButton button = new JButton(b.toString()); |
|
195 |
if (col == 2 || row == 2) { |
|
196 |
button.setVisible(false); |
|
197 |
} |
|
198 |
p.add(button, constraint); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
JFrame frame = new JFrame(); |
|
203 |
Container c = frame.getContentPane(); |
|
204 |
c.setLayout(new BorderLayout()); |
|
205 |
c.add(p, BorderLayout.CENTER); |
|
206 |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|
207 |
frame.pack(); |
|
208 |
frame.setVisible(true); |
|
209 |
} |
|
210 |
} |