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 | |
package net.mtu.eggplant.util.gui; |
29 | |
|
30 | |
import java.awt.Component; |
31 | |
import java.awt.Container; |
32 | |
import java.awt.Dimension; |
33 | |
import java.awt.Insets; |
34 | |
import java.awt.LayoutManager2; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class BetterBoxLayout implements LayoutManager2 { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public static final BetterBoxLayoutOrientation VERTICAL = new BetterBoxLayoutOrientation(); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public static final BetterBoxLayoutOrientation HORIZONTAL = new BetterBoxLayoutOrientation(); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public BetterBoxLayout() { |
58 | 0 | this(HORIZONTAL); |
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public BetterBoxLayout(final BetterBoxLayoutOrientation orientation) { |
65 | 0 | _orientation = orientation; |
66 | 0 | } |
67 | |
|
68 | |
private BetterBoxLayoutOrientation _orientation; |
69 | |
|
70 | |
public void addLayoutComponent(final String name, final Component comp) { |
71 | |
|
72 | 0 | } |
73 | |
|
74 | |
public void addLayoutComponent(final Component comp, final Object constraints) { |
75 | |
|
76 | 0 | } |
77 | |
|
78 | |
public void layoutContainer(final Container target) { |
79 | 0 | final Insets insets = target.getInsets(); |
80 | 0 | final Dimension targetSize = target.getSize(); |
81 | 0 | final Component[] components = target.getComponents(); |
82 | |
|
83 | 0 | if (_orientation == VERTICAL) { |
84 | 0 | final int componentWidth = targetSize.width |
85 | |
- insets.left - insets.right; |
86 | 0 | final int x = insets.left; |
87 | 0 | int y = insets.top; |
88 | 0 | for (int i = 0; i < components.length; i++) { |
89 | 0 | final int height = components[i].getPreferredSize().height; |
90 | 0 | components[i].setBounds(x, y, componentWidth, height); |
91 | 0 | y += height; |
92 | |
} |
93 | 0 | } else { |
94 | 0 | final int componentHeight = targetSize.height |
95 | |
- insets.top - insets.bottom; |
96 | 0 | int x = insets.left; |
97 | 0 | final int y = insets.top; |
98 | 0 | for (int i = 0; i < components.length; i++) { |
99 | 0 | final int width = components[i].getPreferredSize().width; |
100 | 0 | components[i].setBounds(x, y, width, componentHeight); |
101 | 0 | x += width; |
102 | |
} |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
public Dimension minimumLayoutSize(final Container target) { |
107 | 0 | return preferredLayoutSize(target); |
108 | |
} |
109 | |
|
110 | |
public Dimension preferredLayoutSize(final Container target) { |
111 | 0 | final Insets insets = target.getInsets(); |
112 | 0 | final Dimension dim = new Dimension(0, 0); |
113 | 0 | final Component[] components = target.getComponents(); |
114 | 0 | for (int i = 0; i < components.length; i++) { |
115 | 0 | final Dimension preferredSize = components[i].getPreferredSize(); |
116 | 0 | if (_orientation == VERTICAL) { |
117 | 0 | dim.height += preferredSize.height; |
118 | 0 | dim.width = Math.max(preferredSize.width, dim.width); |
119 | |
} else { |
120 | 0 | dim.height = Math.max(preferredSize.height, dim.height); |
121 | 0 | dim.width += preferredSize.width; |
122 | |
} |
123 | |
} |
124 | |
|
125 | 0 | dim.height += insets.top |
126 | |
+ insets.bottom; |
127 | 0 | dim.width += insets.left |
128 | |
+ insets.right; |
129 | 0 | return dim; |
130 | |
} |
131 | |
|
132 | |
public Dimension maximumLayoutSize(final Container target) { |
133 | 0 | return preferredLayoutSize(target); |
134 | |
} |
135 | |
|
136 | |
public void removeLayoutComponent(final Component comp) { |
137 | |
|
138 | 0 | } |
139 | |
|
140 | |
public float getLayoutAlignmentX(final Container target) { |
141 | 0 | return 0F; |
142 | |
} |
143 | |
|
144 | |
public float getLayoutAlignmentY(final Container target) { |
145 | 0 | return 0F; |
146 | |
} |
147 | |
|
148 | |
public void invalidateLayout(final Container target) { |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | 0 | private static final class BetterBoxLayoutOrientation { |
156 | |
} |
157 | |
|
158 | |
} |