001: /*
002: * Copyright (c) 2005-2008 Flamingo / 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 Flamingo 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.flamingo.bcb.ui;
031:
032: import java.awt.AlphaComposite;
033: import java.awt.Graphics2D;
034: import java.awt.event.*;
035: import java.awt.geom.GeneralPath;
036: import java.awt.image.BufferedImage;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: import javax.swing.JComponent;
041: import javax.swing.plaf.ComponentUI;
042:
043: import org.jvnet.flamingo.bcb.ui.*;
044: import org.jvnet.lafwidget.animation.FadeKind;
045: import org.jvnet.lafwidget.animation.FadeTracker;
046: import org.jvnet.substance.SubstanceLookAndFeel;
047: import org.jvnet.substance.border.SubstanceBorderPainter;
048: import org.jvnet.substance.button.BaseButtonShaper;
049: import org.jvnet.substance.color.ColorScheme;
050: import org.jvnet.substance.painter.SubstanceGradientPainter;
051: import org.jvnet.substance.utils.ComponentState;
052: import org.jvnet.substance.utils.SubstanceCoreUtilities;
053: import org.jvnet.substance.utils.ComponentState.ColorSchemeKind;
054:
055: /**
056: * UI delegate for breadcrumb particle.
057: *
058: * @author Kirill Grouchnikov
059: */
060: public class SubstanceBreadcrumbParticleUI extends
061: BasicBreadcrumbParticleUI {
062: /**
063: * Background images.
064: */
065: private static Map<String, BufferedImage> backgrounds = new HashMap<String, BufferedImage>();
066:
067: /**
068: * Mouse listener for fading animations.
069: */
070: protected MouseListener fadeMouseListener;
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
076: */
077: public static ComponentUI createUI(JComponent c) {
078: return new SubstanceBreadcrumbParticleUI();
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.jvnet.flamingo.bcb.ui.BasicBreadcrumbParticleUI#installListeners(org.jvnet.flamingo.bcb.BreadcrumbParticle)
085: */
086: @Override
087: protected void installListeners(final BreadcrumbParticle particle) {
088: super .installListeners(particle);
089: this .fadeMouseListener = new MouseAdapter() {
090: @Override
091: public void mouseEntered(MouseEvent e) {
092: FadeTracker.getInstance().trackFadeIn(
093: FadeKind.ROLLOVER, particle, false, null);
094: }
095:
096: @Override
097: public void mouseExited(MouseEvent e) {
098: FadeTracker.getInstance().trackFadeOut(
099: FadeKind.ROLLOVER, particle, false, null);
100: }
101: };
102: particle.addMouseListener(this .fadeMouseListener);
103: }
104:
105: /*
106: * (non-Javadoc)
107: *
108: * @see org.jvnet.flamingo.bcb.ui.BasicBreadcrumbParticleUI#uninstallListeners(org.jvnet.flamingo.bcb.BreadcrumbParticle)
109: */
110: @Override
111: protected void uninstallListeners(BreadcrumbParticle particle) {
112: particle.removeMouseListener(this .fadeMouseListener);
113: this .fadeMouseListener = null;
114: super .uninstallListeners(particle);
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.jvnet.flamingo.bcb.ui.BasicBreadcrumbParticleUI#paintSelectedBackground(java.awt.Graphics2D,
121: * javax.swing.JComponent, org.jvnet.flamingo.bcb.ChoicesSelector)
122: */
123: @Override
124: protected void paintSelectedBackground(Graphics2D graphics,
125: JComponent c, ChoicesSelector selector) {
126: Graphics2D g2 = (Graphics2D) graphics.create();
127: try {
128: BreadcrumbParticle particle = (BreadcrumbParticle) c;
129: ComponentState state = ComponentState.getState(particle
130: .getModel(), null);
131: ComponentState.ColorSchemeKind kind = state
132: .getColorSchemeKind();
133: // compute cycle count (for animation)
134: int cyclePos = state.getCycleCount();
135: // compute color scheme
136: ColorScheme colorScheme = null;
137: if (kind == ColorSchemeKind.DISABLED) {
138: colorScheme = SubstanceLookAndFeel
139: .getDisabledColorScheme();
140: } else {
141: if (kind == ColorSchemeKind.CURRENT) {
142: colorScheme = SubstanceLookAndFeel
143: .getActiveColorScheme();
144: } else {
145: colorScheme = SubstanceLookAndFeel
146: .getDefaultColorScheme();
147: }
148: }
149:
150: // colorScheme = state.isEnabled() ? SubstanceLookAndFeel.getTheme()
151: // .getColorScheme() : SubstanceLookAndFeel
152: // .getDisabledColorScheme();
153:
154: int width = c.getWidth();
155: int height = c.getHeight();
156: SubstanceGradientPainter painter = SubstanceLookAndFeel
157: .getCurrentGradientPainter();
158: SubstanceBorderPainter borderPainter = SubstanceCoreUtilities
159: .getBorderPainter(selector);
160:
161: ComponentState selectorState = (selector == null) ? ComponentState.DEFAULT
162: : ComponentState
163: .getState(selector.getModel(), null);
164: // System.out.println("Selector : " + selectorState.name());
165: ComponentState.ColorSchemeKind selectorKind = selectorState
166: .getColorSchemeKind();
167: String key = width + ":" + height + ":" + kind.name() + ":"
168: + cyclePos + ":"
169: + SubstanceCoreUtilities.getSchemeId(colorScheme)
170: + ":" + painter.getDisplayName() + ":"
171: + borderPainter.getDisplayName() + ":"
172: + selectorKind.name();
173: if (!backgrounds.containsKey(key)) {
174: BufferedImage newBackground;
175:
176: int totalWidth = width;
177: if (selectorKind == ComponentState.ColorSchemeKind.CURRENT) {
178: totalWidth += 10;
179: }
180:
181: GeneralPath contour = BaseButtonShaper.getBaseOutline(
182: totalWidth, height, 0.0f, null);
183: newBackground = painter
184: .getContourBackground(totalWidth, height,
185: contour, false, colorScheme,
186: colorScheme, cyclePos, true, false);
187: borderPainter.paintBorder(newBackground.getGraphics(),
188: c, totalWidth, height, contour, null,
189: colorScheme, colorScheme, cyclePos, false);
190:
191: backgrounds.put(key, newBackground);
192: }
193:
194: BufferedImage background = backgrounds.get(key);
195: float comp = 0.8f;
196: if (FadeTracker.getInstance().isTracked(particle,
197: FadeKind.ROLLOVER)
198: && !state.isKindActive(FadeKind.SELECTION)
199: && state.isKindActive(FadeKind.ENABLE)) {
200: float fadeCoef = FadeTracker.getInstance().getFade10(
201: particle, FadeKind.ROLLOVER);
202: g2.setComposite(AlphaComposite.getInstance(
203: AlphaComposite.SRC_OVER, comp * fadeCoef
204: / 10.0f));
205: } else {
206: if (state == ComponentState.DEFAULT)
207: return;
208: g2.setComposite(AlphaComposite.getInstance(
209: AlphaComposite.SRC_OVER, comp));
210: }
211: g2.setClip(0, 0, width, height);
212: if (state != ComponentState.DISABLED_UNSELECTED)
213: g2.drawImage(background, 0, 0, null);
214: } finally {
215: g2.dispose();
216: }
217:
218: }
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see org.jvnet.flamingo.bcb.ui.BasicBreadcrumbParticleUI#paintSelectedForeground(java.awt.Graphics2D,
224: * javax.swing.JComponent)
225: */
226: @Override
227: protected void paintSelectedForeground(Graphics2D g, JComponent c) {
228: }
229: }
|