Source Code Cross Referenced for VariableGridLayout.java in  » 6.0-JDK-Modules-sun » awt » sun » awt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules sun » awt » sun.awt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1995 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package sun.awt;
027:
028:        import java.awt.*;
029:        import java.util.BitSet;
030:
031:        /**
032:         * A layout manager for a container that lays out grids.  Allows setting
033:         * the relative sizes of rows and columns.
034:         *
035:         * @version 1.19, 05/05/07
036:         * @author Herb Jellinek
037:         */
038:
039:        public class VariableGridLayout extends GridLayout {
040:
041:            BitSet rowsSet = new BitSet();
042:            double rowFractions[] = null;
043:
044:            BitSet colsSet = new BitSet();
045:            double colFractions[] = null;
046:
047:            int rows;
048:            int cols;
049:            int hgap;
050:            int vgap;
051:
052:            /**
053:             * Creates a grid layout with the specified rows and specified columns.
054:             * @param rows the rows
055:             * @param cols the columns
056:             */
057:            public VariableGridLayout(int rows, int cols) {
058:                this (rows, cols, 0, 0);
059:
060:                if (rows != 0) {
061:                    rowsSet = new BitSet(rows);
062:                    stdRowFractions(rows);
063:                }
064:
065:                if (cols != 0) {
066:                    colsSet = new BitSet(cols);
067:                    stdColFractions(cols);
068:                }
069:            }
070:
071:            /**
072:             * Creates a grid layout with the specified rows, columns,
073:             * horizontal gap, and vertical gap.
074:             * @param rows the rows
075:             * @param cols the columns
076:             * @param hgap the horizontal gap variable
077:             * @param vgap the vertical gap variable
078:             * @exception IllegalArgumentException If the rows and columns are invalid.
079:             */
080:            public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
081:                super (rows, cols, hgap, vgap);
082:
083:                this .rows = rows;
084:                this .cols = cols;
085:                this .hgap = hgap;
086:                this .vgap = vgap;
087:
088:                if (rows != 0) {
089:                    rowsSet = new BitSet(rows);
090:                    stdRowFractions(rows);
091:                }
092:
093:                if (cols != 0) {
094:                    colsSet = new BitSet(cols);
095:                    stdColFractions(cols);
096:                }
097:            }
098:
099:            void stdRowFractions(int nrows) {
100:                rowFractions = new double[nrows];
101:                for (int i = 0; i < nrows; i++) {
102:                    rowFractions[i] = 1.0 / nrows;
103:                }
104:            }
105:
106:            void stdColFractions(int ncols) {
107:                colFractions = new double[ncols];
108:                for (int i = 0; i < ncols; i++) {
109:                    colFractions[i] = 1.0 / ncols;
110:                }
111:            }
112:
113:            public void setRowFraction(int rowNum, double fraction) {
114:                rowsSet.set(rowNum);
115:                rowFractions[rowNum] = fraction;
116:            }
117:
118:            public void setColFraction(int colNum, double fraction) {
119:                colsSet.set(colNum);
120:                colFractions[colNum] = fraction;
121:            }
122:
123:            public double getRowFraction(int rowNum) {
124:                return rowFractions[rowNum];
125:            }
126:
127:            public double getColFraction(int colNum) {
128:                return colFractions[colNum];
129:            }
130:
131:            void allocateExtraSpace(double vec[], BitSet userSet) {
132:                // collect the space that's been explicitly allocated...
133:                double total = 0.0;
134:                int unallocated = 0;
135:                int i;
136:                for (i = 0; i < vec.length; i++) {
137:                    if (userSet.get(i)) {
138:                        total += vec[i];
139:                    } else {
140:                        unallocated++;
141:                    }
142:                }
143:
144:                // ... then spread the extra space
145:                if (unallocated != 0) {
146:                    double space = (1.0 - total) / unallocated;
147:                    for (i = 0; i < vec.length; i++) {
148:                        if (!userSet.get(i)) {
149:                            vec[i] = space;
150:                            userSet.set(i);
151:                        }
152:                    }
153:                }
154:            }
155:
156:            void allocateExtraSpace() {
157:                allocateExtraSpace(rowFractions, rowsSet);
158:                allocateExtraSpace(colFractions, colsSet);
159:            }
160:
161:            /** 
162:             * Lays out the container in the specified panel.  
163:             * @param parent the specified component being laid out
164:             * @see Container
165:             */
166:            public void layoutContainer(Container parent) {
167:                Insets insets = parent.insets();
168:                int ncomponents = parent.countComponents();
169:                int nrows = rows;
170:                int ncols = cols;
171:
172:                if (nrows > 0) {
173:                    ncols = (ncomponents + nrows - 1) / nrows;
174:                } else {
175:                    nrows = (ncomponents + ncols - 1) / ncols;
176:                }
177:
178:                if (rows == 0) {
179:                    stdRowFractions(nrows);
180:                }
181:                if (cols == 0) {
182:                    stdColFractions(ncols);
183:                }
184:
185:                Dimension size = parent.size();
186:                int w = size.width - (insets.left + insets.right);
187:                int h = size.height - (insets.top + insets.bottom);
188:
189:                w = (w - (ncols - 1) * hgap);
190:                h = (h - (nrows - 1) * vgap);
191:
192:                allocateExtraSpace();
193:
194:                for (int c = 0, x = insets.left; c < ncols; c++) {
195:                    int colWidth = (int) (getColFraction(c) * w);
196:                    for (int r = 0, y = insets.top; r < nrows; r++) {
197:                        int i = r * ncols + c;
198:                        int rowHeight = (int) (getRowFraction(r) * h);
199:
200:                        if (i < ncomponents) {
201:                            parent.getComponent(i).reshape(x, y, colWidth,
202:                                    rowHeight);
203:                        }
204:                        y += rowHeight + vgap;
205:                    }
206:                    x += colWidth + hgap;
207:                }
208:            }
209:
210:            static String fracsToString(double array[]) {
211:                String result = "[" + array.length + "]";
212:
213:                for (int i = 0; i < array.length; i++) {
214:                    result += "<" + array[i] + ">";
215:                }
216:                return result;
217:            }
218:
219:            /**
220:             * Returns the String representation of this VariableGridLayout's values.
221:             */
222:            public String toString() {
223:                return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap
224:                        + ",rows=" + rows + ",cols=" + cols + ",rowFracs="
225:                        + fracsToString(rowFractions) + ",colFracs="
226:                        + fracsToString(colFractions) + "]";
227:            }
228:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.