Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / GridBagLayoutPanel.java @ 8138

History | View | Annotate | Download (11.5 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 8138 2006-10-18 07:31:16Z jaume $
45
* $Log$
46
* Revision 1.10  2006-10-18 07:29:21  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.9  2006/08/23 07:33:32  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.8  2006/08/22 07:38:58  jaume
53
* added support for detecting changes keyboard and mouse changes over the componenets added via addComponent methods
54
*
55
* Revision 1.7  2006/08/11 16:36:15  azabala
56
* *** empty log message ***
57
*
58
* Revision 1.6  2006/08/07 14:45:14  azabala
59
* added a convenience method to specify insets and gridbagconstraints
60
*
61
* Revision 1.5  2006/07/28 15:17:43  azabala
62
* added the posibility to customize insets of the panel which has each row
63
*
64
* Revision 1.4  2006/07/12 11:23:56  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.3  2006/07/04 16:56:10  azabala
68
* new method to add three componentes
69
*
70
* Revision 1.2  2006/07/03 09:29:09  jaume
71
* javadoc
72
*
73
* Revision 1.1  2006/06/29 06:27:10  jaume
74
* *** empty log message ***
75
*
76
*
77
*/
78
package org.gvsig.gui.beans.swing;
79

    
80
import java.awt.Component;
81
import java.awt.GridBagConstraints;
82
import java.awt.GridBagLayout;
83
import java.awt.Insets;
84
import java.awt.event.KeyEvent;
85
import java.awt.event.KeyListener;
86
import java.awt.event.MouseEvent;
87
import java.awt.event.MouseListener;
88

    
89
import javax.swing.JComponent;
90
import javax.swing.JLabel;
91
import javax.swing.JPanel;
92
import javax.swing.border.EmptyBorder;
93

    
94
/**
95
 * A panel designed for make the production of forms easier and faster.<br><br>
96
 *
97
 * It is a JPanel with a GridBagLayout that allows easily adding new components
98
 * in rows that are automatically added and aligned by using the
99
 * addComponent(...) methods.
100
 *
101
 * @author jaume dominguez faus - jaume.dominguez@iver.es
102
 *
103
 */
104
public class GridBagLayoutPanel extends JPanel{
105
        private GridBagLayout gridBag;
106
        private boolean changed;
107
        /**
108
         * The number of components already added to the layout manager.
109
         */
110
        protected int y;
111
        private MyValueChangeListener lst;
112

    
113

    
114
        public GridBagLayoutPanel() {
115
                setLayout(gridBag = new GridBagLayout());
116
                lst = new MyValueChangeListener();
117
        }
118

    
119
        /**
120
         * Adds a labeled component to the option pane. Components are
121
         * added in a vertical fashion, one per row. The label is
122
         * displayed to the left of the component.
123
         * @param label The label
124
         * @param comp The component
125
         */
126
        public void addComponent(String label, Component comp)
127
        {
128
                JLabel l = newLabel(label, comp);
129
                l.setBorder(new EmptyBorder(0,0,0,12));
130
                addComponent(l,comp,GridBagConstraints.BOTH);
131
        }
132

    
133
        /**
134
         * Adds a labeled component to the option pane. Components are
135
         * added in a vertical fashion, one per row. The label is
136
         * displayed to the left of the component.
137
         * @param label The label
138
         * @param comp The component
139
         * @param fill Fill parameter to GridBagConstraints for the right
140
         * component
141
         */
142
        public void addComponent(String label, Component comp, int fill)
143
        {
144
                JLabel l = newLabel(label, comp);
145
                l.setBorder(new EmptyBorder(0,0,0,12));
146
                addComponent(l,comp,fill);
147
        }
148

    
149

    
150
        public void addComponent(String label, Component comp, Insets insets)
151
        {
152
                addComponent(label, comp, GridBagConstraints.BOTH, insets);
153
        }
154

    
155
        public void addComponent(String label, Component comp, int fill, Insets insets){
156
                JLabel l = newLabel(label, comp);
157
                l.setBorder(new EmptyBorder(0,0,0,12));
158
                addComponent(l,comp, fill, insets);
159
        }
160

    
161

    
162
        /**
163
         * Adds a labeled component to the option pane. Components are
164
         * added in a vertical fashion, one per row. The label is
165
         * displayed to the left of the component.
166
         * @param comp1 The label
167
         * @param comp2 The component
168
         *
169
         * @since jEdit 4.1pre3
170
         */
171
        public void addComponent(Component comp1, Component comp2)
172
        {
173
                addComponent(comp1,comp2,GridBagConstraints.BOTH);
174
        }
175

    
176
        /**
177
         * Adds two components in a single line using the default inset (borders of margin)
178
         * @param comp1
179
         * @param comp2
180
         * @param fill
181
         */
182
        public void addComponent(Component comp1, Component comp2, int fill){
183
                addComponent(comp1, comp2, fill, new Insets(1,0,1,0));
184
        }
185
        /**
186
         * Adds a labeled component to the option pane. Components are
187
         * added in a vertical fashion, one per row. The label is
188
         * displayed to the left of the component.
189
         * @param comp1 The label
190
         * @param comp2 The component
191
         * @param fill Fill parameter to GridBagConstraints for the right
192
         * component
193
         *
194
         * @since jEdit 4.1pre3
195
         */
196
        public void addComponent(Component comp1, Component comp2, int fill, Insets insets)
197
        {
198
                copyToolTips(comp1, comp2);
199
                GridBagConstraints cons = new GridBagConstraints();
200
                cons.gridy = y++;
201
                cons.gridheight = 1;
202
                cons.gridwidth = 1;
203
                cons.weightx = 0.0f;
204
                cons.insets = insets;
205
                cons.fill = GridBagConstraints.BOTH;
206

    
207
                gridBag.setConstraints(comp1,cons);
208
                add(comp1);
209

    
210
                cons.fill = fill;
211
                cons.gridx = 1;
212
                cons.weightx = 1.0f;
213
                gridBag.setConstraints(comp2,cons);
214
                add(comp2);
215
                comp1.addKeyListener(lst);
216
                comp1.addMouseListener(lst);
217
                comp2.addKeyListener(lst);
218
                comp2.addMouseListener(lst);
219
        }
220

    
221
        /**
222
         * Adds three components in a line using the default insets
223
         * @param comp1
224
         * @param comp2
225
         * @param comp3
226
         * @param fill
227
         */
228
        public void addComponent(Component comp1,
229
                        Component comp2,
230
                        Component comp3,
231
                        int fill){
232
                addComponent(comp1, comp2, comp3, fill, new Insets(1, 0, 1, 0));
233
        }
234
        /**
235
         * Adds three components (azabala)
236
         *
237
         * @param comp1
238
         * @param comp2
239
         * @param comp3
240
         * @param fill
241
         */
242
        public void addComponent(Component comp1,
243
                        Component comp2,
244
                        Component comp3,
245
                        int fill,
246
                        Insets insets)
247
        {
248
                copyToolTips(comp1, comp2);
249
                copyToolTips(comp1, comp3);
250

    
251
                GridBagConstraints cons = new GridBagConstraints();
252
                cons.gridy = y++;
253
                cons.gridheight = 1;
254
                cons.gridwidth = 1;
255
                cons.weightx = 0.0f;
256
                cons.insets = insets;
257
                cons.fill = GridBagConstraints.BOTH;
258

    
259
                gridBag.setConstraints(comp1,cons);
260
                add(comp1);
261

    
262
                cons.gridx = 1;
263
                cons.weightx = 1.0f;
264
                gridBag.setConstraints(comp2,cons);
265
                add(comp2);
266

    
267
                //FIXME. REVISAR ESTO QUE SEGURAMENTE ESTE MAL (AZABALA)
268
                cons.fill = GridBagConstraints.NONE;
269
                cons.gridx = 2;
270
                cons.weightx = 1.0f;
271
                gridBag.setConstraints(comp3, cons);
272
                add(comp3);
273
                comp1.addKeyListener(lst);
274
                comp1.addMouseListener(lst);
275
                comp2.addKeyListener(lst);
276
                comp2.addMouseListener(lst);
277
        }
278

    
279
        /**
280
         * Adds three components in a single line using the default BOTH fill constraint
281
         * @param comp1
282
         * @param comp2
283
         * @param comp3
284
         */
285
        public void addComponent(Component comp1,
286
                        Component comp2,
287
                        Component comp3) {
288
                addComponent(comp1, comp2, comp3, GridBagConstraints.BOTH);
289
        }
290

    
291

    
292

    
293
        public void addComponent(Component comp){
294
                addComponent(comp, new Insets(1, 0, 1, 0));
295
        }
296
        /**
297
         * Adds a component to the option pane. Components are
298
         * added in a vertical fashion, one per row.
299
         * @param comp The component
300
         */
301
        public void addComponent(Component comp, Insets insets)
302
        {
303
                GridBagConstraints cons = new GridBagConstraints();
304
                cons.gridy = y++;
305
                cons.gridheight = 1;
306
                cons.gridwidth = GridBagConstraints.REMAINDER;
307
                cons.fill = GridBagConstraints.NONE;
308
                cons.anchor = GridBagConstraints.WEST;
309
                cons.weightx = 1.0f;
310
                cons.insets = insets;
311

    
312
                gridBag.setConstraints(comp,cons);
313
                add(comp);
314
                comp.addKeyListener(lst);
315
                comp.addMouseListener(lst);
316

    
317
        }
318

    
319
        /**
320
         * (azabala)
321
         * Adds a component which is going to fill many rows of the grid
322
         * (useful to add scrollpanes with list, etc.)
323
         *
324
         * */
325
        public void addComponent(Component comp, Insets insets, int numRows){
326
                GridBagConstraints cons = new GridBagConstraints();
327
                cons.gridy = y++;
328
                cons.gridheight = numRows;
329
                //REVISAR
330
                y += numRows;
331
                cons.gridwidth = GridBagConstraints.REMAINDER;
332
                cons.fill = GridBagConstraints.HORIZONTAL;
333
                cons.anchor = GridBagConstraints.WEST;
334
                cons.weightx = 1.0f;
335
                cons.insets = insets;
336
                cons.weighty = 0.0f;
337

    
338
                gridBag.setConstraints(comp,cons);
339
                add(comp);
340
                comp.addKeyListener(lst);
341
                comp.addMouseListener(lst);
342

    
343
        }
344

    
345

    
346
        ///////ADDING SEPARATORS
347

    
348

    
349

    
350

    
351

    
352

    
353

    
354

    
355

    
356

    
357

    
358
        public void addComponent(Component comp, int fill){
359
                addComponent(comp, fill, new Insets(1, 0, 1, 0));
360
        }
361

    
362
        /**
363
         * Adds a component to the option pane. Components are
364
         * added in a vertical fashion, one per row.
365
         * @param comp The component
366
         * @param fill Fill parameter to GridBagConstraints
367
         */
368
        public void addComponent(Component comp, int fill, Insets insets)
369
        {
370
                GridBagConstraints cons = new GridBagConstraints();
371
                cons.gridy = y++;
372
                cons.gridheight = 1;
373
                cons.gridwidth = GridBagConstraints.REMAINDER;
374
                cons.fill = fill;
375
                cons.anchor = GridBagConstraints.WEST;
376
                cons.weightx = 1.0f;
377
                cons.insets = insets;
378

    
379
                gridBag.setConstraints(comp,cons);
380
                add(comp);
381
                comp.addKeyListener(lst);
382
                comp.addMouseListener(lst);
383
        }
384

    
385
        private void copyToolTips (Component c1, Component c2) {
386
                int tooltips = 0;
387
                int jc=0;
388
                String text = null;
389
                JComponent jc1 = null, jc2 = null;
390
                try {
391
                        jc1 = (JComponent) c1;
392
                        text = jc1.getToolTipText();
393
                        ++jc;
394
                        if (text != null && text.length() > 0) tooltips++;
395
                }
396
                catch (Exception e) {}
397
                try {
398
                        jc2 = (JComponent) c2;
399
                        String text2 = jc2.getToolTipText();
400
                        ++jc;
401
                        if (text2 != null && text2.length() > 0) {
402
                                text = text2;
403
                                tooltips++;
404
                        }
405
                }
406
                catch (Exception e) {}
407
                if (tooltips == 1 && jc == 2) {
408
                        jc1.setToolTipText(text);
409
                        jc2.setToolTipText(text);
410
                }
411
        }
412

    
413
        /**
414
         *        @return a label which has the same tooltiptext as the Component
415
         *    that it is a label for. This is used to create labels from inside
416
         *    AbstractPreferencePage.
417
         */
418
        public JLabel newLabel(String label, Component comp)
419
        {
420
                JLabel retval = new JLabel(label);
421
                try /* to get the tooltip of the component */
422
                {
423
                        JComponent jc = (JComponent) comp;
424
                        String tttext = jc.getToolTipText();
425
                        retval.setToolTipText(tttext);
426
                }
427
                catch (Exception e)
428
                {
429
                        /* There probably wasn't a tooltip,
430
                         * or it wasn't a JComponent.
431
                           We don't care. */
432
                }
433
                return retval;
434
        }
435

    
436
        public boolean hasChanged() {
437
                return changed;
438
        }
439

    
440
        public void setChanged(boolean changed) {
441
                this.changed = changed;
442
        }
443

    
444
        private class MyValueChangeListener implements KeyListener, MouseListener {
445
                public void keyPressed(KeyEvent e)      { changed = true; }
446
                public void keyReleased(KeyEvent e)     { changed = true; }
447
                public void keyTyped(KeyEvent e)        { changed = true; }
448
                public void mouseClicked(MouseEvent e)  { changed = true; }
449
                public void mouseEntered(MouseEvent e)  { changed = true; }
450
                public void mouseExited(MouseEvent e)   { changed = true; }
451
                public void mousePressed(MouseEvent e)  { changed = true; }
452
                public void mouseReleased(MouseEvent e) { changed = true; }
453
        }
454
}