001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.utils.icon;
031:
032: import java.awt.Component;
033: import java.awt.Graphics;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import javax.swing.*;
038: import javax.swing.plaf.UIResource;
039:
040: import org.jvnet.lafwidget.animation.FadeKind;
041: import org.jvnet.lafwidget.animation.FadeState;
042: import org.jvnet.substance.SubstanceImageCreator;
043: import org.jvnet.substance.theme.SubstanceTheme;
044: import org.jvnet.substance.utils.*;
045:
046: /**
047: * Icon for the {@link JRadioButtonMenuItem}s.
048: *
049: * @author Kirill Grouchnikov
050: */
051: public class RadioButtonMenuItemIcon implements Icon, UIResource {
052: /**
053: * The size of <code>this</code> icon.
054: */
055: private int size;
056:
057: /**
058: * The associated menu item.
059: */
060: private JMenuItem menuItem;
061:
062: /**
063: * Icon cache to speed up the painting.
064: */
065: private Map<String, Icon> iconMap;
066:
067: /**
068: * Creates a new icon.
069: *
070: * @param menuItem
071: * The corresponding menu item.
072: * @param size
073: * The size of <code>this</code> icon.
074: */
075: public RadioButtonMenuItemIcon(JMenuItem menuItem, int size) {
076: this .menuItem = menuItem;
077: this .size = size;
078: this .iconMap = new HashMap<String, Icon>();
079: }
080:
081: /**
082: * Returns the current icon to paint.
083: *
084: * @return Icon to paint.
085: */
086: private synchronized Icon getIconToPaint() {
087: if (this .menuItem == null)
088: return null;
089: ComponentState currState = ComponentState.getState(
090: this .menuItem.getModel(), this .menuItem);
091: ComponentState prevState = SubstanceCoreUtilities
092: .getPrevSelComponentState(this .menuItem);
093:
094: float visibility = currState.isKindActive(FadeKind.SELECTION) ? 10
095: : 0;
096:
097: SubstanceTheme theme = SubstanceThemeUtilities.getTheme(
098: this .menuItem).getTheme(this .menuItem, currState,
099: currState == ComponentState.SELECTED);
100: SubstanceTheme theme2 = SubstanceThemeUtilities.getTheme(
101: this .menuItem).getTheme(this .menuItem, prevState,
102: prevState == ComponentState.SELECTED);
103: float cyclePos = 0;
104: FadeState fadeState = SubstanceFadeUtilities.getFadeState(
105: this .menuItem, FadeKind.SELECTION, FadeKind.ROLLOVER,
106: FadeKind.PRESS, FadeKind.ARM);
107: if (fadeState != null) {
108: cyclePos = fadeState.getFadePosition();
109: if (fadeState.isFadingIn())
110: cyclePos = 10 - cyclePos;
111: if (fadeState.fadeKind == FadeKind.SELECTION) {
112: visibility = fadeState.getFadePosition();
113: }
114: }
115:
116: String key = currState.name() + ":" + prevState.name() + ":"
117: + theme.getDisplayName() + ":"
118: + theme2.getDisplayName() + ":" + cyclePos + ":"
119: + visibility;
120:
121: // System.out.println(key);
122:
123: Icon result = this .iconMap.get(key);
124: if (result != null)
125: return result;
126: result = new ImageIcon(SubstanceImageCreator.getRadioButton(
127: this .menuItem, this .size, currState, 2, theme, theme2,
128: cyclePos, visibility / 10.f));
129:
130: this .iconMap.put(key, result);
131: return result;
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
138: * int, int)
139: */
140: public void paintIcon(Component c, Graphics g, int x, int y) {
141: Icon iconToDraw = this .getIconToPaint();
142: if (iconToDraw != null)
143: iconToDraw.paintIcon(c, g, x, y);
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see javax.swing.Icon#getIconWidth()
150: */
151: public int getIconWidth() {
152: return this .size + 2;
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see javax.swing.Icon#getIconHeight()
159: */
160: public int getIconHeight() {
161: return this .size + 2;
162: }
163: }
|