001: /*
002: * Copyright (c) 2002-2004 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.forms.debug;
032:
033: import java.awt.Component;
034: import java.awt.Container;
035:
036: import javax.swing.JLabel;
037:
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.ColumnSpec;
040: import com.jgoodies.forms.layout.FormLayout;
041: import com.jgoodies.forms.layout.RowSpec;
042:
043: /**
044: * Provides static methods that help you understand and fix layout problems when
045: * using the {@link FormLayout}. Dumps information about the layout grid,
046: * layout groups and cell constraints to the console.
047: * <p>
048: *
049: * Implicit values are mapped to concrete. For example, implicit alignments in
050: * column and row specifications will be visible. And cell constraint alignments
051: * that use or override the column and row defaults are visible too.
052: *
053: * <pre>
054: * ColumnSpec("p") -> ColumnSpec("fill:pref:0");
055: * ColumnSpec("p:1") -> ColumnSpec("fill:pref:1");
056: *
057: * RowSpec("p") -> RowSpec("center:pref:0");
058: * RowSpec("p:1") -> RowSpec("center:pref:1");
059: * </pre>
060: *
061: * @author Karsten Lentzsch
062: * @version $Revision: 1.2 $
063: *
064: * @see FormDebugPanel
065: */
066: public final class FormDebugUtils {
067:
068: // Console Dump *********************************************************
069:
070: /**
071: * Dumps all layout state to the console: column and row specifications,
072: * column and row groups, grid bounds and cell constraints.
073: *
074: * @param container
075: * the layout container
076: */
077: public static void dumpAll(Container container) {
078: if (!(container.getLayout() instanceof FormLayout)) {
079: System.out
080: .println("The container's layout is not a FormLayout.");
081: return;
082: }
083: FormLayout layout = (FormLayout) container.getLayout();
084: dumpColumnSpecs(layout);
085: dumpRowSpecs(layout);
086: System.out.println();
087: dumpColumnGroups(layout);
088: dumpRowGroups(layout);
089: System.out.println();
090: dumpConstraints(container);
091: dumpGridBounds(container);
092: }
093:
094: /**
095: * Dumps the layout's column specifications to the console.
096: *
097: * @param layout
098: * the <code>FormLayout</code> to inspect
099: */
100: public static void dumpColumnSpecs(FormLayout layout) {
101: System.out.print("COLUMN SPECS:");
102: for (int col = 1; col <= layout.getColumnCount(); col++) {
103: ColumnSpec colSpec = layout.getColumnSpec(col);
104: System.out.print(colSpec.toShortString());
105: if (col < layout.getColumnCount())
106: System.out.print(", ");
107: }
108: System.out.println();
109: }
110:
111: /**
112: * Dumps the layout's row specifications to the console.
113: *
114: * @param layout
115: * the <code>FormLayout</code> to inspect
116: */
117: public static void dumpRowSpecs(FormLayout layout) {
118: System.out.print("ROW SPECS: ");
119: for (int row = 1; row <= layout.getRowCount(); row++) {
120: RowSpec rowSpec = layout.getRowSpec(row);
121: System.out.print(rowSpec.toShortString());
122: if (row < layout.getRowCount())
123: System.out.print(", ");
124: }
125: System.out.println();
126: }
127:
128: /**
129: * Dumps the layout's column groups to the console.
130: *
131: * @param layout
132: * the <code>FormLayout</code> to inspect
133: */
134: public static void dumpColumnGroups(FormLayout layout) {
135: dumpGroups("COLUMN GROUPS: ", layout.getColumnGroups());
136: }
137:
138: /**
139: * Dumps the layout's row groups to the console.
140: *
141: * @param layout
142: * the <code>FormLayout</code> to inspect
143: */
144: public static void dumpRowGroups(FormLayout layout) {
145: dumpGroups("ROW GROUPS: ", layout.getRowGroups());
146: }
147:
148: /**
149: * Dumps the container's grid info to the console if and only if the
150: * container's layout is a <code>FormLayout</code>.
151: *
152: * @param container
153: * the container to inspect
154: * @throws IllegalArgumentException
155: * if the layout is not FormLayout
156: */
157: public static void dumpGridBounds(Container container) {
158: System.out.println("GRID BOUNDS");
159: dumpGridBounds(getLayoutInfo(container));
160: }
161:
162: /**
163: * Dumps the grid layout info to the console.
164: *
165: * @param layoutInfo
166: * provides the column and row origins
167: */
168: public static void dumpGridBounds(FormLayout.LayoutInfo layoutInfo) {
169: System.out.print("COLUMN ORIGINS: ");
170: for (int col = 0; col < layoutInfo.columnOrigins.length; col++) {
171: System.out.print(layoutInfo.columnOrigins[col] + " ");
172: }
173: System.out.println();
174:
175: System.out.print("ROW ORIGINS: ");
176: for (int row = 0; row < layoutInfo.rowOrigins.length; row++) {
177: System.out.print(layoutInfo.rowOrigins[row] + " ");
178: }
179: System.out.println();
180: }
181:
182: /**
183: * Dumps the component constraints to the console.
184: *
185: * @param container
186: * the layout container to inspect
187: */
188: public static void dumpConstraints(Container container) {
189: System.out.println("COMPONENT CONSTRAINTS");
190: if (!(container.getLayout() instanceof FormLayout)) {
191: System.out
192: .println("The container's layout is not a FormLayout.");
193: return;
194: }
195: FormLayout layout = (FormLayout) container.getLayout();
196: int childCount = container.getComponentCount();
197: for (int i = 0; i < childCount; i++) {
198: Component child = container.getComponent(i);
199: CellConstraints cc = layout.getConstraints(child);
200: String ccString = cc == null ? "no constraints" : cc
201: .toShortString(layout);
202: System.out.print(ccString);
203: System.out.print("; ");
204: String childType = child.getClass().getName();
205: System.out.print(childType);
206: if (child instanceof JLabel) {
207: JLabel label = (JLabel) child;
208: System.out.print(" \"" + label.getText() + "\"");
209: }
210: if (child.getName() != null) {
211: System.out.print("; name=");
212: System.out.print(child.getName());
213: }
214: System.out.println();
215: }
216: System.out.println();
217: }
218:
219: // Helper Code **********************************************************
220:
221: /**
222: * Dumps the given groups to the console.
223: *
224: * @param title
225: * a string title for the dump
226: * @param allGroups
227: * a two-dimensional array with all groups
228: */
229: private static void dumpGroups(String title, int[][] allGroups) {
230: System.out.print(title + " {");
231: for (int group = 0; group < allGroups.length; group++) {
232: int[] groupIndices = allGroups[group];
233: System.out.print(" {");
234: for (int i = 0; i < groupIndices.length; i++) {
235: System.out.print(groupIndices[i]);
236: if (i < groupIndices.length - 1) {
237: System.out.print(", ");
238: }
239: }
240: System.out.print("} ");
241: if (group < allGroups.length - 1) {
242: System.out.print(", ");
243: }
244: }
245: System.out.println("}");
246: }
247:
248: /**
249: * Computes and returns the layout's grid origins.
250: *
251: * @param container
252: * the layout container to inspect
253: * @return an object that comprises the cell origins and extents
254: * @throws IllegalArgumentException
255: * if the layout is not FormLayout
256: */
257: public static FormLayout.LayoutInfo getLayoutInfo(
258: Container container) {
259: if (!(container.getLayout() instanceof FormLayout)) {
260: throw new IllegalArgumentException(
261: "The container must use an instance of FormLayout.");
262: }
263: FormLayout layout = (FormLayout) container.getLayout();
264: return layout.getLayoutInfo(container);
265: }
266:
267: }
|