KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AquaEditorTabCellRenderer.java
21  *
22  * Created on December 28, 2003, 12:04 AM
23  */

24
25 package org.netbeans.swing.tabcontrol.plaf;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Graphics2D JavaDoc;
31 import java.awt.Insets JavaDoc;
32 import java.awt.Polygon JavaDoc;
33 import java.awt.Rectangle JavaDoc;
34 import javax.swing.Icon JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36
37 /**
38  * A tab cell renderer for OS-X. Basically does its work by subclassing JButton
39  * and doing some tricks to use it as a cell renderer, so the Aqua borders do
40  * all the heavy eye-candy lifting (how's that for mixed metaphors?)
41  *
42  * @author Tim Boudreau
43  */

44 final class AquaEditorTabCellRenderer extends AbstractTabCellRenderer {
45     private static final AquaTabPainter AquaTabPainter = new AquaTabPainter();
46     
47     static final int TOP_INSET = 0;
48     static final int LEFT_INSET = 3;
49     static final int RIGHT_INSET = 6;
50     static final int BOTTOM_INSET = 2;
51     
52     
53     private static final ChicletWrapper chiclet = new ChicletWrapper();
54     
55     public AquaEditorTabCellRenderer() {
56         super(AquaTabPainter, AquaTabPainter, AquaTabPainter,
57                 new Dimension JavaDoc(23, 8));
58     }
59     
60     protected int getCaptionYAdjustment() {
61         return 0;
62     }
63     
64     protected int getIconYAdjustment() {
65         return -1;
66     }
67     
68     public Dimension JavaDoc getPadding() {
69         Dimension JavaDoc d = super.getPadding();
70         d.width = isShowCloseButton() && !Boolean.getBoolean("nb.tabs.suppressCloseButton") ? 26 : 13;
71         return d;
72     }
73     
74     private static class AquaTabPainter implements TabPainter {
75         private static Insets JavaDoc insets = new Insets JavaDoc(TOP_INSET, LEFT_INSET,
76                 BOTTOM_INSET, RIGHT_INSET);
77         
78         public AquaTabPainter() {
79         }
80         
81         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
82             boolean leftmost = ((AquaEditorTabCellRenderer) c).isLeftmost();
83             
84             if (leftmost) {
85                 return new Insets JavaDoc(TOP_INSET, LEFT_INSET + 4, BOTTOM_INSET,
86                         RIGHT_INSET);
87             } else {
88                 return insets;
89             }
90         }
91         
92         public void getCloseButtonRectangle(JComponent JavaDoc jc, Rectangle JavaDoc rect,
93                 Rectangle JavaDoc bounds) {
94             boolean rightClip = ((AquaEditorTabCellRenderer) jc).isClipRight();
95             boolean leftClip = ((AquaEditorTabCellRenderer) jc).isClipLeft();
96             boolean notSupported = !((AbstractTabCellRenderer) jc).isShowCloseButton();
97             if (leftClip || rightClip || notSupported) {
98                 rect.x = -100;
99                 rect.y = -100;
100                 rect.width = 0;
101                 rect.height = 0;
102             } else {
103                 String JavaDoc iconPath = findIconPath((AquaEditorTabCellRenderer) jc);
104                 Icon JavaDoc icon = TabControlButtonFactory.getIcon(iconPath);
105                 int iconWidth = icon.getIconWidth();
106                 int iconHeight = icon.getIconHeight();
107                 rect.x = bounds.x + bounds.width - iconWidth - 2;
108                 rect.y = bounds.y + (Math.max(0, bounds.height / 2 - iconHeight / 2));
109                 rect.width = iconWidth;
110                 rect.height = iconHeight;
111             }
112         }
113         
114         
115         /**
116          * Returns path of icon which is correct for currect state of tab at given
117          * index
118          */

119         private String JavaDoc findIconPath( AquaEditorTabCellRenderer renderer ) {
120             if( renderer.inCloseButton() && renderer.isPressed() ) {
121                 return "org/netbeans/swing/tabcontrol/resources/mac_close_pressed.png"; // NOI18N
122
}
123             if( renderer.inCloseButton() ) {
124                 return "org/netbeans/swing/tabcontrol/resources/mac_close_rollover.png"; // NOI18N
125
}
126             return "org/netbeans/swing/tabcontrol/resources/mac_close_enabled.png"; // NOI18N
127
}
128         
129         private void paintCloseButton(Graphics JavaDoc g, JComponent JavaDoc c) {
130             if (((AbstractTabCellRenderer) c).isShowCloseButton()) {
131                 
132                 Rectangle JavaDoc r = new Rectangle JavaDoc(0, 0, c.getWidth(), c.getHeight());
133                 Rectangle JavaDoc cbRect = new Rectangle JavaDoc();
134                 getCloseButtonRectangle((JComponent JavaDoc) c, cbRect, r);
135                 
136                 //paint close button
137
String JavaDoc iconPath = findIconPath( (AquaEditorTabCellRenderer)c );
138                 Icon JavaDoc icon = TabControlButtonFactory.getIcon( iconPath );
139                 icon.paintIcon(c, g, cbRect.x, cbRect.y);
140             }
141         }
142         
143         public Polygon JavaDoc getInteriorPolygon(Component JavaDoc c) {
144             return new Polygon JavaDoc(new int[]{0, c.getWidth(), c.getWidth(), 0}, new int[]{
145                 0, 0, c.getHeight(), c.getHeight()}, 4);
146         }
147         
148         public boolean isBorderOpaque() {
149             return false;
150         }
151         
152         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
153                 int width, int height) {
154             paintCloseButton(g, (JComponent JavaDoc) c);
155         }
156         
157         
158         public void paintInterior(Graphics JavaDoc g, Component JavaDoc c) {
159             if (true) {
160                 Rectangle JavaDoc bds = c.getBounds();
161                 
162                 boolean rightmost = ((AquaEditorTabCellRenderer) c).isRightmost();
163                 boolean rightClip = ((AquaEditorTabCellRenderer) c).isClipRight();
164                 boolean sel = ((AquaEditorTabCellRenderer) c).isSelected();
165                 boolean active = ((AquaEditorTabCellRenderer) c).isActive();
166                 boolean pressed = ((AquaEditorTabCellRenderer) c).isPressed();
167                 boolean leftClip = ((AquaEditorTabCellRenderer) c).isClipLeft();
168                 boolean leftmost = ((AquaEditorTabCellRenderer) c).isLeftmost();
169                 boolean closing = pressed
170                         && ((AquaEditorTabCellRenderer) c).inCloseButton();
171                 boolean attention = !pressed && !closing
172                         && ((AquaEditorTabCellRenderer) c).isAttention();
173                 
174                 //add in a pixel for rightmost/leftmost so we don't clip off
175
//antialiasing of the curve
176
chiclet.setBounds(0, 0, bds.width, bds.height);
177                 
178                 chiclet.setNotch(rightClip, leftClip);
179                 int state = 0;
180                 float leftarc = leftmost && !leftClip ? 0.5f : 0f;
181                 float rightarc = rightmost && !rightClip ? 0.5f : 0f;
182                 
183                 if (pressed && (rightClip || leftClip)) {
184                     state |= GenericGlowingChiclet.STATE_PRESSED;
185                 }
186                 if (active) {
187                     state |= GenericGlowingChiclet.STATE_ACTIVE;
188                 }
189                 if (sel) {
190                     state |= GenericGlowingChiclet.STATE_SELECTED;
191                 }
192                 if (closing) {
193                     state |= GenericGlowingChiclet.STATE_CLOSING;
194                 }
195                 if (attention) {
196                     state |= GenericGlowingChiclet.STATE_ATTENTION;
197                 }
198                 chiclet.setArcs(leftarc, rightarc, leftarc, rightarc);
199                 
200                 chiclet.setState(state);
201                 chiclet.draw((Graphics2D JavaDoc) g);
202                 return;
203             }
204         }
205         
206         public boolean supportsCloseButton(JComponent JavaDoc c) {
207             boolean leftClip = ((AquaEditorTabCellRenderer) c).isClipLeft();
208             boolean rightClip = ((AquaEditorTabCellRenderer) c).isClipRight();
209             boolean supported = ((AquaEditorTabCellRenderer) c).isShowCloseButton();
210             return !leftClip && !rightClip && supported;
211         }
212         
213     }
214 }
215
Popular Tags