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: /** Horizontal 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 HBoxLayout 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 parentHeight = parent.getSize().height - insets.top
050: - insets.bottom;
051: int count = parent.getComponentCount();
052: Dimension preferredSize = parent.getPreferredSize();
053: int x;
054: switch (horizontalAlignment) {
055: case SwingConstants.LEFT:
056: x = insets.left;
057: break;
058: case SwingConstants.CENTER:
059: x = insets.left + (parent.getWidth() - preferredSize.width)
060: / 2;
061: break;
062: case SwingConstants.RIGHT:
063: x = insets.left + (parent.getWidth() - preferredSize.width);
064: break;
065: default:
066: throw new IllegalArgumentException(
067: "invalid horizontal alignment: "
068: + horizontalAlignment);
069: }
070:
071: for (int i = 0; i < count; i++) {
072: Component component = parent.getComponent(i);
073: Dimension childPreferredSize = component.getPreferredSize();
074: int height = Math.min(childPreferredSize.height,
075: parentHeight);
076: int y;
077: switch (verticalAlignment) {
078: case SwingConstants.TOP:
079: y = insets.top;
080: break;
081: case SwingConstants.CENTER:
082: y = insets.top
083: + (parentHeight - childPreferredSize.height)
084: / 2;
085: break;
086: case SwingConstants.BOTTOM:
087: y = insets.top
088: + (parentHeight - childPreferredSize.height);
089: break;
090: default:
091: throw new IllegalArgumentException(
092: "invalid vertical alignment: "
093: + verticalAlignment);
094: }
095: component.setBounds(x, y, childPreferredSize.width, height);
096: x += childPreferredSize.width + spacing;
097: }
098: }
099:
100: public Dimension minimumLayoutSize(Container parent) {
101: int width = (parent.getComponentCount() - 1) * spacing;
102: int height = 0;
103: for (int i = parent.getComponentCount() - 1; i >= 0; i--) {
104: Dimension minimumSize = parent.getComponent(i)
105: .getMinimumSize();
106: width += minimumSize.width;
107: height = Math.max(height, minimumSize.height);
108: }
109: Insets insets = parent.getInsets();
110: return new Dimension(width + insets.left + insets.right, height
111: + insets.top + insets.bottom);
112: }
113:
114: public Dimension preferredLayoutSize(Container parent) {
115: int width = (parent.getComponentCount() - 1) * spacing;
116: int height = 0;
117: for (int i = parent.getComponentCount() - 1; i >= 0; i--) {
118: Dimension preferredSize = parent.getComponent(i)
119: .getPreferredSize();
120: width += preferredSize.width;
121: height = Math.max(height, preferredSize.height);
122: }
123: Insets insets = parent.getInsets();
124: return new Dimension(width + insets.left + insets.right, height
125: + insets.top + insets.bottom);
126: }
127:
128: public void removeLayoutComponent(Component comp) {
129: }
130: }
|