001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. 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 JGoodies Karsten Lentzsch 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:
031: package com.jgoodies.looks.common;
032:
033: import java.awt.Component;
034: import java.awt.Container;
035: import java.awt.Dimension;
036: import java.awt.Insets;
037:
038: import javax.swing.plaf.basic.BasicOptionPaneUI;
039:
040: import com.jgoodies.looks.LookUtils;
041:
042: /**
043: * Unlike its superclass, this layout uses a minimum button width
044: * that complies with Mac and Windows UI style guides.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.5 $
048: */
049:
050: public final class ExtButtonAreaLayout extends
051: BasicOptionPaneUI.ButtonAreaLayout {
052:
053: /**
054: * Constructs an <code>ExtButtonAreaLayout</code>.
055: *
056: * @param syncAllWidths true indicates that all buttons get the same size
057: * @param padding the padding between buttons
058: */
059: public ExtButtonAreaLayout(boolean syncAllWidths, int padding) {
060: super (syncAllWidths, padding);
061: }
062:
063: public void layoutContainer(Container container) {
064: Component[] children = container.getComponents();
065:
066: if (children != null && children.length > 0) {
067: int numChildren = children.length;
068: Dimension[] sizes = new Dimension[numChildren];
069: int counter;
070: int yLocation = container.getInsets().top;
071:
072: if (syncAllWidths) {
073: int maxWidth = getMinimumButtonWidth();
074:
075: for (counter = 0; counter < numChildren; counter++) {
076: sizes[counter] = children[counter]
077: .getPreferredSize();
078: maxWidth = Math.max(maxWidth, sizes[counter].width);
079: }
080:
081: int xLocation;
082: int xOffset;
083:
084: if (getCentersChildren()) {
085: xLocation = (container.getSize().width - (maxWidth
086: * numChildren + (numChildren - 1) * padding)) / 2;
087: xOffset = padding + maxWidth;
088: } else {
089: if (numChildren > 1) {
090: xLocation = 0;
091: xOffset = (container.getSize().width - (maxWidth * numChildren))
092: / (numChildren - 1) + maxWidth;
093: } else {
094: xLocation = (container.getSize().width - maxWidth) / 2;
095: xOffset = 0;
096: }
097: }
098: boolean ltr = container.getComponentOrientation()
099: .isLeftToRight();
100: for (counter = 0; counter < numChildren; counter++) {
101: int index = (ltr) ? counter : numChildren - counter
102: - 1;
103: children[index].setBounds(xLocation, yLocation,
104: maxWidth, sizes[index].height);
105: xLocation += xOffset;
106: }
107: } else {
108: int totalWidth = 0;
109:
110: for (counter = 0; counter < numChildren; counter++) {
111: sizes[counter] = children[counter]
112: .getPreferredSize();
113: totalWidth += sizes[counter].width;
114: }
115: totalWidth += ((numChildren - 1) * padding);
116:
117: boolean cc = getCentersChildren();
118: int xOffset;
119: int xLocation;
120:
121: if (cc) {
122: xLocation = (container.getSize().width - totalWidth) / 2;
123: xOffset = padding;
124: } else {
125: if (numChildren > 1) {
126: xOffset = (container.getSize().width - totalWidth)
127: / (numChildren - 1);
128: xLocation = 0;
129: } else {
130: xLocation = (container.getSize().width - totalWidth) / 2;
131: xOffset = 0;
132: }
133: }
134:
135: boolean ltr = container.getComponentOrientation()
136: .isLeftToRight();
137: for (counter = 0; counter < numChildren; counter++) {
138: int index = (ltr) ? counter : numChildren - counter
139: - 1;
140: children[index].setBounds(xLocation, yLocation,
141: sizes[index].width, sizes[index].height);
142: xLocation += xOffset + sizes[index].width;
143: }
144: }
145: }
146: }
147:
148: public Dimension minimumLayoutSize(Container c) {
149: if (c != null) {
150: Component[] children = c.getComponents();
151:
152: if (children != null && children.length > 0) {
153: Dimension aSize;
154: int numChildren = children.length;
155: int height = 0;
156: Insets cInsets = c.getInsets();
157: int extraHeight = cInsets.top + cInsets.bottom;
158:
159: if (syncAllWidths) {
160: int maxWidth = getMinimumButtonWidth();
161:
162: for (int counter = 0; counter < numChildren; counter++) {
163: aSize = children[counter].getPreferredSize();
164: height = Math.max(height, aSize.height);
165: maxWidth = Math.max(maxWidth, aSize.width);
166: }
167: return new Dimension(maxWidth * numChildren
168: + (numChildren - 1) * padding, extraHeight
169: + height);
170: }
171: int totalWidth = 0;
172:
173: for (int counter = 0; counter < numChildren; counter++) {
174: aSize = children[counter].getPreferredSize();
175: height = Math.max(height, aSize.height);
176: totalWidth += aSize.width;
177: }
178: totalWidth += ((numChildren - 1) * padding);
179: return new Dimension(totalWidth, extraHeight + height);
180: }
181: }
182: return new Dimension(0, 0);
183: }
184:
185: /**
186: * Computes and answers the minimum button width according to
187: * the Microsoft UI style guide. Honors 96dpi and 120dpi
188: * and assumes an 8pt Tahoma.
189: * <p>
190: * Obviously, this won't fit all sizes, it is an improvement
191: * over the superclass' value of 0.
192: * @return the minimum button width
193: */
194: private int getMinimumButtonWidth() {
195: return LookUtils.IS_LOW_RESOLUTION ? 75 : 100;
196: }
197:
198: }
|