KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsTabbedPaneUI


1 /*
2  * @(#)WindowsTabbedPaneUI.java 1.20 06/12/19
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.*;
11
12 import javax.swing.plaf.basic.*;
13 import javax.swing.plaf.*;
14 import javax.swing.*;
15 import java.util.Set JavaDoc;
16 import java.util.TreeSet JavaDoc;
17 import java.awt.event.*;
18
19 import com.sun.java.swing.plaf.windows.TMSchema.*;
20 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
21
22
23 /**
24  * Windows rendition of the component.
25  * <p>
26  * <strong>Warning:</strong>
27  * Serialized objects of this class will not be compatible with
28  * future Swing releases. The current serialization support is appropriate
29  * for short term storage or RMI between applications running the same
30  * version of Swing. A future release of Swing will provide support for
31  * long term persistence.
32  */

33 public class WindowsTabbedPaneUI extends BasicTabbedPaneUI {
34     /**
35      * Keys to use for forward focus traversal when the JComponent is
36      * managing focus.
37      */

38     private static Set JavaDoc managingFocusForwardTraversalKeys;
39   
40     /**
41      * Keys to use for backward focus traversal when the JComponent is
42      * managing focus.
43      */

44     private static Set JavaDoc managingFocusBackwardTraversalKeys;
45
46     private boolean contentOpaque = true;
47
48     protected void installDefaults() {
49         super.installDefaults();
50     contentOpaque = UIManager.getBoolean("TabbedPane.contentOpaque");
51
52         // focus forward traversal key
53
if (managingFocusForwardTraversalKeys==null) {
54             managingFocusForwardTraversalKeys = new TreeSet JavaDoc();
55             managingFocusForwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
56         }
57         tabPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, managingFocusForwardTraversalKeys);
58         // focus backward traversal key
59
if (managingFocusBackwardTraversalKeys==null) {
60             managingFocusBackwardTraversalKeys = new TreeSet JavaDoc();
61             managingFocusBackwardTraversalKeys.add( KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
62         }
63         tabPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, managingFocusBackwardTraversalKeys);
64     }
65
66     protected void uninstallDefaults() {
67         // sets the focus forward and backward traversal keys to null
68
// to restore the defaults
69
tabPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
70         tabPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
71         super.uninstallDefaults();
72     }
73
74     public static ComponentUI createUI(JComponent c) {
75         return new WindowsTabbedPaneUI();
76     }
77
78     protected void setRolloverTab(int index) {
79     // Rollover is only supported on XP
80
if (XPStyle.getXP() != null) {
81         int oldRolloverTab = getRolloverTab();
82         super.setRolloverTab(index);
83         Rectangle r1 = null;
84         Rectangle r2 = null;
85         if ( (oldRolloverTab >= 0) &&
86                  (oldRolloverTab < tabPane.getTabCount()) ) {
87         r1 = getTabBounds(tabPane, oldRolloverTab);
88         }
89         if (index >= 0) {
90         r2 = getTabBounds(tabPane, index);
91         }
92         if (r1 != null) {
93         if (r2 != null) {
94             tabPane.repaint(r1.union(r2));
95         } else {
96             tabPane.repaint(r1);
97         }
98         } else if (r2 != null) {
99         tabPane.repaint(r2);
100         }
101     }
102     }
103
104     protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
105     XPStyle xp = XPStyle.getXP();
106     if (xp != null && (contentOpaque || tabPane.isOpaque())) {
107             Skin skin = xp.getSkin(tabPane, Part.TABP_PANE);
108         if (skin != null) {
109         Insets insets = tabPane.getInsets();
110         // Note: don't call getTabAreaInsets(), because it causes rotation.
111
// Make sure "TabbedPane.tabsOverlapBorder" is set to true in WindowsLookAndFeel
112
Insets tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");
113         int x = insets.left;
114         int y = insets.top;
115         int w = tabPane.getWidth() - insets.right - insets.left;
116         int h = tabPane.getHeight() - insets.top - insets.bottom;
117
118         // Expand area by tabAreaInsets.bottom to allow tabs to overlap onto the border.
119
if (tabPlacement == LEFT || tabPlacement == RIGHT) {
120             int tabWidth = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
121             if (tabPlacement == LEFT) {
122             x += (tabWidth - tabAreaInsets.bottom);
123             }
124             w -= (tabWidth - tabAreaInsets.bottom);
125         } else {
126             int tabHeight = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
127             if (tabPlacement == TOP) {
128             y += (tabHeight - tabAreaInsets.bottom);
129             }
130             h -= (tabHeight - tabAreaInsets.bottom);
131         }
132
133                 paintRotatedSkin(g, skin, tabPlacement, x, y, w, h, null);
134         return;
135         }
136     }
137     super.paintContentBorder(g, tabPlacement, selectedIndex);
138     }
139
140     protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex,
141                                       int x, int y, int w, int h, boolean isSelected ) {
142     if (XPStyle.getXP() == null) {
143         super.paintTabBackground(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
144     }
145     }
146
147     protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
148                                   int x, int y, int w, int h, boolean isSelected ) {
149     XPStyle xp = XPStyle.getXP();
150     if (xp != null) {
151             Part part;
152
153         int tabCount = tabPane.getTabCount();
154         int tabRun = getRunForTab(tabCount, tabIndex);
155         if (tabRuns[tabRun] == tabIndex) {
156                 part = Part.TABP_TABITEMLEFTEDGE;
157         } else if (tabCount > 1 && lastTabInRun(tabCount, tabRun) == tabIndex) {
158                 part = Part.TABP_TABITEMRIGHTEDGE;
159         if (isSelected) {
160             // Align with right edge
161
if (tabPlacement == TOP || tabPlacement == BOTTOM) {
162             w++;
163             } else {
164             h++;
165             }
166         }
167         } else {
168                 part = Part.TABP_TABITEM;
169         }
170
171             State state = State.NORMAL;
172         if (isSelected) {
173                 state = State.SELECTED;
174         } else if (tabIndex == getRolloverTab()) {
175                 state = State.HOT;
176         }
177
178             paintRotatedSkin(g, xp.getSkin(tabPane, part), tabPlacement, x, y, w, h, state);
179     } else {
180         super.paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
181     }
182     }
183
184     private void paintRotatedSkin(Graphics g, Skin skin, int tabPlacement,
185                                   int x, int y, int w, int h, State state) {
186     Graphics2D g2d = (Graphics2D)g.create();
187     g2d.translate(x, y);
188     switch (tabPlacement) {
189        case RIGHT: g2d.translate(w, 0);
190             g2d.rotate(Math.toRadians(90.0));
191                         skin.paintSkin(g2d, 0, 0, h, w, state);
192             break;
193
194        case LEFT: g2d.scale(-1.0, 1.0);
195             g2d.rotate(Math.toRadians(90.0));
196                         skin.paintSkin(g2d, 0, 0, h, w, state);
197             break;
198
199        case BOTTOM: g2d.translate(0, h);
200             g2d.scale(-1.0, 1.0);
201             g2d.rotate(Math.toRadians(180.0));
202                         skin.paintSkin(g2d, 0, 0, w, h, state);
203             break;
204
205        case TOP:
206            default: skin.paintSkin(g2d, 0, 0, w, h, state);
207     }
208     g2d.dispose();
209     }
210 }
211
212
Popular Tags