001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import org.netbeans.lib.profiler.ui.UIUtils;
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import javax.swing.Icon;
047: import javax.swing.JRadioButton;
048: import javax.swing.UIManager;
049:
050: /**
051: *
052: * @author Jiri Sedlacek
053: */
054: public class JExtendedRadioButton extends JRadioButton {
055: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
056:
057: public static class DoubleIcon implements Icon {
058: //~ Instance fields ------------------------------------------------------------------------------------------------------
059:
060: private Icon icon1;
061: private Icon icon2;
062: private int icon1VertOffset = 0;
063: private int icon2HorzOffset;
064: private int icon2VertOffset = 0;
065: private int iconHeight;
066: private int iconWidth;
067: private int iconsGap;
068:
069: //~ Constructors ---------------------------------------------------------------------------------------------------------
070:
071: public DoubleIcon(Icon icon1, Icon icon2, int iconsGap) {
072: this .icon1 = icon1;
073: this .icon2 = icon2;
074: this .iconsGap = iconsGap;
075:
076: initInternals();
077: }
078:
079: //~ Methods --------------------------------------------------------------------------------------------------------------
080:
081: public Icon getIcon1() {
082: return icon1;
083: }
084:
085: public Icon getIcon2() {
086: return icon2;
087: }
088:
089: public int getIconHeight() {
090: return iconHeight;
091: }
092:
093: public int getIconWidth() {
094: return iconWidth;
095: }
096:
097: public int getIconsGap() {
098: return iconsGap;
099: }
100:
101: public void paintIcon(Component c, Graphics g, int x, int y) {
102: icon1.paintIcon(c, g, x, y + icon1VertOffset);
103: icon2.paintIcon(c, g, x + icon2HorzOffset, y
104: + icon2VertOffset);
105: }
106:
107: private void initInternals() {
108: int icon1Width = icon1.getIconWidth();
109: int icon1Height = icon1.getIconHeight();
110: int icon2Height = icon2.getIconHeight();
111:
112: iconWidth = icon1Width + icon2.getIconWidth() + iconsGap;
113: iconHeight = Math.max(icon1Height, icon2Height);
114:
115: if (icon1Height > icon2Height) {
116: icon2VertOffset = (int) Math
117: .ceil((float) (icon1Height - icon2Height)
118: / (float) 2);
119: } else if (icon1Height < icon2Height) {
120: icon1VertOffset = (int) Math
121: .ceil((float) (icon2Height - icon1Height)
122: / (float) 2);
123: }
124:
125: icon2HorzOffset = icon1Width + iconsGap;
126: }
127: }
128:
129: //~ Instance fields ----------------------------------------------------------------------------------------------------------
130:
131: private Icon extraIcon;
132:
133: //~ Constructors -------------------------------------------------------------------------------------------------------------
134:
135: public JExtendedRadioButton(Icon extraIcon) {
136: super ();
137: setExtraIcon(extraIcon);
138: }
139:
140: public JExtendedRadioButton(String text) {
141: super (text);
142: }
143:
144: public JExtendedRadioButton(String text, Icon extraIcon) {
145: this (text);
146: setExtraIcon(extraIcon);
147: }
148:
149: //~ Methods ------------------------------------------------------------------------------------------------------------------
150:
151: public void setExtraIcon(Icon extraIcon) {
152: if (!isSupportedLaF()) {
153: return;
154: }
155:
156: this .extraIcon = extraIcon;
157:
158: if (extraIcon != null) {
159: createExtraIcon();
160: } else {
161: resetExtraIcon();
162: }
163: }
164:
165: public Icon getExtraIcon() {
166: return extraIcon;
167: }
168:
169: private static Icon getDefaultIcon() {
170: return UIManager.getIcon("RadioButton.icon"); // NOI18N
171: }
172:
173: private static Icon getDisabledIconSafe(JRadioButton radio) {
174: Icon icon = radio.getIcon();
175:
176: if (icon == null) {
177: return getDefaultIcon();
178: }
179:
180: Icon disabledIcon = radio.getDisabledIcon();
181:
182: return (disabledIcon != null) ? disabledIcon
183: : getIconSafe(radio);
184: }
185:
186: private static Icon getDisabledSelectedIconSafe(JRadioButton radio) {
187: Icon icon = radio.getIcon();
188:
189: if (icon == null) {
190: return getDefaultIcon();
191: }
192:
193: Icon disabledSelectedIcon = radio.getDisabledSelectedIcon();
194:
195: return (disabledSelectedIcon != null) ? disabledSelectedIcon
196: : getIconSafe(radio);
197: }
198:
199: private static Icon getIconSafe(JRadioButton radio) {
200: Icon icon = radio.getIcon();
201:
202: return (icon != null) ? icon : getDefaultIcon();
203: }
204:
205: private static Icon getPressedIconSafe(JRadioButton radio) {
206: Icon icon = radio.getIcon();
207:
208: if (icon == null) {
209: return getDefaultIcon();
210: }
211:
212: Icon pressedIcon = radio.getPressedIcon();
213:
214: if (pressedIcon == null) {
215: pressedIcon = radio.getSelectedIcon();
216: }
217:
218: return (pressedIcon != null) ? pressedIcon : getIconSafe(radio);
219: }
220:
221: private static Icon getRolloverIconSafe(JRadioButton radio) {
222: Icon icon = radio.getIcon();
223:
224: if (icon == null) {
225: return getDefaultIcon();
226: }
227:
228: Icon rolloverIcon = radio.getRolloverIcon();
229:
230: return (rolloverIcon != null) ? rolloverIcon
231: : getIconSafe(radio);
232: }
233:
234: private static Icon getRolloverSelectedIconSafe(JRadioButton radio) {
235: Icon icon = radio.getIcon();
236:
237: if (icon == null) {
238: return getDefaultIcon();
239: }
240:
241: Icon rolloverSelectedIcon = radio.getRolloverSelectedIcon();
242:
243: if (rolloverSelectedIcon == null) {
244: rolloverSelectedIcon = radio.getSelectedIcon();
245: }
246:
247: return (rolloverSelectedIcon != null) ? rolloverSelectedIcon
248: : getIconSafe(radio);
249: }
250:
251: private static Icon getSelectedIconSafe(JRadioButton radio) {
252: Icon icon = radio.getIcon();
253:
254: if (icon == null) {
255: return getDefaultIcon();
256: }
257:
258: Icon selectedIcon = radio.getSelectedIcon();
259:
260: return (selectedIcon != null) ? selectedIcon
261: : getIconSafe(radio);
262: }
263:
264: private static boolean isSupportedLaF() {
265: return !UIUtils.isGTKLookAndFeel()
266: && !UIUtils.isAquaLookAndFeel();
267: }
268:
269: private void createExtraIcon() {
270: JRadioButton reference = new JRadioButton();
271: int iconTextGap = reference.getIconTextGap();
272:
273: Icon disabledIcon = getDisabledIconSafe(reference);
274: Icon disabledSelectedIcon = getDisabledSelectedIconSafe(reference);
275: Icon icon = getIconSafe(reference);
276: Icon pressedIcon = getPressedIconSafe(reference);
277: Icon rolloverIcon = getRolloverIconSafe(reference);
278: Icon rolloverSelectedIcon = getRolloverSelectedIconSafe(reference);
279: Icon selectedIcon = getSelectedIconSafe(reference);
280:
281: setDisabledIcon((disabledIcon == null) ? extraIcon
282: : new DoubleIcon(disabledIcon, extraIcon, iconTextGap));
283: setDisabledSelectedIcon((disabledSelectedIcon == null) ? extraIcon
284: : new DoubleIcon(disabledSelectedIcon, extraIcon,
285: iconTextGap));
286: setIcon((icon == null) ? extraIcon : new DoubleIcon(icon,
287: extraIcon, iconTextGap));
288: setPressedIcon((pressedIcon == null) ? extraIcon
289: : new DoubleIcon(pressedIcon, extraIcon, iconTextGap));
290: setRolloverIcon((rolloverIcon == null) ? extraIcon
291: : new DoubleIcon(rolloverIcon, extraIcon, iconTextGap));
292: setRolloverSelectedIcon((rolloverSelectedIcon == null) ? extraIcon
293: : new DoubleIcon(rolloverSelectedIcon, extraIcon,
294: iconTextGap));
295: setSelectedIcon((selectedIcon == null) ? extraIcon
296: : new DoubleIcon(selectedIcon, extraIcon, iconTextGap));
297: }
298:
299: private void resetExtraIcon() {
300: JRadioButton reference = new JRadioButton();
301:
302: setDisabledIcon(reference.getDisabledIcon());
303: setDisabledSelectedIcon(reference.getDisabledSelectedIcon());
304: setIcon(reference.getIcon());
305: setPressedIcon(reference.getPressedIcon());
306: setRolloverIcon(reference.getRolloverIcon());
307: setRolloverSelectedIcon(reference.getRolloverSelectedIcon());
308: setSelectedIcon(reference.getSelectedIcon());
309: }
310: }
|