001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.util; 017 018 import java.awt.Color; 019 import java.awt.Graphics; 020 import java.awt.GridBagLayout; 021 import java.awt.Point; 022 023 import javax.swing.JPanel; 024 025 /** 026 * A useful class for debugging layout problems when using {@link GridBagLayout}. 027 * When it is displayed, "guidelines" are shown. 028 */ 029 public class GridBagLayoutDebugPanel extends JPanel { 030 private Color gridColor = Color.red; 031 032 public GridBagLayoutDebugPanel() { 033 super(new GridBagLayout()); 034 } 035 036 /** 037 * Change the color used for painting the guidelines 038 * 039 * @param color 040 * defaults to {@link Color#red} 041 */ 042 public void setGridColor(Color color) { 043 gridColor = color; 044 } 045 046 protected void paintComponent(Graphics g) { 047 super.paintComponent(g); 048 paintGrid(g); 049 } 050 051 private void paintGrid(Graphics g) { 052 if (!(getLayout() instanceof GridBagLayout)) 053 return; 054 055 final GridBagLayout layout = (GridBagLayout)getLayout(); 056 final int[][] layoutDimensions = layout.getLayoutDimensions(); 057 final int[] columnWidths = layoutDimensions[0]; 058 final int[] rowHeights = layoutDimensions[1]; 059 final Point layoutOrigin = layout.getLayoutOrigin(); 060 final int left = layoutOrigin.x; 061 final int top = layoutOrigin.y; 062 g.setColor(gridColor); 063 064 int width = 0; 065 for (int i = 0; i < columnWidths.length; i++) { 066 width += columnWidths[i]; 067 } 068 069 int height = 0; 070 for (int i = 0; i < rowHeights.length; i++) { 071 height += rowHeights[i]; 072 } 073 074 int x = left; 075 for (int i = 0; i < columnWidths.length; i++) { 076 x += columnWidths[i]; 077 g.fillRect(x, top, 1, height); 078 } 079 080 int y = top; 081 for (int i = 0; i < rowHeights.length; i++) { 082 y += rowHeights[i]; 083 g.fillRect(left, y, width, 1); 084 } 085 } 086 087 }