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.painter.decoration;
031:
032: import java.awt.*;
033:
034: import javax.swing.CellRendererPane;
035: import javax.swing.JComponent;
036:
037: import org.jvnet.lafwidget.LafWidgetUtilities;
038: import org.jvnet.lafwidget.layout.TransitionLayout;
039: import org.jvnet.substance.SubstanceLookAndFeel;
040: import org.jvnet.substance.theme.SubstanceTheme;
041: import org.jvnet.substance.utils.*;
042:
043: public class SubstanceDecorationUtilities {
044: private static final String DECORATION_AREA_TYPE = "substancelaf.internal.painter.decorationAreaType";
045:
046: public static void setDecorationType(JComponent comp,
047: DecorationAreaType type) {
048: comp.putClientProperty(DECORATION_AREA_TYPE, type);
049: }
050:
051: public static void clearDecorationType(JComponent comp) {
052: comp.putClientProperty(DECORATION_AREA_TYPE, null);
053: }
054:
055: public static DecorationAreaType getDecorationType(Component comp) {
056: Component c = comp;
057: while (c != null) {
058: if (c instanceof JComponent) {
059: JComponent jc = (JComponent) c;
060: Object prop = jc
061: .getClientProperty(DECORATION_AREA_TYPE);
062: if (prop instanceof DecorationAreaType)
063: return (DecorationAreaType) prop;
064: }
065: c = c.getParent();
066: }
067: return null;
068: }
069:
070: public static DecorationAreaType getImmediateDecorationType(
071: Component comp) {
072: Component c = comp;
073: if (c instanceof JComponent) {
074: JComponent jc = (JComponent) c;
075: Object prop = jc.getClientProperty(DECORATION_AREA_TYPE);
076: if (prop instanceof DecorationAreaType)
077: return (DecorationAreaType) prop;
078: }
079: return null;
080: }
081:
082: public static void paintDecorationBackground(Graphics g,
083: Component c, boolean force) {
084: //System.out.println("Painting " + c.getClass().getSimpleName());
085: boolean isInCellRenderer = (c.getParent() instanceof CellRendererPane);
086: boolean isPreviewMode = false;
087: if (c instanceof JComponent) {
088: isPreviewMode = (Boolean.TRUE
089: .equals(((JComponent) c)
090: .getClientProperty(LafWidgetUtilities.PREVIEW_MODE)));
091: }
092:
093: if (!force && !isPreviewMode && !c.isShowing()
094: && !isInCellRenderer) {
095: return;
096: }
097:
098: DecorationAreaType decorationType = getDecorationType(c);
099: // SubstanceTheme componentTheme = SubstanceCoreUtilities.getTheme(c,
100: // true, true, false);
101: SubstanceTheme decorationTheme = SubstanceThemeUtilities
102: .getDecorationTheme(c);
103: SubstanceDecorationPainter painter = SubstanceLookAndFeel
104: .getCurrentDecorationPainter();
105: //System.out.println("Type " + decorationType.name());
106:
107: //
108: // Color backgr = SubstanceCoreUtilities.getBackgroundFillColor(c);
109: // graphics.setColor(bzackgr);
110: // graphics.fillRect(0, 0, c.getWidth(), c.getHeight());
111: // if (!isPreviewMode
112: // && !isInCellRenderer
113: // && c.isShowing()
114: // && (SubstanceCoreUtilities.toDrawWatermark(c) ||
115: // SubstanceCoreUtilities
116: // .toBleedWatermark(c))) {
117: // SubstanceLookAndFeel.getCurrentWatermark().drawWatermarkImage(
118: // graphics, c, 0, 0, c.getWidth(), c.getHeight());
119: // }
120:
121: Graphics2D g2d = (Graphics2D) g.create();
122: painter.paintDecorationArea(g2d, c, decorationType, c
123: .getWidth(), c.getHeight(), decorationTheme);
124:
125: if (!isPreviewMode
126: && !isInCellRenderer
127: && c.isShowing()
128: && (SubstanceCoreUtilities.toDrawWatermark(c) || SubstanceCoreUtilities
129: .toBleedWatermark(c))) {
130: // paint the watermark over the component
131: SubstanceLookAndFeel.getCurrentWatermark()
132: .drawWatermarkImage(g2d, c, 0, 0, c.getWidth(),
133: c.getHeight());
134:
135: // paint the background second time with 50%
136: // translucency, making the watermark 'bleed' through.
137: g2d.setComposite(TransitionLayout.getAlphaComposite(c,
138: 0.5f, g));
139: painter.paintDecorationArea(g2d, c, decorationType, c
140: .getWidth(), c.getHeight(), decorationTheme);
141: }
142: g2d.dispose();
143: }
144: }
|