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 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing.layout; |
|
27 |
|
28 import java.awt.*; |
|
29 import javax.swing.*; |
|
30 import javax.swing.border.LineBorder; |
|
31 import com.oracle.solaris.vp.util.swing.ColorUtil; |
|
32 |
|
33 public class RowLayout extends AbstractTableLayout<RowLayoutConstraint> { |
|
34 // |
|
35 // Static data |
|
36 // |
|
37 |
|
38 // Defaults, if not specified |
|
39 protected static final HorizontalAnchor DEFAULT_ANCHOR = |
|
40 HorizontalAnchor.LEFT; |
|
41 protected static final RowLayoutConstraint DEFAULT_CONSTRAINT = |
|
42 new RowLayoutConstraint(); |
|
43 |
|
44 // |
|
45 // Instance data |
|
46 // |
|
47 |
|
48 private HorizontalAnchor hAnchor; |
|
49 |
|
50 // |
|
51 // Constructors |
|
52 // |
|
53 |
|
54 /** |
|
55 * Constructs a {@code RowLayout} with the given horizontal |
|
56 * anchor. |
|
57 * |
|
58 * @param hAnchor |
|
59 * the horizontal anchor to use when positioning components |
|
60 * in a container with extra space. |
|
61 */ |
|
62 public RowLayout(HorizontalAnchor hAnchor) { |
|
63 super(hAnchor, VerticalAnchor.FILL, DEFAULT_CONSTRAINT); |
|
64 setHorizontalAnchor(hAnchor); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Constructs a {@code RowLayout} with the default horizontal |
|
69 * anchor ({@code HorizontalAnchor.LEFT}). |
|
70 */ |
|
71 public RowLayout() { |
|
72 this(DEFAULT_ANCHOR); |
|
73 } |
|
74 |
|
75 // |
|
76 // ConstrainedLayout methods |
|
77 // |
|
78 |
|
79 @Override |
|
80 public RowLayoutConstraint cloneConstraint( |
|
81 RowLayoutConstraint constraint) { |
|
82 |
|
83 return constraint.clone(); |
|
84 } |
|
85 |
|
86 // |
|
87 // AbstractTableLayout methods |
|
88 // |
|
89 |
|
90 @Override |
|
91 protected AbstractTableConstraint getColumnConstraint( |
|
92 Container container, int col) { |
|
93 |
|
94 Component[] comps = getLayoutComponents(container.getComponents()); |
|
95 return compToConst.get(comps[col]); |
|
96 } |
|
97 |
|
98 @Override |
|
99 protected AbstractTableConstraint getRowConstraint( |
|
100 Container container, int row) { |
|
101 |
|
102 return new SimpleTableConstraint(); |
|
103 } |
|
104 |
|
105 @Override |
|
106 protected int getColumnCount(Container container) { |
|
107 Component[] comps = getLayoutComponents(container.getComponents()); |
|
108 return comps.length; |
|
109 } |
|
110 |
|
111 // |
|
112 // Static methods |
|
113 // |
|
114 |
|
115 /** |
|
116 * Unit test/example usage. |
|
117 */ |
|
118 public static void main(String[] args) { |
|
119 JFrame frame = new JFrame(); |
|
120 Container contentPane = frame.getContentPane(); |
|
121 contentPane.setBackground(ColorUtil.getRandomColor()); |
|
122 |
|
123 HorizontalAnchor[] allHAnchors = HorizontalAnchor.values(); |
|
124 VerticalAnchor[] allVAnchors = VerticalAnchor.values(); |
|
125 |
|
126 frame.setLayout(new GridLayout( |
|
127 allHAnchors.length + allVAnchors.length - 1, 1, 15, 15)); |
|
128 |
|
129 for (HorizontalAnchor rowAnchor : allHAnchors) { |
|
130 |
|
131 HorizontalAnchor[] hAnchors = rowAnchor == HorizontalAnchor.FILL ? |
|
132 allHAnchors : new HorizontalAnchor[] { HorizontalAnchor.FILL }; |
|
133 |
|
134 for (HorizontalAnchor hAnchor : hAnchors) { |
|
135 RowLayout layout = new RowLayout(rowAnchor); |
|
136 |
|
137 JPanel p = new JPanel(layout); |
|
138 p.setBackground(ColorUtil.getRandomColor()); |
|
139 |
|
140 int gap = 0; |
|
141 int weight = 0; |
|
142 for (VerticalAnchor vAnchor : allVAnchors) { |
|
143 |
|
144 RowLayoutConstraint constraint = |
|
145 new RowLayoutConstraint(hAnchor, vAnchor, gap, weight); |
|
146 gap += 5; |
|
147 weight++; |
|
148 |
|
149 JComponent c = new JTextArea(String.format( |
|
150 "Row: %s\n" + |
|
151 "Element (h, v): %s, %s\n" + |
|
152 "Weight: %f, Gap: %d", |
|
153 rowAnchor, constraint.getHorizontalAnchor(), |
|
154 constraint.getVerticalAnchor(), constraint.getWeight(), |
|
155 constraint.getGap())); |
|
156 |
|
157 c.setBorder(new LineBorder(Color.black, 1)); |
|
158 |
|
159 p.add(c, constraint); |
|
160 } |
|
161 |
|
162 contentPane.add(p); |
|
163 } |
|
164 } |
|
165 |
|
166 GraphicsEnvironment env = |
|
167 GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|
168 Rectangle bounds = env.getMaximumWindowBounds(); |
|
169 frame.setSize(bounds.width, bounds.height); |
|
170 frame.setVisible(true); |
|
171 } |
|
172 } |
|