summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/BetterRootLayout.java
blob: 238dd14d5e6f8d6f01f27f328ca8e9584cf9cd15 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Intersect Software Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.JRootPane;

/**
 * A custom layout for a JRootPane that handles the the layout of a JRootPane's
 * layeredPane, glassPane, and menuBar, and in addition handles four decorative
 * components arranged in a border layout. Add the decorative components to the
 * JRootPane using the directional constants; CENTER is reserved for the content
 * pane and menu bar.
 *
 * @author michael@mpowers.net
 * @version $Revision: 904 $
 */
public class BetterRootLayout extends BorderLayout {
	/**
	 * Returns the amount of space the layout would like to have.
	 *
	 * @param the Container for which this layout manager is being used
	 * @return a Dimension object containing the layout's preferred size
	 * @throws ClassCastException if parent is not a JRootPane
	 */
	public Dimension preferredLayoutSize(Container parent) {
		JRootPane rootPane = (JRootPane) parent;

		JPanel proxyPanel = new JPanel();
		proxyPanel.setLayout(new BorderLayout());

		JPanel contentProxy = null;
		if (rootPane.getContentPane() != null) {
			contentProxy = new JPanel();
			contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize());
			contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize());
			contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize());
			proxyPanel.add(contentProxy, CENTER);
		}
		JPanel menuProxy = null;
		if (rootPane.getJMenuBar() != null) {
			menuProxy = new JPanel();
			menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize());
			menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize());
			menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize());
			proxyPanel.add(menuProxy, NORTH);
		}

		this.addLayoutComponent(proxyPanel, CENTER);

		Dimension result = super.preferredLayoutSize(parent);

		this.removeLayoutComponent(proxyPanel);

		proxyPanel.removeAll();

		return result;
	}

	/**
	 * Returns the minimum amount of space the layout needs.
	 *
	 * @param the Container for which this layout manager is being used
	 * @return a Dimension object containing the layout's minimum size
	 * @throws ClassCastException if parent is not a JRootPane
	 */
	public Dimension minimumLayoutSize(Container parent) {
		JRootPane rootPane = (JRootPane) parent;

		JPanel proxyPanel = new JPanel();
		proxyPanel.setLayout(new BorderLayout());

		JPanel contentProxy = null;
		if (rootPane.getContentPane() != null) {
			contentProxy = new JPanel();
			contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize());
			contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize());
			contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize());
			proxyPanel.add(contentProxy, CENTER);
		}
		JPanel menuProxy = null;
		if (rootPane.getJMenuBar() != null) {
			menuProxy = new JPanel();
			menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize());
			menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize());
			menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize());
			proxyPanel.add(menuProxy, NORTH);
		}

		this.addLayoutComponent(proxyPanel, CENTER);

		Dimension result = super.minimumLayoutSize(parent);

		this.removeLayoutComponent(proxyPanel);

		proxyPanel.removeAll();

		return result;
	}

	/**
	 * Returns the maximum amount of space the layout can use.
	 *
	 * @param the Container for which this layout manager is being used
	 * @return a Dimension object containing the layout's maximum size
	 * @throws ClassCastException if parent is not a JRootPane
	 */
	public Dimension maximumLayoutSize(Container target) {
		JRootPane rootPane = (JRootPane) target;

		JPanel proxyPanel = new JPanel();
		proxyPanel.setLayout(new BorderLayout());

		JPanel contentProxy = null;
		if (rootPane.getContentPane() != null) {
			contentProxy = new JPanel();
			contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize());
			contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize());
			contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize());
			proxyPanel.add(contentProxy, CENTER);
		}
		JPanel menuProxy = null;
		if (rootPane.getJMenuBar() != null) {
			menuProxy = new JPanel();
			menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize());
			menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize());
			menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize());
			proxyPanel.add(menuProxy, NORTH);
		}

		this.addLayoutComponent(proxyPanel, CENTER);

		Dimension result = super.maximumLayoutSize(target);

		this.removeLayoutComponent(proxyPanel);

		proxyPanel.removeAll();

		return result;
	}

	/**
	 * Instructs the layout manager to perform the layout for the specified
	 * container.
	 *
	 * @param the Container for which this layout manager is being used
	 * @throws ClassCastException if parent is not a JRootPane
	 */
	public void layoutContainer(Container parent) {
		JRootPane rootPane = (JRootPane) parent;

		Rectangle b = parent.getBounds();
		Insets i = rootPane.getInsets();
		int w = b.width - i.right - i.left;
		int h = b.height - i.top - i.bottom;

		// layout panes

		if (rootPane.getLayeredPane() != null) {
			rootPane.getLayeredPane().setBounds(i.left, i.top, w, h);
		}
		if (rootPane.getGlassPane() != null) {
			rootPane.getGlassPane().setBounds(i.left, i.top, w, h);
		}

		// handle proxy panel

		JPanel proxyPanel = new JPanel();
		proxyPanel.setLayout(new BorderLayout());

		this.addLayoutComponent(proxyPanel, CENTER);

		super.layoutContainer(parent);

		// use proxy sizes to set sizes of layeredPane's children

		Rectangle proxyRect = proxyPanel.getBounds();
		if (rootPane.getJMenuBar() != null) {
			Rectangle menuRect = proxyPanel.getBounds();
			menuRect.height = rootPane.getJMenuBar().getPreferredSize().height;
			rootPane.getJMenuBar().setBounds(menuRect);
			proxyRect.y += menuRect.height;
			proxyRect.height -= menuRect.height;
		}
		if (rootPane.getContentPane() != null) {
			rootPane.getContentPane().setBounds(proxyRect);
		}

		this.removeLayoutComponent(proxyPanel);

		proxyPanel.removeAll();
	}

	/**
	 * Passes NORTH, SOUTH, EAST, WEST and CENTER to super implementation, and
	 * ignores all others.
	 */
	public void addLayoutComponent(Component comp, Object constraints) {
		if (NORTH.equals(constraints)) {
			super.addLayoutComponent(comp, constraints);
		} else if (SOUTH.equals(constraints)) {
			super.addLayoutComponent(comp, constraints);
		} else if (EAST.equals(constraints)) {
			super.addLayoutComponent(comp, constraints);
		} else if (WEST.equals(constraints)) {
			super.addLayoutComponent(comp, constraints);
		} else if (CENTER.equals(constraints)) {
			super.addLayoutComponent(comp, constraints);
		}

		// otherwise, ignore
	}
}