KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35
36 import javax.swing.JPanel JavaDoc;
37
38 import com.jgoodies.forms.layout.FormLayout;
39
40
41 /**
42  * A panel that paints grid bounds if and only if the panel's layout manager
43  * is a {@link FormLayout}. You can tweak the debug paint process by setting
44  * a custom grid color, painting optional diagonals and painting the grid
45  * in the background.
46  * <p>
47  * This class is not intended to be extended. However, it is no longer
48  * marked as <code>final</code> to allow users to subclass it for
49  * debugging purposes. In general it is recommended to use JPanel
50  * instances, not extend them. You can see this implementation style
51  * in the Forms tutorial classes. Rarely there's a need to extend JPanel;
52  * for example if you provide a custom behavior for
53  * <code>#paintComponent</code> or <code>#updateUI</code>.
54  *
55  * @author Karsten Lentzsch
56  * @version $Revision: 1.3 $
57  * @see FormDebugUtils
58  */

59 public class FormDebugPanel extends JPanel JavaDoc {
60     
61     /**
62      * The default color used to paint the form's debug grid.
63      */

64     private static final Color JavaDoc DEFAULT_GRID_COLOR = Color.red;
65
66
67     /**
68      * Specifies whether the grid shall be painted in the background.
69      * Is off by default and so the grid is painted in the foreground.
70      */

71     private boolean paintInBackground;
72     
73     
74     /**
75      * Specifies whether the container's diagonals should be painted.
76      */

77     private boolean paintDiagonals;
78
79
80     /**
81      * Holds the color used to paint the debug grid.
82      */

83     private Color JavaDoc gridColor = DEFAULT_GRID_COLOR;
84
85     
86     // Instance Creation ****************************************************
87

88     /**
89      * Constructs a <code>FormDebugPanel</code> with all options turned off.
90      */

91     public FormDebugPanel() {
92         this(null);
93     }
94     
95
96     /**
97      * Constructs a <code>FormDebugPanel</code> on the given
98      * <code>FormLayout</code> instance with all options turned off.
99      *
100      * @param layout the panel's FormLayout instance
101      */

102     public FormDebugPanel(FormLayout layout) {
103         this(layout, false, false);
104     }
105
106
107     /**
108      * Constructs a <code>FormDebugPanel</code> on the given
109      * <code>FormLayout</code> using the specified settings that are
110      * otherwise turned off.
111      *
112      * @param paintInBackground true to paint grid lines in the background
113      * @param paintDiagonals true to paint diagonals, false to not paint them
114      */

115     public FormDebugPanel(boolean paintInBackground,
116                            boolean paintDiagonals) {
117         this(null, paintInBackground, paintDiagonals);
118     }
119     
120
121     /**
122      * Constructs a <code>FormDebugPanel</code> on the given
123      * <code>FormLayout</code> using the specified settings that are
124      * otherwise turned off.
125      *
126      * @param layout the panel's FormLayout instance
127      * @param paintInBackground true to paint grid lines in the background
128      * @param paintDiagonals true to paint diagonals, false to not paint them
129      */

130     public FormDebugPanel(FormLayout layout,
131                            boolean paintInBackground,
132                            boolean paintDiagonals) {
133         super(layout);
134         setPaintInBackground(paintInBackground);
135         setPaintDiagonals(paintDiagonals);
136         setGridColor(DEFAULT_GRID_COLOR);
137     }
138     
139
140     // Accessors ************************************************************
141

142     /**
143      * Specifies to paint in background or foreground.
144      *
145      * @param b true to paint in the background, false for the foreground
146      */

147     public void setPaintInBackground(boolean b) {
148         paintInBackground = b;
149     }
150
151     /**
152      * Enables or disables to paint the panel's diagonals.
153      *
154      * @param b 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 the color used to paint the debug grid
164      */

165     public void setGridColor(Color JavaDoc color) {
166         gridColor = color;
167     }
168
169
170     // Painting *************************************************************
171

172     /**
173      * Paints the component.
174      * @see javax.swing.JComponent#paintComponent(Graphics)
175      */

176     protected void paintComponent(Graphics JavaDoc g) {
177         super.paintComponent(g);
178         if (paintInBackground) {
179             paintGrid(g);
180         }
181     }
182
183
184     /**
185      * Paints the panel. If the panel's layout manager is a
186      * <code>FormLayout</code> it paints the form's grid lines.
187      */

188     public void paint(Graphics JavaDoc g) {
189         super.paint(g);
190         if (!paintInBackground) {
191             paintGrid(g);
192         }
193     }
194     
195     
196     /**
197      * Paints the form's grid lines and diagonals.
198      */

199     private void paintGrid(Graphics JavaDoc g) {
200         if (!(getLayout() instanceof FormLayout)) {
201             return;
202         }
203         FormLayout.LayoutInfo layoutInfo = FormDebugUtils.getLayoutInfo(this);
204         int left = layoutInfo.getX();
205         int top = layoutInfo.getY();
206         int width = layoutInfo.getWidth();
207         int height = layoutInfo.getHeight();
208
209         g.setColor(gridColor);
210         // Paint the column bounds.
211
for (int col = 0; col < layoutInfo.columnOrigins.length; col++) {
212             g.fillRect(layoutInfo.columnOrigins[col], top, 1, height);
213         }
214
215         // Paint the row bounds.
216
for (int row = 0; row < layoutInfo.rowOrigins.length; row++) {
217             g.fillRect(left, layoutInfo.rowOrigins[row], width, 1);
218         }
219         
220         if (paintDiagonals) {
221             g.drawLine(left, top, left + width, top + height);
222             g.drawLine(left, top + height, left + width, top);
223         }
224     }
225     
226     
227 }
Popular Tags