1
14
15 package guk;
16
17 import java.awt.*;
18
19 import javax.swing.JPopupMenu;
20 import javax.swing.SwingUtilities;
21
22
23
28 public class MenuLayout implements LayoutManager {
29
30
35 public void addLayoutComponent(String name, Component comp) {}
36
37
41 public void removeLayoutComponent(Component comp) {}
42
43
50 public Dimension preferredLayoutSize(Container target) {
51 int membersCnt = target.getComponentCount();
52 Dimension[] componentPrefSizes = new Dimension[membersCnt];
53 for(int i = 0; i < membersCnt; i++){
55 componentPrefSizes[i] = target.getComponent(i).getPreferredSize();
56 }
57 Dimension dim = getCompositeSize(target, componentPrefSizes);
58 return dim;
59 }
60
61
70 protected Dimension getCompositeSize(Container target,
71 Dimension[] componentSizes){
72 Point location = new Point(0, 0);
74 if(target.isShowing()){
75 location = target.getLocationOnScreen();
76 }else{
77 if(target instanceof JPopupMenu){
78 Component invoker = ((JPopupMenu)target).getInvoker();
79 if(invoker != null) location = invoker.getLocationOnScreen();
80 }
81 }
82
83 if(location.x < 0 || location.y < 0){
85 location.x = Math.max(0, location.x);
86 location.y = Math.max(0, location.y);
87 }
88 Toolkit toolkit = Toolkit.getDefaultToolkit();
90 Rectangle contentsBounds = new Rectangle(toolkit.getScreenSize());
91 Insets screenInsets = new Insets(0, 0, 0, 0);
92 GraphicsConfiguration gc = findGraphicsConfiguration(target);
93 if (gc != null) {
94 contentsBounds = gc.getBounds();
95 screenInsets = toolkit.getScreenInsets(gc);
96 }else{
97 }
98
99 contentsBounds.width -= screenInsets.left + screenInsets.right;
101 contentsBounds.height -= screenInsets.top + screenInsets.bottom;
102 contentsBounds.height = Math.max(location.y,
104 contentsBounds.height - location.y);
105
106 Insets insets = target.getInsets();
108 contentsBounds.width -= insets.left + insets.right;
109 contentsBounds.height -= insets.top + insets.bottom;
110 Dimension dim = new Dimension(0, 0);
111 int previousColumnsWidth = 0;
112 int previousColumnsHeight = 0;
113 for (int i = 0; i < componentSizes.length; i++) {
114 if ( (dim.height +
115 componentSizes[i].height) <= contentsBounds.height) {
116 dim.height += componentSizes[i].height;
118 dim.width = Math.max(dim.width, componentSizes[i].width);
119 }
120 else {
121 previousColumnsWidth += dim.width;
123 previousColumnsHeight = Math.max(previousColumnsHeight, dim.height);
124 dim.height = componentSizes[i].height;
125 dim.width = componentSizes[i].width;
126 }
127 }
128
129 dim.height = Math.max(previousColumnsHeight, dim.height);
131 dim.width += previousColumnsWidth;
132 dim.width += insets.left + insets.right;
134 dim.height += insets.top + insets.bottom;
135 return dim;
136 }
137
138
144 protected GraphicsConfiguration findGraphicsConfiguration(Component target){
145 GraphicsConfiguration gc = null;
146 if(!target.isShowing()){
147 if(target instanceof JPopupMenu){
148 Component invoker = ((JPopupMenu)target).getInvoker();
149 if(invoker != null) target = invoker;
150 }
151 }
152 if(target.isShowing()){
153 Point position = target.getLocationOnScreen();
154 Toolkit toolkit = Toolkit.getDefaultToolkit();
155 gc = target.getGraphicsConfiguration();
156 GraphicsEnvironment ge =
157 GraphicsEnvironment.getLocalGraphicsEnvironment();
158 GraphicsDevice[] gd = ge.getScreenDevices();
159 for (int i = 0; i < gd.length; i++) {
160 if (gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
161 GraphicsConfiguration dgc =
162 gd[i].getDefaultConfiguration();
163 if (dgc.getBounds().contains(position)) {
164 gc = dgc;
165 break;
166 }
167 }
168 }
169 }
170 return gc;
171 }
172
173
179 public Dimension minimumLayoutSize(Container target) {
180 int membersCnt = target.getComponentCount();
181 Dimension[] componentMinSizes = new Dimension[membersCnt];
182 for(int i = 0; i < membersCnt; i++){
184 componentMinSizes[i] = target.getComponent(i).getMinimumSize();
185 }
186 return getCompositeSize(target, componentMinSizes);
187 }
188
189
190 public void layoutContainer(Container target) {
191 Insets insets = target.getInsets();
192 Rectangle bounds = target.getBounds();
193 if(target.isShowing()){
195 Point locationOnScreen = target.getLocationOnScreen();
196 if(locationOnScreen.x < 0 || locationOnScreen.y < 0){
197 Window parent = SwingUtilities.getWindowAncestor(target);
198 int newx = Math.max(0, locationOnScreen.x);
199 int newy = Math.max(0, locationOnScreen.y);
200 parent.setLocation(newx, newy);
201 }
202 }
203 int maxheight = bounds.height - insets.bottom;
204 int compCnt = target.getComponentCount();
205 int y = insets.top;
206 int x = insets.left;
207 int rowWidth = 0;
208
209 for (int i = 0; i < compCnt; i++) {
210 Component comp = target.getComponent(i);
211 if (comp.isVisible()) {
212 Dimension d = comp.getPreferredSize();
213 comp.setSize(d);
214 if (y + d.height <= maxheight) {
215 comp.setLocation(x, y);
216 y += d.height;
217 rowWidth = Math.max(rowWidth, d.width);
218 }
219 else {
220 x += rowWidth;
222 rowWidth = 0;
223 y = insets.top;
224 comp.setLocation(x, y);
225 y += d.height;
226 rowWidth = Math.max(rowWidth, d.width);
227 }
228 }
229 }
230 }
231 }