KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MetalEditorTabCellRenderer.java
21  *
22  * Created on December 2, 2003, 9:30 PM
23  */

24
25 package org.netbeans.swing.tabcontrol.plaf;
26
27 import javax.swing.*;
28 import java.awt.*;
29
30 /**
31  * Renderer for editor tabs in metal l&f
32  *
33  * @author Tim Boudreau
34  */

35 class MetalEditorTabCellRenderer extends AbstractTabCellRenderer {
36     private static final MetalTabPainter metalborder = new MetalTabPainter();
37     private static final MetalRightClippedTabPainter rightBorder = new MetalRightClippedTabPainter();
38     private static final MetalLeftClippedTabPainter leftBorder = new MetalLeftClippedTabPainter();
39
40     static final Color ATTENTION_COLOR = new Color(255, 238, 120);
41     /**
42      * Creates a new instance of MetalEditorTabCellRenderer
43      */

44     public MetalEditorTabCellRenderer() {
45         super(leftBorder, metalborder, rightBorder, new Dimension(34, 29));
46         setBorder(metalborder);
47     }
48
49     protected int getCaptionYAdjustment() {
50         return 0;
51     }
52
53     public Dimension getPadding() {
54         Dimension d = super.getPadding();
55         d.width = isShowCloseButton() && !Boolean.getBoolean("nb.tabs.suppressCloseButton") ? 34 : 24;
56         return d;
57     }
58
59     private static class MetalTabPainter implements TabPainter {
60         public Insets getBorderInsets(Component c) {
61             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
62             return new Insets(mtr.isSelected() ? 3 : 5,
63                               mtr.isSelected() ? 10 : 9, 1, 0); //XXX
64
}
65
66         public boolean supportsCloseButton(JComponent renderer) {
67             return ((AbstractTabCellRenderer) renderer).isShowCloseButton();
68         }
69
70         public boolean isBorderOpaque() {
71             return true;
72         }
73
74         public void paintBorder(Component c, Graphics g, int x, int y,
75                                 int width, int height) {
76             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
77             Polygon p = getInteriorPolygon(c);
78             
79             //Draw the highlight first, one pixel over, to get the left side and
80
//diagonal highlight (the right edge will get cut off, as we want)
81
g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
82
g.drawPolygon(p);
83             p.translate(-1, 0);
84             Insets ins = getBorderInsets(c);
85             g.drawLine(x + 6, y + ins.top + 1, x + width - 1, y + ins.top + 1);
86
87             if (mtr.isSelected()) {
88                 //draw the dark portion of the drag texture dots
89
g.drawLine(4, ins.top + 6, 4, ins.top + 6);
90                 g.drawLine(2, ins.top + 8, 2, ins.top + 8);
91
92                 g.drawLine(4, ins.top + 10, 4, ins.top + 10);
93                 g.drawLine(2, ins.top + 12, 2, ins.top + 12);
94
95                 g.drawLine(4, ins.top + 14, 4, ins.top + 14);
96                 g.drawLine(2, ins.top + 16, 2, ins.top + 16);
97             }
98             
99             //Draw the dark polygon
100
g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
101
g.drawPolygon(p);
102
103             if (mtr.isSelected()) {
104                 //draw the dark portion of the drag texture dots
105
g.drawLine(5, ins.top + 7, 5, ins.top + 7);
106                 g.drawLine(3, ins.top + 9, 3, ins.top + 9);
107
108                 g.drawLine(5, ins.top + 11, 5, ins.top + 11);
109                 g.drawLine(3, ins.top + 13, 3, ins.top + 13);
110
111                 g.drawLine(5, ins.top + 15, 5, ins.top + 15);
112                 g.drawLine(3, ins.top + 17, 3, ins.top + 17);
113             }
114
115             if (!mtr.isSelected()) {
116                 g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
117
g.drawLine(x, mtr.getHeight() - 1, mtr.getWidth() - 1,
118                            mtr.getHeight() - 1);
119             }
120         }
121
122         public Polygon getInteriorPolygon(Component c) {
123             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
124
125             Insets ins = getBorderInsets(c);
126             Polygon p = new Polygon();
127             int x = mtr.isLeftmost() ? 1 : 0;
128             int y = 0;
129
130             int width = mtr.isLeftmost() ? c.getWidth() - 1 : c.getWidth();
131             int height = mtr.isSelected() ?
132                     c.getHeight() + 3 : c.getHeight();
133
134             p.addPoint(x, y + ins.top + 6);
135             p.addPoint(x + 6, y + ins.top);
136             p.addPoint(x + width, y + ins.top);
137             p.addPoint(x + width, y + height);
138             p.addPoint(x, y + height);
139             return p;
140         }
141
142         public void paintInterior(Graphics g, Component c) {
143             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
144
145             if (mtr.isAttention()) {
146                 g.setColor(ATTENTION_COLOR);
147             }
148             
149             Polygon p = getInteriorPolygon(c);
150             g.fillPolygon(p);
151             
152             //Get the close button bounds, more or less
153
Rectangle r = new Rectangle();
154             getCloseButtonRectangle(mtr, r, new Rectangle(0, 0,
155                                                           mtr.getWidth(),
156                                                           mtr.getHeight()));
157
158             if (!g.hitClip(r.x, r.y, r.width, r.height)) {
159                 return;
160             }
161             paintCloseButton( g, (JComponent)c );
162         }
163
164         public void getCloseButtonRectangle(JComponent jc,
165                                             final Rectangle rect,
166                                             Rectangle bounds) {
167             if (!((AbstractTabCellRenderer) jc).isShowCloseButton()) {
168                 rect.x = -100;
169                 rect.y = -100;
170                 rect.width = 0;
171                 rect.height = 0;
172                 return;
173             }
174             String JavaDoc iconPath = findIconPath((MetalEditorTabCellRenderer) 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))+2;
180             rect.width = iconWidth;
181             rect.height = iconHeight;
182         }
183                 
184         private void paintCloseButton(Graphics g, JComponent c) {
185             if (((AbstractTabCellRenderer) c).isShowCloseButton()) {
186                 
187                 Rectangle r = new Rectangle(0, 0, c.getWidth(), c.getHeight());
188                 Rectangle cbRect = new Rectangle();
189                 getCloseButtonRectangle((JComponent) c, cbRect, r);
190                 
191                 //paint close button
192
String JavaDoc iconPath = findIconPath( (MetalEditorTabCellRenderer)c );
193                 Icon icon = TabControlButtonFactory.getIcon( iconPath );
194                 icon.paintIcon(c, g, cbRect.x, cbRect.y);
195             }
196         }
197         
198         /**
199          * Returns path of icon which is correct for currect state of tab at given
200          * index
201          */

202         private String JavaDoc findIconPath( MetalEditorTabCellRenderer renderer ) {
203             if( renderer.inCloseButton() && renderer.isPressed() ) {
204                 return "org/netbeans/swing/tabcontrol/resources/metal_close_pressed.png"; // NOI18N
205
}
206             if( renderer.inCloseButton() ) {
207                 return "org/netbeans/swing/tabcontrol/resources/metal_close_rollover.png"; // NOI18N
208
}
209             return "org/netbeans/swing/tabcontrol/resources/metal_close_enabled.png"; // NOI18N
210
}
211     }
212
213     private static class MetalLeftClippedTabPainter implements TabPainter {
214
215         public Insets getBorderInsets(Component c) {
216             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
217             return new Insets(mtr.isSelected() ? 3 : 5,
218                               mtr.isSelected() ? 10 : 9, 1, 0); //XXX
219
}
220
221         public boolean isBorderOpaque() {
222             return true;
223         }
224
225         public Polygon getInteriorPolygon(Component c) {
226             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
227
228             Insets ins = getBorderInsets(c);
229             Polygon p = new Polygon();
230             //Ensure the left edge is out of bounds
231
int x = -1;
232             int y = ins.top;
233
234             int width = c.getWidth();
235             int height = mtr.isSelected() ?
236                     c.getHeight() + 3 : c.getHeight();
237
238             p.addPoint(x, y);
239             p.addPoint(x + width, y);
240             p.addPoint(x + width, y + height);
241             p.addPoint(x, y + height);
242
243             return p;
244         }
245
246         public void paintBorder(Component c, Graphics g, int x, int y,
247                                 int width, int height) {
248             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
249             g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
250
Polygon p = getInteriorPolygon(c);
251
252             p.translate(0, 1);
253             g.drawPolygon(p);
254             p.translate(0, -1);
255             g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
256
g.drawPolygon(p);
257             if (!mtr.isSelected()) {
258                 g.drawLine(x, y + height - 1, x + width, y + height - 1);
259             }
260         }
261
262         public void paintInterior(Graphics g, Component c) {
263             Polygon p = getInteriorPolygon(c);
264             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
265             if (mtr.isAttention()) {
266                 g.setColor(ATTENTION_COLOR);
267             }
268             
269             g.fillPolygon(p);
270         }
271
272         public void getCloseButtonRectangle(JComponent jc, Rectangle rect,
273                                             Rectangle bounds) {
274             bounds.setBounds(-20, -20, 0, 0);
275         }
276
277         public boolean supportsCloseButton(JComponent renderer) {
278             return false;
279         }
280     }
281
282     private static class MetalRightClippedTabPainter implements TabPainter {
283         public Insets getBorderInsets(Component c) {
284             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
285             return new Insets(mtr.isSelected() ? 3 : 5,
286                               mtr.isSelected() ? 10 : 9, 1, 0); //XXX
287
}
288
289         public boolean isBorderOpaque() {
290             return true;
291         }
292
293         public Polygon getInteriorPolygon(Component c) {
294             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
295
296             Insets ins = getBorderInsets(c);
297             Polygon p = new Polygon();
298             int x = mtr.isLeftmost() ? 1 : 0;
299             int y = 0;
300
301             int width = c.getWidth() + 2;
302             int height = mtr.isSelected() ?
303                     c.getHeight() + 3 : c.getHeight();
304
305             p.addPoint(x, y + ins.top + 6);
306             p.addPoint(x + 6, y + ins.top);
307             p.addPoint(x + width, y + ins.top);
308             p.addPoint(x + width, y + height);
309             p.addPoint(x, y + height);
310             return p;
311         }
312
313         public void paintBorder(Component c, Graphics g, int x, int y,
314                                 int width, int height) {
315             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
316             Polygon p = getInteriorPolygon(c);
317             
318             //Draw the highlight first, one pixel over, to get the left side and
319
//diagonal highlight (the right edge will get cut off, as we want)
320
g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
321
g.drawPolygon(p);
322             p.translate(-1, 0);
323             Insets ins = getBorderInsets(c);
324             g.drawLine(x + 6, y + ins.top + 1, x + width - 1, y + ins.top + 1);
325
326             if (mtr.isSelected()) {
327                 //draw the light portion of the drag texture dots
328
g.drawLine(4, ins.top + 6, 4, ins.top + 6);
329                 g.drawLine(2, ins.top + 8, 2, ins.top + 8);
330
331                 g.drawLine(4, ins.top + 10, 4, ins.top + 10);
332                 g.drawLine(2, ins.top + 12, 2, ins.top + 12);
333
334                 g.drawLine(4, ins.top + 14, 4, ins.top + 14);
335                 g.drawLine(2, ins.top + 16, 2, ins.top + 16);
336             }
337             
338             //Draw the dark polygon
339
g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
340
g.drawPolygon(p);
341
342             if (mtr.isSelected()) {
343                 //draw the dark portion of the drag texture dots
344
g.drawLine(5, ins.top + 7, 5, ins.top + 7);
345                 g.drawLine(3, ins.top + 9, 3, ins.top + 9);
346
347                 g.drawLine(5, ins.top + 11, 5, ins.top + 11);
348                 g.drawLine(3, ins.top + 13, 3, ins.top + 13);
349
350                 g.drawLine(5, ins.top + 15, 5, ins.top + 15);
351                 g.drawLine(3, ins.top + 17, 3, ins.top + 17);
352             }
353
354             if (!mtr.isSelected()) {
355                 g.setColor(UIManager.getColor("controlDkShadow")); //NOI18N
356
g.drawLine(x, c.getHeight() - 1, c.getWidth() - 1,
357                            c.getHeight() - 1);
358             }
359         }
360
361         public void paintInterior(Graphics g, Component c) {
362             Polygon p = getInteriorPolygon(c);
363             MetalEditorTabCellRenderer mtr = (MetalEditorTabCellRenderer) c;
364             if (mtr.isAttention()) {
365                 g.setColor(ATTENTION_COLOR);
366             }
367             g.fillPolygon(p);
368         }
369
370         public void getCloseButtonRectangle(JComponent jc, Rectangle rect,
371                                             Rectangle bounds) {
372             bounds.setBounds(-20, -20, 0, 0);
373         }
374
375         public boolean supportsCloseButton(JComponent renderer) {
376             return false;
377         }
378     }
379 }
380
Popular Tags