author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5769 | 2a390122edff |
child 20168 | 137788883a22 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
2658 | 28 |
import java.awt.Component; |
29 |
import java.awt.Container; |
|
30 |
import java.awt.Dimension; |
|
31 |
import java.awt.Graphics; |
|
32 |
import java.awt.Insets; |
|
33 |
import java.awt.LayoutManager; |
|
34 |
import java.awt.Rectangle; |
|
35 |
import java.beans.PropertyChangeEvent; |
|
36 |
import java.beans.PropertyChangeListener; |
|
37 |
import javax.swing.Box; |
|
38 |
import javax.swing.Icon; |
|
39 |
import javax.swing.JComponent; |
|
40 |
import javax.swing.JSeparator; |
|
41 |
import javax.swing.JToolBar; |
|
42 |
import javax.swing.plaf.ComponentUI; |
|
2 | 43 |
import javax.swing.plaf.basic.BasicToolBarUI; |
2658 | 44 |
import sun.swing.plaf.synth.SynthIcon; |
2 | 45 |
|
46 |
/** |
|
4394 | 47 |
* Provides the Synth L&F UI delegate for |
48 |
* {@link javax.swing.JToolBar}. |
|
2 | 49 |
* |
4394 | 50 |
* @since 1.7 |
2 | 51 |
*/ |
4394 | 52 |
public class SynthToolBarUI extends BasicToolBarUI |
53 |
implements PropertyChangeListener, SynthUI { |
|
54 |
private Icon handleIcon = null; |
|
55 |
private Rectangle contentRect = new Rectangle(); |
|
2 | 56 |
|
57 |
private SynthStyle style; |
|
58 |
private SynthStyle contentStyle; |
|
59 |
private SynthStyle dragWindowStyle; |
|
60 |
||
4394 | 61 |
/** |
62 |
* Creates a new UI object for the given component. |
|
63 |
* |
|
64 |
* @param c component to create UI object for |
|
65 |
* @return the UI object |
|
66 |
*/ |
|
2 | 67 |
public static ComponentUI createUI(JComponent c) { |
68 |
return new SynthToolBarUI(); |
|
69 |
} |
|
70 |
||
4394 | 71 |
/** |
72 |
* @inheritDoc |
|
73 |
*/ |
|
2658 | 74 |
@Override |
2 | 75 |
protected void installDefaults() { |
76 |
toolBar.setLayout(createLayout()); |
|
77 |
updateStyle(toolBar); |
|
78 |
} |
|
79 |
||
4394 | 80 |
/** |
81 |
* @inheritDoc |
|
82 |
*/ |
|
2658 | 83 |
@Override |
2 | 84 |
protected void installListeners() { |
85 |
super.installListeners(); |
|
86 |
toolBar.addPropertyChangeListener(this); |
|
87 |
} |
|
88 |
||
4394 | 89 |
/** |
90 |
* @inheritDoc |
|
91 |
*/ |
|
2658 | 92 |
@Override |
2 | 93 |
protected void uninstallListeners() { |
94 |
super.uninstallListeners(); |
|
95 |
toolBar.removePropertyChangeListener(this); |
|
96 |
} |
|
97 |
||
98 |
private void updateStyle(JToolBar c) { |
|
2658 | 99 |
SynthContext context = getContext( |
100 |
c, Region.TOOL_BAR_CONTENT, null, ENABLED); |
|
101 |
contentStyle = SynthLookAndFeel.updateStyle(context, this); |
|
102 |
context.dispose(); |
|
103 |
||
104 |
context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, null, ENABLED); |
|
105 |
dragWindowStyle = SynthLookAndFeel.updateStyle(context, this); |
|
106 |
context.dispose(); |
|
107 |
||
108 |
context = getContext(c, ENABLED); |
|
2 | 109 |
SynthStyle oldStyle = style; |
110 |
||
111 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
112 |
if (oldStyle != style) { |
|
113 |
handleIcon = |
|
114 |
style.getIcon(context, "ToolBar.handleIcon"); |
|
115 |
if (oldStyle != null) { |
|
116 |
uninstallKeyboardActions(); |
|
117 |
installKeyboardActions(); |
|
118 |
} |
|
119 |
} |
|
120 |
context.dispose(); |
|
121 |
} |
|
122 |
||
4394 | 123 |
/** |
124 |
* @inheritDoc |
|
125 |
*/ |
|
2658 | 126 |
@Override |
2 | 127 |
protected void uninstallDefaults() { |
128 |
SynthContext context = getContext(toolBar, ENABLED); |
|
129 |
||
130 |
style.uninstallDefaults(context); |
|
131 |
context.dispose(); |
|
132 |
style = null; |
|
133 |
||
134 |
handleIcon = null; |
|
135 |
||
2658 | 136 |
context = getContext(toolBar, Region.TOOL_BAR_CONTENT, |
137 |
contentStyle, ENABLED); |
|
2 | 138 |
contentStyle.uninstallDefaults(context); |
139 |
context.dispose(); |
|
140 |
contentStyle = null; |
|
141 |
||
2658 | 142 |
context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, |
143 |
dragWindowStyle, ENABLED); |
|
2 | 144 |
dragWindowStyle.uninstallDefaults(context); |
145 |
context.dispose(); |
|
146 |
dragWindowStyle = null; |
|
147 |
||
148 |
toolBar.setLayout(null); |
|
149 |
} |
|
150 |
||
4394 | 151 |
/** |
152 |
* @inheritDoc |
|
153 |
*/ |
|
2658 | 154 |
@Override |
155 |
protected void installComponents() {} |
|
2 | 156 |
|
4394 | 157 |
/** |
158 |
* @inheritDoc |
|
159 |
*/ |
|
2658 | 160 |
@Override |
161 |
protected void uninstallComponents() {} |
|
2 | 162 |
|
4394 | 163 |
/** |
164 |
* Creates a {@code LayoutManager} to use with the toolbar. |
|
165 |
* |
|
166 |
* @return a {@code LayoutManager} instance |
|
167 |
*/ |
|
2 | 168 |
protected LayoutManager createLayout() { |
169 |
return new SynthToolBarLayoutManager(); |
|
170 |
} |
|
171 |
||
4394 | 172 |
/** |
173 |
* @inheritDoc |
|
174 |
*/ |
|
175 |
@Override |
|
2 | 176 |
public SynthContext getContext(JComponent c) { |
4394 | 177 |
return getContext(c, SynthLookAndFeel.getComponentState(c)); |
2 | 178 |
} |
179 |
||
180 |
private SynthContext getContext(JComponent c, int state) { |
|
181 |
return SynthContext.getContext(SynthContext.class, c, |
|
182 |
SynthLookAndFeel.getRegion(c), style, state); |
|
183 |
} |
|
184 |
||
2658 | 185 |
private SynthContext getContext(JComponent c, Region region, SynthStyle style) { |
186 |
return SynthContext.getContext(SynthContext.class, c, region, |
|
187 |
style, getComponentState(c, region)); |
|
2 | 188 |
} |
189 |
||
2658 | 190 |
private SynthContext getContext(JComponent c, Region region, |
191 |
SynthStyle style, int state) { |
|
2 | 192 |
return SynthContext.getContext(SynthContext.class, c, region, |
2658 | 193 |
style, state); |
2 | 194 |
} |
195 |
||
196 |
private int getComponentState(JComponent c, Region region) { |
|
197 |
return SynthLookAndFeel.getComponentState(c); |
|
198 |
} |
|
199 |
||
4394 | 200 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
201 |
* Notifies this UI delegate to repaint the specified component. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
202 |
* This method paints the component background, then calls |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
203 |
* the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
204 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
205 |
* <p>In general, this method does not need to be overridden by subclasses. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
206 |
* All Look and Feel rendering code should reside in the {@code paint} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
207 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
208 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
209 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
210 |
* @see #paint(SynthContext,Graphics) |
4394 | 211 |
*/ |
2658 | 212 |
@Override |
2 | 213 |
public void update(Graphics g, JComponent c) { |
214 |
SynthContext context = getContext(c); |
|
215 |
||
216 |
SynthLookAndFeel.update(context, g); |
|
217 |
context.getPainter().paintToolBarBackground(context, |
|
218 |
g, 0, 0, c.getWidth(), c.getHeight(), |
|
219 |
toolBar.getOrientation()); |
|
220 |
paint(context, g); |
|
221 |
context.dispose(); |
|
222 |
} |
|
223 |
||
4394 | 224 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
225 |
* Paints the specified component according to the Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
226 |
* <p>This method is not used by Synth Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
227 |
* Painting is handled by the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
228 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
229 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
230 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
231 |
* @see #paint(SynthContext,Graphics) |
4394 | 232 |
*/ |
2658 | 233 |
@Override |
2 | 234 |
public void paint(Graphics g, JComponent c) { |
235 |
SynthContext context = getContext(c); |
|
236 |
||
237 |
paint(context, g); |
|
238 |
context.dispose(); |
|
239 |
} |
|
240 |
||
4394 | 241 |
/** |
242 |
* @inheritDoc |
|
243 |
*/ |
|
244 |
@Override |
|
2 | 245 |
public void paintBorder(SynthContext context, Graphics g, int x, |
246 |
int y, int w, int h) { |
|
247 |
context.getPainter().paintToolBarBorder(context, g, x, y, w, h, |
|
248 |
toolBar.getOrientation()); |
|
249 |
} |
|
250 |
||
4394 | 251 |
/** |
4962
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
252 |
* This implementation does nothing, because the {@code rollover} |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
253 |
* property of the {@code JToolBar} class is not used |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
254 |
* in the Synth Look and Feel. |
4394 | 255 |
*/ |
2658 | 256 |
@Override |
2 | 257 |
protected void setBorderToNonRollover(Component c) {} |
258 |
||
4394 | 259 |
/** |
4962
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
260 |
* This implementation does nothing, because the {@code rollover} |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
261 |
* property of the {@code JToolBar} class is not used |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
262 |
* in the Synth Look and Feel. |
4394 | 263 |
*/ |
2658 | 264 |
@Override |
2 | 265 |
protected void setBorderToRollover(Component c) {} |
266 |
||
4394 | 267 |
/** |
4962
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
268 |
* This implementation does nothing, because the {@code rollover} |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
269 |
* property of the {@code JToolBar} class is not used |
8e27c99de375
6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents:
4848
diff
changeset
|
270 |
* in the Synth Look and Feel. |
4394 | 271 |
*/ |
2658 | 272 |
@Override |
2 | 273 |
protected void setBorderToNormal(Component c) {} |
274 |
||
4394 | 275 |
/** |
276 |
* Paints the toolbar. |
|
277 |
* |
|
278 |
* @param context context for the component being painted |
|
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
279 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
280 |
* @see #update(Graphics,JComponent) |
4394 | 281 |
*/ |
2 | 282 |
protected void paint(SynthContext context, Graphics g) { |
283 |
if (handleIcon != null && toolBar.isFloatable()) { |
|
284 |
int startX = toolBar.getComponentOrientation().isLeftToRight() ? |
|
285 |
0 : toolBar.getWidth() - |
|
286 |
SynthIcon.getIconWidth(handleIcon, context); |
|
287 |
SynthIcon.paintIcon(handleIcon, context, g, startX, 0, |
|
288 |
SynthIcon.getIconWidth(handleIcon, context), |
|
289 |
SynthIcon.getIconHeight(handleIcon, context)); |
|
290 |
} |
|
291 |
||
2658 | 292 |
SynthContext subcontext = getContext( |
293 |
toolBar, Region.TOOL_BAR_CONTENT, contentStyle); |
|
2 | 294 |
paintContent(subcontext, g, contentRect); |
295 |
subcontext.dispose(); |
|
296 |
} |
|
297 |
||
4394 | 298 |
/** |
299 |
* Paints the toolbar content. |
|
300 |
* |
|
301 |
* @param context context for the component being painted |
|
302 |
* @param g {@code Graphics} object used for painting |
|
303 |
* @param bounds bounding box for the toolbar |
|
304 |
*/ |
|
305 |
protected void paintContent(SynthContext context, Graphics g, |
|
2 | 306 |
Rectangle bounds) { |
307 |
SynthLookAndFeel.updateSubregion(context, g, bounds); |
|
308 |
context.getPainter().paintToolBarContentBackground(context, g, |
|
309 |
bounds.x, bounds.y, bounds.width, bounds.height, |
|
310 |
toolBar.getOrientation()); |
|
311 |
context.getPainter().paintToolBarContentBorder(context, g, |
|
312 |
bounds.x, bounds.y, bounds.width, bounds.height, |
|
313 |
toolBar.getOrientation()); |
|
314 |
} |
|
315 |
||
4394 | 316 |
/** |
317 |
* @inheritDoc |
|
318 |
*/ |
|
2658 | 319 |
@Override |
2 | 320 |
protected void paintDragWindow(Graphics g) { |
321 |
int w = dragWindow.getWidth(); |
|
322 |
int h = dragWindow.getHeight(); |
|
2658 | 323 |
SynthContext context = getContext( |
324 |
toolBar, Region.TOOL_BAR_DRAG_WINDOW, dragWindowStyle); |
|
325 |
SynthLookAndFeel.updateSubregion( |
|
326 |
context, g, new Rectangle(0, 0, w, h)); |
|
2 | 327 |
context.getPainter().paintToolBarDragWindowBackground(context, |
328 |
g, 0, 0, w, h, |
|
329 |
dragWindow.getOrientation()); |
|
330 |
context.getPainter().paintToolBarDragWindowBorder(context, g, 0, 0, w, h, |
|
331 |
dragWindow.getOrientation()); |
|
332 |
context.dispose(); |
|
333 |
} |
|
334 |
||
335 |
// |
|
336 |
// PropertyChangeListener |
|
337 |
// |
|
338 |
||
4394 | 339 |
/** |
340 |
* @inheritDoc |
|
341 |
*/ |
|
342 |
@Override |
|
2 | 343 |
public void propertyChange(PropertyChangeEvent e) { |
344 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
345 |
updateStyle((JToolBar)e.getSource()); |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
||
350 |
class SynthToolBarLayoutManager implements LayoutManager { |
|
351 |
public void addLayoutComponent(String name, Component comp) {} |
|
352 |
||
353 |
public void removeLayoutComponent(Component comp) {} |
|
354 |
||
355 |
public Dimension minimumLayoutSize(Container parent) { |
|
356 |
JToolBar tb = (JToolBar)parent; |
|
357 |
Insets insets = tb.getInsets(); |
|
358 |
Dimension dim = new Dimension(); |
|
359 |
SynthContext context = getContext(tb); |
|
360 |
||
361 |
if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
|
362 |
dim.width = tb.isFloatable() ? |
|
363 |
SynthIcon.getIconWidth(handleIcon, context) : 0; |
|
364 |
Dimension compDim; |
|
365 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
366 |
Component component = tb.getComponent(i); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
367 |
if (component.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
368 |
compDim = component.getMinimumSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
369 |
dim.width += compDim.width; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
370 |
dim.height = Math.max(dim.height, compDim.height); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
371 |
} |
2 | 372 |
} |
373 |
} else { |
|
374 |
dim.height = tb.isFloatable() ? |
|
375 |
SynthIcon.getIconHeight(handleIcon, context) : 0; |
|
376 |
Dimension compDim; |
|
377 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
378 |
Component component = tb.getComponent(i); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
379 |
if (component.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
380 |
compDim = component.getMinimumSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
381 |
dim.width = Math.max(dim.width, compDim.width); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
382 |
dim.height += compDim.height; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
383 |
} |
2 | 384 |
} |
385 |
} |
|
386 |
dim.width += insets.left + insets.right; |
|
387 |
dim.height += insets.top + insets.bottom; |
|
388 |
||
389 |
context.dispose(); |
|
390 |
return dim; |
|
391 |
} |
|
392 |
||
393 |
public Dimension preferredLayoutSize(Container parent) { |
|
394 |
JToolBar tb = (JToolBar)parent; |
|
395 |
Insets insets = tb.getInsets(); |
|
396 |
Dimension dim = new Dimension(); |
|
397 |
SynthContext context = getContext(tb); |
|
398 |
||
399 |
if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
|
400 |
dim.width = tb.isFloatable() ? |
|
401 |
SynthIcon.getIconWidth(handleIcon, context) : 0; |
|
402 |
Dimension compDim; |
|
403 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
404 |
Component component = tb.getComponent(i); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
405 |
if (component.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
406 |
compDim = component.getPreferredSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
407 |
dim.width += compDim.width; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
408 |
dim.height = Math.max(dim.height, compDim.height); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
409 |
} |
2 | 410 |
} |
411 |
} else { |
|
412 |
dim.height = tb.isFloatable() ? |
|
413 |
SynthIcon.getIconHeight(handleIcon, context) : 0; |
|
414 |
Dimension compDim; |
|
415 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
416 |
Component component = tb.getComponent(i); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
417 |
if (component.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
418 |
compDim = component.getPreferredSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
419 |
dim.width = Math.max(dim.width, compDim.width); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
420 |
dim.height += compDim.height; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
421 |
} |
2 | 422 |
} |
423 |
} |
|
424 |
dim.width += insets.left + insets.right; |
|
425 |
dim.height += insets.top + insets.bottom; |
|
426 |
||
427 |
context.dispose(); |
|
428 |
return dim; |
|
429 |
} |
|
430 |
||
431 |
public void layoutContainer(Container parent) { |
|
432 |
JToolBar tb = (JToolBar)parent; |
|
433 |
Insets insets = tb.getInsets(); |
|
434 |
boolean ltr = tb.getComponentOrientation().isLeftToRight(); |
|
435 |
SynthContext context = getContext(tb); |
|
436 |
||
437 |
Component c; |
|
438 |
Dimension d; |
|
2658 | 439 |
|
440 |
// JToolBar by default uses a somewhat modified BoxLayout as |
|
441 |
// its layout manager. For compatibility reasons, we want to |
|
442 |
// support Box "glue" as a way to move things around on the |
|
443 |
// toolbar. "glue" is represented in BoxLayout as a Box.Filler |
|
444 |
// with a minimum and preferred size of (0,0). |
|
445 |
// So what we do here is find the number of such glue fillers |
|
446 |
// and figure out how much space should be allocated to them. |
|
447 |
int glueCount = 0; |
|
448 |
for (int i=0; i<tb.getComponentCount(); i++) { |
|
449 |
if (isGlue(tb.getComponent(i))) glueCount++; |
|
450 |
} |
|
451 |
||
2 | 452 |
if (tb.getOrientation() == JToolBar.HORIZONTAL) { |
453 |
int handleWidth = tb.isFloatable() ? |
|
454 |
SynthIcon.getIconWidth(handleIcon, context) : 0; |
|
455 |
||
456 |
// Note: contentRect does not take insets into account |
|
457 |
// since it is used for determining the bounds that are |
|
458 |
// passed to paintToolBarContentBackground(). |
|
459 |
contentRect.x = ltr ? handleWidth : 0; |
|
460 |
contentRect.y = 0; |
|
461 |
contentRect.width = tb.getWidth() - handleWidth; |
|
462 |
contentRect.height = tb.getHeight(); |
|
463 |
||
464 |
// However, we do take the insets into account here for |
|
465 |
// the purposes of laying out the toolbar child components. |
|
466 |
int x = ltr ? |
|
467 |
handleWidth + insets.left : |
|
468 |
tb.getWidth() - handleWidth - insets.right; |
|
469 |
int baseY = insets.top; |
|
470 |
int baseH = tb.getHeight() - insets.top - insets.bottom; |
|
471 |
||
2658 | 472 |
// we need to get the minimum width for laying things out |
473 |
// so that we can calculate how much empty space needs to |
|
474 |
// be distributed among the "glue", if any |
|
475 |
int extraSpacePerGlue = 0; |
|
476 |
if (glueCount > 0) { |
|
477 |
int minWidth = minimumLayoutSize(parent).width; |
|
478 |
extraSpacePerGlue = (tb.getWidth() - minWidth) / glueCount; |
|
479 |
if (extraSpacePerGlue < 0) extraSpacePerGlue = 0; |
|
480 |
} |
|
481 |
||
2 | 482 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
483 |
c = tb.getComponent(i); |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
484 |
if (c.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
485 |
d = c.getPreferredSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
486 |
int y, h; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
487 |
if (d.height >= baseH || c instanceof JSeparator) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
488 |
// Fill available height |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
489 |
y = baseY; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
490 |
h = baseH; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
491 |
} else { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
492 |
// Center component vertically in the available space |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
493 |
y = baseY + (baseH / 2) - (d.height / 2); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
494 |
h = d.height; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
495 |
} |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
496 |
//if the component is a "glue" component then add to its |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
497 |
//width the extraSpacePerGlue it is due |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
498 |
if (isGlue(c)) d.width += extraSpacePerGlue; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
499 |
c.setBounds(ltr ? x : x - d.width, y, d.width, h); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
500 |
x = ltr ? x + d.width : x - d.width; |
2 | 501 |
} |
502 |
} |
|
503 |
} else { |
|
504 |
int handleHeight = tb.isFloatable() ? |
|
505 |
SynthIcon.getIconHeight(handleIcon, context) : 0; |
|
506 |
||
507 |
// See notes above regarding the use of insets |
|
508 |
contentRect.x = 0; |
|
509 |
contentRect.y = handleHeight; |
|
510 |
contentRect.width = tb.getWidth(); |
|
511 |
contentRect.height = tb.getHeight() - handleHeight; |
|
512 |
||
513 |
int baseX = insets.left; |
|
514 |
int baseW = tb.getWidth() - insets.left - insets.right; |
|
515 |
int y = handleHeight + insets.top; |
|
516 |
||
2658 | 517 |
// we need to get the minimum height for laying things out |
518 |
// so that we can calculate how much empty space needs to |
|
519 |
// be distributed among the "glue", if any |
|
520 |
int extraSpacePerGlue = 0; |
|
521 |
if (glueCount > 0) { |
|
522 |
int minHeight = minimumLayoutSize(parent).height; |
|
523 |
extraSpacePerGlue = (tb.getHeight() - minHeight) / glueCount; |
|
524 |
if (extraSpacePerGlue < 0) extraSpacePerGlue = 0; |
|
525 |
} |
|
526 |
||
2 | 527 |
for (int i = 0; i < tb.getComponentCount(); i++) { |
528 |
c = tb.getComponent(i); |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
529 |
if (c.isVisible()) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
530 |
d = c.getPreferredSize(); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
531 |
int x, w; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
532 |
if (d.width >= baseW || c instanceof JSeparator) { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
533 |
// Fill available width |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
534 |
x = baseX; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
535 |
w = baseW; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
536 |
} else { |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
537 |
// Center component horizontally in the available space |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
538 |
x = baseX + (baseW / 2) - (d.width / 2); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
539 |
w = d.width; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
540 |
} |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
541 |
//if the component is a "glue" component then add to its |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
542 |
//height the extraSpacePerGlue it is due |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
543 |
if (isGlue(c)) d.height += extraSpacePerGlue; |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
544 |
c.setBounds(x, y, w, d.height); |
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
545 |
y += d.height; |
2 | 546 |
} |
547 |
} |
|
548 |
} |
|
549 |
context.dispose(); |
|
550 |
} |
|
2658 | 551 |
|
552 |
private boolean isGlue(Component c) { |
|
5769
2a390122edff
6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents:
5506
diff
changeset
|
553 |
if (c.isVisible() && c instanceof Box.Filler) { |
2658 | 554 |
Box.Filler f = (Box.Filler)c; |
555 |
Dimension min = f.getMinimumSize(); |
|
556 |
Dimension pref = f.getPreferredSize(); |
|
557 |
return min.width == 0 && min.height == 0 && |
|
558 |
pref.width == 0 && pref.height == 0; |
|
559 |
} |
|
560 |
return false; |
|
561 |
} |
|
2 | 562 |
} |
563 |
} |