KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34 import org.netbeans.swing.tabcontrol.TabDisplayer;
35
36 import javax.swing.plaf.ComponentUI JavaDoc;
37
38
39 import org.openide.awt.HtmlRenderer;
40
41 /**
42  * User interface of view type tabs designed to be consistent with Swing metal
43  * look and feel.
44  *
45  * @author Dafe Simonek
46  */

47 public final class MetalViewTabDisplayerUI extends AbstractViewTabDisplayerUI {
48
49     /**
50      * *********** constants ******************
51      */

52     private static final int TXT_X_PAD = 5;
53
54     private static final int ICON_X_LEFT_PAD = 5;
55     private static final int ICON_X_RIGHT_PAD = 2;
56
57     private static final int BUMP_X_PAD = 5;
58     private static final int BUMP_Y_PAD = 4;
59     
60     /**
61      * ****** static fields **********
62      */

63
64     private static Color JavaDoc inactBgColor, actBgColor, borderHighlight, borderShadow;
65     
66     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
67
68     /**
69      * ******* instance fields *********
70      */

71
72     private Dimension JavaDoc prefSize;
73
74     /**
75      * Reusable Rectangle to optimize rectangle creation/garbage collection
76      * during paints
77      */

78     private Rectangle JavaDoc tempRect = new Rectangle JavaDoc();
79
80     /**
81      * Should be constructed only from createUI method.
82      */

83     private MetalViewTabDisplayerUI(TabDisplayer displayer) {
84         super(displayer);
85         prefSize = new Dimension JavaDoc(100, 19);
86     }
87
88     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
89         return new MetalViewTabDisplayerUI((TabDisplayer) c);
90     }
91
92     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
93         FontMetrics JavaDoc fm = getTxtFontMetrics();
94         int height = fm == null ?
95                 21 : fm.getAscent() + 2 * fm.getDescent() + 4;
96         Insets JavaDoc insets = c.getInsets();
97         prefSize.height = height + insets.bottom + insets.top;
98         return prefSize;
99     }
100
101     /**
102      * Overrides basic paint mathod, adds painting of overall blue or gray
103      * bottom area, depending on activation status value
104      */

105     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
106         super.paint(g, c);
107         paintBottomBorder(g, c);
108     }
109
110     /**
111      * Paints bottom "activation" line
112      */

113     private void paintBottomBorder(Graphics JavaDoc g, JComponent JavaDoc c) {
114         Color JavaDoc color = isActive() ? getActBgColor() : getInactBgColor();
115         g.setColor(color);
116         Rectangle JavaDoc bounds = c.getBounds();
117         g.fillRect(1, bounds.height - 3, bounds.width - 1, 2);
118         g.setColor(getBorderShadow());
119         g.drawLine(1, bounds.height - 1, bounds.width - 1, bounds.height - 1);
120     }
121
122     protected void paintTabContent(Graphics JavaDoc g, int index, String JavaDoc text, int x,
123                                    int y, int width, int height) {
124         FontMetrics JavaDoc fm = getTxtFontMetrics();
125         // setting font already here to compute string width correctly
126
g.setFont(getTxtFont());
127         int txtWidth = width;
128         if (isSelected(index)) {
129             Component JavaDoc buttons = getControlButtons();
130             int buttonsWidth = 0;
131             if( null != buttons ) {
132                 Dimension JavaDoc buttonsSize = buttons.getPreferredSize();
133                 buttonsWidth = buttonsSize.width + ICON_X_LEFT_PAD + ICON_X_RIGHT_PAD;
134                 txtWidth = width - (buttonsWidth + 2*TXT_X_PAD);
135                 buttons.setLocation( x + txtWidth+2*TXT_X_PAD+ICON_X_LEFT_PAD, y + (height-buttonsSize.height)/2+1 );
136             }
137             
138             txtWidth = (int)HtmlRenderer.renderString(text, g, x + TXT_X_PAD, height -
139                     fm.getDescent() - 4, txtWidth, height, getTxtFont(),
140                     UIManager.getColor("textText"),
141                     HtmlRenderer.STYLE_TRUNCATE, true);
142             int bumpWidth = width
143                     - (TXT_X_PAD + txtWidth + BUMP_X_PAD + buttonsWidth);
144             if (bumpWidth > 0) {
145                 paintBump(index, g, x + TXT_X_PAD + txtWidth + BUMP_X_PAD,
146                           y + BUMP_Y_PAD, bumpWidth, height - 2 * BUMP_Y_PAD);
147             }
148         } else {
149             txtWidth = width - 2 * TXT_X_PAD;
150             HtmlRenderer.renderString(text, g, x + TXT_X_PAD, height -
151                 fm.getDescent() - 4, txtWidth, height, getTxtFont(),
152                 UIManager.getColor("textText"),
153                 HtmlRenderer.STYLE_TRUNCATE, true);
154         }
155     }
156
157     protected void paintTabBorder(Graphics JavaDoc g, int index, int x, int y,
158                                   int width, int height) {
159         Color JavaDoc highlight = getBorderHighlight();
160         Color JavaDoc shadow = getBorderShadow();
161         boolean isSelected = isSelected(index);
162
163         boolean isFirst = index == 0;
164         boolean isLast = index == getDataModel().size() - 1;
165
166         g.translate(x, y);
167         
168         // paint darker lines
169
g.setColor(shadow);
170         if (!isFirst) {
171             g.drawLine(0, 0, 0, height - 5);
172         }
173         if (!isSelected) {
174             g.drawLine(1, height - 5, isLast ? width - 1 : width, height - 5);
175         }
176         // paint brighter lines
177
g.setColor(highlight);
178         g.drawLine(1, 0, width - 1, 0);
179         if (isFirst) {
180             g.drawLine(0, 0, 0, height - 2);
181         }
182         if (!isSelected) {
183             g.drawLine(0, height - 4, isLast ? width - 1 : width, height - 4);
184         }
185
186         g.translate(-x, -y);
187     }
188
189     protected void paintTabBackground(Graphics JavaDoc g, int index, int x, int y,
190                                       int width, int height) {
191         boolean selected = isSelected(index);
192         boolean highlighted = selected && isActive();
193         boolean attention = isAttention (index);
194         if (highlighted && !attention) {
195             g.setColor(getActBgColor());
196             g.fillRect(x, y, width, height - 3);
197         } else if (attention) {
198             g.setColor(MetalEditorTabCellRenderer.ATTENTION_COLOR);
199             g.fillRect(x, y, width, height - 3);
200         } else {
201             g.setColor(getInactBgColor());
202             g.fillRect(x, y, width, height - 3);
203         }
204     }
205
206     private void paintBump(int index, Graphics JavaDoc g, int x, int y, int width,
207                            int height) {
208         ColorUtil.paintViewTabBump(g, x, y, width, height, isFocused(index) ?
209                                                            ColorUtil.FOCUS_TYPE :
210                                                            ColorUtil.UNSEL_TYPE);
211     }
212     
213     static Color JavaDoc getInactBgColor() {
214         if (inactBgColor == null) {
215             inactBgColor = (Color JavaDoc) UIManager.get("inactiveCaption");
216             if (inactBgColor == null) {
217                 inactBgColor = new Color JavaDoc(204, 204, 204);
218             }
219         }
220         return inactBgColor;
221     }
222
223     static Color JavaDoc getActBgColor() {
224         if (actBgColor == null) {
225             actBgColor = (Color JavaDoc) UIManager.get("activeCaption");
226             if (actBgColor == null) {
227                 actBgColor = new Color JavaDoc(204, 204, 255);
228             }
229         }
230         return actBgColor;
231     }
232
233     private Color JavaDoc getBorderHighlight() {
234         if (borderHighlight == null) {
235             borderHighlight = getInactBgColor().brighter();
236         }
237         return borderHighlight;
238     }
239
240     private Color JavaDoc getBorderShadow() {
241         if (borderShadow == null) {
242             borderShadow = getInactBgColor().darker();
243         }
244         return borderShadow;
245     }
246
247
248     private static void initIcons() {
249         if( null == buttonIconPaths ) {
250             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
251             
252             //close button
253
String JavaDoc[] iconPaths = new String JavaDoc[4];
254             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_enabled.png"; // NOI18N
255
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_pressed.png"; // NOI18N
256
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
257             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_bigclose_rollover.png"; // NOI18N
258
buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths );
259             
260             //slide/pin button
261
iconPaths = new String JavaDoc[4];
262             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_enabled.png"; // NOI18N
263
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_pressed.png"; // NOI18N
264
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
265             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slideright_rollover.png"; // NOI18N
266
buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths );
267             
268             iconPaths = new String JavaDoc[4];
269             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_enabled.png"; // NOI18N
270
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_pressed.png"; // NOI18N
271
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
272             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slideleft_rollover.png"; // NOI18N
273
buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths );
274             
275             iconPaths = new String JavaDoc[4];
276             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_enabled.png"; // NOI18N
277
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_pressed.png"; // NOI18N
278
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
279             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_slidebottom_rollover.png"; // NOI18N
280
buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths );
281             
282             iconPaths = new String JavaDoc[4];
283             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/metal_pin_enabled.png"; // NOI18N
284
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/metal_pin_pressed.png"; // NOI18N
285
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
286             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/metal_pin_rollover.png"; // NOI18N
287
buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths );
288         }
289     }
290
291     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
292         Icon JavaDoc res = null;
293         initIcons();
294         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
295         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
296             res = TabControlButtonFactory.getIcon( paths[buttonState] );
297         }
298         return res;
299     }
300 }
301
Popular Tags