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.Color;
034: import java.awt.Graphics;
035:
036: import javax.swing.JPanel;
037:
038: import com.jgoodies.forms.layout.FormLayout;
039:
040: /**
041: * A panel that paints grid bounds if and only if the panel's layout manager is
042: * a {@link FormLayout}. You can tweak the debug paint process by setting a
043: * custom grid color, painting optional diagonals and painting the grid in the
044: * background.
045: * <p>
046: *
047: * This class is not intended to be extended. However, it is not marked as
048: * <code>final</code> to allow users to subclass it for debugging purposes. In
049: * general it is recommended to <em>use</em> JPanel instances, not
050: * <em>extend</em> them. You can see this implementation style in the Forms
051: * tutorial classes. Rarely there's a need to extend JPanel; for example if you
052: * provide a custom behavior for <code>#paintComponent</code> or
053: * <code>#updateUI</code>.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.2 $
057: *
058: * @see FormDebugUtils
059: */
060: public class FormDebugPanel extends JPanel {
061:
062: /**
063: * The default color used to paint the form's debug grid.
064: */
065: private static final Color DEFAULT_GRID_COLOR = Color.red;
066:
067: /**
068: * Specifies whether the grid shall be painted in the background. Is off by
069: * default and so the grid is painted in the foreground.
070: */
071: private boolean paintInBackground;
072:
073: /**
074: * Specifies whether the container's diagonals should be painted.
075: */
076: private boolean paintDiagonals;
077:
078: /**
079: * Holds the color used to paint the debug grid.
080: */
081: private Color gridColor = DEFAULT_GRID_COLOR;
082:
083: // Instance Creation ****************************************************
084:
085: /**
086: * Constructs a FormDebugPanel with all options turned off.
087: */
088: public FormDebugPanel() {
089: this (null);
090: }
091:
092: /**
093: * Constructs a FormDebugPanel on the given FormLayout instance that paints
094: * the grid in the foreground and paints no diagonals.
095: *
096: * @param layout
097: * the panel's FormLayout instance
098: */
099: public FormDebugPanel(FormLayout layout) {
100: this (layout, false, false);
101: }
102:
103: /**
104: * Constructs a FormDebugPanel on the given FormLayout using the specified
105: * settings that are otherwise turned off.
106: *
107: * @param paintInBackground
108: * true to paint grid lines in the background, false to paint the
109: * grid in the foreground
110: * @param paintDiagonals
111: * true to paint diagonals, false to not paint them
112: */
113: public FormDebugPanel(boolean paintInBackground,
114: boolean paintDiagonals) {
115: this (null, paintInBackground, paintDiagonals);
116: }
117:
118: /**
119: * Constructs a FormDebugPanel on the given FormLayout using the specified
120: * settings that are otherwise turned off.
121: *
122: * @param layout
123: * the panel's FormLayout instance
124: * @param paintInBackground
125: * true to paint grid lines in the background, false to paint the
126: * grid in the foreground
127: * @param paintDiagonals
128: * true to paint diagonals, false to not paint them
129: */
130: public FormDebugPanel(FormLayout layout, boolean paintInBackground,
131: boolean paintDiagonals) {
132: super (layout);
133: setPaintInBackground(paintInBackground);
134: setPaintDiagonals(paintDiagonals);
135: setGridColor(DEFAULT_GRID_COLOR);
136: }
137:
138: // Accessors ************************************************************
139:
140: /**
141: * Specifies to paint in background or foreground.
142: *
143: * @param b
144: * true to paint in the background, false for the foreground
145: */
146: public void setPaintInBackground(boolean b) {
147: paintInBackground = b;
148: }
149:
150: /**
151: * Enables or disables to paint the panel's diagonals.
152: *
153: * @param b
154: * true to paint diagonals, false to not paint them
155: */
156: public void setPaintDiagonals(boolean b) {
157: paintDiagonals = b;
158: }
159:
160: /**
161: * Sets the debug grid's color.
162: *
163: * @param color
164: * the color used to paint the debug grid
165: */
166: public void setGridColor(Color color) {
167: gridColor = color;
168: }
169:
170: // Painting *************************************************************
171:
172: /**
173: * Paints the component and - if background painting is enabled - the grid
174: *
175: * @param g
176: * the Graphics object to paint on
177: */
178: protected void paintComponent(Graphics g) {
179: super .paintComponent(g);
180: if (paintInBackground) {
181: paintGrid(g);
182: }
183: }
184:
185: /**
186: * Paints the panel. If the panel's layout manager is a FormLayout it paints
187: * the form's grid lines.
188: *
189: * @param g
190: * the Graphics object to paint on
191: */
192: public void paint(Graphics g) {
193: super .paint(g);
194: if (!paintInBackground) {
195: paintGrid(g);
196: }
197: }
198:
199: /**
200: * Paints the form's grid lines and diagonals.
201: *
202: * @param g
203: * the Graphics object used to paint
204: */
205: private void paintGrid(Graphics g) {
206: if (!(getLayout() instanceof FormLayout)) {
207: return;
208: }
209: FormLayout.LayoutInfo layoutInfo = FormDebugUtils
210: .getLayoutInfo(this );
211: int left = layoutInfo.getX();
212: int top = layoutInfo.getY();
213: int width = layoutInfo.getWidth();
214: int height = layoutInfo.getHeight();
215:
216: g.setColor(gridColor);
217: // Paint the column bounds.
218: for (int col = 0; col < layoutInfo.columnOrigins.length; col++) {
219: g.fillRect(layoutInfo.columnOrigins[col], top, 1, height);
220: }
221:
222: // Paint the row bounds.
223: for (int row = 0; row < layoutInfo.rowOrigins.length; row++) {
224: g.fillRect(left, layoutInfo.rowOrigins[row], width, 1);
225: }
226:
227: if (paintDiagonals) {
228: g.drawLine(left, top, left + width, top + height);
229: g.drawLine(left, top + height, left + width, top);
230: }
231: }
232:
233: }
|