43 |
import java.awt.Container; |
import java.awt.Container; |
44 |
import java.awt.Dimension; |
import java.awt.Dimension; |
45 |
import java.awt.IllegalComponentStateException; |
import java.awt.IllegalComponentStateException; |
46 |
|
import java.awt.Insets; |
47 |
import java.awt.LayoutManager; |
import java.awt.LayoutManager; |
48 |
import java.awt.LayoutManager2; |
import java.awt.LayoutManager2; |
49 |
|
import java.awt.Rectangle; |
50 |
import java.io.Serializable; |
import java.io.Serializable; |
51 |
|
|
52 |
import javax.accessibility.Accessible; |
import javax.accessibility.Accessible; |
100 |
private static final long serialVersionUID = -4100116998559815027L; |
private static final long serialVersionUID = -4100116998559815027L; |
101 |
|
|
102 |
/** |
/** |
103 |
|
* The cached layout info for the glass pane. |
104 |
|
*/ |
105 |
|
private Rectangle glassPaneBounds; |
106 |
|
|
107 |
|
/** |
108 |
|
* The cached layout info for the layered pane. |
109 |
|
*/ |
110 |
|
private Rectangle layeredPaneBounds; |
111 |
|
|
112 |
|
/** |
113 |
|
* The cached layout info for the content pane. |
114 |
|
*/ |
115 |
|
private Rectangle contentPaneBounds; |
116 |
|
|
117 |
|
/** |
118 |
|
* The cached layout info for the menu bar. |
119 |
|
*/ |
120 |
|
private Rectangle menuBarBounds; |
121 |
|
|
122 |
|
/** |
123 |
|
* The cached preferred size. |
124 |
|
*/ |
125 |
|
private Dimension prefSize; |
126 |
|
|
127 |
|
/** |
128 |
* Creates a new <code>RootLayout</code> object. |
* Creates a new <code>RootLayout</code> object. |
129 |
*/ |
*/ |
130 |
protected RootLayout() |
protected RootLayout() |
131 |
{ |
{ |
132 |
// Nothing to do here. |
// Nothing to do here. |
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
163 |
*/ |
*/ |
164 |
public float getLayoutAlignmentX(Container target) |
public float getLayoutAlignmentX(Container target) |
165 |
{ |
{ |
166 |
return target.getAlignmentX(); |
return 0.0F; |
167 |
} |
} |
168 |
|
|
169 |
/** |
/** |
175 |
*/ |
*/ |
176 |
public float getLayoutAlignmentY(Container target) |
public float getLayoutAlignmentY(Container target) |
177 |
{ |
{ |
178 |
return target.getAlignmentY(); |
return 0.0F; |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
185 |
*/ |
*/ |
186 |
public void invalidateLayout(Container target) |
public void invalidateLayout(Container target) |
187 |
{ |
{ |
188 |
// Nothing to do here. |
synchronized (this) |
189 |
|
{ |
190 |
|
glassPaneBounds = null; |
191 |
|
layeredPaneBounds = null; |
192 |
|
contentPaneBounds = null; |
193 |
|
menuBarBounds = null; |
194 |
|
prefSize = null; |
195 |
|
} |
196 |
} |
} |
197 |
|
|
198 |
/** |
/** |
202 |
*/ |
*/ |
203 |
public void layoutContainer(Container c) |
public void layoutContainer(Container c) |
204 |
{ |
{ |
205 |
Dimension menuBarSize; |
if (glassPaneBounds == null || layeredPaneBounds == null |
206 |
int containerWidth = c.getBounds().width - getInsets().left |
|| contentPaneBounds == null || menuBarBounds == null) |
207 |
- getInsets().right; |
{ |
208 |
int containerHeight = c.getBounds().height - getInsets().top |
Insets i = getInsets(); |
209 |
- getInsets().bottom; |
int containerWidth = c.getBounds().width - i.left - i.right; |
210 |
Dimension contentPaneSize = contentPane.getPreferredSize(); |
int containerHeight = c.getBounds().height - i.top - i.bottom; |
211 |
|
|
212 |
// 1. the glassPane fills entire viewable region (bounds - insets). |
// 1. the glassPane fills entire viewable region (bounds - insets). |
213 |
// 2. the layeredPane filles entire viewable region. |
// 2. the layeredPane filles entire viewable region. |
214 |
// 3. the menuBar is positioned at the upper edge of layeredPane. |
// 3. the menuBar is positioned at the upper edge of layeredPane. |
215 |
// 4. the contentPane fills viewable region minus menuBar, if present. |
// 4. the contentPane fills viewable region minus menuBar, if present. |
216 |
|
|
|
/* |
|
|
if size of top-level window wasn't set then just set |
|
|
contentPane and menuBar to its preferred sizes. |
|
|
Otherwise, if the size of top-level window was specified then |
|
|
set menuBar to its preferred size and make content pane |
|
|
to fit into the remaining space |
|
|
|
|
|
|
|
|
+-------------------------------+ |
|
|
| JLayeredPane | |
|
|
| +--------------------------+ | |
|
|
| | menuBar | | |
|
|
| +--------------------------+ | |
|
|
| +--------------------------+ | |
|
|
| |contentPane | | |
|
|
| | | | |
|
|
| | | | |
|
|
| | | | |
|
|
| +--------------------------+ | |
|
|
+-------------------------------+ |
|
217 |
|
|
218 |
*/ |
// +-------------------------------+ |
219 |
if (containerWidth == 0 && containerHeight == 0) |
// | JLayeredPane | |
220 |
{ |
// | +--------------------------+ | |
221 |
if (menuBar != null) |
// | | menuBar | | |
222 |
{ |
// | +--------------------------+ | |
223 |
int maxWidth; |
// | +--------------------------+ | |
224 |
menuBarSize = menuBar.getPreferredSize(); |
// | |contentPane | | |
225 |
maxWidth = Math.max(menuBarSize.width, contentPaneSize.width); |
// | | | | |
226 |
menuBar.setBounds(0, 0, maxWidth, menuBarSize.height); |
// | | | | |
227 |
glassPane.setBounds(0, 0, maxWidth, menuBarSize.height |
// | | | | |
228 |
+ contentPaneSize.height); |
// | +--------------------------+ | |
229 |
contentPane.setBounds(0, menuBarSize.height, maxWidth, |
// +-------------------------------+ |
230 |
contentPaneSize.height); |
|
|
layeredPane.setBounds(0, 0, maxWidth, menuBarSize.height |
|
|
+ contentPaneSize.height); |
|
|
} |
|
|
else |
|
|
{ |
|
|
glassPane.setBounds(0, 0, contentPaneSize.width, |
|
|
contentPaneSize.height); |
|
|
contentPane.setBounds(0, 0, contentPaneSize.width, |
|
|
contentPaneSize.height); |
|
|
layeredPane.setBounds(0, 0, contentPaneSize.width, |
|
|
contentPaneSize.height); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
231 |
if (menuBar != null) |
if (menuBar != null) |
232 |
{ |
{ |
233 |
menuBarSize = menuBar.getPreferredSize(); |
Dimension menuBarSize = menuBar.getPreferredSize(); |
234 |
if (menuBarSize.height > containerHeight) |
if (menuBarSize.height > containerHeight) |
235 |
menuBarSize.height = containerHeight; |
menuBarSize.height = containerHeight; |
236 |
menuBar.setBounds(0, 0, containerWidth, menuBarSize.height); |
menuBarBounds = new Rectangle(0, 0, containerWidth, |
237 |
glassPane.setBounds(0, 0, containerWidth, containerHeight); |
menuBarSize.height); |
238 |
contentPane.setBounds(0, menuBarSize.height, containerWidth, |
contentPaneBounds = new Rectangle(0, menuBarSize.height, |
239 |
(containerHeight - menuBarSize.height)); |
containerWidth, |
240 |
|
containerHeight - menuBarSize.height); |
241 |
} |
} |
242 |
else |
else |
243 |
{ |
contentPaneBounds = new Rectangle(0, 0, containerWidth, |
244 |
glassPane.setBounds(0, 0, containerWidth, containerHeight); |
containerHeight); |
245 |
contentPane.setBounds(0, 0, containerWidth, containerHeight); |
|
246 |
} |
glassPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight); |
247 |
layeredPane.setBounds(0, 0, containerWidth, containerHeight); |
layeredPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight); |
248 |
} |
} |
249 |
|
|
250 |
|
glassPane.setBounds(glassPaneBounds); |
251 |
|
layeredPane.setBounds(layeredPaneBounds); |
252 |
|
if (menuBar != null) |
253 |
|
menuBar.setBounds(menuBarBounds); |
254 |
|
contentPane.setBounds(contentPaneBounds); |
255 |
} |
} |
256 |
|
|
257 |
/** |
/** |
287 |
*/ |
*/ |
288 |
public Dimension preferredLayoutSize(Container c) |
public Dimension preferredLayoutSize(Container c) |
289 |
{ |
{ |
290 |
Dimension menuBarSize; |
// We must synchronize here, otherwise we cannot guarantee that the |
291 |
Dimension prefSize; |
// prefSize is still non-null when returning. |
292 |
|
synchronized (this) |
|
Dimension containerSize = c.getSize(); |
|
|
Dimension contentPaneSize = contentPane.getPreferredSize(); |
|
|
|
|
|
if (containerSize.width == 0 && containerSize.height == 0) |
|
293 |
{ |
{ |
294 |
if (menuBar != null) |
if (prefSize == null) |
295 |
{ |
{ |
296 |
int maxWidth; |
Insets i = getInsets(); |
297 |
menuBarSize = menuBar.getPreferredSize(); |
prefSize = new Dimension(i.left + i.right, i.top + i.bottom); |
298 |
maxWidth = Math.max(menuBarSize.width, contentPaneSize.width); |
Dimension contentPrefSize = contentPane.getPreferredSize(); |
299 |
prefSize = new Dimension(maxWidth, |
prefSize.width += contentPrefSize.width; |
300 |
contentPaneSize.height |
prefSize.height += contentPrefSize.height; |
301 |
+ menuBarSize.height); |
if (menuBar != null) |
302 |
|
{ |
303 |
|
Dimension menuBarSize = menuBar.getPreferredSize(); |
304 |
|
if (menuBarSize.width > contentPrefSize.width) |
305 |
|
prefSize.width += menuBarSize.width - contentPrefSize.width; |
306 |
|
prefSize.height += menuBarSize.height; |
307 |
|
} |
308 |
} |
} |
309 |
else |
// Return a copy here so the cached value won't get trashed by some |
310 |
prefSize = contentPaneSize; |
// other component. |
311 |
} |
return new Dimension(prefSize); |
312 |
else |
} |
|
prefSize = c.getSize(); |
|
|
|
|
|
return prefSize; |
|
313 |
} |
} |
314 |
|
|
315 |
/** |
/** |