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.*;
035:
036: import org.jvnet.substance.color.ColorScheme;
037: import org.jvnet.substance.theme.SubstanceTheme;
038: import org.jvnet.substance.utils.SubstanceCoreUtilities;
039:
040: /**
041: * Implementation of {@link SubstanceDecorationPainter} that uses matte painting
042: * on decoration areas.
043: *
044: * @author Kirill Grouchnikov
045: * @since version 4.3
046: */
047: public class MatteDecorationPainter implements
048: SubstanceDecorationPainter {
049: /**
050: * The display name for the decoration painters of this class.
051: */
052: public static final String DISPLAY_NAME = "Matte";
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.jvnet.substance.utils.SubstanceTrait#getDisplayName()
058: */
059: public String getDisplayName() {
060: return DISPLAY_NAME;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see org.jvnet.substance.painter.decoration.SubstanceDecorationPainter#paintDecorationArea(java.awt.Graphics2D,
067: * java.awt.Component,
068: * org.jvnet.substance.painter.decoration.DecorationAreaType, int, int,
069: * org.jvnet.substance.theme.SubstanceTheme)
070: */
071: public void paintDecorationArea(Graphics2D graphics,
072: Component comp, DecorationAreaType decorationAreaType,
073: int width, int height, SubstanceTheme theme) {
074: switch (decorationAreaType) {
075: case PRIMARY_TITLE_PANE:
076: case SECONDARY_TITLE_PANE:
077: this .paintTitleBackground(graphics, comp, width, height,
078: theme);
079: break;
080: default:
081: this .paintExtraBackground(graphics, SubstanceCoreUtilities
082: .getHeaderParent(comp), comp, width, height, theme);
083: }
084: }
085:
086: private void paintTitleBackground(Graphics2D graphics,
087: Component comp, int width, int height, SubstanceTheme theme) {
088: ColorScheme scheme = theme.getColorScheme();
089: Graphics2D temp = (Graphics2D) graphics.create();
090: this .fill(temp, scheme, 0, 0, 0, width, height);
091: temp.dispose();
092: }
093:
094: private void paintExtraBackground(Graphics2D graphics,
095: Container parent, Component comp, int width, int height,
096: SubstanceTheme theme) {
097:
098: JRootPane rootPane = SwingUtilities.getRootPane(parent);
099: // fix for bug 234 - Window doesn't have a root pane.
100: int dx = 0;
101: int dy = 0;
102: JComponent titlePane = null;
103:
104: if (rootPane != null) {
105: titlePane = SubstanceCoreUtilities.getTitlePane(rootPane);
106:
107: if (titlePane != null) {
108: if (comp.isShowing() && titlePane.isShowing()) {
109: dx += (comp.getLocationOnScreen().x - titlePane
110: .getLocationOnScreen().x);
111: dy += (comp.getLocationOnScreen().y - titlePane
112: .getLocationOnScreen().y);
113: } else {
114: // have to traverse the hierarchy
115: Component c = comp;
116: dx = 0;
117: dy = 0;
118: while (c != rootPane) {
119: dx += c.getX();
120: dy += c.getY();
121: c = c.getParent();
122: }
123: c = titlePane;
124: while (c != rootPane) {
125: dx -= c.getX();
126: dy -= c.getY();
127: c = c.getParent();
128: }
129: }
130: }
131: }
132:
133: // int pWidth = (titlePane == null) ? parent.getWidth() : titlePane
134: // .getWidth();
135: // int pHeight = parent.getHeight();
136: //
137: ColorScheme scheme = theme.getColorScheme();
138: Graphics2D temp = (Graphics2D) graphics.create();
139: this .fill(temp, scheme, dy, 0, 0, width, height);
140: temp.dispose();
141: }
142:
143: /**
144: * Fills the relevant part with the gradient fill.
145: *
146: * @param graphics
147: * Graphics.
148: * @param scheme
149: * Color scheme to use.
150: * @param offsetY
151: * Vertical offset.
152: * @param x
153: * X coordinate of the fill area.
154: * @param y
155: * Y coordinate of the fill area.
156: * @param width
157: * Fill area width.
158: * @param height
159: * Fill area height.
160: */
161: protected void fill(Graphics2D graphics, ColorScheme scheme,
162: int offsetY, int x, int y, int width, int height) {
163: int bandHeight = 20;
164: // 0 - 20 : ultralight -> extra light
165: // 20 - 40 : extra light -> light
166: // 40 - 60 : light -> medium
167: // 60 - 80 : medium -> dark
168: // 80 - 100 : dark -> ultra dark
169: // 100 - : ultra dark fill
170:
171: int startY = y + offsetY;
172: int endY = y + offsetY + height;
173:
174: int currStart = 0;
175: int currEnd = bandHeight;
176:
177: if (currEnd >= startY) {
178: graphics.setPaint(new GradientPaint(x, currStart - offsetY,
179: scheme.getUltraLightColor(), x, currEnd - offsetY,
180: scheme.getExtraLightColor()));
181: graphics
182: .fillRect(x, currStart - offsetY, width, bandHeight);
183: }
184: currStart += bandHeight;
185: currEnd += bandHeight;
186:
187: if (currStart > endY)
188: return;
189:
190: if (currEnd >= startY) {
191: graphics.setPaint(new GradientPaint(x, currStart - offsetY,
192: scheme.getExtraLightColor(), x, currEnd - offsetY,
193: scheme.getLightColor()));
194: graphics
195: .fillRect(x, currStart - offsetY, width, bandHeight);
196: }
197: currStart += bandHeight;
198: currEnd += bandHeight;
199:
200: if (currStart > endY)
201: return;
202:
203: if (currEnd >= startY) {
204: graphics.setPaint(new GradientPaint(x, currStart - offsetY,
205: scheme.getLightColor(), x, currEnd - offsetY,
206: scheme.getMidColor()));
207: graphics
208: .fillRect(x, currStart - offsetY, width, bandHeight);
209: }
210: currStart += bandHeight;
211: currEnd += bandHeight;
212:
213: if (currStart > endY)
214: return;
215:
216: if (currEnd >= startY) {
217: graphics.setPaint(new GradientPaint(x, currStart - offsetY,
218: scheme.getMidColor(), x, currEnd - offsetY, scheme
219: .getDarkColor()));
220: graphics
221: .fillRect(x, currStart - offsetY, width, bandHeight);
222: }
223: currStart += bandHeight;
224: currEnd += bandHeight;
225:
226: if (currStart > endY)
227: return;
228:
229: if (currEnd >= startY) {
230: graphics.setPaint(new GradientPaint(x, currStart - offsetY,
231: scheme.getDarkColor(), x, currEnd - offsetY, scheme
232: .getUltraDarkColor()));
233: graphics
234: .fillRect(x, currStart - offsetY, width, bandHeight);
235: }
236: currStart += bandHeight;
237: currEnd += bandHeight;
238:
239: if (currStart > endY)
240: return;
241:
242: graphics.setColor(scheme.getUltraDarkColor());
243: graphics.fillRect(x, currStart - offsetY, width, endY
244: - currStart + offsetY);
245: }
246: }
|