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 JCheckBoxMenuItem}s.
048: *
049: * @author Kirill Grouchnikov
050: */
051: public class CheckBoxMenuItemIcon 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 CheckBoxMenuItemIcon(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, false);
091: // ComponentState currStateIgnoreSelection =
092: // ComponentState.getState(this.menuItem
093: // .getModel(), this.menuItem, true);
094: ComponentState prevState = SubstanceCoreUtilities
095: .getPrevSelComponentState(this .menuItem);
096:
097: float checkMarkVisibility = currState
098: .isKindActive(FadeKind.SELECTION) ? 10 : 0;
099: boolean isCheckMarkFadingOut = false;
100:
101: SubstanceTheme theme = SubstanceThemeUtilities.getTheme(
102: this .menuItem).getTheme(this .menuItem, currState,
103: currState == ComponentState.SELECTED);
104: SubstanceTheme theme2 = SubstanceThemeUtilities.getTheme(
105: this .menuItem).getTheme(this .menuItem, prevState,
106: prevState == ComponentState.SELECTED);
107: float cyclePos = 0;
108: FadeState fadeState = SubstanceFadeUtilities.getFadeState(
109: this .menuItem, FadeKind.SELECTION, FadeKind.ROLLOVER,
110: FadeKind.PRESS, FadeKind.ARM);
111: if (fadeState != null) {
112: cyclePos = fadeState.getFadePosition();
113: if (fadeState.isFadingIn())
114: cyclePos = 10 - cyclePos;
115: if (fadeState.fadeKind == FadeKind.SELECTION) {
116: checkMarkVisibility = fadeState.getFadePosition();
117: isCheckMarkFadingOut = !fadeState.isFadingIn();
118: }
119: }
120:
121: String key = currState.name() + ":" + prevState.name() + ":"
122: + theme.getDisplayName() + ":"
123: + theme2.getDisplayName() + ":" + cyclePos + ":"
124: + checkMarkVisibility;
125:
126: // System.out.println(key);
127:
128: Icon result = this .iconMap.get(key);
129: if (result != null)
130: return result;
131: result = new ImageIcon(SubstanceImageCreator.getCheckBox(
132: this .menuItem, this .size + 3, currState, theme, theme2,
133: cyclePos, checkMarkVisibility / 10.f,
134: isCheckMarkFadingOut));
135:
136: // if (this.menuItem.isSelected()) {
137: // System.out.println("Sel menu : " + currState + " : "
138: // + theme.getDisplayName());
139: // }
140:
141: this .iconMap.put(key, result);
142: return result;
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
149: * int, int)
150: */
151: public void paintIcon(Component c, Graphics g, int x, int y) {
152: Icon iconToDraw = this .getIconToPaint();
153: if (iconToDraw != null)
154: iconToDraw.paintIcon(c, g, x, y);
155: }
156:
157: /*
158: * (non-Javadoc)
159: *
160: * @see javax.swing.Icon#getIconWidth()
161: */
162: public int getIconWidth() {
163: return this .size + 2;
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see javax.swing.Icon#getIconHeight()
170: */
171: public int getIconHeight() {
172: return this .size + 2;
173: }
174: }
|