KickJava   Java API By Example, From Geeks To Geeks.

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


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.Font JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.Paint JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import org.netbeans.swing.tabcontrol.TabDisplayer;
33
34 import javax.swing.plaf.ComponentUI JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.UIManager JavaDoc;
40 import org.openide.awt.HtmlRenderer;
41
42 /**
43  * Windows classic-like user interface of view type tabs. Implements Border just
44  * to save one class, change if apropriate.
45  *
46  * @author Dafe Simonek
47  */

48 public final class WinClassicViewTabDisplayerUI extends AbstractViewTabDisplayerUI {
49
50     private static final boolean isGenericUI =
51         !"Windows".equals(UIManager.getLookAndFeel().getID()); //NOI18N
52

53     private static final Color JavaDoc GTK_TABBED_PANE_BACKGROUND_1 = new Color JavaDoc(255, 255, 255);
54     
55     /**
56      * ******** constants ************
57      */

58
59     private static final int BUMP_X_PAD = isGenericUI ? 0 : 3;
60     private static final int BUMP_WIDTH = isGenericUI ? 0 : 3;
61     private static final int TXT_X_PAD = isGenericUI ? 3 : BUMP_X_PAD + BUMP_WIDTH + 5;
62     private static final int TXT_Y_PAD = 3;
63
64     private static final int ICON_X_PAD = 2;
65     
66     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
67
68
69     /**
70      * ******** instance fields ********
71      */

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

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

84     private WinClassicViewTabDisplayerUI(TabDisplayer displayer) {
85         super(displayer);
86         prefSize = new Dimension JavaDoc(100, 19);
87     }
88
89     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
90         return new WinClassicViewTabDisplayerUI((TabDisplayer) c);
91     }
92
93     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
94         FontMetrics JavaDoc fm = getTxtFontMetrics();
95         int height = fm == null ?
96                 19 : fm.getAscent() + 2 * fm.getDescent() + 2;
97         Insets JavaDoc insets = c.getInsets();
98         prefSize.height = height + insets.bottom + insets.top;
99         return prefSize;
100     }
101
102     /**
103      * adds painting of overall border
104      */

105     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
106
107         ColorUtil.setupAntialiasing(g);
108
109         Color JavaDoc col = c.getBackground();
110         if (col != null) {
111             g.setColor (col);
112             g.fillRect (0, 0, c.getWidth(), c.getHeight());
113         }
114         paintOverallBorder(g, c);
115         super.paint(g, c);
116     }
117
118     /**
119      * Paints lower border, bottom line, separating tabs from content
120      */

121     protected void paintOverallBorder(Graphics JavaDoc g, JComponent JavaDoc c) {
122         if (isGenericUI) {
123             return;
124         }
125         Rectangle JavaDoc r = c.getBounds();
126         g.setColor(UIManager.getColor("InternalFrame.borderDarkShadow")); //NOI18N
127
g.drawLine(0, r.height - 1, r.width - 1, r.height - 1);
128     }
129     
130     protected Font JavaDoc getTxtFont() {
131         if (isGenericUI) {
132             Font JavaDoc result = UIManager.getFont("controlFont");
133             if (result != null) {
134                 return result;
135             }
136         }
137         return super.getTxtFont();
138     }
139
140     protected void paintTabContent(Graphics JavaDoc g, int index, String JavaDoc text, int x,
141                                    int y, int width, int height) {
142         // substract lower border
143
height--;
144         y -= 2; //align to center
145
FontMetrics JavaDoc fm = getTxtFontMetrics();
146         // setting font already here to compute string width correctly
147
g.setFont(getTxtFont());
148         int txtWidth = width;
149         if (isSelected(index)) {
150             Component JavaDoc buttons = getControlButtons();
151             if( null != buttons ) {
152                 Dimension JavaDoc buttonsSize = buttons.getPreferredSize();
153                 txtWidth = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD);
154                 buttons.setLocation( x + txtWidth+2*TXT_X_PAD, y + (height-buttonsSize.height)/2+1 );
155             }
156         } else {
157             txtWidth = width - 2 * TXT_X_PAD;
158         }
159         // draw bump (dragger)
160
drawBump(g, index, x + 4, y + 6, BUMP_WIDTH, height - 8);
161         
162         // draw text in right color
163
Color JavaDoc txtC = UIManager.getColor("TabbedPane.foreground"); //NOI18N
164

165         HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent()
166             + TXT_Y_PAD,
167             txtWidth, height, getTxtFont(),
168             txtC,
169             HtmlRenderer.STYLE_TRUNCATE, true);
170     }
171
172     protected void paintTabBorder(Graphics JavaDoc g, int index, int x, int y,
173                                   int width, int height) {
174                                       
175         // subtract lower border
176
height--;
177         boolean isSelected = isSelected(index);
178
179         g.translate(x, y);
180
181         g.setColor(UIManager.getColor("InternalFrame.borderShadow")); //NOI18N
182
g.drawLine(0, height - 1, width - 2, height - 1);
183         g.drawLine(width - 1, height - 1, width - 1, 0);
184
185         g.setColor(isSelected ? UIManager.getColor(
186                 "InternalFrame.borderHighlight") //NOI18N
187
: UIManager.getColor("InternalFrame.borderLight")); //NOI18N
188
g.drawLine(0, 0, 0, height - 1);
189         g.drawLine(1, 0, width - 2, 0);
190
191         g.translate(-x, -y);
192     }
193
194     protected void paintTabBackground(Graphics JavaDoc g, int index, int x, int y,
195                                       int width, int height) {
196         // substract lower border
197
height--;
198         ((Graphics2D JavaDoc) g).setPaint(
199                 getBackgroundPaint(g, index, x, y, width, height));
200         if (isFocused(index)) {
201             g.fillRect(x, y, width, height);
202         } else {
203             g.fillRect(x + 1, y + 1, width - 2, height - 2);
204         }
205     }
206
207     private Paint JavaDoc getBackgroundPaint(Graphics JavaDoc g, int index, int x, int y,
208                                      int width, int height) {
209         // background body, colored according to state
210
boolean selected = isSelected(index);
211         boolean focused = isFocused(index);
212         boolean attention = isAttention(index);
213         
214         Paint JavaDoc result = null;
215         if (focused && !attention) {
216             result = ColorUtil.getGradientPaint(x, y, getSelGradientColor(), x + width, y, getSelGradientColor2());
217         } else if (selected && !attention) {
218             result = UIManager.getColor("TabbedPane.background"); //NOI18N
219
} else if (attention) {
220             result = WinClassicEditorTabCellRenderer.ATTENTION_COLOR;
221         } else {
222             result = UIManager.getColor("tab_unsel_fill");
223         }
224         return result;
225     }
226
227     /**
228      * Paints dragger in given rectangle
229      */

230     private void drawBump(Graphics JavaDoc g, int index, int x, int y, int width,
231                           int height) {
232         if (isGenericUI) {
233             //This look and feel is also used as the default UI on non-JDS
234
return;
235         }
236                               
237         // prepare colors
238
Color JavaDoc highlightC, bodyC, shadowC;
239         if (isFocused(index)) {
240             bodyC = new Color JavaDoc(210, 220, 243); //XXX
241
highlightC = bodyC.brighter();
242             shadowC = bodyC.darker();
243         } else if (isSelected(index)) {
244             highlightC =
245                     UIManager.getColor("InternalFrame.borderHighlight"); //NOI18N
246
bodyC = UIManager.getColor("InternalFrame.borderLight"); //NOI18N
247
shadowC = UIManager.getColor("InternalFrame.borderShadow"); //NOI18N
248
} else {
249             highlightC = UIManager.getColor("InternalFrame.borderLight"); //NOI18N
250
bodyC = UIManager.getColor("tab_unsel_fill");
251             shadowC = UIManager.getColor("InternalFrame.borderShadow"); //NOI18N
252
}
253         // draw
254
for (int i = 0; i < width / 3; i++, x += 3) {
255             g.setColor(highlightC);
256             g.drawLine(x, y, x, y + height - 1);
257             g.drawLine(x, y, x + 1, y);
258             g.setColor(bodyC);
259             g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
260             g.setColor(shadowC);
261             g.drawLine(x + 2, y, x + 2, y + height - 1);
262             g.drawLine(x, y + height - 1, x + 1, y + height - 1);
263         }
264     }
265
266     private static final Color JavaDoc getSelGradientColor() {
267         if ("GTK".equals(UIManager.getLookAndFeel().getID())) { // NOI18N
268
return GTK_TABBED_PANE_BACKGROUND_1; // #68200
269
} else {
270             return UIManager.getColor("winclassic_tab_sel_gradient"); // NOI18N
271
}
272     }
273     
274     private static final Color JavaDoc getSelGradientColor2() {
275         return UIManager.getColor("TabbedPane.background"); // NOI18N
276
}
277
278     private static void initIcons() {
279         if( null == buttonIconPaths ) {
280             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
281             
282             //close button
283
String JavaDoc[] iconPaths = new String JavaDoc[4];
284             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_bigclose_enabled.png"; // NOI18N
285
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_bigclose_pressed.png"; // NOI18N
286
iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_bigclose_disabled.png"; // NOI18N
287
iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_bigclose_rollover.png"; // NOI18N
288
buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths );
289             
290             //slide/pin button
291
iconPaths = new String JavaDoc[4];
292             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_slideright_enabled.png"; // NOI18N
293
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_slideright_pressed.png"; // NOI18N
294
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
295             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_slideright_rollover.png"; // NOI18N
296
buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths );
297             
298             iconPaths = new String JavaDoc[4];
299             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_slideleft_enabled.png"; // NOI18N
300
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_slideleft_pressed.png"; // NOI18N
301
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
302             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_slideleft_rollover.png"; // NOI18N
303
buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths );
304             
305             iconPaths = new String JavaDoc[4];
306             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_slidebottom_enabled.png"; // NOI18N
307
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_slidebottom_pressed.png"; // NOI18N
308
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
309             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_slidebottom_rollover.png"; // NOI18N
310
buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths );
311             
312             iconPaths = new String JavaDoc[4];
313             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_pin_enabled.png"; // NOI18N
314
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_pin_pressed.png"; // NOI18N
315
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
316             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_pin_rollover.png"; // NOI18N
317
buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths );
318         }
319     }
320
321     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
322         Icon JavaDoc res = null;
323         initIcons();
324         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
325         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
326             res = TabControlButtonFactory.getIcon( paths[buttonState] );
327         }
328         return res;
329     }
330 }
331
Popular Tags