KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
23 import java.awt.*;
24 import javax.swing.plaf.synth.Region JavaDoc;
25 import javax.swing.plaf.synth.SynthConstants JavaDoc;
26 import javax.swing.plaf.synth.SynthContext JavaDoc;
27 import javax.swing.plaf.synth.SynthLookAndFeel JavaDoc;
28 import javax.swing.plaf.synth.SynthPainter JavaDoc;
29 import javax.swing.plaf.synth.SynthStyle JavaDoc;
30 import javax.swing.plaf.synth.SynthStyleFactory JavaDoc;
31
32 /**
33  * Gtk implementation of tab renderer
34  *
35  * @author Marek Slama
36  */

37 final class GtkEditorTabCellRenderer extends AbstractTabCellRenderer {
38
39     private static final TabPainter leftClip = new GtkLeftClipPainter();
40     private static final TabPainter rightClip = new GtkRightClipPainter();
41     private static final TabPainter normal = new GtkPainter();
42     
43     private static JTabbedPane dummyTab;
44
45     static final Color ATTENTION_COLOR = new Color(255, 238, 120);
46     
47     /**
48      * Creates a new instance of GtkEditorTabCellRenderer
49      */

50     public GtkEditorTabCellRenderer() {
51           super(leftClip, normal, rightClip, new Dimension (28, 32));
52     }
53     
54     public Color getSelectedForeground() {
55         return UIManager.getColor("textText"); //NOI18N
56
}
57
58     public Color getForeground() {
59         return getSelectedForeground();
60     }
61     
62     /**
63      * #56245 - need more space between icon and edge on classic for the case
64      * of full 16x16 icons.
65      */

66     public int getPixelsToAddToSelection() {
67         return 4;
68     }
69
70     protected int getCaptionYAdjustment() {
71         return 0;
72     }
73
74     public Dimension getPadding() {
75         Dimension d = super.getPadding();
76         d.width = isShowCloseButton() && !Boolean.getBoolean("nb.tabs.suppressCloseButton") ? 28 : 14;
77         return d;
78     }
79     
80     private static final Insets INSETS = new Insets(0, 2, 0, 10);
81     
82     private static void paintTabBackground (Graphics g, int index, int state,
83     int x, int y, int w, int h) {
84         if (dummyTab == null) {
85             dummyTab = new JTabbedPane();
86         }
87         Region JavaDoc region = Region.TABBED_PANE_TAB;
88         SynthLookAndFeel JavaDoc laf = (SynthLookAndFeel JavaDoc) UIManager.getLookAndFeel();
89         SynthStyleFactory JavaDoc sf = laf.getStyleFactory();
90         SynthStyle JavaDoc style = sf.getStyle(dummyTab, region);
91         SynthContext JavaDoc context =
92             new SynthContext JavaDoc(dummyTab, region, style, state);
93         SynthPainter JavaDoc painter = style.getPainter(context);
94         painter.paintTabbedPaneTabBackground(context, g, x, y, w, h, index);
95     }
96     
97     private static class GtkPainter implements TabPainter {
98
99         public Insets getBorderInsets(Component c) {
100             return INSETS;
101         }
102
103         public Polygon getInteriorPolygon(Component c) {
104             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
105
106             Insets ins = getBorderInsets(c);
107             Polygon p = new Polygon();
108             int x = ren.isLeftmost() ? 1 : 0;
109             int y = 1;
110
111             int width = ren.isLeftmost() ? c.getWidth() - 1 : c.getWidth();
112             int height = ren.isSelected() ?
113                     c.getHeight() + 2 : c.getHeight() - 1;
114                     
115             //Modified to return rectangle
116
p.addPoint(x, y);
117             p.addPoint(x + width, y);
118             p.addPoint(x + width, y + height);
119             p.addPoint(x, y + height);
120             return p;
121         }
122
123         public boolean isBorderOpaque() {
124             return true;
125         }
126
127         public void paintBorder(Component c, Graphics g, int x, int y,
128                                 int width, int height) {
129             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
130             Polygon p = getInteriorPolygon(c);
131             return;
132         }
133         
134
135         public void paintInterior(Graphics g, Component c) {
136             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
137             Polygon p = getInteriorPolygon(c);
138             if (ren.isSelected()) {
139                 paintTabBackground(g, 0, SynthConstants.SELECTED,
140                 p.getBounds().x, p.getBounds().y, p.getBounds().width, p.getBounds().height);
141             } else {
142                 paintTabBackground(g, 0, 0,
143                 p.getBounds().x, p.getBounds().y + 2, p.getBounds().width, p.getBounds().height - 2);
144             }
145             
146             if (!supportsCloseButton((JComponent)c)) {
147                 return;
148             }
149             
150             paintCloseButton( g, (JComponent)c );
151         }
152
153         public void getCloseButtonRectangle(JComponent jc, Rectangle rect, Rectangle bounds) {
154             boolean rightClip = ((GtkEditorTabCellRenderer) jc).isClipRight();
155             boolean leftClip = ((GtkEditorTabCellRenderer) jc).isClipLeft();
156             boolean notSupported = !((GtkEditorTabCellRenderer) jc).isShowCloseButton();
157             if (leftClip || rightClip || notSupported) {
158                 rect.x = -100;
159                 rect.y = -100;
160                 rect.width = 0;
161                 rect.height = 0;
162             } else {
163                 String JavaDoc iconPath = findIconPath((GtkEditorTabCellRenderer) jc);
164                 Icon icon = TabControlButtonFactory.getIcon(iconPath);
165                 int iconWidth = icon.getIconWidth();
166                 int iconHeight = icon.getIconHeight();
167                 rect.x = bounds.x + bounds.width - iconWidth - 2;
168                 rect.y = bounds.y + (Math.max(0, bounds.height / 2 - iconHeight / 2));
169                 rect.width = iconWidth;
170                 rect.height = iconHeight;
171             }
172         }
173         
174         private void paintCloseButton(Graphics g, JComponent c) {
175             if (((AbstractTabCellRenderer) c).isShowCloseButton()) {
176                 
177                 Rectangle r = new Rectangle(0, 0, c.getWidth(), c.getHeight());
178                 Rectangle cbRect = new Rectangle();
179                 getCloseButtonRectangle((JComponent) c, cbRect, r);
180                 
181                 //paint close button
182
String JavaDoc iconPath = findIconPath( (GtkEditorTabCellRenderer)c );
183                 Icon icon = TabControlButtonFactory.getIcon( iconPath );
184                 icon.paintIcon(c, g, cbRect.x, cbRect.y);
185             }
186         }
187         
188         /**
189          * Returns path of icon which is correct for currect state of tab at given
190          * index
191          */

192         private String JavaDoc findIconPath( GtkEditorTabCellRenderer renderer ) {
193             if( renderer.inCloseButton() && renderer.isPressed() ) {
194                 return "org/netbeans/swing/tabcontrol/resources/gtk_close_pressed.png"; // NOI18N
195
}
196             if( renderer.inCloseButton() ) {
197                 return "org/netbeans/swing/tabcontrol/resources/gtk_close_rollover.png"; // NOI18N
198
}
199             return "org/netbeans/swing/tabcontrol/resources/gtk_close_enabled.png"; // NOI18N
200
}
201         
202         public boolean supportsCloseButton(JComponent renderer) {
203             return ((AbstractTabCellRenderer) renderer).isShowCloseButton();
204         }
205
206     }
207
208
209     private static class GtkLeftClipPainter implements TabPainter {
210
211         public Insets getBorderInsets(Component c) {
212             return INSETS;
213         }
214
215         public Polygon getInteriorPolygon(Component c) {
216             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
217
218             Insets ins = getBorderInsets(c);
219             Polygon p = new Polygon();
220             int x = -3;
221             int y = 1;
222
223             int width = c.getWidth() + 3;
224             int height = ren.isSelected() ?
225                     c.getHeight() + 2 : c.getHeight() - 1;
226
227             //Modified to return rectangle
228
p.addPoint(x, y);
229             p.addPoint(x + width, y);
230             p.addPoint(x + width, y + height);
231             p.addPoint(x, y + height);
232             return p;
233         }
234
235         public void paintBorder(Component c, Graphics g, int x, int y,
236                                 int width, int height) {
237             return;
238         }
239
240         public void paintInterior(Graphics g, Component c) {
241             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
242             Polygon p = getInteriorPolygon(c);
243             if (ren.isSelected()) {
244                 paintTabBackground(g, 0, SynthConstants.SELECTED,
245                 p.getBounds().x, p.getBounds().y, p.getBounds().width, p.getBounds().height);
246             } else {
247                 paintTabBackground(g, 0, 0,
248                 p.getBounds().x, p.getBounds().y + 2, p.getBounds().width, p.getBounds().height - 2);
249             }
250         }
251
252         public boolean isBorderOpaque() {
253             return true;
254         }
255
256         public void getCloseButtonRectangle(JComponent jc,
257                                             final Rectangle rect,
258                                             Rectangle bounds) {
259             rect.setBounds(-20, -20, 0, 0);
260         }
261
262         public boolean supportsCloseButton(JComponent renderer) {
263             return false;
264         }
265     }
266
267     private static class GtkRightClipPainter implements TabPainter {
268
269         public Insets getBorderInsets(Component c) {
270             return INSETS;
271         }
272
273         public boolean isBorderOpaque() {
274             return true;
275         }
276
277         public Polygon getInteriorPolygon(Component c) {
278             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
279
280             Insets ins = getBorderInsets(c);
281             Polygon p = new Polygon();
282             int x = 0;
283             int y = 1;
284
285             int width = c.getWidth() + 10;
286             int height = ren.isSelected() ?
287                     c.getHeight() + 2 : c.getHeight() - 1;
288
289             //Modified to return rectangle
290
p.addPoint(x, y);
291             p.addPoint(x + width, y);
292             p.addPoint(x + width, y + height);
293             p.addPoint(x, y + height);
294             return p;
295         }
296
297         public void paintBorder(Component c, Graphics g, int x, int y,
298                                 int width, int height) {
299         }
300
301         public void paintInterior(Graphics g, Component c) {
302             GtkEditorTabCellRenderer ren = (GtkEditorTabCellRenderer) c;
303             
304             Polygon p = getInteriorPolygon(c);
305             if (ren.isSelected()) {
306                 paintTabBackground(g, 0, SynthConstants.SELECTED,
307                 p.getBounds().x, p.getBounds().y, p.getBounds().width, p.getBounds().height);
308             } else {
309                 paintTabBackground(g, 0, 0,
310                 p.getBounds().x, p.getBounds().y + 2, p.getBounds().width, p.getBounds().height - 2);
311             }
312         }
313
314         public boolean supportsCloseButton(JComponent renderer) {
315             return false;
316         }
317
318         public void getCloseButtonRectangle(JComponent jc,
319                                             final Rectangle rect,
320                                             Rectangle bounds) {
321             rect.setBounds(-20, -20, 0, 0);
322         }
323     }
324     
325 }
326
Popular Tags