KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > debug > FormDebugUtils


1 /*
2  * Copyright (c) 2003 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.forms.debug;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35
36 import javax.swing.JLabel JavaDoc;
37
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.ColumnSpec;
40 import com.jgoodies.forms.layout.FormLayout;
41 import com.jgoodies.forms.layout.RowSpec;
42
43
44 /**
45  * Provides static methods that help you understand and fix layout problems
46  * when using the {@link FormLayout}. Therefore it can dump information
47  * about the layout grid, layout groups and cell constraints to the console.
48  *
49  * @author Karsten Lentzsch
50  * @version $Revision: 1.2 $
51  */

52 public final class FormDebugUtils {
53     
54     // Console Dump *********************************************************
55

56     /**
57      * Dumps all layout state to the console: column and row specifications,
58      * column and row groups, grid bounds and cell constraints.
59      */

60     public static void dumpAll(Container JavaDoc container) {
61         if (!(container.getLayout() instanceof FormLayout)) {
62             System.out.println("The container's layout is not a FormLayout.");
63             return;
64         }
65         FormLayout layout = (FormLayout) container.getLayout();
66         dumpColumnSpecs(layout);
67         dumpRowSpecs(layout);
68         System.out.println();
69         dumpColumnGroups(layout);
70         dumpRowGroups(layout);
71         System.out.println();
72         dumpConstraints(container);
73         dumpGridBounds(container);
74     }
75     
76     /**
77      * Dumps the layout's column specifications to the console.
78      *
79      * @param layout the <code>FormLayout</code> to inspect
80      */

81     public static void dumpColumnSpecs(FormLayout layout) {
82         System.out.print("COLUMN SPECS:");
83         for (int col = 1; col <= layout.getColumnCount(); col++) {
84             ColumnSpec colSpec = layout.getColumnSpec(col);
85             System.out.print(colSpec.toShortString());
86             if (col < layout.getColumnCount())
87                 System.out.print(", ");
88         }
89         System.out.println();
90     }
91
92
93     /**
94      * Dumps the layout's row specifications to the console.
95      *
96      * @param layout the <code>FormLayout</code> to inspect
97      */

98     public static void dumpRowSpecs(FormLayout layout) {
99         System.out.print("ROW SPECS: ");
100         for (int row = 1; row <= layout.getRowCount(); row++) {
101             RowSpec rowSpec = layout.getRowSpec(row);
102             System.out.print(rowSpec.toShortString());
103             if (row < layout.getRowCount())
104                 System.out.print(", ");
105         }
106         System.out.println();
107     }
108
109
110     /**
111      * Dumps the layout's column groups to the console.
112      *
113      * @param layout the <code>FormLayout</code> to inspect
114      */

115     public static void dumpColumnGroups(FormLayout layout) {
116         dumpGroups("COLUMN GROUPS: ", layout.getColumnGroups());
117     }
118
119
120     /**
121      * Dumps the layout's row groups to the console.
122      *
123      * @param layout the <code>FormLayout</code> to inspect
124      */

125     public static void dumpRowGroups(FormLayout layout) {
126         dumpGroups("ROW GROUPS: ", layout.getRowGroups());
127     }
128
129
130     /**
131      * Dumps the container's grid info to the console if and only
132      * if the container's layout is a <code>FormLayout</code>.
133      *
134      * @param container the container to inspect
135      * @throws IllegalArgumentException if the layout is not FormLayout
136      */

137     public static void dumpGridBounds(Container JavaDoc container) {
138         System.out.println("GRID BOUNDS");
139         dumpGridBounds(getLayoutInfo(container));
140     }
141
142
143     /**
144      * Dumps the grid layout info to the console.
145      *
146      * @param layoutInfo provides the column and row origins
147      */

148     public static void dumpGridBounds(FormLayout.LayoutInfo layoutInfo) {
149         System.out.print("COLUMN ORIGINS: ");
150         for (int col = 0; col < layoutInfo.columnOrigins.length; col++) {
151             System.out.print(layoutInfo.columnOrigins[col] + " ");
152         }
153         System.out.println();
154
155         System.out.print("ROW ORIGINS: ");
156         for (int row = 0; row < layoutInfo.rowOrigins.length; row++) {
157             System.out.print(layoutInfo.rowOrigins[row] + " ");
158         }
159         System.out.println();
160     }
161
162
163     /**
164      * Dumps the component constraints to the console.
165      *
166      * @param container the layout container to inspect
167      */

168     public static void dumpConstraints(Container JavaDoc container) {
169         System.out.println("COMPONENT CONSTRAINTS");
170         if (!(container.getLayout() instanceof FormLayout)) {
171             System.out.println("The container's layout is not a FormLayout.");
172             return;
173         }
174         FormLayout layout = (FormLayout) container.getLayout();
175         int childCount = container.getComponentCount();
176         for (int i = 0; i < childCount; i++) {
177             Component JavaDoc child = container.getComponent(i);
178             CellConstraints cc = layout.getConstraints(child);
179             String JavaDoc ccString = cc == null
180                 ? "no constraints"
181                 : cc.toShortString(layout);
182             System.out.print(ccString);
183             System.out.print("; ");
184             String JavaDoc childType = child.getClass().getName();
185             System.out.print(childType);
186             if (child instanceof JLabel JavaDoc) {
187                 JLabel JavaDoc label = (JLabel JavaDoc) child;
188                 System.out.print(" \"" + label.getText() + "\"");
189             }
190             if (child.getName() != null) {
191                 System.out.print("; name=");
192                 System.out.print(child.getName());
193             }
194             System.out.println();
195         }
196         System.out.println();
197     }
198
199
200     // Helper Code **********************************************************
201

202     /**
203      * Dumps the given groups to the console.
204      *
205      * @param title a string title for the dump
206      * @param allGroups a two-dimensional array with all groups
207      */

208     private static void dumpGroups(String JavaDoc title, int[][] allGroups) {
209         System.out.print(title + " {");
210         for (int group = 0; group < allGroups.length; group++) {
211             int[] groupIndices = allGroups[group];
212             System.out.print(" {");
213             for (int i = 0; i < groupIndices.length; i++) {
214                 System.out.print(groupIndices[i]);
215                 if (i < groupIndices.length - 1) {
216                     System.out.print(", ");
217                 }
218             }
219             System.out.print("} ");
220             if (group < allGroups.length - 1) {
221                 System.out.print(", ");
222             }
223         }
224         System.out.println("}");
225     }
226     
227     /**
228      * Computes and answers the layout's grid origins.
229      *
230      * @param container the layout container to inspect
231      * @return an object that comprises the cell origins and extents
232      * @throws IllegalArgumentException if the layout is not FormLayout
233      */

234     public static FormLayout.LayoutInfo getLayoutInfo(Container JavaDoc container) {
235         if (!(container.getLayout() instanceof FormLayout)) {
236             throw new IllegalArgumentException JavaDoc("The container must use an instance of FormLayout.");
237         }
238         FormLayout layout = (FormLayout) container.getLayout();
239         return layout.getLayoutInfo(container);
240     }
241
242 }
Popular Tags