Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / GridBagLayoutPanel.java @ 6180

History | View | Annotate | Download (7.54 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: GridBagLayoutPanel.java 6180 2006-07-04 16:56:10Z azabala $
45
* $Log$
46
* Revision 1.3  2006-07-04 16:56:10  azabala
47
* new method to add three componentes
48
*
49
* Revision 1.2  2006/07/03 09:29:09  jaume
50
* javadoc
51
*
52
* Revision 1.1  2006/06/29 06:27:10  jaume
53
* *** empty log message ***
54
*
55
*
56
*/
57
package org.gvsig.gui.beans.swing;
58

    
59
import java.awt.Component;
60
import java.awt.GridBagConstraints;
61
import java.awt.GridBagLayout;
62
import java.awt.Insets;
63

    
64
import javax.swing.JComponent;
65
import javax.swing.JLabel;
66
import javax.swing.JPanel;
67
import javax.swing.border.EmptyBorder;
68

    
69
/**
70
 * A panel designed for make the production of forms easier and faster.<br><br>
71
 *
72
 * It is a JPanel with a GridBagLayout that allows easily adding new components
73
 * in rows that are automatically added and aligned by using the
74
 * addComponent(...) methods.
75
 *
76
 * @author jaume dominguez faus - jaume.dominguez@iver.es
77
 *
78
 */
79
public class GridBagLayoutPanel extends JPanel{
80
        private GridBagLayout gridBag;
81

    
82
        /**
83
         * The number of components already added to the layout manager.
84
         */
85
        protected int y;
86

    
87
        public GridBagLayoutPanel() {
88
                setLayout(gridBag = new GridBagLayout());
89
        }
90

    
91
        /**
92
         * Adds a labeled component to the option pane. Components are
93
         * added in a vertical fashion, one per row. The label is
94
         * displayed to the left of the component.
95
         * @param label The label
96
         * @param comp The component
97
         */
98
        public void addComponent(String label, Component comp)
99
        {
100
                JLabel l = newLabel(label, comp);
101
                l.setBorder(new EmptyBorder(0,0,0,12));
102
                addComponent(l,comp,GridBagConstraints.BOTH);
103
        }
104

    
105
        /**
106
         * Adds a labeled component to the option pane. Components are
107
         * added in a vertical fashion, one per row. The label is
108
         * displayed to the left of the component.
109
         * @param label The label
110
         * @param comp The component
111
         * @param fill Fill parameter to GridBagConstraints for the right
112
         * component
113
         */
114
        public void addComponent(String label, Component comp, int fill)
115
        {
116
                JLabel l = newLabel(label, comp);
117
                l.setBorder(new EmptyBorder(0,0,0,12));
118
                addComponent(l,comp,fill);
119
        }
120

    
121
        /**
122
         * Adds a labeled component to the option pane. Components are
123
         * added in a vertical fashion, one per row. The label is
124
         * displayed to the left of the component.
125
         * @param comp1 The label
126
         * @param comp2 The component
127
         *
128
         * @since jEdit 4.1pre3
129
         */
130
        public void addComponent(Component comp1, Component comp2)
131
        {
132
                addComponent(comp1,comp2,GridBagConstraints.BOTH);
133
        }
134

    
135
        /**
136
         * Adds a labeled component to the option pane. Components are
137
         * added in a vertical fashion, one per row. The label is
138
         * displayed to the left of the component.
139
         * @param comp1 The label
140
         * @param comp2 The component
141
         * @param fill Fill parameter to GridBagConstraints for the right
142
         * component
143
         *
144
         * @since jEdit 4.1pre3
145
         */
146
        public void addComponent(Component comp1, Component comp2, int fill)
147
        {
148
                copyToolTips(comp1, comp2);
149
                GridBagConstraints cons = new GridBagConstraints();
150
                cons.gridy = y++;
151
                cons.gridheight = 1;
152
                cons.gridwidth = 1;
153
                cons.weightx = 0.0f;
154
                cons.insets = new Insets(1,0,1,0);
155
                cons.fill = GridBagConstraints.BOTH;
156

    
157
                gridBag.setConstraints(comp1,cons);
158
                add(comp1);
159

    
160
                cons.fill = fill;
161
                cons.gridx = 1;
162
                cons.weightx = 1.0f;
163
                gridBag.setConstraints(comp2,cons);
164
                add(comp2);
165
        }
166
        
167
        
168
/**
169
 * AZABALA
170
 * Estoy viendo si no seria conveniente a?adir un Component[]
171
 * como parametro, para que pueda crecer hasta infinito y mas alla
172
 * 
173
 * 
174
 * @param comp1
175
 * @param comp2
176
 * @param comp3
177
 * @param fill
178
 */
179
public void addComponent(Component comp1, 
180
                                                Component comp2, 
181
                                                Component comp3,
182
                                                int fill)
183
{
184
        copyToolTips(comp1, comp2);
185
        copyToolTips(comp1, comp3);
186
        
187
        GridBagConstraints cons = new GridBagConstraints();
188
        cons.gridy = y++;
189
        cons.gridheight = 1;
190
        cons.gridwidth = 1;
191
        cons.weightx = 0.0f;
192
        cons.insets = new Insets(1,0,1,0);
193
        cons.fill = GridBagConstraints.BOTH;
194

    
195
        gridBag.setConstraints(comp1,cons);
196
        add(comp1);
197

    
198
        cons.gridx = 1;
199
        cons.weightx = 1.0f;
200
        gridBag.setConstraints(comp2,cons);
201
        add(comp2);
202
        
203
        //FIXME. REVISAR ESTO QUE SEGURAMENTE ESTE MAL (AZABALA)
204
        cons.fill = GridBagConstraints.NONE;
205
        cons.gridx = 2;
206
        cons.weightx = 1.0f;
207
        gridBag.setConstraints(comp3, cons);
208
        add(comp3);
209
}
210
        
211
        
212
        
213
        
214
        
215

    
216
        /**
217
         * Adds a component to the option pane. Components are
218
         * added in a vertical fashion, one per row.
219
         * @param comp The component
220
         */
221
        public void addComponent(Component comp)
222
        {
223
                GridBagConstraints cons = new GridBagConstraints();
224
                cons.gridy = y++;
225
                cons.gridheight = 1;
226
                cons.gridwidth = GridBagConstraints.REMAINDER;
227
                cons.fill = GridBagConstraints.NONE;
228
                cons.anchor = GridBagConstraints.WEST;
229
                cons.weightx = 1.0f;
230
                cons.insets = new Insets(1,0,1,0);
231

    
232
                gridBag.setConstraints(comp,cons);
233
                add(comp);
234
        }
235

    
236
        /**
237
         * Adds a component to the option pane. Components are
238
         * added in a vertical fashion, one per row.
239
         * @param comp The component
240
         * @param fill Fill parameter to GridBagConstraints
241
         */
242
        public void addComponent(Component comp, int fill)
243
        {
244
                GridBagConstraints cons = new GridBagConstraints();
245
                cons.gridy = y++;
246
                cons.gridheight = 1;
247
                cons.gridwidth = GridBagConstraints.REMAINDER;
248
                cons.fill = fill;
249
                cons.anchor = GridBagConstraints.WEST;
250
                cons.weightx = 1.0f;
251
                cons.insets = new Insets(1,0,1,0);
252

    
253
                gridBag.setConstraints(comp,cons);
254
                add(comp);
255
        }
256

    
257
        private void copyToolTips (Component c1, Component c2) {
258
                int tooltips = 0;
259
                int jc=0;
260
                String text = null;
261
                JComponent jc1 = null, jc2 = null;
262
                try {
263
                        jc1 = (JComponent) c1;
264
                        text = jc1.getToolTipText();
265
                        ++jc;
266
                        if (text != null && text.length() > 0) tooltips++;
267
                }
268
                catch (Exception e) {}
269
                try {
270
                        jc2 = (JComponent) c2;
271
                        String text2 = jc2.getToolTipText();
272
                        ++jc;
273
                        if (text2 != null && text2.length() > 0) {
274
                                text = text2;
275
                                tooltips++;
276
                        }
277
                }
278
                catch (Exception e) {}
279
                if (tooltips == 1 && jc == 2) {
280
                        jc1.setToolTipText(text);
281
                        jc2.setToolTipText(text);
282
                }
283
        }
284

    
285
        /**
286
         *        @return a label which has the same tooltiptext as the Component
287
         *    that it is a label for. This is used to create labels from inside
288
         *    AbstractPreferencePage.
289
         */
290
        public JLabel newLabel(String label, Component comp)
291
        {
292
                JLabel retval = new JLabel(label);
293
                try /* to get the tooltip of the component */
294
                {
295
                        JComponent jc = (JComponent) comp;
296
                        String tttext = jc.getToolTipText();
297
                        retval.setToolTipText(tttext);
298
                }
299
                catch (Exception e)
300
                {
301
                        /* There probably wasn't a tooltip,
302
                         * or it wasn't a JComponent.
303
                           We don't care. */
304
                }
305
                return retval;
306
        }
307
}