KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalInternalFrameTitlePane


1 /*
2  * @(#)MetalInternalFrameTitlePane.java 1.57 04/03/17
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.metal;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.*;
14 import javax.swing.border.*;
15 import javax.swing.event.InternalFrameEvent JavaDoc;
16 import java.util.EventListener JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.beans.PropertyChangeEvent JavaDoc;
19 import javax.swing.plaf.basic.BasicInternalFrameTitlePane JavaDoc;
20
21
22 /**
23  * Class that manages a JLF title bar
24  * @version 1.57 03/17/04
25  * @author Steve Wilson
26  * @author Brian Beck
27  * @since 1.3
28  */

29
30 public class MetalInternalFrameTitlePane extends BasicInternalFrameTitlePane JavaDoc {
31
32     protected boolean isPalette = false;
33     protected Icon paletteCloseIcon;
34     protected int paletteTitleHeight;
35
36     private static final Border handyEmptyBorder = new EmptyBorder(0,0,0,0);
37
38     /**
39      * Key used to lookup Color from UIManager. If this is null,
40      * <code>getWindowTitleBackground</code> is used.
41      */

42     private String JavaDoc selectedBackgroundKey;
43     /**
44      * Key used to lookup Color from UIManager. If this is null,
45      * <code>getWindowTitleForeground</code> is used.
46      */

47     private String JavaDoc selectedForegroundKey;
48     /**
49      * Key used to lookup shadow color from UIManager. If this is null,
50      * <code>getPrimaryControlDarkShadow</code> is used.
51      */

52     private String JavaDoc selectedShadowKey;
53     /**
54      * Boolean indicating the state of the <code>JInternalFrame</code>s
55      * closable property at <code>updateUI</code> time.
56      */

57     private boolean wasClosable;
58
59     int buttonsWidth = 0;
60     
61     MetalBumps JavaDoc activeBumps
62         = new MetalBumps JavaDoc( 0, 0,
63                           MetalLookAndFeel.getPrimaryControlHighlight(),
64                           MetalLookAndFeel.getPrimaryControlDarkShadow(),
65           (UIManager.get("InternalFrame.activeTitleGradient") != null) ? null :
66                           MetalLookAndFeel.getPrimaryControl() );
67     MetalBumps JavaDoc inactiveBumps
68         = new MetalBumps JavaDoc( 0, 0,
69                           MetalLookAndFeel.getControlHighlight(),
70                           MetalLookAndFeel.getControlDarkShadow(),
71         (UIManager.get("InternalFrame.inactiveTitleGradient") != null) ? null :
72                           MetalLookAndFeel.getControl() );
73     MetalBumps JavaDoc paletteBumps;
74
75     private Color activeBumpsHighlight = MetalLookAndFeel.
76                              getPrimaryControlHighlight();
77     private Color activeBumpsShadow = MetalLookAndFeel.
78                              getPrimaryControlDarkShadow();
79                         
80     public MetalInternalFrameTitlePane(JInternalFrame f) {
81         super( f );
82     }
83
84     public void addNotify() {
85         super.addNotify();
86         // This is done here instead of in installDefaults as I was worried
87
// that the BasicInternalFrameUI might not be fully initialized, and
88
// that if this resets the closable state the BasicInternalFrameUI
89
// Listeners that get notified might be in an odd/uninitialized state.
90
updateOptionPaneState();
91     }
92
93     protected void installDefaults() {
94         super.installDefaults();
95         setFont( UIManager.getFont("InternalFrame.titleFont") );
96         paletteTitleHeight
97             = UIManager.getInt("InternalFrame.paletteTitleHeight");
98         paletteCloseIcon = UIManager.getIcon("InternalFrame.paletteCloseIcon");
99         wasClosable = frame.isClosable();
100         selectedForegroundKey = selectedBackgroundKey = null;
101         if (MetalLookAndFeel.usingOcean()) {
102             setOpaque(true);
103         }
104     }
105     
106     protected void uninstallDefaults() {
107         super.uninstallDefaults();
108         if (wasClosable != frame.isClosable()) {
109             frame.setClosable(wasClosable);
110         }
111     }
112
113     protected void createButtons() {
114         super.createButtons();
115
116         Boolean JavaDoc paintActive = frame.isSelected() ? Boolean.TRUE:Boolean.FALSE;
117         iconButton.putClientProperty("paintActive", paintActive);
118         iconButton.setBorder(handyEmptyBorder);
119     
120         maxButton.putClientProperty("paintActive", paintActive);
121         maxButton.setBorder(handyEmptyBorder);
122         
123         closeButton.putClientProperty("paintActive", paintActive);
124         closeButton.setBorder(handyEmptyBorder);
125
126         // The palette close icon isn't opaque while the regular close icon is.
127
// This makes sure palette close buttons have the right background.
128
closeButton.setBackground(MetalLookAndFeel.getPrimaryControlShadow());
129
130         if (MetalLookAndFeel.usingOcean()) {
131             iconButton.setContentAreaFilled(false);
132             maxButton.setContentAreaFilled(false);
133             closeButton.setContentAreaFilled(false);
134         }
135     }
136
137     /**
138      * Override the parent's method to do nothing. Metal frames do not
139      * have system menus.
140      */

141     protected void assembleSystemMenu() {}
142
143     /**
144      * Override the parent's method to do nothing. Metal frames do not
145      * have system menus.
146      */

147     protected void addSystemMenuItems(JMenu systemMenu) {}
148
149     /**
150      * Override the parent's method to do nothing. Metal frames do not
151      * have system menus.
152      */

153     protected void showSystemMenu() {}
154
155     /**
156      * Override the parent's method avoid creating a menu bar. Metal frames
157      * do not have system menus.
158      */

159     protected void addSubComponents() {
160         add(iconButton);
161         add(maxButton);
162         add(closeButton);
163     }
164
165     protected PropertyChangeListener JavaDoc createPropertyChangeListener() {
166         return new MetalPropertyChangeHandler();
167     }
168     
169     protected LayoutManager createLayout() {
170         return new MetalTitlePaneLayout();
171     }
172
173     class MetalPropertyChangeHandler
174         extends BasicInternalFrameTitlePane.PropertyChangeHandler JavaDoc
175     {
176         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
177         String JavaDoc prop = (String JavaDoc)evt.getPropertyName();
178             if( prop.equals(JInternalFrame.IS_SELECTED_PROPERTY) ) {
179                 Boolean JavaDoc b = (Boolean JavaDoc)evt.getNewValue();
180                 iconButton.putClientProperty("paintActive", b);
181                 closeButton.putClientProperty("paintActive", b);
182                 maxButton.putClientProperty("paintActive", b);
183             }
184             else if ("JInternalFrame.messageType".equals(prop)) {
185                 updateOptionPaneState();
186                 frame.repaint();
187             }
188             super.propertyChange(evt);
189         }
190     }
191
192     class MetalTitlePaneLayout extends TitlePaneLayout {
193         public void addLayoutComponent(String JavaDoc name, Component c) {}
194         public void removeLayoutComponent(Component c) {}
195         public Dimension preferredLayoutSize(Container c) {
196             return minimumLayoutSize(c);
197         }
198
199         public Dimension minimumLayoutSize(Container c) {
200             // Compute width.
201
int width = 30;
202             if (frame.isClosable()) {
203                 width += 21;
204             }
205             if (frame.isMaximizable()) {
206                 width += 16 + (frame.isClosable() ? 10 : 4);
207             }
208             if (frame.isIconifiable()) {
209                 width += 16 + (frame.isMaximizable() ? 2 :
210                     (frame.isClosable() ? 10 : 4));
211             }
212             FontMetrics fm = frame.getFontMetrics(getFont());
213             String JavaDoc frameTitle = frame.getTitle();
214             int title_w = frameTitle != null ? SwingUtilities2.stringWidth(
215                                frame, fm, frameTitle) : 0;
216             int title_length = frameTitle != null ? frameTitle.length() : 0;
217
218             if (title_length > 2) {
219                 int subtitle_w = SwingUtilities2.stringWidth(frame, fm,
220                                      frame.getTitle().substring(0, 2) + "...");
221                 width += (title_w < subtitle_w) ? title_w : subtitle_w;
222             }
223             else {
224                 width += title_w;
225             }
226
227             // Compute height.
228
int height = 0;
229             if (isPalette) {
230                 height = paletteTitleHeight;
231             } else {
232                 int fontHeight = fm.getHeight();
233                 fontHeight += 7;
234                 Icon icon = frame.getFrameIcon();
235                 int iconHeight = 0;
236                 if (icon != null) {
237                     // SystemMenuBar forces the icon to be 16x16 or less.
238
iconHeight = Math.min(icon.getIconHeight(), 16);
239                 }
240                 iconHeight += 5;
241                 height = Math.max(fontHeight, iconHeight);
242             }
243
244             return new Dimension(width, height);
245         }
246     
247         public void layoutContainer(Container c) {
248             boolean leftToRight = MetalUtils.isLeftToRight(frame);
249        
250             int w = getWidth();
251             int x = leftToRight ? w : 0;
252             int y = 2;
253             int spacing;
254       
255             // assumes all buttons have the same dimensions
256
// these dimensions include the borders
257
int buttonHeight = closeButton.getIcon().getIconHeight();
258             int buttonWidth = closeButton.getIcon().getIconWidth();
259
260             if(frame.isClosable()) {
261                 if (isPalette) {
262                     spacing = 3;
263                     x += leftToRight ? -spacing -(buttonWidth+2) : spacing;
264                     closeButton.setBounds(x, y, buttonWidth+2, getHeight()-4);
265                     if( !leftToRight ) x += (buttonWidth+2);
266                 } else {
267                     spacing = 4;
268                     x += leftToRight ? -spacing -buttonWidth : spacing;
269                     closeButton.setBounds(x, y, buttonWidth, buttonHeight);
270                     if( !leftToRight ) x += buttonWidth;
271                 }
272             }
273
274             if(frame.isMaximizable() && !isPalette ) {
275                 spacing = frame.isClosable() ? 10 : 4;
276                 x += leftToRight ? -spacing -buttonWidth : spacing;
277                 maxButton.setBounds(x, y, buttonWidth, buttonHeight);
278                 if( !leftToRight ) x += buttonWidth;
279             }
280         
281             if(frame.isIconifiable() && !isPalette ) {
282                 spacing = frame.isMaximizable() ? 2
283                           : (frame.isClosable() ? 10 : 4);
284                 x += leftToRight ? -spacing -buttonWidth : spacing;
285                 iconButton.setBounds(x, y, buttonWidth, buttonHeight);
286                 if( !leftToRight ) x += buttonWidth;
287             }
288         
289             buttonsWidth = leftToRight ? w - x : x;
290         }
291     }
292
293     public void paintPalette(Graphics g) {
294         boolean leftToRight = MetalUtils.isLeftToRight(frame);
295
296         int width = getWidth();
297         int height = getHeight();
298     
299         if (paletteBumps == null) {
300             paletteBumps
301                 = new MetalBumps JavaDoc(0, 0,
302                                  MetalLookAndFeel.getPrimaryControlHighlight(),
303                                  MetalLookAndFeel.getPrimaryControlInfo(),
304                                  MetalLookAndFeel.getPrimaryControlShadow() );
305         }
306
307         Color background = MetalLookAndFeel.getPrimaryControlShadow();
308         Color darkShadow = MetalLookAndFeel.getPrimaryControlDarkShadow();
309
310         g.setColor(background);
311         g.fillRect(0, 0, width, height);
312
313         g.setColor( darkShadow );
314         g.drawLine ( 0, height - 1, width, height -1);
315
316         int xOffset = leftToRight ? 4 : buttonsWidth + 4;
317         int bumpLength = width - buttonsWidth -2*4;
318         int bumpHeight = getHeight() - 4;
319         paletteBumps.setBumpArea( bumpLength, bumpHeight );
320         paletteBumps.paintIcon( this, g, xOffset, 2);
321     }
322
323     public void paintComponent(Graphics g) {
324         if(isPalette) {
325             paintPalette(g);
326             return;
327         }
328
329         boolean leftToRight = MetalUtils.isLeftToRight(frame);
330         boolean isSelected = frame.isSelected();
331
332         int width = getWidth();
333         int height = getHeight();
334     
335         Color background = null;
336         Color foreground = null;
337         Color shadow = null;
338
339         MetalBumps JavaDoc bumps;
340         String JavaDoc gradientKey;
341
342         if (isSelected) {
343             if (!MetalLookAndFeel.usingOcean()) {
344                 closeButton.setContentAreaFilled(true);
345                 maxButton.setContentAreaFilled(true);
346                 iconButton.setContentAreaFilled(true);
347             }
348             if (selectedBackgroundKey != null) {
349                 background = UIManager.getColor(selectedBackgroundKey);
350             }
351             if (background == null) {
352                 background = MetalLookAndFeel.getWindowTitleBackground();
353             }
354             if (selectedForegroundKey != null) {
355                 foreground = UIManager.getColor(selectedForegroundKey);
356             }
357             if (selectedShadowKey != null) {
358                 shadow = UIManager.getColor(selectedShadowKey);
359             }
360             if (shadow == null) {
361                 shadow = MetalLookAndFeel.getPrimaryControlDarkShadow();
362             }
363             if (foreground == null) {
364                 foreground = MetalLookAndFeel.getWindowTitleForeground();
365             }
366             activeBumps.setBumpColors(activeBumpsHighlight, activeBumpsShadow,
367                         UIManager.get("InternalFrame.activeTitleGradient") !=
368                                       null ? null : background);
369             bumps = activeBumps;
370             gradientKey = "InternalFrame.activeTitleGradient";
371         } else {
372             if (!MetalLookAndFeel.usingOcean()) {
373                 closeButton.setContentAreaFilled(false);
374                 maxButton.setContentAreaFilled(false);
375                 iconButton.setContentAreaFilled(false);
376             }
377             background = MetalLookAndFeel.getWindowTitleInactiveBackground();
378             foreground = MetalLookAndFeel.getWindowTitleInactiveForeground();
379             shadow = MetalLookAndFeel.getControlDarkShadow();
380             bumps = inactiveBumps;
381             gradientKey = "InternalFrame.inactiveTitleGradient";
382         }
383
384         if (!MetalUtils.drawGradient(this, g, gradientKey, 0, 0, width,
385                                      height, true)) {
386             g.setColor(background);
387             g.fillRect(0, 0, width, height);
388         }
389
390         g.setColor( shadow );
391         g.drawLine ( 0, height - 1, width, height -1);
392         g.drawLine ( 0, 0, 0 ,0);
393         g.drawLine ( width - 1, 0 , width -1, 0);
394
395
396         int titleLength = 0;
397         int xOffset = leftToRight ? 5 : width - 5;
398         String JavaDoc frameTitle = frame.getTitle();
399
400         Icon icon = frame.getFrameIcon();
401         if ( icon != null ) {
402             if( !leftToRight )
403                 xOffset -= icon.getIconWidth();
404             int iconY = ((height / 2) - (icon.getIconHeight() /2));
405             icon.paintIcon(frame, g, xOffset, iconY);
406             xOffset += leftToRight ? icon.getIconWidth() + 5 : -5;
407         }
408
409         if(frameTitle != null) {
410             Font f = getFont();
411             g.setFont(f);
412             FontMetrics fm = SwingUtilities2.getFontMetrics(frame, g, f);
413             int fHeight = fm.getHeight();
414
415             g.setColor(foreground);
416
417             int yOffset = ( (height - fm.getHeight() ) / 2 ) + fm.getAscent();
418
419             Rectangle rect = new Rectangle(0, 0, 0, 0);
420             if (frame.isIconifiable()) { rect = iconButton.getBounds(); }
421             else if (frame.isMaximizable()) { rect = maxButton.getBounds(); }
422             else if (frame.isClosable()) { rect = closeButton.getBounds(); }
423             int titleW;
424
425             if( leftToRight ) {
426               if (rect.x == 0) {
427         rect.x = frame.getWidth()-frame.getInsets().right-2;
428           }
429               titleW = rect.x - xOffset - 4;
430               frameTitle = getTitle(frameTitle, fm, titleW);
431             } else {
432               titleW = xOffset - rect.x - rect.width - 4;
433               frameTitle = getTitle(frameTitle, fm, titleW);
434               xOffset -= SwingUtilities2.stringWidth(frame, fm, frameTitle);
435             }
436
437             titleLength = SwingUtilities2.stringWidth(frame, fm, frameTitle);
438             SwingUtilities2.drawString(frame, g, frameTitle, xOffset, yOffset);
439             xOffset += leftToRight ? titleLength + 5 : -5;
440         }
441   
442         int bumpXOffset;
443         int bumpLength;
444         if( leftToRight ) {
445             bumpLength = width - buttonsWidth - xOffset - 5;
446             bumpXOffset = xOffset;
447         } else {
448             bumpLength = xOffset - buttonsWidth - 5;
449             bumpXOffset = buttonsWidth + 5;
450         }
451         int bumpYOffset = 3;
452         int bumpHeight = getHeight() - (2 * bumpYOffset);
453         bumps.setBumpArea( bumpLength, bumpHeight );
454         bumps.paintIcon(this, g, bumpXOffset, bumpYOffset);
455     }
456                                             
457     public void setPalette(boolean b) {
458         isPalette = b;
459
460     if (isPalette) {
461             closeButton.setIcon(paletteCloseIcon);
462          if( frame.isMaximizable() )
463                 remove(maxButton);
464             if( frame.isIconifiable() )
465                 remove(iconButton);
466         } else {
467         closeButton.setIcon(closeIcon);
468             if( frame.isMaximizable() )
469                 add(maxButton);
470             if( frame.isIconifiable() )
471                 add(iconButton);
472     }
473     revalidate();
474     repaint();
475     }
476
477     /**
478      * Updates any state dependant upon the JInternalFrame being shown in
479      * a <code>JOptionPane</code>.
480      */

481     private void updateOptionPaneState() {
482         int type = -2;
483         boolean closable = wasClosable;
484         Object JavaDoc obj = frame.getClientProperty("JInternalFrame.messageType");
485
486         if (obj == null) {
487             // Don't change the closable state unless in an JOptionPane.
488
return;
489         }
490         if (obj instanceof Integer JavaDoc) {
491             type = ((Integer JavaDoc) obj).intValue();
492         }
493         switch (type) {
494         case JOptionPane.ERROR_MESSAGE:
495             selectedBackgroundKey =
496                               "OptionPane.errorDialog.titlePane.background";
497             selectedForegroundKey =
498                               "OptionPane.errorDialog.titlePane.foreground";
499             selectedShadowKey = "OptionPane.errorDialog.titlePane.shadow";
500             closable = false;
501             break;
502         case JOptionPane.QUESTION_MESSAGE:
503             selectedBackgroundKey =
504                             "OptionPane.questionDialog.titlePane.background";
505             selectedForegroundKey =
506                     "OptionPane.questionDialog.titlePane.foreground";
507             selectedShadowKey =
508                           "OptionPane.questionDialog.titlePane.shadow";
509             closable = false;
510             break;
511         case JOptionPane.WARNING_MESSAGE:
512             selectedBackgroundKey =
513                               "OptionPane.warningDialog.titlePane.background";
514             selectedForegroundKey =
515                               "OptionPane.warningDialog.titlePane.foreground";
516             selectedShadowKey = "OptionPane.warningDialog.titlePane.shadow";
517             closable = false;
518             break;
519         case JOptionPane.INFORMATION_MESSAGE:
520         case JOptionPane.PLAIN_MESSAGE:
521             selectedBackgroundKey = selectedForegroundKey = selectedShadowKey =
522                                     null;
523             closable = false;
524             break;
525         default:
526             selectedBackgroundKey = selectedForegroundKey = selectedShadowKey =
527                                     null;
528             break;
529         }
530         if (closable != frame.isClosable()) {
531             frame.setClosable(closable);
532         }
533     }
534 }
535
Popular Tags