KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SynthSplitPaneUI.java 1.15 03/12/19
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
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.beans.*;
14 import java.util.*;
15 import javax.swing.*;
16 import javax.swing.event.*;
17 import javax.swing.plaf.*;
18 import javax.swing.plaf.basic.*;
19 import sun.swing.plaf.synth.SynthUI;
20
21
22 /**
23  * Synth's SplitPaneUI.
24  *
25  * @version 1.15, 12/19/03
26  * @author Scott Violet
27  */

28 class SynthSplitPaneUI extends BasicSplitPaneUI implements
29                                     PropertyChangeListener, SynthUI {
30     /**
31      * Keys to use for forward focus traversal when the JComponent is
32      * managing focus.
33      */

34     private static Set managingFocusForwardTraversalKeys;
35
36     /**
37      * Keys to use for backward focus traversal when the JComponent is
38      * managing focus.
39      */

40     private static Set managingFocusBackwardTraversalKeys;
41
42     /**
43      * Style for the JSplitPane.
44      */

45     private SynthStyle JavaDoc style;
46     /**
47      * Style for the divider.
48      */

49     private SynthStyle JavaDoc dividerStyle;
50
51
52     /**
53      * Creates a new SynthSplitPaneUI instance
54      */

55     public static ComponentUI createUI(JComponent x) {
56         return new SynthSplitPaneUI JavaDoc();
57     }
58
59     /**
60      * Installs the UI defaults.
61      */

62     protected void installDefaults() {
63         updateStyle(splitPane);
64
65         setOrientation(splitPane.getOrientation());
66         setContinuousLayout(splitPane.isContinuousLayout());
67
68         resetLayoutManager();
69
70         /* Install the nonContinuousLayoutDivider here to avoid having to
71         add/remove everything later. */

72         if(nonContinuousLayoutDivider == null) {
73             setNonContinuousLayoutDivider(
74                                 createDefaultNonContinuousLayoutDivider(),
75                                 true);
76         } else {
77             setNonContinuousLayoutDivider(nonContinuousLayoutDivider, true);
78         }
79
80     // focus forward traversal key
81
if (managingFocusForwardTraversalKeys==null) {
82         managingFocusForwardTraversalKeys = new TreeSet();
83         managingFocusForwardTraversalKeys.add(
84         KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
85     }
86     splitPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
87                     managingFocusForwardTraversalKeys);
88     // focus backward traversal key
89
if (managingFocusBackwardTraversalKeys==null) {
90         managingFocusBackwardTraversalKeys = new TreeSet();
91         managingFocusBackwardTraversalKeys.add(
92         KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
93     }
94     splitPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
95                     managingFocusBackwardTraversalKeys);
96     }
97
98     private void updateStyle(JSplitPane splitPane) {
99         SynthContext JavaDoc context = getContext(splitPane, Region.SPLIT_PANE_DIVIDER,
100                                           ENABLED);
101         SynthStyle JavaDoc oldDividerStyle = dividerStyle;
102         dividerStyle = SynthLookAndFeel.updateStyle(context, this);
103         context.dispose();
104
105         context = getContext(splitPane, ENABLED);
106         SynthStyle JavaDoc oldStyle = style;
107
108         style = SynthLookAndFeel.updateStyle(context, this);
109
110         if (style != oldStyle) {
111             Object JavaDoc value = style.get(context, "SplitPane.size");
112             if (value == null) {
113                 value = new Integer JavaDoc(6);
114             }
115             LookAndFeel.installProperty(splitPane, "dividerSize", value);
116
117             value = style.get(context, "SplitPane.oneTouchExpandable");
118             if (value != null) {
119                 LookAndFeel.installProperty(splitPane, "oneTouchExpandable", value);
120             }
121
122             if (divider != null) {
123                 splitPane.remove(divider);
124                 divider.setDividerSize(splitPane.getDividerSize());
125             }
126             if (oldStyle != null) {
127                 uninstallKeyboardActions();
128                 installKeyboardActions();
129             }
130         }
131         if (style != oldStyle || dividerStyle != oldDividerStyle) {
132             // Only way to force BasicSplitPaneDivider to reread the
133
// necessary properties.
134
if (divider != null) {
135                 splitPane.remove(divider);
136             }
137             divider = createDefaultDivider();
138             divider.setBasicSplitPaneUI(this);
139             splitPane.add(divider, JSplitPane.DIVIDER);
140         }
141         context.dispose();
142     }
143
144     /**
145      * Installs the event listeners for the UI.
146      */

147     protected void installListeners() {
148         super.installListeners();
149         splitPane.addPropertyChangeListener(this);
150     }
151
152     /**
153      * Uninstalls the UI defaults.
154      */

155     protected void uninstallDefaults() {
156         SynthContext JavaDoc context = getContext(splitPane, ENABLED);
157
158         style.uninstallDefaults(context);
159         context.dispose();
160         style = null;
161
162         context = getContext(splitPane, Region.SPLIT_PANE_DIVIDER, ENABLED);
163         dividerStyle.uninstallDefaults(context);
164         context.dispose();
165         dividerStyle = null;
166
167         super.uninstallDefaults();
168     }
169
170
171     /**
172      * Uninstalls the event listeners for the UI.
173      */

174     protected void uninstallListeners() {
175         super.uninstallListeners();
176         splitPane.removePropertyChangeListener(this);
177     }
178
179
180     public SynthContext JavaDoc getContext(JComponent c) {
181         return getContext(c, getComponentState(c));
182     }
183
184     private SynthContext JavaDoc getContext(JComponent c, int state) {
185         return SynthContext.getContext(SynthContext JavaDoc.class, c,
186                     SynthLookAndFeel.getRegion(c), style, state);
187     }
188
189     private Region JavaDoc getRegion(JComponent c) {
190         return SynthLookAndFeel.getRegion(c);
191     }
192
193     private int getComponentState(JComponent c) {
194         return SynthLookAndFeel.getComponentState(c);
195     }
196
197     SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region) {
198         return getContext(c, region, getComponentState(c, region));
199     }
200
201     private SynthContext JavaDoc getContext(JComponent c, Region JavaDoc region, int state) {
202         if (region == Region.SPLIT_PANE_DIVIDER) {
203             return SynthContext.getContext(SynthContext JavaDoc.class, c, region,
204                                            dividerStyle, state);
205         }
206         return SynthContext.getContext(SynthContext JavaDoc.class, c, region,
207                                        style, state);
208     }
209
210     private int getComponentState(JComponent c, Region JavaDoc subregion) {
211         int state = SynthLookAndFeel.getComponentState(c);
212
213         if (divider.isMouseOver()) {
214             state |= MOUSE_OVER;
215         }
216         return state;
217     }
218
219
220     public void propertyChange(PropertyChangeEvent e) {
221         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
222             updateStyle((JSplitPane)e.getSource());
223         }
224     }
225
226     /**
227      * Creates the default divider.
228      */

229     public BasicSplitPaneDivider createDefaultDivider() {
230         SynthSplitPaneDivider JavaDoc divider = new SynthSplitPaneDivider JavaDoc(this);
231
232         divider.setDividerSize(splitPane.getDividerSize());
233         return divider;
234     }
235
236     protected Component createDefaultNonContinuousLayoutDivider() {
237         return new Canvas() {
238             public void paint(Graphics g) {
239                 paintDragDivider(g, 0, 0, getWidth(), getHeight());
240             }
241         };
242     }
243
244     public void update(Graphics g, JComponent c) {
245         SynthContext JavaDoc context = getContext(c);
246
247         SynthLookAndFeel.update(context, g);
248         context.getPainter().paintSplitPaneBackground(context,
249                           g, 0, 0, c.getWidth(), c.getHeight());
250         paint(context, g);
251         context.dispose();
252     }
253
254     public void paint(Graphics g, JComponent c) {
255         SynthContext JavaDoc context = getContext(c);
256
257         paint(context, g);
258         context.dispose();
259     }
260
261     protected void paint(SynthContext JavaDoc context, Graphics g) {
262         // This is done to update package private variables in
263
// BasicSplitPaneUI
264
super.paint(g, splitPane);
265     }
266
267
268     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
269                             int y, int w, int h) {
270         context.getPainter().paintSplitPaneBorder(context, g, x, y, w, h);
271     }
272
273     private void paintDragDivider(Graphics g, int x, int y, int w, int h) {
274         SynthContext JavaDoc context = getContext(splitPane,Region.SPLIT_PANE_DIVIDER);
275         context.setComponentState(((context.getComponentState() | MOUSE_OVER) ^
276                                    MOUSE_OVER) | PRESSED);
277         Shape oldClip = g.getClip();
278         g.clipRect(x, y, w, h);
279         context.getPainter().paintSplitPaneDragDivider(context, g, x, y, w, h,
280                                            splitPane.getOrientation());
281         g.setClip(oldClip);
282         context.dispose();
283     }
284
285     public void finishedPaintingChildren(JSplitPane jc, Graphics g) {
286         if(jc == splitPane && getLastDragLocation() != -1 &&
287                               !isContinuousLayout() && !draggingHW) {
288             if(jc.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
289                 paintDragDivider(g, getLastDragLocation(), 0, dividerSize - 1,
290                                  splitPane.getHeight() - 1);
291             } else {
292                 paintDragDivider(g, 0, getLastDragLocation(),
293                                  splitPane.getWidth() - 1, dividerSize - 1);
294             }
295         }
296     }
297 }
298
Popular Tags