001/*
002Below is the copyright agreement for the Ptolemy II system.
003Version: $Id$
004
005Copyright (c) 2007-2009 The Regents of the University of California.
006All rights reserved.
007
008Permission is hereby granted, without written agreement and without
009license or royalty fees, to use, copy, modify, and distribute this
010software and its documentation for any purpose, provided that the above
011copyright notice and the following two paragraphs appear in all copies
012of this software.
013
014IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
015FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
016ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
017THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
018SUCH DAMAGE.
019
020THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
021INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
022MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
023PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
024CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
025ENHANCEMENTS, OR MODIFICATIONS.
026 */
027/*
028 * FormDebugPanel.java
029 *
030 * Stolen from FormLayout and tweaked by Kevin Routley.
031 *
032 * Created on March 23, 2005, 9:14 PM
033 */
034
035package org.mlc.swing.layout;
036
037import java.awt.Color;
038import java.awt.Graphics;
039
040import javax.swing.JPanel;
041
042import com.jgoodies.forms.layout.FormLayout;
043
044/**
045 * [Taken from the FormLayout codebase. Hacked to work with @see ContainerLayout
046    and deactivate() added.]
047 <p>
048 * A panel that paints grid bounds if and only if the panel's layout manager
049 * is a {@link FormLayout}. You can tweak the debug paint process by setting
050 * a custom grid color, painting optional diagonals and painting the grid
051 * in the background.<p>
052 *
053 * This class is not intended to be extended. However, it is not
054 * marked as <code>final</code> to allow users to subclass it for
055 * debugging purposes. In general it is recommended to <em>use</em> JPanel
056 * instances, not <em>extend</em> them. You can see this implementation style
057 * in the Forms tutorial classes. Rarely there's a need to extend JPanel;
058 * for example if you provide a custom behavior for
059 * <code>#paintComponent</code> or <code>#updateUI</code>.
060 *
061 * @author  Karsten Lentzsch
062@version $Id$
063@since Ptolemy II 8.0
064 */
065@SuppressWarnings("serial")
066public class FormDebugPanel extends JPanel {
067
068    /**
069     * The default color used to paint the form's debug grid.
070     */
071    private static final Color DEFAULT_GRID_COLOR = Color.red;
072
073    /**
074     * Specifies whether the grid shall be painted in the background.
075     * Is off by default and so the grid is painted in the foreground.
076     */
077    private boolean paintInBackground;
078
079    /**
080     * Specifies whether the container's diagonals should be painted.
081     */
082    private boolean paintDiagonals;
083
084    /**
085     * Holds the color used to paint the debug grid.
086     */
087    private Color gridColor = DEFAULT_GRID_COLOR;
088
089    // Instance Creation ****************************************************
090
091    /**
092     * Constructs a FormDebugPanel with all options turned off.
093     */
094    public FormDebugPanel() {
095        this(null);
096    }
097
098    /**
099     * Constructs a FormDebugPanel on the given FormLayout instance
100     * that paints the grid in the foreground and paints no diagonals.
101     *
102     * @param layout  the panel's FormLayout instance
103     */
104    public FormDebugPanel(FormLayout layout) {
105        this(layout, false, false);
106    }
107
108    /**
109     * Constructs a FormDebugPanel on the given FormLayout
110     * using the specified settings that are otherwise turned off.
111     *
112     * @param paintInBackground
113     *     true to paint grid lines in the background,
114     *     false to paint the grid in the foreground
115     * @param paintDiagonals
116     *     true to paint diagonals,
117     *     false to not paint them
118     */
119    public FormDebugPanel(boolean paintInBackground, boolean paintDiagonals) {
120        this(null, paintInBackground, paintDiagonals);
121    }
122
123    /**
124     * Constructs a FormDebugPanel on the given FormLayout using
125     * the specified settings that are otherwise turned off.
126     *
127     * @param layout
128     *     the panel's FormLayout instance
129     * @param paintInBackground
130     *     true to paint grid lines in the background,
131     *     false to paint the grid in the foreground
132     * @param paintDiagonals
133     *     true to paint diagonals,
134     *     false to not paint them
135     */
136    public FormDebugPanel(FormLayout layout, boolean paintInBackground,
137            boolean paintDiagonals) {
138        super(layout);
139        setPaintInBackground(paintInBackground);
140        setPaintDiagonals(paintDiagonals);
141        setGridColor(DEFAULT_GRID_COLOR);
142    }
143
144    // Accessors ************************************************************
145
146    /**
147     * Specifies to paint in background or foreground.
148     *
149     * @param b    true to paint in the background, false for the foreground
150     */
151    public void setPaintInBackground(boolean b) {
152        paintInBackground = b;
153    }
154
155    /**
156     * Enables or disables to paint the panel's diagonals.
157     *
158     * @param b    true to paint diagonals, false to not paint them
159     */
160    public void setPaintDiagonals(boolean b) {
161        paintDiagonals = b;
162    }
163
164    /**
165     * Sets the debug grid's color.
166     *
167     * @param color  the color used to paint the debug grid
168     */
169    public void setGridColor(Color color) {
170        gridColor = color;
171    }
172
173    // Painting *************************************************************
174
175    /**
176     * Paints the component and - if background painting is enabled - the grid
177     *
178     * @param g   the Graphics object to paint on
179     */
180    @Override
181    protected void paintComponent(Graphics g) {
182        super.paintComponent(g);
183        if (paintInBackground) {
184            paintGrid(g);
185        }
186    }
187
188    /**
189     * Paints the panel. If the panel's layout manager is a
190     * FormLayout it paints the form's grid lines.
191     *
192     * @param g   the Graphics object to paint on
193     */
194    @Override
195    public void paint(Graphics g) {
196        super.paint(g);
197        if (!paintInBackground) {
198            paintGrid(g);
199        }
200    }
201
202    // KBR Add flag to allow consumer control over gridlines
203    private boolean deactivated = false;
204
205    public void deactivate(boolean turnoff) {
206        deactivated = turnoff;
207        repaint();
208    }
209
210    /**
211     * Paints the form's grid lines and diagonals.
212     *
213     * @param g    the Graphics object used to paint
214     */
215    private void paintGrid(Graphics g) {
216
217        if (deactivated) {
218            return;
219        }
220
221        if (!(getLayout() instanceof ContainerLayout)) {
222            return;
223        }
224
225        // KBR hack to work with FLM
226        ContainerLayout mylayout = (ContainerLayout) getLayout();
227        FormLayout.LayoutInfo layoutInfo = mylayout.getLayoutInfo(this);
228
229        //        FormLayout.LayoutInfo layoutInfo = FormDebugUtils.getLayoutInfo(this);
230        int left = layoutInfo.getX();
231        int top = layoutInfo.getY();
232        int width = layoutInfo.getWidth();
233        int height = layoutInfo.getHeight();
234
235        g.setColor(gridColor);
236        // Paint the column bounds.
237        for (int columnOrigin : layoutInfo.columnOrigins) {
238            g.fillRect(columnOrigin, top, 1, height);
239        }
240
241        // Paint the row bounds.
242        for (int rowOrigin : layoutInfo.rowOrigins) {
243            g.fillRect(left, rowOrigin, width, 1);
244        }
245
246        if (paintDiagonals) {
247            g.drawLine(left, top, left + width, top + height);
248            g.drawLine(left, top + height, left + width, top);
249        }
250    }
251
252}