View Javadoc

1   package org.telscenter.pas.common.ui.layouts;
2   
3   import java.awt.Component;
4   import java.awt.Container;
5   import java.awt.Dimension;
6   import java.awt.Insets;
7   import java.awt.LayoutManager;
8   
9   import javax.swing.SwingConstants;
10  
11  public class EqualsLayout implements LayoutManager, SwingConstants {
12  	private int gap;
13  
14  	private int alignment;
15  
16  	private int orientation;
17  
18  	public EqualsLayout(int orientation, int alignment, int gap) {
19  		setOrientation(orientation);
20  		setGap(gap);
21  		setAlignment(alignment);
22  	}
23  
24  
25  
26  	public int getOrientation() {
27  		return orientation;
28  	}
29  
30  	public void setOrientation(int orientation) {
31  		if (orientation != HORIZONTAL && orientation != VERTICAL)
32  			throw new IllegalArgumentException("Invalid Orientation: "
33  					+ orientation);
34  		this.orientation = orientation;
35  	}
36  
37  	public int getAlignment() {
38  		return alignment;
39  	}
40  
41  	public void setAlignment(int alignment) {
42  		this.alignment = alignment;
43  	}
44  
45  	public int getGap() {
46  		return gap;
47  	}
48  
49  	public void setGap(int gap) {
50  		this.gap = gap;
51  	}
52  
53  	private Dimension[] dimensions(Component children[]) {
54  		int maxWidth = 0;
55  		int maxHeight = 0;
56  		int visibleCount = 0;
57  		Dimension componentPreferredSize;
58  
59  		for (int i = 0, c = children.length; i < c; i++) {
60  			if (children[i].isVisible()) {
61  				componentPreferredSize = children[i].getPreferredSize();
62  				maxWidth = Math.max(maxWidth, componentPreferredSize.width);
63  				maxHeight = Math.max(maxHeight, componentPreferredSize.height);
64  				visibleCount++;
65  			}
66  		}
67  
68  		int usedWidth = orientation == HORIZONTAL ? maxWidth * visibleCount
69  				+ gap * (visibleCount - 1) : maxWidth;
70  		int usedHeight = orientation == VERTICAL ? maxHeight * visibleCount
71  				+ gap * (visibleCount - 1) : maxHeight;
72  		return new Dimension[] { new Dimension(maxWidth, maxHeight),
73  				new Dimension(usedWidth, usedHeight), };
74  	}
75  
76  	public void layoutContainer(Container container) {
77  		Insets insets = container.getInsets();
78  
79  		Component[] children = container.getComponents();
80  		Dimension dim[] = dimensions(children);
81  
82  		int maxWidth = dim[0].width;
83  		int maxHeight = dim[0].height;
84  		int usedWidth = dim[1].width;
85  		int usedHeight = dim[1].height;
86  
87  		switch (orientation) {
88  		case HORIZONTAL:
89  			switch (alignment) {
90  			case LEFT:
91  			case TOP:
92  				for (int i = 0, c = children.length; i < c; i++) {
93  					if (!children[i].isVisible())
94  						continue;
95  					children[i].setBounds(insets.left + (maxWidth + gap) * i,
96  							insets.top, maxWidth, maxHeight);
97  				}
98  				break;
99  			case RIGHT:
100 			case BOTTOM:
101 				for (int i = 0, c = children.length; i < c; i++) {
102 					if (!children[i].isVisible())
103 						continue;
104 					children[i].setBounds(container.getWidth() - insets.right
105 							- usedWidth + (maxWidth + gap) * i, insets.top,
106 							maxWidth, maxHeight);
107 				}
108 				break;
109 			}
110 			break;
111 		case VERTICAL:
112 			switch (alignment) {
113 			case LEFT:
114 			case TOP:
115 				for (int i = 0, c = children.length; i < c; i++) {
116 					if (!children[i].isVisible())
117 						continue;
118 					children[i].setBounds(insets.left, insets.top
119 							+ (maxHeight + gap) * i, maxWidth, maxHeight);
120 				}
121 				break;
122 			case RIGHT:
123 			case BOTTOM:
124 				for (int i = 0, c = children.length; i < c; i++) {
125 					if (!children[i].isVisible())
126 						continue;
127 					children[i].setBounds(insets.left, container.getHeight()
128 							- insets.bottom - usedHeight + (maxHeight + gap)
129 							* i, maxWidth, maxHeight);
130 				}
131 			}
132 		}
133 	}
134 
135 	public Dimension minimumLayoutSize(Container c) {
136 		return preferredLayoutSize(c);
137 	}
138 
139 	public Dimension preferredLayoutSize(Container container) {
140 		Insets insets = container.getInsets();
141 
142 		Component[] children = container.getComponents();
143 		Dimension dim[] = dimensions(children);
144 
145 		int usedWidth = dim[1].width;
146 		int usedHeight = dim[1].height;
147 
148 		return new Dimension(insets.left + usedWidth + insets.right, insets.top
149 				+ usedHeight + insets.bottom);
150 	}
151 
152 	public void addLayoutComponent(String string, Component comp) {
153 	}
154 
155 	public void removeLayoutComponent(Component c) {
156 	}
157 }