KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthInternalFrameTitlePane


1 /*
2  * @(#)SynthInternalFrameTitlePane.java 1.22 04/09/10
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.synth;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.BasicInternalFrameTitlePane JavaDoc;
15 import javax.swing.border.*;
16 import javax.swing.event.InternalFrameEvent JavaDoc;
17 import java.util.EventListener JavaDoc;
18 import java.beans.PropertyChangeListener JavaDoc;
19 import java.beans.PropertyChangeEvent JavaDoc;
20 import java.beans.VetoableChangeListener JavaDoc;
21 import java.beans.PropertyVetoException JavaDoc;
22 import sun.swing.plaf.synth.SynthUI;
23
24 /**
25  * The class that manages a synth title bar
26  *
27  * @version 1.22 09/10/04
28  * @author David Kloba
29  * @author Joshua Outwater
30  * @author Steve Wilson
31  */

32 class SynthInternalFrameTitlePane extends BasicInternalFrameTitlePane JavaDoc
33         implements SynthUI, PropertyChangeListener JavaDoc {
34
35     protected JPopupMenu systemPopupMenu;
36     protected JButton menuButton;
37
38     private SynthStyle JavaDoc style;
39
40     public SynthInternalFrameTitlePane(JInternalFrame f) {
41         super(f);
42     }
43
44     public String JavaDoc getUIClassID() {
45         return "InternalFrameTitlePaneUI";
46     }
47
48     public SynthContext JavaDoc getContext(JComponent c) {
49         return getContext(c, getComponentState(c));
50     }
51
52     public SynthContext JavaDoc getContext(JComponent c, int state) {
53         return SynthContext.getContext(SynthContext JavaDoc.class, c,
54                     SynthLookAndFeel.getRegion(c), style, state);
55     }
56
57     private Region JavaDoc getRegion(JComponent c) {
58         return SynthLookAndFeel.getRegion(c);
59     }
60
61     private int getComponentState(JComponent c) {
62         if (frame != null) {
63             if (frame.isSelected()) {
64                 return SELECTED;
65             }
66         }
67         return SynthLookAndFeel.getComponentState(c);
68     }
69
70     protected void addSubComponents() {
71     menuButton.setName("InternalFrameTitlePane.menuButton");
72         iconButton.setName("InternalFrameTitlePane.iconifyButton");
73         maxButton.setName("InternalFrameTitlePane.maximizeButton");
74         closeButton.setName("InternalFrameTitlePane.closeButton");
75
76     add(menuButton);
77     add(iconButton);
78     add(maxButton);
79     add(closeButton);
80     }
81
82     protected void installListeners() {
83         super.installListeners();
84         frame.addPropertyChangeListener(this);
85     }
86
87     protected void uninstallListeners() {
88         frame.removePropertyChangeListener(this);
89         super.uninstallListeners();
90     }
91
92     private void updateStyle(JComponent c) {
93         SynthContext JavaDoc context = getContext(this, ENABLED);
94         SynthStyle JavaDoc oldStyle = style;
95         style = SynthLookAndFeel.updateStyle(context, this);
96         if (style != oldStyle) {
97             maxIcon =
98                 style.getIcon(context,"InternalFrameTitlePane.maximizeIcon");
99             minIcon =
100                 style.getIcon(context,"InternalFrameTitlePane.minimizeIcon");
101             iconIcon =
102                 style.getIcon(context,"InternalFrameTitlePane.iconifyIcon");
103             closeIcon =
104                 style.getIcon(context,"InternalFrameTitlePane.closeIcon");
105         }
106         context.dispose();
107     }
108
109     protected void installDefaults() {
110         super.installDefaults();
111         updateStyle(this);
112     }
113
114     protected void uninstallDefaults() {
115         SynthContext JavaDoc context = getContext(this, ENABLED);
116         style.uninstallDefaults(context);
117         context.dispose();
118         style = null;
119         JInternalFrame.JDesktopIcon di = frame.getDesktopIcon();
120         if(di != null && di.getComponentPopupMenu() == systemPopupMenu) {
121             // Release link to systemMenu from the JInternalFrame
122
di.setComponentPopupMenu(null);
123         }
124         super.uninstallDefaults();
125     }
126
127     private static class JPopupMenuUIResource extends JPopupMenu implements
128         UIResource { }
129
130     protected void assembleSystemMenu() {
131         systemPopupMenu = new JPopupMenuUIResource();
132         addSystemMenuItems(systemPopupMenu);
133     enableActions();
134         menuButton = createNoFocusButton();
135     menuButton.setIcon(frame.getFrameIcon());
136         menuButton.addMouseListener(new MouseAdapter() {
137             public void mousePressed(MouseEvent e) {
138                 showSystemMenu();
139             }
140         });
141     JPopupMenu p = frame.getComponentPopupMenu();
142     if (p == null || p instanceof UIResource) {
143         frame.setComponentPopupMenu(systemPopupMenu);
144     }
145     if (frame.getDesktopIcon() != null) {
146         p = frame.getDesktopIcon().getComponentPopupMenu();
147         if (p == null || p instanceof UIResource) {
148         frame.getDesktopIcon().setComponentPopupMenu(systemPopupMenu);
149         }
150     }
151     setInheritsPopupMenu(true);
152     }
153
154     protected void addSystemMenuItems(JPopupMenu menu) {
155         // PENDING: this should all be localizable!
156
JMenuItem mi = (JMenuItem)menu.add(restoreAction);
157     mi.setMnemonic('R');
158     mi = (JMenuItem)menu.add(moveAction);
159     mi.setMnemonic('M');
160     mi = (JMenuItem)menu.add(sizeAction);
161     mi.setMnemonic('S');
162     mi = (JMenuItem)menu.add(iconifyAction);
163     mi.setMnemonic('n');
164     mi = (JMenuItem)menu.add(maximizeAction);
165     mi.setMnemonic('x');
166     menu.add(new JSeparator());
167     mi = (JMenuItem)menu.add(closeAction);
168     mi.setMnemonic('C');
169     }
170
171     protected void showSystemMenu() {
172         Dimension dim = new Dimension();
173         Insets insets = frame.getInsets();
174         dim.width += insets.left + insets.right;
175         dim.height += insets.bottom + insets.top;
176         if (!frame.isIcon()) {
177             systemPopupMenu.show(menuButton,
178                 getX() - dim.width,
179                 getY() + getHeight() - dim.height);
180         } else {
181             systemPopupMenu.show(menuButton,
182                 getX() - dim.width,
183                 getY() - systemPopupMenu.getPreferredSize().height -
184                     dim.height);
185         }
186     }
187
188     // SynthInternalFrameTitlePane has no UI, we'll invoke paint on it.
189
public void paintComponent(Graphics g) {
190         SynthContext JavaDoc context = getContext(this);
191         SynthLookAndFeel.update(context, g);
192         context.getPainter().paintInternalFrameTitlePaneBackground(context,
193                           g, 0, 0, getWidth(), getHeight());
194         paint(context, g);
195         context.dispose();
196     }
197
198     public void paint(Graphics g, JComponent c) {
199         SynthContext JavaDoc context = getContext(c);
200
201         paint(context, g);
202         context.dispose();
203     }
204
205     protected void paint(SynthContext JavaDoc context, Graphics g) {
206     }
207
208     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
209                             int y, int w, int h) {
210         context.getPainter().paintInternalFrameTitlePaneBorder(context,
211                                                             g, x, y, w, h);
212     }
213
214     protected LayoutManager createLayout() {
215         SynthContext JavaDoc context = getContext(this);
216     LayoutManager lm =
217         (LayoutManager)style.get(context, "InternalFrameTitlePane.titlePaneLayout");
218     context.dispose();
219         return (lm != null) ? lm : new SynthTitlePaneLayout();
220     }
221
222     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
223         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
224             updateStyle(SynthInternalFrameTitlePane.this);
225         }
226     }
227
228     class SynthTitlePaneLayout implements LayoutManager {
229         public void addLayoutComponent(String JavaDoc name, Component c) {}
230         public void removeLayoutComponent(Component c) {}
231         public Dimension preferredLayoutSize(Container c) {
232         return minimumLayoutSize(c);
233     }
234     
235         public Dimension minimumLayoutSize(Container c) {
236             // Calculate width.
237
int width = 22;
238  
239             if (frame.isClosable()) {
240                 width += 19;
241             }
242             if (frame.isMaximizable()) {
243                 width += 19;
244             }
245             if (frame.isIconifiable()) {
246                 width += 19;
247             }
248
249             FontMetrics fm = frame.getFontMetrics(getFont());
250             SynthContext JavaDoc context = getContext(frame);
251             SynthGraphicsUtils JavaDoc graphicsUtils = context.getStyle().
252                                        getGraphicsUtils(context);
253             String JavaDoc frameTitle = frame.getTitle();
254             int title_w = frameTitle != null ? graphicsUtils.
255                                computeStringWidth(context, fm.getFont(),
256                                fm, frameTitle) : 0;
257             int title_length = frameTitle != null ? frameTitle.length() : 0;
258
259             // Leave room for three characters in the title.
260
if (title_length > 3) {
261                 int subtitle_w = graphicsUtils.computeStringWidth(context,
262                     fm.getFont(), fm, frameTitle.substring(0, 3) + "...");
263                 width += (title_w < subtitle_w) ? title_w : subtitle_w;
264             } else {
265                 width += title_w;
266             }
267
268             // Calculate height.
269
Icon icon = frame.getFrameIcon();
270             int fontHeight = fm.getHeight();
271             fontHeight += 2;
272             int iconHeight = 0;
273             if (icon != null) {
274                 // SystemMenuBar forces the icon to be 16x16 or less.
275
iconHeight = Math.min(icon.getIconHeight(), 16);
276             }
277             iconHeight += 2;
278       
279             int height = Math.max( fontHeight, iconHeight );
280
281             Dimension dim = new Dimension(width, height);
282
283             // Take into account the border insets if any.
284
if (getBorder() != null) {
285                 Insets insets = getBorder().getBorderInsets(c);
286                 dim.height += insets.top + insets.bottom;
287                 dim.width += insets.left + insets.right;
288             }
289             context.dispose();
290             return dim;
291     }
292     
293         public void layoutContainer(Container c) {
294             boolean leftToRight = SynthLookAndFeel.isLeftToRight(frame);
295
296             int w = getWidth();
297             int h = getHeight();
298             int x;
299
300             Icon closeIcon = closeButton.getIcon();
301             int buttonHeight = (closeIcon != null) ? closeIcon.getIconHeight(): 12;
302         if (buttonHeight == 0) {
303         buttonHeight = 12;
304         }
305             //int buttonWidth = closeButton.getIcon().getIconWidth();
306

307             Icon icon = frame.getFrameIcon();
308             int iconHeight = (icon != null) ? icon.getIconHeight() : buttonHeight;
309
310         Insets insets = frame.getInsets();
311
312             x = (leftToRight) ? insets.left : w - 16 - insets.right;
313             menuButton.setBounds(x, (h - iconHeight) / 2, 16, 14);
314
315             x = (leftToRight) ? w - 16 - insets.right : insets.left;
316
317             if (frame.isClosable()) {
318                 closeButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
319                 x += (leftToRight) ? -(16 + 2) : 16 + 2;
320             }
321
322             if (frame.isMaximizable()) {
323                 maxButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
324                 x += (leftToRight) ? -(16 + 2) : 16 + 2;
325             }
326
327             if (frame.isIconifiable()) {
328                 iconButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
329             }
330         }
331     }
332
333     private JButton createNoFocusButton() {
334         JButton button = new JButton();
335         button.setFocusable(false);
336         button.setMargin(new Insets(0,0,0,0));
337         return button;
338     }
339 }
340
Popular Tags