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.common.ui;
031:
032: import java.awt.Graphics;
033: import java.awt.Graphics2D;
034: import java.awt.image.BufferedImage;
035:
036: import javax.swing.AbstractButton;
037: import javax.swing.JComponent;
038: import javax.swing.plaf.ComponentUI;
039: import javax.swing.plaf.basic.BasicButtonUI;
040:
041: import org.jvnet.flamingo.common.JButtonStrip.StripOrientation;
042: import org.jvnet.flamingo.common.ui.BasicButtonStripUI;
043: import org.jvnet.substance.SubstanceBorder;
044: import org.jvnet.substance.SubstanceImageCreator;
045: import org.jvnet.substance.border.SubstanceBorderPainter;
046: import org.jvnet.substance.button.SubstanceButtonShaper;
047: import org.jvnet.substance.painter.SubstanceGradientPainter;
048: import org.jvnet.substance.utils.ButtonBackgroundDelegate;
049: import org.jvnet.substance.utils.SubstanceCoreUtilities;
050:
051: /**
052: * UI for button strips in <b>Substance</b> look and feel.
053: *
054: * @author Kirill Grouchnikov
055: */
056: public class SubstanceButtonStripUI extends BasicButtonStripUI {
057: /*
058: * (non-Javadoc)
059: *
060: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
061: */
062: public static ComponentUI createUI(JComponent c) {
063: return new SubstanceButtonStripUI();
064: }
065:
066: /**
067: * Simple constructor.
068: */
069: public SubstanceButtonStripUI() {
070: super ();
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.jvnet.flamingo.common.ui.BasicButtonStripUI#paintStripButtonBackground(java.awt.Graphics,
077: * javax.swing.AbstractButton, boolean, boolean, int, int)
078: */
079: @Override
080: protected void paintStripButtonBackground(Graphics g,
081: AbstractButton button, boolean isFirst, boolean isLast,
082: int totalStripDimension, int relativeOffset) {
083: // System.out.println("background [" + button.hashCode() + "] " +
084: // isFirst
085: // + " " + isLast);
086: button.setOpaque(false);
087:
088: int width = button.getWidth();
089: int height = button.getHeight();
090:
091: SubstanceGradientPainter painter = SubstanceCoreUtilities
092: .getGradientPainter(button);
093: SubstanceBorderPainter borderPainter = SubstanceCoreUtilities
094: .getBorderPainter(button);
095:
096: SubstanceButtonShaper shaper = SubstanceCoreUtilities
097: .getButtonShaper(button);
098:
099: Graphics2D graphics = (Graphics2D) g.create();
100: graphics.setClip(0, 0, width, height);
101:
102: if (this .buttonStrip.getOrientation() == StripOrientation.HORIZONTAL) {
103: // create background that is strip wide
104: BufferedImage bigImage = ButtonBackgroundDelegate
105: .getBackground(button, shaper, painter,
106: borderPainter, totalStripDimension, height).backgroundImage;
107:
108: // three distinct cases - leftmost, rightmost, and all the rest
109: if (isFirst) {
110: graphics.drawImage(bigImage, 0, 0, width, height, 0, 0,
111: width, height, null);
112: } else {
113: if (isLast) {
114: // create background that is strip wide and take its
115: // right part
116: graphics.drawImage(bigImage, 0, 0, width, height,
117: relativeOffset, 0, relativeOffset + width,
118: height, null);
119: } else {
120: // create background that is strip wide and take
121: // its mid part
122: graphics.drawImage(bigImage, 0, 0, width, height,
123: relativeOffset, 0, relativeOffset + width,
124: height, null);
125: }
126: }
127: } else {
128: // create background that is strip high
129: BufferedImage bigImage = ButtonBackgroundDelegate
130: .getBackground(button, shaper, painter,
131: borderPainter, totalStripDimension, width).backgroundImage;
132: bigImage = SubstanceImageCreator.getRotated(bigImage, 3);
133: // three distinct cases - top, bottom, and all the rest
134: if (isFirst) {
135: graphics.drawImage(bigImage, 0, 0, width, height, 0, 0,
136: width, height, null);
137: } else {
138: if (isLast) {
139: // take its bottom part
140: graphics.drawImage(bigImage, 0, 0, width, height,
141: 0, relativeOffset, width, relativeOffset
142: + height, null);
143: } else {
144: // take its mid part
145: graphics.drawImage(bigImage, 0, 0, width, height,
146: 0, relativeOffset, width, relativeOffset
147: + height, null);
148: }
149: }
150: }
151: graphics.dispose();
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see org.jvnet.flamingo.common.ui.BasicButtonStripUI#paintStripButtonBorder(java.awt.Graphics,
158: * javax.swing.AbstractButton, boolean, boolean)
159: */
160: @Override
161: protected void paintStripButtonBorder(Graphics g,
162: AbstractButton button, boolean isFirst, boolean isLast) {
163: // System.out.println("border [" + button.hashCode() + "] " + isFirst
164: // + " " + isLast);
165: if (this .buttonStrip.getOrientation() == StripOrientation.HORIZONTAL) {
166: // need to put the right border for all buttons except the last
167: if (!isLast) {
168: Graphics2D graphics = (Graphics2D) g.create(button
169: .getWidth() - 1, 0, 2, button.getHeight());
170: SubstanceBorder sb = new SubstanceBorder();
171: sb.paintBorder(this .buttonStrip, graphics, 1 - button
172: .getWidth(), 0, button.getWidth(), button
173: .getHeight());
174: graphics.dispose();
175: }
176: } else {
177: // need to put the bottom border for all buttons except the last
178: if (!isLast) {
179: Graphics2D graphics = (Graphics2D) g.create(0, button
180: .getHeight() - 1, button.getWidth(), 2);
181: SubstanceBorder sb = new SubstanceBorder();
182: sb.paintBorder(this .buttonStrip, graphics, 0, 0, button
183: .getWidth(), button.getHeight());
184: graphics.dispose();
185: }
186: }
187: }
188:
189: /*
190: * (non-Javadoc)
191: *
192: * @see org.jvnet.flamingo.common.ui.BasicButtonStripUI#paintButtonContents(java.awt.Graphics,
193: * javax.swing.AbstractButton)
194: */
195: @Override
196: protected void paintButtonContents(Graphics g, AbstractButton button) {
197: // System.out.println("contents [" + button.hashCode() + "] "
198: // + g.getClip());
199: // get the original UI delegate
200: BasicButtonUI buttonUI = (BasicButtonUI) button.getUI();
201: // and ask it to paint the button contents
202: buttonUI.paint(g, button);
203: }
204: }
|