001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx.runtime.swing;
006:
007: import java.awt.*;
008: import javax.swing.*;
009:
010: /** Vertical box layout. The layout rules followed by this class are quite different than the core BoxLayout class,
011: * and in general represent a more useful algorithm.
012: *
013: *@author Ethan Nicholas
014: */
015: public class VBoxLayout implements LayoutManager {
016: private int spacing = 6;
017: private int horizontalAlignment = SwingConstants.LEFT;
018: private int verticalAlignment = SwingConstants.TOP;
019:
020: public int getSpacing() {
021: return spacing;
022: }
023:
024: public void setSpacing(int spacing) {
025: this .spacing = spacing;
026: }
027:
028: public int getHorizontalAlignment() {
029: return horizontalAlignment;
030: }
031:
032: public void setHorizontalAlignment(int horizontalAlignment) {
033: this .horizontalAlignment = horizontalAlignment;
034: }
035:
036: public int getVerticalAlignment() {
037: return verticalAlignment;
038: }
039:
040: public void setVerticalAlignment(int verticalAlignment) {
041: this .verticalAlignment = verticalAlignment;
042: }
043:
044: public void addLayoutComponent(String name, Component comp) {
045: }
046:
047: public void layoutContainer(Container parent) {
048: Insets insets = parent.getInsets();
049: int parentWidth = parent.getSize().width - insets.left
050: - insets.right;
051: int count = parent.getComponentCount();
052: Dimension preferredSize = parent.getPreferredSize();
053: int y;
054: switch (verticalAlignment) {
055: case SwingConstants.TOP:
056: y = insets.top;
057: break;
058: case SwingConstants.CENTER:
059: y = insets.top
060: + (parent.getHeight() - preferredSize.height) / 2;
061: break;
062: case SwingConstants.BOTTOM:
063: y = insets.top
064: + (parent.getHeight() - preferredSize.height);
065: break;
066: default:
067: throw new IllegalArgumentException(
068: "invalid vertical alignment: " + verticalAlignment);
069: }
070:
071: for (int i = 0; i < count; i++) {
072: Component component = parent.getComponent(i);
073: Dimension childPreferredSize = component.getPreferredSize();
074: int width = Math.min(childPreferredSize.width, parentWidth);
075: int x;
076: switch (horizontalAlignment) {
077: case SwingConstants.LEFT:
078: x = insets.left;
079: break;
080: case SwingConstants.CENTER:
081: x = insets.left
082: + (parentWidth - childPreferredSize.width) / 2;
083: break;
084: case SwingConstants.RIGHT:
085: x = insets.left
086: + (parentWidth - childPreferredSize.width);
087: break;
088: default:
089: throw new IllegalArgumentException(
090: "invalid horizontal alignment: "
091: + horizontalAlignment);
092: }
093: component.setBounds(x, y, width, childPreferredSize.height);
094: y += childPreferredSize.height + spacing;
095: }
096: }
097:
098: public Dimension minimumLayoutSize(Container parent) {
099: int width = 0;
100: int height = (parent.getComponentCount() - 1) * spacing;
101: for (int i = parent.getComponentCount() - 1; i >= 0; i--) {
102: Dimension minimumSize = parent.getComponent(i)
103: .getMinimumSize();
104: width = Math.max(width, minimumSize.width);
105: height += minimumSize.height;
106: }
107: Insets insets = parent.getInsets();
108: return new Dimension(width + insets.left + insets.right, height
109: + insets.top + insets.bottom);
110: }
111:
112: public Dimension preferredLayoutSize(Container parent) {
113: int width = 0;
114: int height = (parent.getComponentCount() - 1) * spacing;
115: for (int i = parent.getComponentCount() - 1; i >= 0; i--) {
116: Dimension preferredSize = parent.getComponent(i)
117: .getPreferredSize();
118: width = Math.max(width, preferredSize.width);
119: height += preferredSize.height;
120: }
121: Insets insets = parent.getInsets();
122: return new Dimension(width + insets.left + insets.right, height
123: + insets.top + insets.bottom);
124: }
125:
126: public void removeLayoutComponent(Component comp) {
127: }
128: }
|