KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MetalScrollPaneUI.java 1.20 04/09/08
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 javax.swing.*;
11 import javax.swing.event.*;
12 import javax.swing.border.*;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.*;
15
16 import java.awt.*;
17 import java.beans.*;
18 import java.awt.event.*;
19
20
21 /**
22  * A Metal L&F implementation of ScrollPaneUI.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.20 09/08/04
34  * @author Steve Wilson
35  */

36 public class MetalScrollPaneUI extends BasicScrollPaneUI
37 {
38
39     private PropertyChangeListener scrollBarSwapListener;
40
41     public static ComponentUI createUI(JComponent x) {
42     return new MetalScrollPaneUI JavaDoc();
43     }
44
45     public void installUI(JComponent c) {
46
47         super.installUI(c);
48
49     JScrollPane sp = (JScrollPane)c;
50         updateScrollbarsFreeStanding();
51     }
52
53     public void uninstallUI(JComponent c) {
54         super.uninstallUI(c);
55
56     JScrollPane sp = (JScrollPane)c;
57     JScrollBar hsb = sp.getHorizontalScrollBar();
58     JScrollBar vsb = sp.getVerticalScrollBar();
59         if (hsb != null) {
60             hsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null);
61         }
62         if (vsb != null) {
63             vsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null);
64         }
65     }
66
67
68     public void installListeners(JScrollPane scrollPane) {
69         super.installListeners(scrollPane);
70     scrollBarSwapListener = createScrollBarSwapListener();
71     scrollPane.addPropertyChangeListener(scrollBarSwapListener);
72     }
73
74
75     public void uninstallListeners(JScrollPane scrollPane) {
76         super.uninstallListeners(scrollPane);
77
78     scrollPane.removePropertyChangeListener(scrollBarSwapListener);
79     }
80
81     /**
82      * If the border of the scrollpane is an instance of
83      * <code>MetalBorders.ScrollPaneBorder</code>, the client property
84      * <code>FREE_STANDING_PROP</code> of the scrollbars
85      * is set to false, otherwise it is set to true.
86      */

87     private void updateScrollbarsFreeStanding() {
88         if (scrollpane == null) {
89             return;
90         }
91         Border border = scrollpane.getBorder();
92         Object JavaDoc value;
93
94         if (border instanceof MetalBorders.ScrollPaneBorder JavaDoc) {
95             value = Boolean.FALSE;
96         }
97         else {
98             value = Boolean.TRUE;
99         }
100         JScrollBar sb = scrollpane.getHorizontalScrollBar();
101         if (sb != null) {
102             sb.putClientProperty
103                    (MetalScrollBarUI.FREE_STANDING_PROP, value);
104         }
105         sb = scrollpane.getVerticalScrollBar();
106         if (sb != null) {
107             sb.putClientProperty
108                    (MetalScrollBarUI.FREE_STANDING_PROP, value);
109         }
110     }
111
112     protected PropertyChangeListener createScrollBarSwapListener() {
113         return new PropertyChangeListener() {
114         public void propertyChange(PropertyChangeEvent e) {
115           String JavaDoc propertyName = e.getPropertyName();
116           if (propertyName.equals("verticalScrollBar") ||
117               propertyName.equals("horizontalScrollBar")) {
118                       JScrollBar oldSB = (JScrollBar)e.getOldValue();
119                       if (oldSB != null) {
120                           oldSB.putClientProperty(
121                               MetalScrollBarUI.FREE_STANDING_PROP, null);
122                       }
123                       JScrollBar newSB = (JScrollBar)e.getNewValue();
124                       if (newSB != null) {
125                           newSB.putClientProperty(
126                               MetalScrollBarUI.FREE_STANDING_PROP,
127                               Boolean.FALSE);
128                       }
129           }
130                   else if ("border".equals(propertyName)) {
131                       updateScrollbarsFreeStanding();
132                   }
133     }};
134     }
135
136 }
137
Popular Tags