KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > WinClassicEditorTabDisplayerUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * WinClassicEditorTabDisplayerUI.java
21  *
22  * Created on 09 December 2003, 16:53
23  */

24
25 package org.netbeans.swing.tabcontrol.plaf;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Container JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.FontMetrics JavaDoc;
31 import java.awt.GradientPaint JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.Graphics2D JavaDoc;
34 import java.awt.Insets JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41 import org.netbeans.swing.tabcontrol.TabDisplayer;
42
43 import javax.swing.plaf.ComponentUI JavaDoc;
44
45 /**
46  * Windows classic impl of tabs ui
47  *
48  * @author Tim Boudreau
49  */

50 public final class WinClassicEditorTabDisplayerUI extends BasicScrollingTabDisplayerUI {
51     
52     private static final Rectangle JavaDoc scratch5 = new Rectangle JavaDoc();
53     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
54     
55     private static boolean isGenericUI = !"Windows".equals( //NOI18N
56
UIManager.getLookAndFeel().getID());
57
58     /**
59      * Creates a new instance of WinClassicEditorTabDisplayerUI
60      */

61     public WinClassicEditorTabDisplayerUI(TabDisplayer displayer) {
62         super (displayer);
63     }
64     
65     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
66         return new WinClassicEditorTabDisplayerUI ((TabDisplayer) c);
67     }
68
69     public Rectangle JavaDoc getTabRect(int idx, Rectangle JavaDoc rect) {
70         Rectangle JavaDoc r = super.getTabRect (idx, rect);
71         //For win classic, take up the full space, even the insets, to match
72
//earlier appearance
73
r.y = 0;
74         r.height = displayer.getHeight();
75         return r;
76     }
77
78     public void install() {
79         super.install();
80         if (!isGenericUI) {
81             displayer.setBackground( UIManager.getColor("tab_unsel_fill") );
82             displayer.setOpaque(true);
83         }
84     }
85
86     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
87         int prefHeight = 28;
88         Graphics JavaDoc g = BasicScrollingTabDisplayerUI.getOffscreenGraphics();
89         if (g != null) {
90             FontMetrics JavaDoc fm = g.getFontMetrics(displayer.getFont());
91             Insets JavaDoc ins = getTabAreaInsets();
92             prefHeight = fm.getHeight() + ins.top + ins.bottom + (isGenericUI ? 5 : 6);
93         }
94         return new Dimension JavaDoc(displayer.getWidth(), prefHeight);
95     }
96
97     private void genericPaintAfterTabs (Graphics JavaDoc g) {
98         g.setColor (UIManager.getColor("controlShadow")); //NOI18N
99
Insets JavaDoc ins = displayer.getInsets();
100         Rectangle JavaDoc r = new Rectangle JavaDoc();
101         getTabsVisibleArea(r);
102         r.width = displayer.getWidth();
103         int selEnd = 0;
104         int last = getLastVisibleTab();
105         if (last > -1) {
106             getTabRect (last, scratch5);
107             g.drawLine (scratch5.x + scratch5.width, displayer.getHeight() -1,
108                 displayer.getWidth() - (ins.left + ins.right) - 4,
109                 displayer.getHeight() - 1);
110             g.drawLine (0, displayer.getHeight() - 2, 2, displayer.getHeight() -2);
111             //TODO remove when a specific GTK l&f UI class is available
112
if ("GTK".equals(UIManager.getLookAndFeel().getID())) {
113                 boolean sel = last == displayer.getSelectionModel().getSelectedIndex();
114                 //paint a fading shadow to match the view tabs
115
int x = scratch5.x + scratch5.width;
116                 g.setColor (sel ? UIManager.getColor("controlShadow") :
117                     ColorUtil.adjustTowards(g.getColor(), 20,
118                     UIManager.getColor("control"))); //NOI18N
119
g.drawLine (x,
120                     scratch5.y + 5, x,
121                     scratch5.y + scratch5.height -2);
122                 g.setColor (ColorUtil.adjustTowards(g.getColor(), 20,
123                     UIManager.getColor("control"))); //NOI18N
124
g.drawLine (x + 1,
125                     scratch5.y + 6, x + 1,
126                     scratch5.y + scratch5.height -2);
127             }
128             if ((tabState.getState(getFirstVisibleTab()) & TabState.CLIP_LEFT)
129                 !=0 && getFirstVisibleTab() !=
130                 displayer.getSelectionModel().getSelectedIndex()) {
131                     //Draw a small gradient line continuing the left edge of
132
//the displayer up the left side of a left clipped tab
133
GradientPaint JavaDoc gp = ColorUtil.getGradientPaint(
134                     0, displayer.getHeight() / 2, UIManager.getColor("control"),
135                     0, displayer.getHeight(), UIManager.getColor("controlShadow"));
136                 ((Graphics2D JavaDoc) g).setPaint(gp);
137                 g.drawLine (0, displayer.getHeight() / 2, 0, displayer.getHeight());
138             } else {
139                 //Fill the small gap between the top of the content displayer
140
//and the bottom of the tabs, caused by the tab area bottom inset
141
g.setColor (UIManager.getColor("controlShadow"));
142                 g.drawLine (0, displayer.getHeight(), 0, displayer.getHeight() - 2);
143             }
144             if ((tabState.getState(getLastVisibleTab()) & TabState.CLIP_RIGHT) != 0
145                 && getLastVisibleTab() !=
146                 displayer.getSelectionModel().getSelectedIndex()) {
147                 GradientPaint JavaDoc gp = ColorUtil.getGradientPaint(
148                     0, displayer.getHeight() / 2, UIManager.getColor("control"),
149                     0, displayer.getHeight(), UIManager.getColor("controlShadow"));
150                 ((Graphics2D JavaDoc) g).setPaint(gp);
151                 getTabRect (getLastVisibleTab(), scratch5);
152                 g.drawLine (scratch5.x + scratch5.width, displayer.getHeight() / 2,
153                     scratch5.x + scratch5.width, displayer.getHeight());
154             }
155             
156         } else {
157             g.drawLine(r.x, displayer.getHeight() - ins.bottom, r.x + r.width - 4,
158                        displayer.getHeight() - ins.bottom);
159         }
160     }
161     
162     protected void paintAfterTabs(Graphics JavaDoc g) {
163         if (isGenericUI) {
164             genericPaintAfterTabs(g);
165             return;
166         }
167         Rectangle JavaDoc r = new Rectangle JavaDoc();
168         getTabsVisibleArea(r);
169         r.width = displayer.getWidth();
170         g.setColor(displayer.isActive() ?
171                    defaultRenderer.getSelectedActivatedBackground() :
172                    defaultRenderer.getSelectedBackground());
173
174         Insets JavaDoc ins = getTabAreaInsets();
175         ins.bottom++;
176         g.fillRect(r.x, r.y + r.height, r.x + r.width,
177                    displayer.getHeight() - (r.y + r.height));
178
179         g.setColor(UIManager.getColor("controlLtHighlight")); //NOI18N
180

181         int selEnd = 0;
182         int i = selectionModel.getSelectedIndex();
183         if (i != -1) {
184             getTabRect(i, scratch5);
185             if (scratch5.width != 0) {
186                 if (r.x < scratch5.x) {
187                     g.drawLine(r.x, displayer.getHeight() - ins.bottom,
188                                scratch5.x - 1,
189                                displayer.getHeight() - ins.bottom);
190                 }
191                 if (scratch5.x + scratch5.width < r.x + r.width) {
192                     selEnd = scratch5.x + scratch5.width;
193                     //If the last tab is not clipped, the final tab is one
194
//pixel smaller; we need to overwrite one pixel of the
195
//border or there will be a small stub sticking down
196
if (!scroll().isLastTabClipped()) {
197                         selEnd--;
198                     }
199                     g.drawLine(selEnd, displayer.getHeight() - ins.bottom,
200                                r.x + r.width,
201                                displayer.getHeight() - ins.bottom);
202                 }
203             }
204             return;
205         }
206
207         g.drawLine(r.x, displayer.getHeight() - ins.bottom, r.x + r.width,
208                    displayer.getHeight() - ins.bottom);
209     }
210
211     protected TabCellRenderer createDefaultRenderer() {
212         return new WinClassicEditorTabCellRenderer();
213     }
214
215     private static void initIcons() {
216         if( null == buttonIconPaths ) {
217             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
218             
219             //left button
220
String JavaDoc[] iconPaths = new String JavaDoc[4];
221             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_enabled.png"; // NOI18N
222
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_disabled.png"; // NOI18N
223
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_rollover.png"; // NOI18N
224
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_pressed.png"; // NOI18N
225
buttonIconPaths.put( TabControlButton.ID_SCROLL_LEFT_BUTTON, iconPaths );
226             
227             //right button
228
iconPaths = new String JavaDoc[4];
229             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_enabled.png"; // NOI18N
230
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_disabled.png"; // NOI18N
231
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_rollover.png"; // NOI18N
232
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_pressed.png"; // NOI18N
233
buttonIconPaths.put( TabControlButton.ID_SCROLL_RIGHT_BUTTON, iconPaths );
234             
235             //drop down button
236
iconPaths = new String JavaDoc[4];
237             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_popup_enabled.png"; // NOI18N
238
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_popup_disabled.png"; // NOI18N
239
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_popup_rollover.png"; // NOI18N
240
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_popup_pressed.png"; // NOI18N
241
buttonIconPaths.put( TabControlButton.ID_DROP_DOWN_BUTTON, iconPaths );
242             
243             //maximize/restore button
244
iconPaths = new String JavaDoc[4];
245             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_maximize_enabled.png"; // NOI18N
246
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_maximize_disabled.png"; // NOI18N
247
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_maximize_rollover.png"; // NOI18N
248
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_maximize_pressed.png"; // NOI18N
249
buttonIconPaths.put( TabControlButton.ID_MAXIMIZE_BUTTON, iconPaths );
250             
251             iconPaths = new String JavaDoc[4];
252             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_restore_enabled.png"; // NOI18N
253
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_restore_disabled.png"; // NOI18N
254
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_restore_rollover.png"; // NOI18N
255
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_restore_pressed.png"; // NOI18N
256
buttonIconPaths.put( TabControlButton.ID_RESTORE_BUTTON, iconPaths );
257         }
258     }
259
260     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
261         Icon JavaDoc res = null;
262         initIcons();
263         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
264         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
265             res = TabControlButtonFactory.getIcon( paths[buttonState] );
266         }
267         return res;
268     }
269     
270     protected Rectangle JavaDoc getControlButtonsRectangle( Container JavaDoc parent ) {
271         Component JavaDoc c = getControlButtons();
272         return new Rectangle JavaDoc( parent.getWidth()-c.getWidth()-4, 4, c.getWidth(), c.getHeight() );
273     }
274
275     public Insets JavaDoc getTabAreaInsets() {
276         Insets JavaDoc retValue = super.getTabAreaInsets();
277         retValue.right += 4;
278         return retValue;
279     }
280 }
281
Popular Tags