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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* SerialLayout.java
*/
package org.tigris.swidgets;
import java.awt.*;
/**
* Lays out components in a single row or column starting from any
* side and aligning components to eachother.<p>
*
* Components can be set to start draw from, LEFTTORIGHT, TOPTOBOTTOM,
* RIGHTTOLEFT or BOTTOMTOTOP.<p>
*
* Components will line up with eachother by edge or follow a common
* central line.<p>
*
* The gap to leave before the first component and the following gaps
* between each component can be set.
*
* @author Bob Tarling
*/
public class SerialLayout extends LineLayout {
public static final int LEFTTORIGHT = 10;
public static final int TOPTOBOTTOM = 10;
public static final int RIGHTTOLEFT = 11;
public static final int BOTTOMTOTOP = 11;
public static final String NORTH = "North";
public static final String SOUTH = "South";
public static final String EAST = "East";
public static final String WEST = "West";
public static final String NORTHEAST = "NorthEast";
public static final String NORTHWEST = "NorthWest";
public static final String SOUTHEAST = "SouthEast";
public static final String SOUTHWEST = "SouthWest";
public static final int LEFT = 20;
public static final int RIGHT = 21;
public static final int TOP = 20;
public static final int BOTTOM = 21;
public static final int CENTER = 22;
public static final int FILL = 23;
private String position = WEST;
private int direction = LEFTTORIGHT;
private int alignment = TOP;
public SerialLayout() {
this(Horizontal.getInstance(), WEST, LEFTTORIGHT, TOP);
}
public SerialLayout(Orientation orientation) {
this(orientation, WEST, LEFTTORIGHT, TOP);
}
public SerialLayout(Orientation orientation, String position) {
this(orientation, position, LEFTTORIGHT, TOP);
}
public SerialLayout(Orientation orientation, String position,
int direction) {
this(orientation, position, direction, TOP);
}
public SerialLayout(Orientation orientation, String position,
int direction, int alignment) {
super(orientation);
this.position = position;
this.direction = direction;
this.alignment = alignment;
}
public SerialLayout(Orientation orientation, String position,
int direction, int alignment, int gap) {
super(orientation, gap);
this.position = position;
this.direction = direction;
this.alignment = alignment;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Point loc;
int preferredBreadth =
_orientation.getBreadth(parent.getPreferredSize());
if (direction == LEFTTORIGHT) {
if (position.equals(EAST)) {
loc =
new Point(parent.getWidth()
- (insets.right
+ preferredLayoutSize(parent).width),
insets.top);
} else {
loc = new Point(insets.left, insets.top);
}
int nComps = parent.getComponentCount();
for (int i = 0; i < nComps; i++) {
Component comp = parent.getComponent(i);
if (comp != null && comp.isVisible()) {
Dimension size = comp.getPreferredSize();
if (alignment == FILL) {
_orientation.setBreadth(size, preferredBreadth);
}
comp.setSize(size);
comp.setLocation(loc);
loc = _orientation.addToPosition(loc, comp);
loc = _orientation.addToPosition(loc, _gap);
}
}
}
else {
int lastUsablePosition = _orientation.getLastUsablePosition(parent);
int firstUsableOffset = _orientation.getFirstUsableOffset(parent);
loc = _orientation.newPoint(lastUsablePosition, firstUsableOffset);
int nComps = parent.getComponentCount();
for (int i = 0; i < nComps; i++) {
Component comp = parent.getComponent(i);
if (comp != null && comp.isVisible()) {
loc = _orientation.subtractFromPosition(loc, comp);
Dimension size = comp.getPreferredSize();
if (alignment == FILL) {
_orientation.setBreadth(size, preferredBreadth);
}
comp.setSize(size);
comp.setLocation(loc);
loc = _orientation.subtractFromPosition(loc, _gap);
}
}
}
}
}
|