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.button;
031:
032: import java.awt.*;
033: import java.awt.geom.GeneralPath;
034: import java.util.Set;
035:
036: import javax.swing.AbstractButton;
037: import javax.swing.Icon;
038: import javax.swing.border.Border;
039:
040: import org.jvnet.substance.SubstanceButtonBorder;
041: import org.jvnet.substance.SubstanceLookAndFeel;
042: import org.jvnet.substance.utils.*;
043:
044: /**
045: * Button shaper that returns rectangular buttons with slightly rounded corners
046: * (ala Windows XP). This class is part of officially supported API.
047: *
048: * @author Kirill Grouchnikov
049: */
050: public class ClassicButtonShaper extends BaseButtonShaper implements
051: RectangularButtonShaper {
052: // /**
053: // * The default width of button
054: // */
055: // public static final int DEFAULT_WIDTH = 70;
056: //
057: // /**
058: // * The default height of button
059: // */
060: // public static final int DEFAULT_HEIGHT = 20;
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see org.jvnet.substance.button.SubstanceButtonShaper#getDisplayName()
066: */
067: public String getDisplayName() {
068: return "Classic";
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see org.jvnet.substance.button.SubstanceButtonShaper#getButtonOutline(javax.swing.AbstractButton,
075: * java.awt.Insets, int, int)
076: */
077: public GeneralPath getButtonOutline(AbstractButton button,
078: Insets insets, int width, int height) {
079: Set<SubstanceConstants.Side> straightSides = SubstanceCoreUtilities
080: .getSides(button,
081: SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY);
082:
083: float radius = this .getCornerRadius(button, insets);
084: return getBaseOutline(width, height, radius, straightSides,
085: insets);
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.jvnet.substance.button.SubstanceButtonShaper#getButtonBorder(javax.swing.AbstractButton)
092: */
093: public Border getButtonBorder(AbstractButton button) {
094: return new SubstanceButtonBorder(ClassicButtonShaper.class) {
095: public Insets getBorderInsets(Component c) {
096: int extraPadding = SubstanceSizeUtils
097: .getExtraPadding(SubstanceSizeUtils
098: .getComponentFontSize(c));
099: // if (c instanceof SubstanceComboBoxButton)
100: // return new Insets(extraPadding, extraPadding, extraPadding,
101: // extraPadding);
102: return new Insets(2 + extraPadding, 3 + extraPadding,
103: 2 + extraPadding, 3 + extraPadding);
104: }
105: };
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see org.jvnet.substance.button.SubstanceButtonShaper#getPreferredSize(javax.swing.AbstractButton,
112: * java.awt.Dimension)
113: */
114: public Dimension getPreferredSize(AbstractButton button,
115: Dimension uiPreferredSize) {
116: Dimension result;
117: boolean toTweakWidth = false;
118: boolean toTweakHeight = false;
119:
120: Icon icon = button.getIcon();
121: boolean hasIcon = SubstanceCoreUtilities.hasIcon(button);
122: boolean hasText = SubstanceCoreUtilities.hasText(button);
123: Insets margin = button.getMargin();
124:
125: result = uiPreferredSize;
126:
127: boolean hasNoMinSizeProperty = SubstanceCoreUtilities
128: .hasNoMinSizeProperty(button);
129: if ((!hasNoMinSizeProperty) && hasText) {
130: int baseWidth = uiPreferredSize.width;
131: baseWidth = Math.max(baseWidth, SubstanceSizeUtils
132: .getMinButtonWidth(SubstanceSizeUtils
133: .getComponentFontSize(button)));
134: // if (baseWidth < DEFAULT_WIDTH) {
135: // baseWidth = DEFAULT_WIDTH;
136: // }
137: result = new Dimension(baseWidth, uiPreferredSize.height);
138: int baseHeight = result.height;
139: baseHeight = Math.max(baseHeight, SubstanceSizeUtils
140: .getMinButtonHeight(SubstanceSizeUtils
141: .getComponentFontSize(button)));
142: // if (baseHeight < DEFAULT_HEIGHT) {
143: // baseHeight = DEFAULT_HEIGHT;
144: // }
145: result = new Dimension(result.width, baseHeight);
146: } else {
147: if (hasNoMinSizeProperty) {
148: if (margin != null) {
149: result = new Dimension(result.width + margin.left
150: + margin.right, result.height + margin.top
151: + margin.bottom);
152: }
153: }
154: }
155:
156: int extraPadding = SubstanceSizeUtils
157: .getExtraPadding(SubstanceSizeUtils
158: .getComponentFontSize(button));
159: int iconPaddingWidth = 6 + 2 * extraPadding;
160: int iconPaddingHeight = 6 + 2 * extraPadding;
161: if (margin != null) {
162: iconPaddingWidth = Math.max(iconPaddingWidth, margin.left
163: + margin.right);
164: iconPaddingHeight = Math.max(iconPaddingHeight, margin.top
165: + margin.bottom);
166: }
167: if (hasIcon) {
168: // check the icon height
169: int iconHeight = icon.getIconHeight();
170: if (iconHeight > (result.getHeight() - iconPaddingHeight)) {
171: result = new Dimension(result.width, iconHeight);
172: toTweakHeight = true;
173: }
174: int iconWidth = icon.getIconWidth();
175: if (iconWidth > (result.getWidth() - iconPaddingWidth)) {
176: result = new Dimension(iconWidth, result.height);
177: toTweakWidth = true;
178: }
179: }
180:
181: if (SubstanceCoreUtilities.isScrollBarButton(button)) {
182: toTweakWidth = false;
183: toTweakHeight = false;
184: }
185:
186: if (toTweakWidth) {
187: result = new Dimension(result.width + iconPaddingWidth,
188: result.height);
189: }
190: if (toTweakHeight) {
191: result = new Dimension(result.width, result.height
192: + iconPaddingHeight);
193: }
194:
195: return result;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see org.jvnet.substance.button.SubstanceButtonShaper#isProportionate()
202: */
203: public boolean isProportionate() {
204: return true;
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see org.jvnet.substance.button.RectangularButtonShaper#getCornerRadius(javax.swing.AbstractButton,
211: * java.awt.Insets)
212: */
213: public float getCornerRadius(AbstractButton button, Insets insets) {
214: float radius = SubstanceSizeUtils
215: .getClassicButtonCornerRadius(SubstanceSizeUtils
216: .getComponentFontSize(button));
217: if (SubstanceCoreUtilities.isToolBarButton(button)) {
218: radius = SubstanceCoreUtilities
219: .getToolbarButtonCornerRadius(button, insets);
220: } else {
221: if (insets != null) {
222: int max = Math.max(Math.max(insets.left, insets.right),
223: Math.max(insets.top, insets.bottom));
224: if (max >= 2)
225: radius = 1;
226: }
227: }
228: return radius;
229: }
230: }
|