Statistics
| Revision:

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

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

    
83
import java.awt.Component;
84
import java.awt.GridBagConstraints;
85
import java.awt.GridBagLayout;
86
import java.awt.Insets;
87
import java.awt.event.KeyEvent;
88
import java.awt.event.KeyListener;
89
import java.awt.event.MouseEvent;
90
import java.awt.event.MouseListener;
91

    
92
import javax.swing.JComponent;
93
import javax.swing.JLabel;
94
import javax.swing.JPanel;
95
import javax.swing.border.EmptyBorder;
96

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

    
116

    
117
        public GridBagLayoutPanel() {
118
                setLayout(gridBag = new GridBagLayout());
119
                lst = new MyValueChangeListener();
120
        }
121

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

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

    
152

    
153
        public void addComponent(String label, Component comp, Insets insets)
154
        {
155
                addComponent(label, comp, GridBagConstraints.BOTH, insets);
156
        }
157

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

    
164

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

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

    
210
                gridBag.setConstraints(comp1,cons);
211
                add(comp1);
212

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

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

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

    
262
                gridBag.setConstraints(comp1,cons);
263
                add(comp1);
264

    
265
                cons.gridx = 1;
266
                cons.weightx = 1.0f;
267
                gridBag.setConstraints(comp2,cons);
268
                add(comp2);
269

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

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

    
294

    
295

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

    
315
                gridBag.setConstraints(comp,cons);
316
                add(comp);
317
                comp.addKeyListener(lst);
318
                comp.addMouseListener(lst);
319

    
320
        }
321

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

    
341
                gridBag.setConstraints(comp,cons);
342
                add(comp);
343
                comp.addKeyListener(lst);
344
                comp.addMouseListener(lst);
345

    
346
        }
347

    
348

    
349
        ///////ADDING SEPARATORS
350

    
351

    
352

    
353

    
354

    
355

    
356

    
357

    
358

    
359

    
360

    
361
        public void addComponent(Component comp, int fill){
362
                addComponent(comp, fill, new Insets(1, 0, 1, 0));
363
        }
364

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

    
382
                gridBag.setConstraints(comp,cons);
383
                add(comp);
384
                comp.addKeyListener(lst);
385
                comp.addMouseListener(lst);
386
        }
387

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

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

    
439
        /**
440
         * Adds an empty row to the form. It can be used as a separator to
441
         * improve panel appearance and comprehension.
442
         */
443
        public void addBlank() {
444
                addComponent(new JBlank(1,1));
445
        }
446

    
447
        public boolean hasChanged() {
448
                return changed;
449
        }
450

    
451
        public void setChanged(boolean changed) {
452
                this.changed = changed;
453
        }
454

    
455
        private class MyValueChangeListener implements KeyListener, MouseListener {
456
                public void keyPressed(KeyEvent e)      { changed = true; }
457
                public void keyReleased(KeyEvent e)     { changed = true; }
458
                public void keyTyped(KeyEvent e)        { changed = true; }
459
                public void mouseClicked(MouseEvent e)  { changed = true; }
460
                public void mouseEntered(MouseEvent e)  { changed = true; }
461
                public void mouseExited(MouseEvent e)   { changed = true; }
462
                public void mousePressed(MouseEvent e)  { changed = true; }
463
                public void mouseReleased(MouseEvent e) { changed = true; }
464
        }
465
}