KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > swing > JGridBagPanel


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */
package org.apache.batik.ext.swing;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.GridBagConstraints JavaDoc;
21 import java.awt.GridBagLayout JavaDoc;
22 import java.awt.Insets JavaDoc;
23 import java.awt.LayoutManager JavaDoc;
24
25 import javax.swing.JPanel JavaDoc;
26
27 /**
28  * An implementation of JPanel that uses the GridBagLayout.
29  *
30  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: JGridBagPanel.java,v 1.6 2005/03/27 08:58:33 cam Exp $
32  */

33
34 public class JGridBagPanel extends JPanel JavaDoc implements GridBagConstants{
35     /**
36      * Provides insets desired for a given grid cell
37      */

38     public static interface InsetsManager{
39         /**
40          * Returns the insets for cell (gridx, gridy);
41          */

42         public Insets JavaDoc getInsets(int gridx, int gridy);
43     }
44
45     /**
46      * Always use 0 insets
47      */

48     private static class ZeroInsetsManager implements InsetsManager{
49         private Insets JavaDoc insets = new Insets JavaDoc(0, 0, 0, 0);
50
51         public Insets JavaDoc getInsets(int gridx, int gridy){
52             return insets;
53         }
54     }
55
56     /**
57      * Default implemenation
58      */

59     private static class DefaultInsetsManager implements InsetsManager{
60         /**
61          * leftInset is the space used by default as a padding to the
62          * left of each grid cell.
63          */

64         int leftInset=5;
65         
66         /**
67          * topInset is the space used by default as a padding to the
68          * top of each grid cell.
69          */

70         int topInset=5;
71         
72         public Insets JavaDoc positiveInsets = new Insets JavaDoc(topInset, leftInset, 0, 0);
73         public Insets JavaDoc leftInsets = new Insets JavaDoc(topInset, 0, 0, 0);
74         public Insets JavaDoc topInsets = new Insets JavaDoc(0, leftInset, 0, 0);
75         public Insets JavaDoc topLeftInsets = new Insets JavaDoc(0, 0, 0, 0);
76
77         public Insets JavaDoc getInsets(int gridx, int gridy){
78             if(gridx > 0){
79                 if(gridy > 0)
80                     return positiveInsets;
81                 else
82                     return topInsets;
83             }
84             else{
85                 if(gridy > 0)
86                     return leftInsets;
87                 else
88                     return topLeftInsets;
89             }
90         }
91     }
92
93     /**
94      * An InsetsManager that uses zero insets
95      */

96     public static final InsetsManager ZERO_INSETS = new ZeroInsetsManager();
97
98     /**
99      * An InsetsManager that uses padding for inside cells
100      */

101     public static final InsetsManager DEFAULT_INSETS = new DefaultInsetsManager();
102
103
104     /**
105      * Used to get insets at any given cell location
106      */

107     public InsetsManager insetsManager;
108
109     /**
110      * Sets the layout manager to GridBagLayout
111      */

112     public JGridBagPanel(){
113         this(new DefaultInsetsManager());
114     }
115
116     /**
117      * Initializes panel with a given insets manager
118      */

119     public JGridBagPanel(InsetsManager insetsManager){
120         super(new GridBagLayout JavaDoc());
121
122         if(insetsManager != null)
123             this.insetsManager = insetsManager;
124         else
125             this.insetsManager = new DefaultInsetsManager();
126     }
127
128     /**
129      * This method only takes effect if the LayoutManager is a GridBagLayout
130      */

131     public void setLayout(LayoutManager JavaDoc layout){
132         if(layout instanceof GridBagLayout JavaDoc)
133             super.setLayout(layout);
134     }
135
136     /**
137      * This version uses default insets and assumes that components are added in
138      * positive cell coordinates. Top inset for components added to the top
139      * is 0. Left inset for components added to the left is 0. For compoents at
140      * index gridx more than zero and index gridy more than zero, the insets
141      * are set to a default value.
142      *
143      * @param cmp Component to add to the panel
144      * @param gridx x position of the cell into which component should be added
145      * @param gridy y position of the cell into which component should be added
146      * @param gridwidth width, in cells, of the space occupied by the component in the grid
147      * @param gridheight height, in cells, of the space occupied by the component in the grid
148      * @param anchor placement of the component in its allocated space: WEST, NORTH, SOUTH, NORTHWEST, ...
149      * @param fill out should the component be resized within its space? NONE, BOTH, HORIZONTAL, VERTICAL.
150      * @param weightx what amount of extra horizontal space, if any, should be given to this component?
151      * @param weighty what amount of extra vertical space, if any, should be given to this component?
152      */

153     public void add(Component JavaDoc cmp, int gridx, int gridy,
154                     int gridwidth, int gridheight, int anchor, int fill,
155                     double weightx, double weighty){
156         Insets JavaDoc insets = insetsManager.getInsets(gridx, gridy);
157         GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
158         constraints.gridx = gridx;
159         constraints.gridy = gridy;
160         constraints.gridwidth = gridwidth;
161         constraints.gridheight = gridheight;
162         constraints.anchor = anchor;
163         constraints.fill = fill;
164         constraints.weightx = weightx;
165         constraints.weighty = weighty;
166         constraints.insets = insets;
167         add(cmp, constraints);
168     }
169
170 }
171
Popular Tags