View Javadoc

1   /*
2    * FormDebugPanel.java
3    *
4    * Stolen from FormLayout and tweaked by Kevin Routley.
5    *
6    * Created on March 23, 2005, 9:14 PM
7    */
8   
9   package org.mlc.swing.layout;
10  
11  import java.awt.Color;
12  import java.awt.Graphics;
13  
14  import javax.swing.JPanel;
15  
16  import com.jgoodies.forms.layout.FormLayout;
17  
18  /***
19   * [Taken from the FormLayout codebase. Hacked to work with @see ContainerLayout 
20      and deactivate() added.]
21   <p>
22   * A panel that paints grid bounds if and only if the panel's layout manager 
23   * is a {@link FormLayout}. You can tweak the debug paint process by setting
24   * a custom grid color, painting optional diagonals and painting the grid
25   * in the background.<p>
26   * 
27   * This class is not intended to be extended. However, it is not
28   * marked as <code>final</code> to allow users to subclass it for 
29   * debugging purposes. In general it is recommended to <em>use</em> JPanel
30   * instances, not <em>extend</em> them. You can see this implementation style
31   * in the Forms tutorial classes. Rarely there's a need to extend JPanel; 
32   * for example if you provide a custom behavior for 
33   * <code>#paintComponent</code> or <code>#updateUI</code>.  
34   *
35   * @author  Karsten Lentzsch
36   */
37  @SuppressWarnings("serial")
38  public class FormDebugPanel extends JPanel {
39      
40      /*** 
41       * The default color used to paint the form's debug grid. 
42       */
43      private static final Color DEFAULT_GRID_COLOR = Color.red;
44  
45  
46      /*** 
47       * Specifies whether the grid shall be painted in the background. 
48       * Is off by default and so the grid is painted in the foreground.
49       */
50      private boolean paintInBackground;
51      
52      
53      /***
54       * Specifies whether the container's diagonals should be painted.
55       */
56      private boolean paintDiagonals;
57  
58  
59      /***
60       * Holds the color used to paint the debug grid.
61       */
62      private Color gridColor = DEFAULT_GRID_COLOR;
63  
64      
65      // Instance Creation ****************************************************
66      
67      /***
68       * Constructs a FormDebugPanel with all options turned off. 
69       */
70      public FormDebugPanel() {
71          this(null);
72      }
73      
74  
75      /***
76       * Constructs a FormDebugPanel on the given FormLayout instance 
77       * that paints the grid in the foreground and paints no diagonals.
78       * 
79       * @param layout  the panel's FormLayout instance 
80       */
81      public FormDebugPanel(FormLayout layout) {
82          this(layout, false, false);
83      }
84  
85  
86      /***
87       * Constructs a FormDebugPanel on the given FormLayout 
88       * using the specified settings that are otherwise turned off.
89       * 
90       * @param paintInBackground 
91       *     true to paint grid lines in the background,
92       *     false to paint the grid in the foreground
93       * @param paintDiagonals    
94       *     true to paint diagonals, 
95       *     false to not paint them 
96       */
97      public FormDebugPanel(boolean paintInBackground, 
98                             boolean paintDiagonals) {
99          this(null, paintInBackground, paintDiagonals);
100     }
101     
102 
103     /***
104      * Constructs a FormDebugPanel on the given FormLayout using 
105      * the specified settings that are otherwise turned off.
106      * 
107      * @param layout  
108      *     the panel's FormLayout instance
109      * @param paintInBackground 
110      *     true to paint grid lines in the background,
111      *     false to paint the grid in the foreground
112      * @param paintDiagonals    
113      *     true to paint diagonals, 
114      *     false to not paint them 
115      */
116     public FormDebugPanel(FormLayout layout,
117                            boolean paintInBackground, 
118                            boolean paintDiagonals) {
119         super(layout);
120         setPaintInBackground(paintInBackground);
121         setPaintDiagonals(paintDiagonals);
122         setGridColor(DEFAULT_GRID_COLOR);
123     }
124     
125 
126     // Accessors ************************************************************
127     
128     /***
129      * Specifies to paint in background or foreground.
130      * 
131      * @param b    true to paint in the background, false for the foreground
132      */
133     public void setPaintInBackground(boolean b) { 
134         paintInBackground = b; 
135     }
136 
137     /***
138      * Enables or disables to paint the panel's diagonals.
139      * 
140      * @param b    true to paint diagonals, false to not paint them
141      */
142     public void setPaintDiagonals(boolean b) { 
143         paintDiagonals = b; 
144     }
145 
146     /***
147      * Sets the debug grid's color.
148      * 
149      * @param color  the color used to paint the debug grid
150      */
151     public void setGridColor(Color color) { 
152         gridColor = color; 
153     }
154 
155 
156     // Painting *************************************************************
157 
158     /***
159      * Paints the component and - if background painting is enabled - the grid
160      * 
161      * @param g   the Graphics object to paint on 
162      */
163     protected void paintComponent(Graphics g) {
164         super.paintComponent(g);
165         if (paintInBackground) {
166             paintGrid(g);
167         }
168     }
169 
170 
171     /***
172      * Paints the panel. If the panel's layout manager is a 
173      * FormLayout it paints the form's grid lines.
174      * 
175      * @param g   the Graphics object to paint on 
176      */
177     public void paint(Graphics g) {
178         super.paint(g);
179         if (!paintInBackground) {
180             paintGrid(g);
181         }
182     }
183 
184     // KBR Add flag to allow consumer control over gridlines
185     private boolean deactivated = false;
186     public void deactivate(boolean turnoff)
187     {
188       deactivated = turnoff;
189       repaint();
190     }
191     
192     /***
193      * Paints the form's grid lines and diagonals.
194      * 
195      * @param g    the Graphics object used to paint
196      */
197     private void paintGrid(Graphics g) {
198       
199         if (deactivated)
200           return;
201         
202         if (!(getLayout() instanceof ContainerLayout)) {
203             return;
204         }
205 
206         // KBR hack to work with FLM
207         ContainerLayout mylayout = (ContainerLayout)getLayout();
208         FormLayout.LayoutInfo layoutInfo = mylayout.getLayoutInfo(this);
209         
210 //        FormLayout.LayoutInfo layoutInfo = FormDebugUtils.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     
234 }