KickJava   Java API By Example, From Geeks To Geeks.

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


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.Point 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.awt.event.MouseEvent JavaDoc;
36
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import org.netbeans.swing.tabcontrol.event.TabActionEvent;
43
44
45 import org.openide.awt.HtmlRenderer;
46
47 /**
48  * Win Vista-like user interface of view type tabs.
49  *
50  * @author S. Aubrecht
51  */

52 public final class WinVistaViewTabDisplayerUI extends AbstractViewTabDisplayerUI {
53
54     /*********** constants *************/
55
56     /**
57      * Space between text and left side of the tab
58      */

59     private static final int TXT_X_PAD = 9;
60     private static final int TXT_Y_PAD = 3;
61
62     private static final int ICON_X_PAD = 4;
63
64     private static final int BUMP_X_PAD = 3;
65     private static final int BUMP_Y_PAD_UPPER = 6;
66     private static final int BUMP_Y_PAD_BOTTOM = 3;
67
68     /*********** static fields **********/
69     
70     /**
71      * True when colors were already initialized, false otherwise
72      */

73     private static boolean colorsReady = false;
74
75     private static Color JavaDoc
76             unselFillBrightUpperC,
77             unselFillDarkUpperC,
78             unselFillBrightLowerC,
79             unselFillDarkLowerC,
80             selFillC,
81             focusFillUpperC,
82             focusFillBrightLowerC,
83             focusFillDarkLowerC,
84             mouseOverFillBrightUpperC,
85             mouseOverFillDarkUpperC,
86             mouseOverFillBrightLowerC,
87             mouseOverFillDarkLowerC,
88             txtC,
89             borderC,
90             selBorderC,
91             borderInnerC;
92
93     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
94     
95     /**
96      * ******** instance fields ********
97      */

98
99     private Dimension JavaDoc prefSize;
100
101     /**
102      * rectangle instance used to speedup recurring computations in painting
103      * methods
104      */

105     private Rectangle JavaDoc tempRect = new Rectangle JavaDoc();
106
107     /**
108      * Should be constructed only from createUI method.
109      */

110     private WinVistaViewTabDisplayerUI(TabDisplayer displayer) {
111         super(displayer);
112         prefSize = new Dimension JavaDoc(100, 17);
113     }
114
115     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
116         return new WinVistaViewTabDisplayerUI((TabDisplayer)c);
117     }
118      
119     public void installUI (JComponent JavaDoc c) {
120         super.installUI(c);
121         initColors();
122         c.setOpaque(true);
123     }
124
125     protected AbstractViewTabDisplayerUI.Controller createController() {
126         return new OwnController();
127     }
128
129     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
130         FontMetrics JavaDoc fm = getTxtFontMetrics();
131         int height = fm == null ?
132                 17 : fm.getAscent() + 2 * fm.getDescent() + 3;
133         Insets JavaDoc insets = c.getInsets();
134         prefSize.height = height + insets.bottom + insets.top;
135         return prefSize;
136     }
137
138     protected void paintTabContent(Graphics JavaDoc g, int index, String JavaDoc text, int x,
139                                    int y, int width, int height) {
140         FontMetrics JavaDoc fm = getTxtFontMetrics();
141         // setting font already here to compute string width correctly
142
g.setFont(getTxtFont());
143         if( 0 == index )
144             x++;
145         int txtWidth = width;
146         if (isSelected(index)) {
147             Component JavaDoc buttons = getControlButtons();
148             if( null != buttons ) {
149                 Dimension JavaDoc buttonsSize = buttons.getPreferredSize();
150                 txtWidth = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD);
151                 buttons.setLocation( x + txtWidth+2*TXT_X_PAD, y + (height-buttonsSize.height)/2 );
152             }
153         } else {
154             txtWidth = width - 2 * TXT_X_PAD;
155         }
156         
157         // draw bump (dragger)
158
ColorUtil.paintVistaTabDragTexture(getDisplayer(), g, x + BUMP_X_PAD, y
159                  + BUMP_Y_PAD_UPPER, height - (BUMP_Y_PAD_UPPER
160                  + BUMP_Y_PAD_BOTTOM));
161         HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent()
162                 + TXT_Y_PAD, txtWidth, height, getTxtFont(),
163                 txtC,
164                 HtmlRenderer.STYLE_TRUNCATE, true);
165     }
166
167     protected void paintTabBorder(Graphics JavaDoc g, int index, int x, int y,
168                                   int width, int height) {
169         boolean isFirst = index == 0;
170         boolean isHighlighted = isTabHighlighted(index);
171
172         g.translate(x, y);
173
174         Color JavaDoc borderColor = isHighlighted ? selBorderC : borderC;
175         g.setColor(borderColor);
176         int left = 0;
177         //left
178
if (isFirst )
179             g.drawLine(0, 0, 0, height - 2);
180         //top
181
g.drawLine(0, 0, width - 1, 0);
182         //right
183
if( index < getDataModel().size()-1 && isTabHighlighted(index+1) )
184             g.setColor( selBorderC );
185         g.drawLine(width - 1, 0, width - 1, height - 2);
186         //bottom
187
g.setColor(borderC);
188         g.drawLine(0, height - 1, width - 1, height - 1);
189         
190         //inner white border
191
g.setColor(borderInnerC);
192         //left
193
if (isFirst)
194             g.drawLine(1, 1, 1, height - 2);
195         else
196             g.drawLine(0, 1, 0, height - 2);
197         //right
198
g.drawLine(width-2, 1, width-2, height - 2);
199         //top
200
g.drawLine(1, 1, width-2, 1);
201
202         g.translate(-x, -y);
203     }
204
205     protected void paintTabBackground(Graphics JavaDoc g, int index, int x, int y,
206                                       int width, int height) {
207         // shrink rectangle - don't affect border and tab header
208
y += 2;
209         height -= 2;
210         // background body, colored according to state
211
boolean selected = isSelected(index);
212         boolean focused = selected && isActive();
213         boolean attention = isAttention(index);
214         boolean mouseOver = isMouseOver(index);
215         if (focused && !attention) {
216             ColorUtil.vistaFillRectGradient((Graphics2D JavaDoc) g, x, y, width, height,
217                                          focusFillUpperC,
218                                          focusFillBrightLowerC, focusFillDarkLowerC );
219         } else if (selected && !attention) {
220             g.setColor(selFillC);
221             g.fillRect(x, y, width, height);
222         } else if (mouseOver && !attention) {
223             ColorUtil.vistaFillRectGradient((Graphics2D JavaDoc) g, x, y, width, height,
224                                          mouseOverFillBrightUpperC, mouseOverFillDarkUpperC,
225                                          mouseOverFillBrightLowerC, mouseOverFillDarkLowerC );
226         } else if (attention) {
227             Color JavaDoc a = new Color JavaDoc (255, 255, 128);
228             Color JavaDoc b = new Color JavaDoc (230, 200, 64);
229             ColorUtil.xpFillRectGradient((Graphics2D JavaDoc) g, x, y, width, height, a, b);
230         } else {
231             ColorUtil.vistaFillRectGradient((Graphics2D JavaDoc) g, x, y, width, height,
232                                          unselFillBrightUpperC, unselFillDarkUpperC,
233                                          unselFillBrightLowerC, unselFillDarkLowerC );
234         }
235     }
236
237     /**
238      * Override to bold font
239      */

240     protected Font JavaDoc getTxtFont() {
241         Font JavaDoc font = super.getTxtFont();
242         if (!font.isBold()) {
243             font = font.deriveFont(Font.BOLD);
244         }
245         return font;
246     }
247
248     /**
249      * @return true if tab with given index should have highlighted border, false otherwise.
250      */

251     private boolean isTabHighlighted(int index) {
252         if (((OwnController) getController()).getMouseIndex() == index) {
253             return true;
254         }
255         return isSelected(index) && isActive();
256     }
257
258     /**
259      * @return true if tab with given index has mouse cursor above and is not
260      * the selected one, false otherwise.
261      */

262     private boolean isMouseOver(int index) {
263         return ((OwnController) getController()).getMouseIndex() == index
264                 && !isSelected(index);
265     }
266
267     /**
268      * Initialization of colors
269      */

270     private static void initColors() {
271         if (!colorsReady) {
272             txtC = UIManager.getColor("TabbedPane.foreground"); // NOI18N
273

274             selFillC = UIManager.getColor("tab_sel_fill"); // NOI18N
275

276             focusFillUpperC = UIManager.getColor("tab_focus_fill_upper"); // NOI18N
277
focusFillBrightLowerC = UIManager.getColor("tab_focus_fill_bright_lower"); // NOI18N
278
focusFillDarkLowerC = UIManager.getColor("tab_focus_fill_dark_lower"); // NOI18N
279

280             unselFillBrightUpperC = UIManager.getColor("tab_unsel_fill_bright_upper"); // NOI18N
281
unselFillDarkUpperC = UIManager.getColor("tab_unsel_fill_dark_upper"); // NOI18N
282
unselFillBrightLowerC = UIManager.getColor("tab_unsel_fill_bright_lower"); // NOI18N
283
unselFillDarkLowerC = UIManager.getColor("tab_unsel_fill_dark_lower"); // NOI18N
284

285             mouseOverFillBrightUpperC = UIManager.getColor("tab_mouse_over_fill_bright_upper"); // NOI18N
286
mouseOverFillDarkUpperC = UIManager.getColor("tab_mouse_over_fill_dark_upper"); // NOI18N
287
mouseOverFillBrightLowerC = UIManager.getColor("tab_mouse_over_fill_bright_lower"); // NOI18N
288
mouseOverFillDarkLowerC = UIManager.getColor("tab_mouse_over_fill_dark_lower"); // NOI18N
289

290             borderC = UIManager.getColor("tab_border"); // NOI18N
291
selBorderC = UIManager.getColor("tab_sel_border"); // NOI18N
292
borderInnerC = UIManager.getColor("tab_border_inner"); // NOI18N
293

294             colorsReady = true;
295         }
296     }
297
298     private static void initIcons() {
299         if( null == buttonIconPaths ) {
300             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
301             
302             //close button
303
String JavaDoc[] iconPaths = new String JavaDoc[4];
304             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_enabled.png"; // NOI18N
305
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_pressed.png"; // NOI18N
306
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
307             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_bigclose_rollover.png"; // NOI18N
308
buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths );
309             
310             //slide/pin button
311
iconPaths = new String JavaDoc[4];
312             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_enabled.png"; // NOI18N
313
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_pressed.png"; // NOI18N
314
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
315             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slideright_rollover.png"; // NOI18N
316
buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths );
317             
318             iconPaths = new String JavaDoc[4];
319             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_enabled.png"; // NOI18N
320
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_pressed.png"; // NOI18N
321
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
322             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slideleft_rollover.png"; // NOI18N
323
buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths );
324             
325             iconPaths = new String JavaDoc[4];
326             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_enabled.png"; // NOI18N
327
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_pressed.png"; // NOI18N
328
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
329             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_slidebottom_rollover.png"; // NOI18N
330
buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths );
331             
332             iconPaths = new String JavaDoc[4];
333             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/vista_pin_enabled.png"; // NOI18N
334
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/vista_pin_pressed.png"; // NOI18N
335
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
336             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/vista_pin_rollover.png"; // NOI18N
337
buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths );
338         }
339     }
340
341     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
342         Icon JavaDoc res = null;
343         initIcons();
344         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
345         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
346             res = TabControlButtonFactory.getIcon( paths[buttonState] );
347         }
348         return res;
349     }
350
351     public void postTabAction(TabActionEvent e) {
352         super.postTabAction(e);
353         if( TabDisplayer.COMMAND_MAXIMIZE.equals( e.getActionCommand() ) ) {
354             ((OwnController)getController()).updateHighlight( -1 );
355         }
356     }
357
358     /**
359      * Own close icon button controller
360      */

361     private class OwnController extends Controller {
362
363         /**
364          * holds index of tab in which mouse pointer was lastly located. -1
365          * means mouse pointer is out of component's area
366          */

367         // TBD - should be part of model, not controller
368
private int lastIndex = -1;
369
370         /**
371          * @return Index of tab in which mouse pointer is currently located.
372          */

373         public int getMouseIndex() {
374             return lastIndex;
375         }
376
377         /**
378          * Triggers visual tab header change when mouse enters/leaves tab in
379          * advance to superclass functionality.
380          */

381         public void mouseMoved(MouseEvent JavaDoc e) {
382             super.mouseMoved(e);
383             Point JavaDoc pos = e.getPoint();
384             updateHighlight(getLayoutModel().indexOfPoint(pos.x, pos.y));
385         }
386
387         /**
388          * Resets tab header in advance to superclass functionality
389          */

390         public void mouseExited(MouseEvent JavaDoc e) {
391             super.mouseExited(e);
392             if( !inControlButtonsRect(e.getPoint())) {
393                 updateHighlight(-1);
394             }
395         }
396
397         /**
398          * Invokes repaint of dirty region if needed
399          */

400         private void updateHighlight(int curIndex) {
401             if (curIndex == lastIndex) {
402                 return;
403             }
404             // compute region which needs repaint
405
TabLayoutModel tlm = getLayoutModel();
406             int x, y, w, h;
407             Rectangle JavaDoc repaintRect = null;
408             if (curIndex != -1) {
409                 x = tlm.getX(curIndex)-1;
410                 y = tlm.getY(curIndex);
411                 w = tlm.getW(curIndex)+2;
412                 h = tlm.getH(curIndex);
413                 repaintRect = new Rectangle JavaDoc(x, y, w, h);
414             }
415             // due to model changes, lastIndex may become invalid, so check
416
if ((lastIndex != -1) && (lastIndex < getDataModel().size())) {
417                 x = tlm.getX(lastIndex)-1;
418                 y = tlm.getY(lastIndex);
419                 w = tlm.getW(lastIndex)+2;
420                 h = tlm.getH(lastIndex);
421                 if (repaintRect != null) {
422                     repaintRect =
423                             repaintRect.union(new Rectangle JavaDoc(x, y, w, h));
424                 } else {
425                     repaintRect = new Rectangle JavaDoc(x, y, w, h);
426                 }
427             }
428             // trigger repaint if needed, update index
429
if (repaintRect != null) {
430                 getDisplayer().repaint(repaintRect);
431             }
432             lastIndex = curIndex;
433         }
434     } // end of OwnController
435
}
436
Popular Tags