KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 /**
26  * Windows classic implementation of tab renderer
27  *
28  * @author Tim Boudreau
29  */

30 final class WinClassicEditorTabCellRenderer extends AbstractTabCellRenderer {
31
32     private static final TabPainter leftClip = new WinClassicLeftClipPainter();
33     private static final TabPainter rightClip = new WinClassicRightClipPainter();
34     private static final TabPainter normal = new WinClassicPainter();
35
36     private static final Color GTK_TABBED_PANE_BACKGROUND_1 = new Color(255, 255, 255);
37     
38     static final Color ATTENTION_COLOR = new Color(255, 238, 120);
39     
40     private static boolean isGenericUI = !"Windows".equals(
41         UIManager.getLookAndFeel().getID());
42
43     /**
44      * Creates a new instance of WinClassicEditorTabCellRenderer
45      */

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

62     public int getPixelsToAddToSelection() {
63         return 4;
64     }
65
66     protected int getCaptionYAdjustment() {
67         return 0;
68     }
69
70     public Dimension getPadding() {
71         Dimension d = super.getPadding();
72         d.width = isShowCloseButton() && !Boolean.getBoolean("nb.tabs.suppressCloseButton") ? 28 : 14;
73         return d;
74     }
75     
76     private static final Insets INSETS = new Insets(0, 2, 0, 10);
77
78     private static class WinClassicPainter implements TabPainter {
79
80         public Insets getBorderInsets(Component c) {
81             return INSETS;
82         }
83
84         public Polygon getInteriorPolygon(Component c) {
85             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
86
87             Insets ins = getBorderInsets(c);
88             Polygon p = new Polygon();
89             int x = ren.isLeftmost() ? 1 : 0;
90             int y = isGenericUI ? 0 : 1;
91
92             int width = ren.isLeftmost() ? c.getWidth() - 1 : c.getWidth();
93             int height = ren.isSelected() ?
94                     c.getHeight() + 2 : c.getHeight() - 1;
95                     
96             p.addPoint(x, y + ins.top + 2);
97             p.addPoint(x + 2, y + ins.top);
98             p.addPoint(x + width - 3, y + ins.top);
99             p.addPoint(x + width - 1, y + ins.top + 2);
100             p.addPoint(x + width - 1, y + height - 2);
101             p.addPoint(x, y + height - 2);
102             return p;
103         }
104
105         public boolean isBorderOpaque() {
106             return true;
107         }
108
109         public void paintBorder(Component c, Graphics g, int x, int y,
110                                 int width, int height) {
111             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
112             Polygon p = getInteriorPolygon(c);
113             g.setColor(ren.isSelected() ?
114                        UIManager.getColor("controlLtHighlight") :
115                        UIManager.getColor("controlHighlight")); //NOI18N
116

117             int[] xpoints = p.xpoints;
118             int[] ypoints = p.ypoints;
119
120             g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1],
121                        ypoints[p.npoints - 1]);
122
123             for (int i = 0; i < p.npoints - 1; i++) {
124                 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1],
125                            ypoints[i + 1]);
126                 if (i == p.npoints - 4) {
127                     g.setColor(ren.isSelected() ?
128                                UIManager.getColor("controlDkShadow") :
129                                UIManager.getColor("controlShadow")); //NOI18N
130
g.drawLine(xpoints[i] + 1, ypoints[i] + 1,
131                                xpoints[i] + 2, ypoints[i] + 2);
132                 }
133             }
134         }
135
136         public void paintInterior(Graphics g, Component c) {
137
138             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
139             boolean wantGradient = ren.isSelected() && ren.isActive() || ((ren.isClipLeft()
140                     || ren.isClipRight())
141                     && ren.isPressed());
142
143             if (wantGradient) {
144                 ((Graphics2D) g).setPaint(ColorUtil.getGradientPaint(0, 0, getSelGradientColor(), ren.getWidth(), 0, getSelGradientColor2()));
145             } else {
146                 if (!ren.isAttention()) {
147                     g.setColor(ren.isSelected() ?
148                                UIManager.getColor("TabbedPane.background") :
149                                UIManager.getColor("tab_unsel_fill")); //NOI18N
150
} else {
151                     g.setColor(ATTENTION_COLOR);
152                 }
153             }
154             Polygon p = getInteriorPolygon(c);
155             g.fillPolygon(p);
156
157             if (!supportsCloseButton((JComponent)c)) {
158                 return;
159             }
160             
161             paintCloseButton( g, (JComponent)c );
162         }
163
164         public void getCloseButtonRectangle(JComponent jc, Rectangle rect, Rectangle bounds) {
165             boolean rightClip = ((WinClassicEditorTabCellRenderer) jc).isClipRight();
166             boolean leftClip = ((WinClassicEditorTabCellRenderer) jc).isClipLeft();
167             boolean notSupported = !((WinClassicEditorTabCellRenderer) jc).isShowCloseButton();
168             if (leftClip || rightClip || notSupported) {
169                 rect.x = -100;
170                 rect.y = -100;
171                 rect.width = 0;
172                 rect.height = 0;
173             } else {
174                 String JavaDoc iconPath = findIconPath((WinClassicEditorTabCellRenderer) jc);
175                 Icon icon = TabControlButtonFactory.getIcon(iconPath);
176                 int iconWidth = icon.getIconWidth();
177                 int iconHeight = icon.getIconHeight();
178                 rect.x = bounds.x + bounds.width - iconWidth - 2;
179                 rect.y = bounds.y + (Math.max(0, bounds.height / 2 - iconHeight / 2));
180                 rect.width = iconWidth;
181                 rect.height = iconHeight;
182             }
183         }
184         
185         private void paintCloseButton(Graphics g, JComponent c) {
186             if (((AbstractTabCellRenderer) c).isShowCloseButton()) {
187                 
188                 Rectangle r = new Rectangle(0, 0, c.getWidth(), c.getHeight());
189                 Rectangle cbRect = new Rectangle();
190                 getCloseButtonRectangle((JComponent) c, cbRect, r);
191                 
192                 //paint close button
193
String JavaDoc iconPath = findIconPath( (WinClassicEditorTabCellRenderer)c );
194                 Icon icon = TabControlButtonFactory.getIcon( iconPath );
195                 icon.paintIcon(c, g, cbRect.x, cbRect.y);
196             }
197         }
198         
199         /**
200          * Returns path of icon which is correct for currect state of tab at given
201          * index
202          */

203         private String JavaDoc findIconPath( WinClassicEditorTabCellRenderer renderer ) {
204             if( renderer.inCloseButton() && renderer.isPressed() ) {
205                 return "org/netbeans/swing/tabcontrol/resources/win_close_pressed.png"; // NOI18N
206
}
207             if( renderer.inCloseButton() ) {
208                 return "org/netbeans/swing/tabcontrol/resources/win_close_rollover.png"; // NOI18N
209
}
210             return "org/netbeans/swing/tabcontrol/resources/win_close_enabled.png"; // NOI18N
211
}
212         
213         public boolean supportsCloseButton(JComponent renderer) {
214             return
215                 ((AbstractTabCellRenderer) renderer).isShowCloseButton();
216         }
217
218     }
219
220
221     private static class WinClassicLeftClipPainter implements TabPainter {
222
223         public Insets getBorderInsets(Component c) {
224             return INSETS;
225         }
226
227         public Polygon getInteriorPolygon(Component c) {
228             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
229
230             Insets ins = getBorderInsets(c);
231             Polygon p = new Polygon();
232             int x = -3;
233             int y = isGenericUI ? 0 : 1;
234
235             int width = c.getWidth() + 3;
236             int height = ren.isSelected() ?
237                     c.getHeight() + 2 : c.getHeight() - 1;
238
239             p.addPoint(x, y + ins.top + 2);
240             p.addPoint(x + 2, y + ins.top);
241             p.addPoint(x + width - 3, y + ins.top);
242             p.addPoint(x + width - 1, y + ins.top + 2);
243             p.addPoint(x + width - 1, y + height - 1);
244             p.addPoint(x, y + height - 1);
245             return p;
246         }
247
248         public void paintBorder(Component c, Graphics g, int x, int y,
249                                 int width, int height) {
250             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
251             Polygon p = getInteriorPolygon(c);
252             g.setColor(ren.isSelected() ?
253                        UIManager.getColor("controlLtHighlight") :
254                        UIManager.getColor("controlHighlight")); //NOI18N
255

256             int[] xpoints = p.xpoints;
257             int[] ypoints = p.ypoints;
258
259             g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1],
260                        ypoints[p.npoints - 1]);
261
262             for (int i = 0; i < p.npoints - 1; i++) {
263                 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1],
264                            ypoints[i + 1]);
265                 if (i == p.npoints - 4) {
266                     g.setColor(ren.isSelected() ?
267                                UIManager.getColor("controlDkShadow") :
268                                UIManager.getColor("controlShadow")); //NOI18N
269
g.drawLine(xpoints[i] + 1, ypoints[i] + 1,
270                                xpoints[i] + 2, ypoints[i] + 2);
271                 }
272             }
273         }
274
275         public void paintInterior(Graphics g, Component c) {
276             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
277             boolean wantGradient = ren.isSelected() && ren.isActive() || ((ren.isClipLeft()
278                     || ren.isClipRight())
279                     && ren.isPressed());
280
281             if (wantGradient) {
282                 ((Graphics2D) g).setPaint(ColorUtil.getGradientPaint(0, 0, getSelGradientColor(), ren.getWidth(), 0, getSelGradientColor2()));
283             } else {
284                 if (!ren.isAttention()) {
285                     g.setColor(ren.isSelected() ?
286                            UIManager.getColor("TabbedPane.background") :
287                            UIManager.getColor("tab_unsel_fill")); //NOI18N
288
} else {
289                     g.setColor(ATTENTION_COLOR);
290                 }
291             }
292             Polygon p = getInteriorPolygon(c);
293             g.fillPolygon(p);
294         }
295
296         public boolean isBorderOpaque() {
297             return true;
298         }
299
300         public void getCloseButtonRectangle(JComponent jc,
301                                             final Rectangle rect,
302                                             Rectangle bounds) {
303             rect.setBounds(-20, -20, 0, 0);
304         }
305
306         public boolean supportsCloseButton(JComponent renderer) {
307             return false;
308         }
309     }
310
311     private static class WinClassicRightClipPainter implements TabPainter {
312
313         public Insets getBorderInsets(Component c) {
314             return INSETS;
315         }
316
317         public boolean isBorderOpaque() {
318             return true;
319         }
320
321         public Polygon getInteriorPolygon(Component c) {
322             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
323
324             Insets ins = getBorderInsets(c);
325             Polygon p = new Polygon();
326             int x = 0;
327             int y = isGenericUI ? 0 : 1;
328
329             int width = c.getWidth();
330             int height = ren.isSelected() ?
331                     c.getHeight() + 2 : c.getHeight() - 1;
332
333             p.addPoint(x, y + ins.top + 2);
334             p.addPoint(x + 2, y + ins.top);
335             p.addPoint(x + width - 1, y + ins.top);
336             p.addPoint(x + width - 1, y + height - 1);
337             p.addPoint(x, y + height - 1);
338             return p;
339         }
340
341         public void paintBorder(Component c, Graphics g, int x, int y,
342                                 int width, int height) {
343             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
344             Polygon p = getInteriorPolygon(c);
345             g.setColor(ren.isSelected() ?
346                        UIManager.getColor("controlLtHighlight") :
347                        UIManager.getColor("controlHighlight")); //NOI18N
348

349             int[] xpoints = p.xpoints;
350             int[] ypoints = p.ypoints;
351
352             g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1],
353                        ypoints[p.npoints - 1]);
354
355             for (int i = 0; i < p.npoints - 1; i++) {
356                 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1],
357                            ypoints[i + 1]);
358                 if (ren.isSelected() && i == p.npoints - 4) {
359                     g.setColor(ren.isActive() ?
360                                UIManager.getColor("Table.selectionBackground") :
361                                UIManager.getColor("control")); //NOI18n
362
} else if (i == p.npoints - 4) {
363                     break;
364                 }
365                 if (i == p.npoints - 3) {
366                     break;
367                 }
368             }
369         }
370
371         public void paintInterior(Graphics g, Component c) {
372             WinClassicEditorTabCellRenderer ren = (WinClassicEditorTabCellRenderer) c;
373             boolean wantGradient = ren.isSelected() && ren.isActive() || ((ren.isClipLeft()
374                     || ren.isClipRight())
375                     && ren.isPressed());
376
377             if (wantGradient) {
378                 ((Graphics2D) g).setPaint(ColorUtil.getGradientPaint(0, 0, getSelGradientColor(), ren.getWidth(), 0, getSelGradientColor2()));
379             } else {
380                 if (!ren.isAttention()) {
381                     g.setColor(ren.isSelected() ?
382                            UIManager.getColor("TabbedPane.background") : //NOI18N
383
UIManager.getColor("tab_unsel_fill")); //NOI18N
384
} else {
385                     g.setColor(ATTENTION_COLOR);
386                 }
387             }
388
389             Polygon p = getInteriorPolygon(c);
390             g.fillPolygon(p);
391         }
392
393         public boolean supportsCloseButton(JComponent renderer) {
394             return false;
395         }
396
397         public void getCloseButtonRectangle(JComponent jc,
398                                             final Rectangle rect,
399                                             Rectangle bounds) {
400             rect.setBounds(-20, -20, 0, 0);
401         }
402     }
403     
404     private static final Color getSelGradientColor() {
405         if ("GTK".equals(UIManager.getLookAndFeel().getID())) { // NOI18N
406
return GTK_TABBED_PANE_BACKGROUND_1; // #68200
407
} else {
408             return UIManager.getColor("winclassic_tab_sel_gradient"); // NOI18N
409
}
410     }
411     
412     private static final Color getSelGradientColor2() {
413         return UIManager.getColor("TabbedPane.background"); // NOI18N
414
}
415     
416 }
417
Popular Tags