1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
/*
* ProportionalLayout.java
*/
package org.tigris.swidgets;
import java.awt.*;
import java.util.*;
/**
* Allows components to be a set as a proportion to their container or
* left as fixed size. Components are resized accordingly when the
* parent is resized.
*
* @author Bob Tarling
*/
public class ProportionalLayout extends LineLayout {
protected Hashtable componentTable;
public ProportionalLayout() {
this(HORIZONTAL);
}
public ProportionalLayout(Orientation orientation) {
super(orientation);
componentTable = new Hashtable();
}
public final void addLayoutComponent(Component comp, Object constraints) {
if (constraints == null) constraints = "";
addLayoutComponent((String) constraints, comp);
}
public void addLayoutComponent(String name, Component comp) {
try {
componentTable.put(comp, name.toString());
}
catch (Exception e) {
componentTable.put(comp, "");
}
}
public void removeLayoutComponent(Component comp) {
componentTable.remove(comp);
}
public void layoutContainer(Container parent) {
// Find the total proportional size of all visible components
double totalProportionalLength = 0;
int totalLength;
totalLength = _orientation.getLengthMinusInsets(parent);
Enumeration enumKeys = componentTable.keys();
while (enumKeys.hasMoreElements()) {
Component comp = (Component) enumKeys.nextElement();
if (comp.isVisible()) {
String size = (String) (componentTable.get(comp));
if (size.length() != 0) {
totalProportionalLength += Double.parseDouble(size);
}
else {
totalLength -= _orientation.getLength(comp);
}
}
}
Insets insets = parent.getInsets();
Point loc = new Point(insets.top, insets.left);
int length = 0;
int nComps = parent.getComponentCount();
for (int i = 0; i < nComps; i++) {
Component comp = parent.getComponent(i);
if (comp.isVisible()) {
String proportionalLength = (String) (componentTable.get(comp));
if (proportionalLength.length() != 0) {
length =
(int)
((totalLength * Double.parseDouble(proportionalLength))
/ totalProportionalLength);
if (length < 0) length = 0;
}
else {
length = _orientation.getLength(comp);
}
comp.setSize(_orientation.setLength(parent.getSize(), length));
comp.setLocation(loc);
loc = _orientation.addToPosition(loc, length);
}
}
}
}
|