KickJava   Java API By Example, From Geeks To Geeks.

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


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.Insets JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import org.netbeans.swing.tabcontrol.TabDisplayer;
31
32 import javax.swing.plaf.ComponentUI JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JTabbedPane JavaDoc;
38 import javax.swing.UIManager JavaDoc;
39 import javax.swing.plaf.synth.Region JavaDoc;
40 import javax.swing.plaf.synth.SynthConstants JavaDoc;
41 import javax.swing.plaf.synth.SynthContext JavaDoc;
42 import javax.swing.plaf.synth.SynthLookAndFeel JavaDoc;
43 import javax.swing.plaf.synth.SynthPainter JavaDoc;
44 import javax.swing.plaf.synth.SynthStyle JavaDoc;
45 import javax.swing.plaf.synth.SynthStyleFactory JavaDoc;
46 import org.openide.awt.HtmlRenderer;
47
48 /**
49  * GTK user interface of view type tabs. It uses native engine to paint tab
50  * background.
51  *
52  * @author Dafe Simonek
53  */

54 public final class GtkViewTabDisplayerUI extends AbstractViewTabDisplayerUI {
55     
56     /**
57      * ******** constants ************
58      */

59
60     private static final int BUMP_X_PAD = 0;
61     private static final int BUMP_WIDTH = 0;
62     private static final int TXT_X_PAD = 3;
63     private static final int TXT_Y_PAD = 6;
64
65     private static final int ICON_X_PAD = 2;
66     
67     private static Map JavaDoc<Integer JavaDoc, String JavaDoc[]> buttonIconPaths;
68
69     private static JTabbedPane JavaDoc dummyTab;
70     
71     /**
72      * ******** instance fields ********
73      */

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

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

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

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

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

160         HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent()
161             + TXT_Y_PAD,
162             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
170         return;
171     }
172     
173     private static void paintTabBackgroundNative (Graphics JavaDoc g, int index, int state,
174     int x, int y, int w, int h) {
175         if (dummyTab == null) {
176             dummyTab = new JTabbedPane JavaDoc();
177         }
178         Region JavaDoc region = Region.TABBED_PANE_TAB;
179         SynthLookAndFeel JavaDoc laf = (SynthLookAndFeel JavaDoc) UIManager.getLookAndFeel();
180         SynthStyleFactory JavaDoc sf = laf.getStyleFactory();
181         SynthStyle JavaDoc style = sf.getStyle(dummyTab, region);
182         SynthContext JavaDoc context =
183             new SynthContext JavaDoc(dummyTab, region, style, state);
184         SynthPainter JavaDoc painter = style.getPainter(context);
185         painter.paintTabbedPaneTabBackground(context, g, x, y, w, h, index);
186     }
187
188     protected void paintTabBackground(Graphics JavaDoc g, int index, int x, int y,
189                                       int width, int height) {
190         if (isSelected(index)) {
191             paintTabBackgroundNative(g, 0, SynthConstants.SELECTED,
192             x, y, width, height);
193         } else {
194             paintTabBackgroundNative(g, 0, 0,
195             x, y + 2, width, height - 2);
196         }
197     }
198
199     /**
200      * Paints dragger in given rectangle
201      */

202     private void drawBump(Graphics JavaDoc g, int index, int x, int y, int width,
203                           int height) {
204             //This look and feel is also used as the default UI on non-JDS
205
return;
206     }
207     
208     private static void initIcons() {
209         if( null == buttonIconPaths ) {
210             buttonIconPaths = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc[]>(7);
211             
212             //close button
213
String JavaDoc[] iconPaths = new String JavaDoc[4];
214             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_enabled.png"; // NOI18N
215
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_pressed.png"; // NOI18N
216
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
217             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_bigclose_rollover.png"; // NOI18N
218
buttonIconPaths.put( TabControlButton.ID_CLOSE_BUTTON, iconPaths );
219             
220             //slide/pin button
221
iconPaths = new String JavaDoc[4];
222             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_enabled.png"; // NOI18N
223
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_pressed.png"; // NOI18N
224
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
225             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slideright_rollover.png"; // NOI18N
226
buttonIconPaths.put( TabControlButton.ID_SLIDE_RIGHT_BUTTON, iconPaths );
227             
228             iconPaths = new String JavaDoc[4];
229             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_enabled.png"; // NOI18N
230
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_pressed.png"; // NOI18N
231
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
232             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slideleft_rollover.png"; // NOI18N
233
buttonIconPaths.put( TabControlButton.ID_SLIDE_LEFT_BUTTON, iconPaths );
234             
235             iconPaths = new String JavaDoc[4];
236             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_enabled.png"; // NOI18N
237
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_pressed.png"; // NOI18N
238
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
239             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_slidebottom_rollover.png"; // NOI18N
240
buttonIconPaths.put( TabControlButton.ID_SLIDE_DOWN_BUTTON, iconPaths );
241             
242             iconPaths = new String JavaDoc[4];
243             iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_enabled.png"; // NOI18N
244
iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_pressed.png"; // NOI18N
245
iconPaths[TabControlButton.STATE_DISABLED] = iconPaths[TabControlButton.STATE_DEFAULT];
246             iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/gtk_pin_rollover.png"; // NOI18N
247
buttonIconPaths.put( TabControlButton.ID_PIN_BUTTON, iconPaths );
248         }
249     }
250
251     public Icon JavaDoc getButtonIcon(int buttonId, int buttonState) {
252         Icon JavaDoc res = null;
253         initIcons();
254         String JavaDoc[] paths = buttonIconPaths.get( buttonId );
255         if( null != paths && buttonState >=0 && buttonState < paths.length ) {
256             res = TabControlButtonFactory.getIcon( paths[buttonState] );
257         }
258         return res;
259     }
260     
261 }
262
Popular Tags