KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import org.netbeans.swing.tabcontrol.TabDisplayer;
31 import org.openide.awt.HtmlRenderer;
32
33 import javax.swing.plaf.ComponentUI JavaDoc;
34 import java.awt.event.MouseEvent 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
41 /**
42  * A view tabs ui for OS-X adapted from the view tabs UI for Metal.
43  *
44  * @author Tim Boudreau
45  */

46 public final class AquaViewTabDisplayerUI extends AbstractViewTabDisplayerUI {
47
48     private static final int TXT_X_PAD = 5;
49     private static final int ICON_X_PAD = 2;
50
51     /********* static fields ***********/
52     
53     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
54     
55     /**
56      * ******* instance fields *********
57      */

58
59     private Dimension JavaDoc prefSize;
60     /**
61      * Reusable Rectangle to optimize rectangle creation/garbage collection
62      * during paints
63      */

64     private Rectangle JavaDoc tempRect = new Rectangle JavaDoc();
65
66     /**
67      * Should be constructed only from createUI method.
68      */

69     private AquaViewTabDisplayerUI(TabDisplayer displayer) {
70         super(displayer);
71         prefSize = new Dimension JavaDoc(100, 19); //XXX huh?
72
}
73
74     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
75         return new AquaViewTabDisplayerUI((TabDisplayer) c);
76     }
77
78     protected AbstractViewTabDisplayerUI.Controller createController() {
79         return new OwnController();
80     }
81     
82     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
83         FontMetrics JavaDoc fm = getTxtFontMetrics();
84         int height = fm == null ?
85                 21 : fm.getAscent() + 2 * fm.getDescent() + 3;
86         Insets JavaDoc insets = c.getInsets();
87         prefSize.height = height + insets.bottom + insets.top;
88         return prefSize;
89     }
90
91     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
92         ColorUtil.setupAntialiasing(g);
93         super.paint(g, c);
94         paintBottomBorder(g, c);
95     }
96     
97     protected Font JavaDoc getTxtFont() {
98         return getDisplayer().getFont();
99     }
100
101     /**
102      * Paints bottom "activation" line
103      */

104     private void paintBottomBorder(Graphics JavaDoc g, JComponent JavaDoc c) {
105     }
106
107     protected void paintTabContent(Graphics JavaDoc g, int index, String JavaDoc text, int x,
108                                    int y, int width, int height) {
109         FontMetrics JavaDoc fm = getTxtFontMetrics();
110
111         // setting font already here to compute string width correctly
112
g.setFont(getTxtFont());
113         int textW = width;
114
115         if (isSelected(index)) {
116             Component JavaDoc buttons = getControlButtons();
117             if( null != buttons ) {
118                 Dimension JavaDoc buttonsSize = buttons.getPreferredSize();
119
120                 textW = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD);
121                 if (index == getDataModel().size() - 1) {
122                     textW -= 3;
123                 }
124                 buttons.setLocation( x + textW+2*TXT_X_PAD, y + (height-buttonsSize.height)/2 );
125             }
126         } else {
127             textW = width - 2 * TXT_X_PAD;
128         }
129
130         if (text.length() == 0) {
131             return;
132         }
133
134         int textHeight = fm.getHeight();
135         int textY;
136         int textX = x + TXT_X_PAD;
137     if (index == 0)
138         textX = x + 5;
139
140         if (textHeight > height) {
141             textY = (-1 * ((textHeight - height) / 2)) + fm.getAscent()
142                     - 1;
143         } else {
144             textY = (height / 2) - (textHeight / 2) + fm.getAscent();
145         }
146
147         HtmlRenderer.renderString(text, g, textX, textY, textW, height, getTxtFont(),
148                           UIManager.getColor("textText"),
149                           HtmlRenderer.STYLE_TRUNCATE, true);
150     }
151     
152     protected void paintTabBorder(Graphics JavaDoc g, int index, int x, int y,
153                                   int width, int height) {
154
155     }
156
157     private static final ChicletWrapper chiclet = new ChicletWrapper();
158
159     protected void paintTabBackground(Graphics JavaDoc g, int index, int x, int y,
160                                       int width, int height) {
161         boolean first = index == 0;
162         boolean last = index == getDataModel().size() - 1;
163         int state = 0;
164         if (isActive()) {
165             state |= GenericGlowingChiclet.STATE_ACTIVE;
166         }
167         if (isSelected(index)) {
168             state |= GenericGlowingChiclet.STATE_SELECTED;
169         }
170         if (isAttention(index)) {
171             state |= GenericGlowingChiclet.STATE_ATTENTION;
172         }
173         
174         y+=1; //align with top of editor tabs
175

176         chiclet.setState(state);
177         chiclet.setBounds(x, y, width, height);
178         chiclet.setArcs(first ? 0.5f : 0f, last ? 0.5f : 0f,
179                          first ? 0.0f : 0f, last ? 0.0f : 0f);
180         chiclet.setNotch(false, false);
181         g.translate (x, y);
182         chiclet.draw((Graphics2D JavaDoc) g);
183         g.translate (-x, -y);
184     }
185
186     private boolean containsMouse = false;
187
188     private void setContainsMouse(boolean val) {
189         if (val != containsMouse) {
190             containsMouse = val;
191             getDisplayer().repaint();
192         }
193     }
194
195     private boolean isContainsMouse() {
196         return containsMouse;
197     }
198
199     private static void initIcons() {
200         //TODO add icons for aqua l&f
201

202         if( null == buttonIconPaths ) {
203             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
204             
205             //close button
206
String JavaDoc[] iconPaths = new String JavaDoc[4];
207             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_enabled.png"; // NOI18N
208
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_pressed.png"; // NOI18N
209
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
210             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_bigclose_rollover.png"; // NOI18N
211
buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths );
212             
213             //slide/pin button
214
iconPaths = new String JavaDoc[4];
215             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_enabled.png"; // NOI18N
216
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_pressed.png"; // NOI18N
217
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
218             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slideright_rollover.png"; // NOI18N
219
buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths );
220             
221             iconPaths = new String JavaDoc[4];
222             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_enabled.png"; // NOI18N
223
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_pressed.png"; // NOI18N
224
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
225             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slideleft_rollover.png"; // NOI18N
226
buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths );
227             
228             iconPaths = new String JavaDoc[4];
229             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_enabled.png"; // NOI18N
230
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_pressed.png"; // NOI18N
231
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
232             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_slidebottom_rollover.png"; // NOI18N
233
buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths );
234             
235             iconPaths = new String JavaDoc[4];
236             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/mac_pin_enabled.png"; // NOI18N
237
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/mac_pin_pressed.png"; // NOI18N
238
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
239             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/mac_pin_rollover.png"; // NOI18N
240
buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths );
241         }
242     }
243
244     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
245         Icon JavaDoc res = null;
246         initIcons();
247         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
248         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
249             res = TabControlButtonFactory.getIcon( paths[buttonState] );
250         }
251         return res;
252     }
253     
254     /**
255      * Own close icon button controller
256      */

257     private class OwnController extends Controller {
258         
259         public void mouseEntered(MouseEvent JavaDoc me) {
260             super.mouseEntered(me);
261             setContainsMouse(true);
262         }
263
264         public void mouseExited(MouseEvent JavaDoc me) {
265             super.mouseExited(me);
266             setContainsMouse(false);
267         }
268     } // end of OwnController
269

270 }
271
Popular Tags