KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > ExtButtonAreaLayout


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.common;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Insets JavaDoc;
37
38 import javax.swing.plaf.basic.BasicOptionPaneUI JavaDoc;
39
40 import com.jgoodies.looks.LookUtils;
41
42 /**
43  * Unlike its superclass, this layout uses a minimum button width
44  * that complies with Mac and Windows UI style guides.
45  *
46  * @author Karsten Lentzsch
47  * @version $Revision: 1.2 $
48  */

49
50 public final class ExtButtonAreaLayout
51     extends BasicOptionPaneUI.ButtonAreaLayout JavaDoc {
52
53     /**
54      * Constructs an <code>ExtButtonAreaLayout</code>.
55      *
56      * @param syncAllWidths true indicates that all buttons get the same size
57      * @param padding the padding between buttons
58      */

59     public ExtButtonAreaLayout(boolean syncAllWidths, int padding) {
60         super(syncAllWidths, padding);
61     }
62
63     public void layoutContainer(Container JavaDoc container) {
64         Component JavaDoc[] children = container.getComponents();
65
66         if (children != null && children.length > 0) {
67             int numChildren = children.length;
68             Dimension JavaDoc[] sizes = new Dimension JavaDoc[numChildren];
69             int counter;
70             int yLocation = container.getInsets().top;
71
72             if (syncAllWidths) {
73                 int maxWidth = getMinimumButtonWidth();
74
75                 for (counter = 0; counter < numChildren; counter++) {
76                     sizes[counter] = children[counter].getPreferredSize();
77                     maxWidth = Math.max(maxWidth, sizes[counter].width);
78                 }
79
80                 int xLocation;
81                 int xOffset;
82
83                 if (getCentersChildren()) {
84                     xLocation =
85                         (container.getSize().width
86                             - (maxWidth * numChildren
87                                 + (numChildren - 1) * padding))
88                             / 2;
89                     xOffset = padding + maxWidth;
90                 } else {
91                     if (numChildren > 1) {
92                         xLocation = 0;
93                         xOffset =
94                             (container.getSize().width
95                                 - (maxWidth * numChildren))
96                                 / (numChildren - 1)
97                                 + maxWidth;
98                     } else {
99                         xLocation = (container.getSize().width - maxWidth) / 2;
100                         xOffset = 0;
101                     }
102                 }
103                 for (counter = 0; counter < numChildren; counter++) {
104                     children[counter].setBounds(
105                         xLocation,
106                         yLocation,
107                         maxWidth,
108                         sizes[counter].height);
109                     xLocation += xOffset;
110                 }
111             } else {
112                 int totalWidth = 0;
113
114                 for (counter = 0; counter < numChildren; counter++) {
115                     sizes[counter] = children[counter].getPreferredSize();
116                     totalWidth += sizes[counter].width;
117                 }
118                 totalWidth += ((numChildren - 1) * padding);
119
120                 boolean cc = getCentersChildren();
121                 int xOffset;
122                 int xLocation;
123
124                 if (cc) {
125                     xLocation = (container.getSize().width - totalWidth) / 2;
126                     xOffset = padding;
127                 } else {
128                     if (numChildren > 1) {
129                         xOffset =
130                             (container.getSize().width - totalWidth)
131                                 / (numChildren - 1);
132                         xLocation = 0;
133                     } else {
134                         xLocation =
135                             (container.getSize().width - totalWidth) / 2;
136                         xOffset = 0;
137                     }
138                 }
139
140                 for (counter = 0; counter < numChildren; counter++) {
141                     children[counter].setBounds(
142                         xLocation,
143                         yLocation,
144                         sizes[counter].width,
145                         sizes[counter].height);
146                     xLocation += xOffset + sizes[counter].width;
147                 }
148             }
149         }
150     }
151
152     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc c) {
153         if (c != null) {
154             Component JavaDoc[] children = c.getComponents();
155
156             if (children != null && children.length > 0) {
157                 Dimension JavaDoc aSize;
158                 int numChildren = children.length;
159                 int height = 0;
160                 Insets JavaDoc cInsets = c.getInsets();
161                 int extraHeight = cInsets.top + cInsets.bottom;
162
163                 if (syncAllWidths) {
164                     int maxWidth = getMinimumButtonWidth();
165
166                     for (int counter = 0; counter < numChildren; counter++) {
167                         aSize = children[counter].getPreferredSize();
168                         height = Math.max(height, aSize.height);
169                         maxWidth = Math.max(maxWidth, aSize.width);
170                     }
171                     return new Dimension JavaDoc(
172                         maxWidth * numChildren + (numChildren - 1) * padding,
173                         extraHeight + height);
174                 } else {
175                     int totalWidth = 0;
176
177                     for (int counter = 0; counter < numChildren; counter++) {
178                         aSize = children[counter].getPreferredSize();
179                         height = Math.max(height, aSize.height);
180                         totalWidth += aSize.width;
181                     }
182                     totalWidth += ((numChildren - 1) * padding);
183                     return new Dimension JavaDoc(totalWidth, extraHeight + height);
184                 }
185             }
186         }
187         return new Dimension JavaDoc(0, 0);
188     }
189     
190     /**
191      * Computes and answers the minimum button width according to
192      * the Microsoft UI style guide. Honors 96dpi and 120dpi
193      * and assumes an 8pt Tahoma.
194      * <p>
195      * Obviously, this won't fit all sizes, it is an improvement
196      * over the superclass' value of 0.
197      * @return the minimum button width
198      */

199     private int getMinimumButtonWidth() {
200         return LookUtils.IS_LOW_RESOLUTION ? 75 : 100;
201     }
202
203 }
Popular Tags