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, Version 1.0 only
|
|
6 * (the "License"). You may not use this file except in compliance
|
|
7 * with the License.
|
|
8 *
|
|
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
10 * or http://www.opensolaris.org/os/licensing.
|
|
11 * See the License for the specific language governing permissions
|
|
12 * and limitations under the License.
|
|
13 *
|
|
14 * When distributing Covered Code, include this CDDL HEADER in each
|
|
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
16 * If applicable, add the following below this CDDL HEADER, with the
|
|
17 * fields enclosed by brackets "[]" replaced with your own identifying
|
|
18 * information: Portions Copyright [yyyy] [name of copyright owner]
|
|
19 *
|
|
20 * CDDL HEADER END
|
|
21 */
|
|
22 /*
|
|
23 * ident "@(#)ProportionalLayout.java 1.3 05/06/08 SMI"
|
|
24 *
|
|
25 * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
|
|
26 * All rights reserved.
|
|
27 */
|
|
28 package com.sun.dhcpmgr.ui;
|
|
29
|
|
30 import java.awt.*;
|
|
31 import java.util.Hashtable;
|
|
32
|
|
33 /**
|
|
34 * This layout manager provides a layout which allocates space either
|
|
35 * horizontally or vertically to components in proportion to a weight assigned
|
|
36 * to each component. All components receive the same dimension along the
|
|
37 * other axis.
|
|
38 */
|
|
39 public class ProportionalLayout implements LayoutManager {
|
|
40 /**
|
|
41 * Constant for a horizontal proportional layout.
|
|
42 */
|
|
43 public static final int HORIZONTAL = 0;
|
|
44 /**
|
|
45 * Constant for a vertical proportional layout.
|
|
46 */
|
|
47 public static final int VERTICAL = 1;
|
|
48 private int direction;
|
|
49 private Hashtable components;
|
|
50 private int totalWeight = 0;
|
|
51
|
|
52 /**
|
|
53 * Construct a horizontal version of this layout.
|
|
54 */
|
|
55 public ProportionalLayout() {
|
|
56 this(HORIZONTAL);
|
|
57 }
|
|
58
|
|
59 /**
|
|
60 * Construct a proportional layout in the direction specified.
|
|
61 * @param direction Must be either HORIZONTAL or VERTICAL
|
|
62 */
|
|
63 public ProportionalLayout(int direction) {
|
|
64 this.direction = direction;
|
|
65 components = new Hashtable();
|
|
66 }
|
|
67
|
|
68 public void addLayoutComponent(String name, Component component) {
|
|
69 Integer weight;
|
|
70 try {
|
|
71 weight = Integer.decode(name);
|
|
72 } catch (NumberFormatException e) {
|
|
73 weight = new Integer(1);
|
|
74 }
|
|
75 totalWeight += weight.intValue();
|
|
76 components.put(component, weight);
|
|
77 }
|
|
78
|
|
79 public void removeLayoutComponent(Component component) {
|
|
80 Integer weight = (Integer)components.get(component);
|
|
81 totalWeight -= weight.intValue();
|
|
82 components.remove(component);
|
|
83 }
|
|
84
|
|
85 private Dimension computeLayoutSize(Container target, boolean minimum) {
|
|
86 Dimension dim = new Dimension(0, 0);
|
|
87 for (int i = 0; i < target.getComponentCount(); ++i) {
|
|
88 Component c = target.getComponent(i);
|
|
89 if (c.isVisible()) {
|
|
90 Dimension d;
|
|
91 if (minimum) {
|
|
92 d = c.getMinimumSize();
|
|
93 } else {
|
|
94 d = c.getPreferredSize();
|
|
95 }
|
|
96 if (direction == HORIZONTAL) {
|
|
97 dim.height = Math.max(dim.height, d.height);
|
|
98 dim.width += d.width;
|
|
99 } else {
|
|
100 dim.height += d.height;
|
|
101 dim.width = Math.max(dim.width, d.width);
|
|
102 }
|
|
103 }
|
|
104 }
|
|
105 Insets insets = target.getInsets();
|
|
106 dim.width += insets.left + insets.right;
|
|
107 dim.height += insets.top + insets.bottom;
|
|
108 return dim;
|
|
109 }
|
|
110
|
|
111 public Dimension preferredLayoutSize(Container target) {
|
|
112 return computeLayoutSize(target, false);
|
|
113 }
|
|
114
|
|
115 public Dimension minimumLayoutSize(Container target) {
|
|
116 return computeLayoutSize(target, true);
|
|
117 }
|
|
118
|
|
119 public void layoutContainer(Container target) {
|
|
120 Insets insets = target.getInsets();
|
|
121 Dimension dim = target.getSize();
|
|
122 int x = insets.left;
|
|
123 int y = insets.top;
|
|
124 int totalHeight = dim.height - insets.bottom;
|
|
125 int totalWidth = dim.width - insets.right;
|
|
126
|
|
127 for (int i = 0; i < target.getComponentCount(); ++i) {
|
|
128 Component c = target.getComponent(i);
|
|
129 if (c.isVisible()) {
|
|
130 if (direction == HORIZONTAL) {
|
|
131 float fw = (float)totalWidth
|
|
132 * (float)((Integer)components.get(c)).intValue()
|
|
133 / (float)totalWeight;
|
|
134 c.setBounds(x, y, (int)fw, totalHeight);
|
|
135 x += (int)fw;
|
|
136 } else {
|
|
137 float fh = (float)totalHeight
|
|
138 * (float)((Integer)components.get(c)).intValue()
|
|
139 / (float)totalWeight;
|
|
140 c.setBounds(x, y, totalWidth, (int)fh);
|
|
141 y += (int)fh;
|
|
142 }
|
|
143 }
|
|
144 }
|
|
145 }
|
|
146 }
|