author | alexsch |
Wed, 25 Sep 2013 17:08:31 +0400 | |
changeset 20168 | 137788883a22 |
parent 5506 | 202f599c92aa |
child 21259 | 4780cf380d96 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2008, 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 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import javax.swing.*; |
|
31 |
import javax.swing.plaf.*; |
|
32 |
import javax.swing.plaf.basic.BasicInternalFrameTitlePane; |
|
33 |
import java.beans.PropertyChangeListener; |
|
34 |
import java.beans.PropertyChangeEvent; |
|
35 |
import java.beans.PropertyVetoException; |
|
36 |
import sun.swing.SwingUtilities2; |
|
37 |
||
38 |
/** |
|
39 |
* The class that manages a synth title bar |
|
40 |
* |
|
41 |
* @author David Kloba |
|
42 |
* @author Joshua Outwater |
|
43 |
* @author Steve Wilson |
|
44 |
*/ |
|
45 |
class SynthInternalFrameTitlePane extends BasicInternalFrameTitlePane |
|
46 |
implements SynthUI, PropertyChangeListener { |
|
47 |
||
48 |
protected JPopupMenu systemPopupMenu; |
|
49 |
protected JButton menuButton; |
|
50 |
||
51 |
private SynthStyle style; |
|
52 |
private int titleSpacing; |
|
53 |
private int buttonSpacing; |
|
54 |
// Alignment for the title, one of SwingConstants.(LEADING|TRAILING|CENTER) |
|
55 |
private int titleAlignment; |
|
56 |
||
57 |
public SynthInternalFrameTitlePane(JInternalFrame f) { |
|
58 |
super(f); |
|
59 |
} |
|
60 |
||
61 |
public String getUIClassID() { |
|
62 |
return "InternalFrameTitlePaneUI"; |
|
63 |
} |
|
64 |
||
65 |
public SynthContext getContext(JComponent c) { |
|
66 |
return getContext(c, getComponentState(c)); |
|
67 |
} |
|
68 |
||
69 |
public SynthContext getContext(JComponent c, int state) { |
|
70 |
return SynthContext.getContext(SynthContext.class, c, |
|
71 |
SynthLookAndFeel.getRegion(c), style, state); |
|
72 |
} |
|
73 |
||
74 |
private Region getRegion(JComponent c) { |
|
75 |
return SynthLookAndFeel.getRegion(c); |
|
76 |
} |
|
77 |
||
78 |
private int getComponentState(JComponent c) { |
|
79 |
if (frame != null) { |
|
80 |
if (frame.isSelected()) { |
|
81 |
return SELECTED; |
|
82 |
} |
|
83 |
} |
|
84 |
return SynthLookAndFeel.getComponentState(c); |
|
85 |
} |
|
86 |
||
87 |
protected void addSubComponents() { |
|
88 |
menuButton.setName("InternalFrameTitlePane.menuButton"); |
|
89 |
iconButton.setName("InternalFrameTitlePane.iconifyButton"); |
|
90 |
maxButton.setName("InternalFrameTitlePane.maximizeButton"); |
|
91 |
closeButton.setName("InternalFrameTitlePane.closeButton"); |
|
92 |
||
93 |
add(menuButton); |
|
94 |
add(iconButton); |
|
95 |
add(maxButton); |
|
96 |
add(closeButton); |
|
97 |
} |
|
98 |
||
99 |
protected void installListeners() { |
|
100 |
super.installListeners(); |
|
101 |
frame.addPropertyChangeListener(this); |
|
102 |
addPropertyChangeListener(this); |
|
103 |
} |
|
104 |
||
105 |
protected void uninstallListeners() { |
|
106 |
frame.removePropertyChangeListener(this); |
|
107 |
removePropertyChangeListener(this); |
|
108 |
super.uninstallListeners(); |
|
109 |
} |
|
110 |
||
111 |
private void updateStyle(JComponent c) { |
|
112 |
SynthContext context = getContext(this, ENABLED); |
|
113 |
SynthStyle oldStyle = style; |
|
114 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
115 |
if (style != oldStyle) { |
|
116 |
maxIcon = |
|
117 |
style.getIcon(context,"InternalFrameTitlePane.maximizeIcon"); |
|
118 |
minIcon = |
|
119 |
style.getIcon(context,"InternalFrameTitlePane.minimizeIcon"); |
|
120 |
iconIcon = |
|
121 |
style.getIcon(context,"InternalFrameTitlePane.iconifyIcon"); |
|
122 |
closeIcon = |
|
123 |
style.getIcon(context,"InternalFrameTitlePane.closeIcon"); |
|
124 |
titleSpacing = style.getInt(context, |
|
125 |
"InternalFrameTitlePane.titleSpacing", 2); |
|
126 |
buttonSpacing = style.getInt(context, |
|
127 |
"InternalFrameTitlePane.buttonSpacing", 2); |
|
128 |
String alignString = (String)style.get(context, |
|
129 |
"InternalFrameTitlePane.titleAlignment"); |
|
130 |
titleAlignment = SwingConstants.LEADING; |
|
131 |
if (alignString != null) { |
|
132 |
alignString = alignString.toUpperCase(); |
|
133 |
if (alignString.equals("TRAILING")) { |
|
134 |
titleAlignment = SwingConstants.TRAILING; |
|
135 |
} |
|
136 |
else if (alignString.equals("CENTER")) { |
|
137 |
titleAlignment = SwingConstants.CENTER; |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
context.dispose(); |
|
142 |
} |
|
143 |
||
144 |
protected void installDefaults() { |
|
145 |
super.installDefaults(); |
|
146 |
updateStyle(this); |
|
147 |
} |
|
148 |
||
149 |
protected void uninstallDefaults() { |
|
150 |
SynthContext context = getContext(this, ENABLED); |
|
151 |
style.uninstallDefaults(context); |
|
152 |
context.dispose(); |
|
153 |
style = null; |
|
154 |
JInternalFrame.JDesktopIcon di = frame.getDesktopIcon(); |
|
155 |
if(di != null && di.getComponentPopupMenu() == systemPopupMenu) { |
|
156 |
// Release link to systemMenu from the JInternalFrame |
|
157 |
di.setComponentPopupMenu(null); |
|
158 |
} |
|
159 |
super.uninstallDefaults(); |
|
160 |
} |
|
161 |
||
162 |
private static class JPopupMenuUIResource extends JPopupMenu implements |
|
163 |
UIResource { } |
|
164 |
||
165 |
protected void assembleSystemMenu() { |
|
166 |
systemPopupMenu = new JPopupMenuUIResource(); |
|
167 |
addSystemMenuItems(systemPopupMenu); |
|
168 |
enableActions(); |
|
169 |
menuButton = createNoFocusButton(); |
|
170 |
updateMenuIcon(); |
|
171 |
menuButton.addMouseListener(new MouseAdapter() { |
|
172 |
public void mousePressed(MouseEvent e) { |
|
173 |
try { |
|
174 |
frame.setSelected(true); |
|
175 |
} catch(PropertyVetoException pve) { |
|
176 |
} |
|
177 |
showSystemMenu(); |
|
178 |
} |
|
179 |
}); |
|
180 |
JPopupMenu p = frame.getComponentPopupMenu(); |
|
181 |
if (p == null || p instanceof UIResource) { |
|
182 |
frame.setComponentPopupMenu(systemPopupMenu); |
|
183 |
} |
|
184 |
if (frame.getDesktopIcon() != null) { |
|
185 |
p = frame.getDesktopIcon().getComponentPopupMenu(); |
|
186 |
if (p == null || p instanceof UIResource) { |
|
187 |
frame.getDesktopIcon().setComponentPopupMenu(systemPopupMenu); |
|
188 |
} |
|
189 |
} |
|
190 |
setInheritsPopupMenu(true); |
|
191 |
} |
|
192 |
||
193 |
protected void addSystemMenuItems(JPopupMenu menu) { |
|
194 |
// PENDING: this should all be localizable! |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
195 |
JMenuItem mi = menu.add(restoreAction); |
2 | 196 |
mi.setMnemonic('R'); |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
197 |
mi = menu.add(moveAction); |
2 | 198 |
mi.setMnemonic('M'); |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
199 |
mi = menu.add(sizeAction); |
2 | 200 |
mi.setMnemonic('S'); |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
201 |
mi = menu.add(iconifyAction); |
2 | 202 |
mi.setMnemonic('n'); |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
203 |
mi = menu.add(maximizeAction); |
2 | 204 |
mi.setMnemonic('x'); |
205 |
menu.add(new JSeparator()); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
206 |
mi = menu.add(closeAction); |
2 | 207 |
mi.setMnemonic('C'); |
208 |
} |
|
209 |
||
210 |
protected void showSystemMenu() { |
|
211 |
Insets insets = frame.getInsets(); |
|
212 |
if (!frame.isIcon()) { |
|
1297
59aad2292413
6579243: Windows, GTK: Internal frame title is drawn wrong if the frame has RTL orientation
mlapshin
parents:
1290
diff
changeset
|
213 |
systemPopupMenu.show(frame, menuButton.getX(), getY() + getHeight()); |
2 | 214 |
} else { |
215 |
systemPopupMenu.show(menuButton, |
|
216 |
getX() - insets.left - insets.right, |
|
217 |
getY() - systemPopupMenu.getPreferredSize().height - |
|
218 |
insets.bottom - insets.top); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
// SynthInternalFrameTitlePane has no UI, we'll invoke paint on it. |
|
223 |
public void paintComponent(Graphics g) { |
|
224 |
SynthContext context = getContext(this); |
|
225 |
SynthLookAndFeel.update(context, g); |
|
226 |
context.getPainter().paintInternalFrameTitlePaneBackground(context, |
|
227 |
g, 0, 0, getWidth(), getHeight()); |
|
228 |
paint(context, g); |
|
229 |
context.dispose(); |
|
230 |
} |
|
231 |
||
232 |
protected void paint(SynthContext context, Graphics g) { |
|
233 |
String title = frame.getTitle(); |
|
234 |
||
235 |
if (title != null) { |
|
236 |
SynthStyle style = context.getStyle(); |
|
237 |
||
238 |
g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND)); |
|
239 |
g.setFont(style.getFont(context)); |
|
240 |
||
241 |
// Center text vertically. |
|
242 |
FontMetrics fm = SwingUtilities2.getFontMetrics(frame, g); |
|
243 |
int baseline = (getHeight() + fm.getAscent() - fm.getLeading() - |
|
244 |
fm.getDescent()) / 2; |
|
245 |
JButton lastButton = null; |
|
246 |
if (frame.isIconifiable()) { |
|
247 |
lastButton = iconButton; |
|
248 |
} |
|
249 |
else if (frame.isMaximizable()) { |
|
250 |
lastButton = maxButton; |
|
251 |
} |
|
252 |
else if (frame.isClosable()) { |
|
253 |
lastButton = closeButton; |
|
254 |
} |
|
255 |
int maxX; |
|
256 |
int minX; |
|
257 |
boolean ltr = SynthLookAndFeel.isLeftToRight(frame); |
|
258 |
int titleAlignment = this.titleAlignment; |
|
259 |
if (ltr) { |
|
260 |
if (lastButton != null) { |
|
261 |
maxX = lastButton.getX() - titleSpacing; |
|
262 |
} |
|
263 |
else { |
|
264 |
maxX = frame.getWidth() - frame.getInsets().right - |
|
265 |
titleSpacing; |
|
266 |
} |
|
267 |
minX = menuButton.getX() + menuButton.getWidth() + |
|
268 |
titleSpacing; |
|
269 |
} |
|
270 |
else { |
|
271 |
if (lastButton != null) { |
|
272 |
minX = lastButton.getX() + lastButton.getWidth() + |
|
273 |
titleSpacing; |
|
274 |
} |
|
275 |
else { |
|
276 |
minX = frame.getInsets().left + titleSpacing; |
|
277 |
} |
|
278 |
maxX = menuButton.getX() - titleSpacing; |
|
279 |
if (titleAlignment == SwingConstants.LEADING) { |
|
280 |
titleAlignment = SwingConstants.TRAILING; |
|
281 |
} |
|
282 |
else if (titleAlignment == SwingConstants.TRAILING) { |
|
283 |
titleAlignment = SwingConstants.LEADING; |
|
284 |
} |
|
285 |
} |
|
286 |
String clippedTitle = getTitle(title, fm, maxX - minX); |
|
287 |
if (clippedTitle == title) { |
|
288 |
// String fit, align as necessary. |
|
289 |
if (titleAlignment == SwingConstants.TRAILING) { |
|
290 |
minX = maxX - style.getGraphicsUtils(context). |
|
291 |
computeStringWidth(context, g.getFont(), fm, title); |
|
292 |
} |
|
293 |
else if (titleAlignment == SwingConstants.CENTER) { |
|
294 |
int width = style.getGraphicsUtils(context). |
|
295 |
computeStringWidth(context, g.getFont(), fm, title); |
|
296 |
minX = Math.max(minX, (getWidth() - width) / 2); |
|
297 |
minX = Math.min(maxX - width, minX); |
|
298 |
} |
|
299 |
} |
|
300 |
style.getGraphicsUtils(context).paintText( |
|
301 |
context, g, clippedTitle, minX, baseline - fm.getAscent(), -1); |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
public void paintBorder(SynthContext context, Graphics g, int x, |
|
306 |
int y, int w, int h) { |
|
307 |
context.getPainter().paintInternalFrameTitlePaneBorder(context, |
|
308 |
g, x, y, w, h); |
|
309 |
} |
|
310 |
||
311 |
protected LayoutManager createLayout() { |
|
312 |
SynthContext context = getContext(this); |
|
313 |
LayoutManager lm = |
|
314 |
(LayoutManager)style.get(context, "InternalFrameTitlePane.titlePaneLayout"); |
|
315 |
context.dispose(); |
|
316 |
return (lm != null) ? lm : new SynthTitlePaneLayout(); |
|
317 |
} |
|
318 |
||
319 |
public void propertyChange(PropertyChangeEvent evt) { |
|
320 |
if (evt.getSource() == this) { |
|
321 |
if (SynthLookAndFeel.shouldUpdateStyle(evt)) { |
|
322 |
updateStyle(this); |
|
323 |
} |
|
324 |
} |
|
325 |
else { |
|
326 |
// Changes for the internal frame |
|
327 |
if (evt.getPropertyName() == JInternalFrame.FRAME_ICON_PROPERTY) { |
|
328 |
updateMenuIcon(); |
|
329 |
} |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Resets the menuButton icon to match that of the frame. |
|
335 |
*/ |
|
336 |
private void updateMenuIcon() { |
|
337 |
Icon frameIcon = frame.getFrameIcon(); |
|
338 |
SynthContext context = getContext(this); |
|
339 |
if (frameIcon != null) { |
|
340 |
Dimension maxSize = (Dimension)context.getStyle().get(context, |
|
341 |
"InternalFrameTitlePane.maxFrameIconSize"); |
|
342 |
int maxWidth = 16; |
|
343 |
int maxHeight = 16; |
|
344 |
if (maxSize != null) { |
|
345 |
maxWidth = maxSize.width; |
|
346 |
maxHeight = maxSize.height; |
|
347 |
} |
|
348 |
if ((frameIcon.getIconWidth() > maxWidth || |
|
349 |
frameIcon.getIconHeight() > maxHeight) && |
|
350 |
(frameIcon instanceof ImageIcon)) { |
|
351 |
frameIcon = new ImageIcon(((ImageIcon)frameIcon). |
|
352 |
getImage().getScaledInstance(maxWidth, maxHeight, |
|
353 |
Image.SCALE_SMOOTH)); |
|
354 |
} |
|
355 |
} |
|
356 |
context.dispose(); |
|
357 |
menuButton.setIcon(frameIcon); |
|
358 |
} |
|
359 |
||
360 |
||
361 |
class SynthTitlePaneLayout implements LayoutManager { |
|
362 |
public void addLayoutComponent(String name, Component c) {} |
|
363 |
public void removeLayoutComponent(Component c) {} |
|
364 |
public Dimension preferredLayoutSize(Container c) { |
|
365 |
return minimumLayoutSize(c); |
|
366 |
} |
|
367 |
||
368 |
public Dimension minimumLayoutSize(Container c) { |
|
369 |
SynthContext context = getContext( |
|
370 |
SynthInternalFrameTitlePane.this); |
|
371 |
int width = 0; |
|
372 |
int height = 0; |
|
373 |
||
374 |
int buttonCount = 0; |
|
375 |
Dimension pref; |
|
376 |
||
377 |
if (frame.isClosable()) { |
|
378 |
pref = closeButton.getPreferredSize(); |
|
379 |
width += pref.width; |
|
380 |
height = Math.max(pref.height, height); |
|
381 |
buttonCount++; |
|
382 |
} |
|
383 |
if (frame.isMaximizable()) { |
|
384 |
pref = maxButton.getPreferredSize(); |
|
385 |
width += pref.width; |
|
386 |
height = Math.max(pref.height, height); |
|
387 |
buttonCount++; |
|
388 |
} |
|
389 |
if (frame.isIconifiable()) { |
|
390 |
pref = iconButton.getPreferredSize(); |
|
391 |
width += pref.width; |
|
392 |
height = Math.max(pref.height, height); |
|
393 |
buttonCount++; |
|
394 |
} |
|
395 |
pref = menuButton.getPreferredSize(); |
|
396 |
width += pref.width; |
|
397 |
height = Math.max(pref.height, height); |
|
398 |
||
399 |
width += Math.max(0, (buttonCount - 1) * buttonSpacing); |
|
400 |
||
401 |
FontMetrics fm = SynthInternalFrameTitlePane.this.getFontMetrics( |
|
402 |
getFont()); |
|
403 |
SynthGraphicsUtils graphicsUtils = context.getStyle(). |
|
404 |
getGraphicsUtils(context); |
|
405 |
String frameTitle = frame.getTitle(); |
|
406 |
int title_w = frameTitle != null ? graphicsUtils. |
|
407 |
computeStringWidth(context, fm.getFont(), |
|
408 |
fm, frameTitle) : 0; |
|
409 |
int title_length = frameTitle != null ? frameTitle.length() : 0; |
|
410 |
||
411 |
// Leave room for three characters in the title. |
|
412 |
if (title_length > 3) { |
|
413 |
int subtitle_w = graphicsUtils.computeStringWidth(context, |
|
414 |
fm.getFont(), fm, frameTitle.substring(0, 3) + "..."); |
|
415 |
width += (title_w < subtitle_w) ? title_w : subtitle_w; |
|
416 |
} else { |
|
417 |
width += title_w; |
|
418 |
} |
|
419 |
||
420 |
height = Math.max(fm.getHeight() + 2, height); |
|
421 |
||
422 |
width += titleSpacing + titleSpacing; |
|
423 |
||
424 |
Insets insets = getInsets(); |
|
425 |
height += insets.top + insets.bottom; |
|
426 |
width += insets.left + insets.right; |
|
427 |
context.dispose(); |
|
428 |
return new Dimension(width, height); |
|
429 |
} |
|
430 |
||
431 |
private int center(Component c, Insets insets, int x, |
|
432 |
boolean trailing) { |
|
433 |
Dimension pref = c.getPreferredSize(); |
|
434 |
if (trailing) { |
|
435 |
x -= pref.width; |
|
436 |
} |
|
437 |
c.setBounds(x, insets.top + |
|
438 |
(getHeight() - insets.top - insets.bottom - |
|
439 |
pref.height) / 2, pref.width, pref.height); |
|
440 |
if (pref.width > 0) { |
|
441 |
if (trailing) { |
|
442 |
return x - buttonSpacing; |
|
443 |
} |
|
444 |
return x + pref.width + buttonSpacing; |
|
445 |
} |
|
446 |
return x; |
|
447 |
} |
|
448 |
||
449 |
public void layoutContainer(Container c) { |
|
450 |
Insets insets = c.getInsets(); |
|
451 |
Dimension pref; |
|
452 |
||
453 |
if (SynthLookAndFeel.isLeftToRight(frame)) { |
|
454 |
center(menuButton, insets, insets.left, false); |
|
455 |
int x = getWidth() - insets.right; |
|
456 |
if (frame.isClosable()) { |
|
457 |
x = center(closeButton, insets, x, true); |
|
458 |
} |
|
459 |
if (frame.isMaximizable()) { |
|
460 |
x = center(maxButton, insets, x, true); |
|
461 |
} |
|
462 |
if (frame.isIconifiable()) { |
|
463 |
x = center(iconButton, insets, x, true); |
|
464 |
} |
|
465 |
} |
|
466 |
else { |
|
467 |
center(menuButton, insets, getWidth() - insets.right, |
|
468 |
true); |
|
469 |
int x = insets.left; |
|
470 |
if (frame.isClosable()) { |
|
471 |
x = center(closeButton, insets, x, false); |
|
472 |
} |
|
473 |
if (frame.isMaximizable()) { |
|
474 |
x = center(maxButton, insets, x, false); |
|
475 |
} |
|
476 |
if (frame.isIconifiable()) { |
|
477 |
x = center(iconButton, insets, x, false); |
|
478 |
} |
|
479 |
} |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
private JButton createNoFocusButton() { |
|
484 |
JButton button = new JButton(); |
|
485 |
button.setFocusable(false); |
|
486 |
button.setMargin(new Insets(0,0,0,0)); |
|
487 |
return button; |
|
488 |
} |
|
489 |
} |