Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / GridBagLayoutPanel.java @ 40561

History | View | Annotate | Download (11.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: GridBagLayoutPanel.java 13655 2007-09-12 16:28:55Z bsanchez $
27
* $Log$
28
* Revision 1.2  2007-09-12 16:28:23  bsanchez
29
* *** empty log message ***
30
*
31
* Revision 1.1  2007/08/20 08:34:46  evercher
32
* He fusionado LibUI con LibUIComponents
33
*
34
* Revision 1.11  2006/10/23 09:53:07  jaume
35
* *** empty log message ***
36
*
37
* Revision 1.10  2006/10/18 07:29:21  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.9  2006/08/23 07:33:32  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.8  2006/08/22 07:38:58  jaume
44
* added support for detecting changes keyboard and mouse changes over the componenets added via addComponent methods
45
*
46
* Revision 1.7  2006/08/11 16:36:15  azabala
47
* *** empty log message ***
48
*
49
* Revision 1.6  2006/08/07 14:45:14  azabala
50
* added a convenience method to specify insets and gridbagconstraints
51
*
52
* Revision 1.5  2006/07/28 15:17:43  azabala
53
* added the posibility to customize insets of the panel which has each row
54
*
55
* Revision 1.4  2006/07/12 11:23:56  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.3  2006/07/04 16:56:10  azabala
59
* new method to add three componentes
60
*
61
* Revision 1.2  2006/07/03 09:29:09  jaume
62
* javadoc
63
*
64
* Revision 1.1  2006/06/29 06:27:10  jaume
65
* *** empty log message ***
66
*
67
*
68
*/
69
package org.gvsig.gui.beans.swing;
70

    
71
import java.awt.Component;
72
import java.awt.GridBagConstraints;
73
import java.awt.GridBagLayout;
74
import java.awt.Insets;
75
import java.awt.event.KeyEvent;
76
import java.awt.event.KeyListener;
77
import java.awt.event.MouseEvent;
78
import java.awt.event.MouseListener;
79

    
80
import javax.swing.JComponent;
81
import javax.swing.JLabel;
82
import javax.swing.JPanel;
83
import javax.swing.border.EmptyBorder;
84

    
85
/**
86
 * A panel designed for make the production of forms easier and faster.<br><br>
87
 *
88
 * It is a JPanel with a GridBagLayout that allows easily adding new components
89
 * in rows that are automatically added and aligned by using the
90
 * addComponent(...) methods.
91
 *
92
 * @author jaume dominguez faus - jaume.dominguez@iver.es
93
 *
94
 */
95
public class GridBagLayoutPanel extends JPanel{
96
  private static final long serialVersionUID = -5583978286938513624L;
97
        private GridBagLayout gridBag;
98
        private boolean changed;
99
        /**
100
         * The number of components already added to the layout manager.
101
         */
102
        protected int y;
103
        private MyValueChangeListener lst;
104

    
105

    
106
        public GridBagLayoutPanel() {
107
                setLayout(gridBag = new GridBagLayout());
108
                lst = new MyValueChangeListener();
109
        }
110

    
111
        /**
112
         * Adds a labeled component to the option pane. Components are
113
         * added in a vertical fashion, one per row. The label is
114
         * displayed to the left of the component.
115
         * @param label The label
116
         * @param comp The component
117
         */
118
        public void addComponent(String label, Component comp)
119
        {
120
                JLabel l = newLabel(label, comp);
121
                l.setBorder(new EmptyBorder(0,0,0,12));
122
                addComponent(l,comp,GridBagConstraints.BOTH);
123
        }
124

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

    
141

    
142
        public void addComponent(String label, Component comp, Insets insets)
143
        {
144
                addComponent(label, comp, GridBagConstraints.BOTH, insets);
145
        }
146

    
147
        public void addComponent(String label, Component comp, int fill, Insets insets){
148
                JLabel l = newLabel(label, comp);
149
                l.setBorder(new EmptyBorder(0,0,0,12));
150
                addComponent(l,comp, fill, insets);
151
        }
152

    
153

    
154
        /**
155
         * Adds a labeled component to the option pane. Components are
156
         * added in a vertical fashion, one per row. The label is
157
         * displayed to the left of the component.
158
         * @param comp1 The label
159
         * @param comp2 The component
160
         *
161
         * @since jEdit 4.1pre3
162
         */
163
        public void addComponent(Component comp1, Component comp2)
164
        {
165
                addComponent(comp1,comp2,GridBagConstraints.BOTH);
166
        }
167

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

    
199
                gridBag.setConstraints(comp1,cons);
200
                add(comp1);
201

    
202
                cons.fill = fill;
203
                cons.gridx = 1;
204
                cons.weightx = 1.0f;
205
                gridBag.setConstraints(comp2,cons);
206
                add(comp2);
207
                comp1.addKeyListener(lst);
208
                comp1.addMouseListener(lst);
209
                comp2.addKeyListener(lst);
210
                comp2.addMouseListener(lst);
211
        }
212

    
213
        /**
214
         * Adds three components in a line using the default insets
215
         * @param comp1
216
         * @param comp2
217
         * @param comp3
218
         * @param fill
219
         */
220
        public void addComponent(Component comp1,
221
                        Component comp2,
222
                        Component comp3,
223
                        int fill){
224
                addComponent(comp1, comp2, comp3, fill, new Insets(1, 0, 1, 0));
225
        }
226
        /**
227
         * Adds three components (azabala)
228
         *
229
         * @param comp1
230
         * @param comp2
231
         * @param comp3
232
         * @param fill
233
         */
234
        public void addComponent(Component comp1,
235
                        Component comp2,
236
                        Component comp3,
237
                        int fill,
238
                        Insets insets)
239
        {
240
                copyToolTips(comp1, comp2);
241
                copyToolTips(comp1, comp3);
242

    
243
                GridBagConstraints cons = new GridBagConstraints();
244
                cons.gridy = y++;
245
                cons.gridheight = 1;
246
                cons.gridwidth = 1;
247
                cons.weightx = 0.0f;
248
                cons.insets = insets;
249
                cons.fill = GridBagConstraints.BOTH;
250

    
251
                gridBag.setConstraints(comp1,cons);
252
                add(comp1);
253

    
254
                cons.gridx = 1;
255
                cons.weightx = 1.0f;
256
                gridBag.setConstraints(comp2,cons);
257
                add(comp2);
258

    
259
                //FIXME. REVISAR ESTO QUE SEGURAMENTE ESTE MAL (AZABALA)
260
                cons.fill = GridBagConstraints.NONE;
261
                cons.gridx = 2;
262
                cons.weightx = 1.0f;
263
                gridBag.setConstraints(comp3, cons);
264
                add(comp3);
265
                comp1.addKeyListener(lst);
266
                comp1.addMouseListener(lst);
267
                comp2.addKeyListener(lst);
268
                comp2.addMouseListener(lst);
269
        }
270

    
271
        /**
272
         * Adds three components in a single line using the default BOTH fill constraint
273
         * @param comp1
274
         * @param comp2
275
         * @param comp3
276
         */
277
        public void addComponent(Component comp1,
278
                        Component comp2,
279
                        Component comp3) {
280
                addComponent(comp1, comp2, comp3, GridBagConstraints.BOTH);
281
        }
282

    
283

    
284

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

    
304
                gridBag.setConstraints(comp,cons);
305
                add(comp);
306
                comp.addKeyListener(lst);
307
                comp.addMouseListener(lst);
308

    
309
        }
310

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

    
330
                gridBag.setConstraints(comp,cons);
331
                add(comp);
332
                comp.addKeyListener(lst);
333
                comp.addMouseListener(lst);
334

    
335
        }
336

    
337

    
338
        ///////ADDING SEPARATORS
339

    
340

    
341

    
342

    
343

    
344

    
345

    
346

    
347

    
348

    
349

    
350
        public void addComponent(Component comp, int fill){
351
                addComponent(comp, fill, new Insets(1, 0, 1, 0));
352
        }
353

    
354
        /**
355
         * Adds a component to the option pane. Components are
356
         * added in a vertical fashion, one per row.
357
         * @param comp The component
358
         * @param fill Fill parameter to GridBagConstraints
359
         */
360
        public void addComponent(Component comp, int fill, Insets insets)
361
        {
362
                GridBagConstraints cons = new GridBagConstraints();
363
                cons.gridy = y++;
364
                cons.gridheight = 1;
365
                cons.gridwidth = GridBagConstraints.REMAINDER;
366
                cons.fill = fill;
367
                cons.anchor = GridBagConstraints.WEST;
368
                cons.weightx = 1.0f;
369
                cons.insets = insets;
370

    
371
                gridBag.setConstraints(comp,cons);
372
                add(comp);
373
                comp.addKeyListener(lst);
374
                comp.addMouseListener(lst);
375
        }
376

    
377
        private void copyToolTips (Component c1, Component c2) {
378
                int tooltips = 0;
379
                int jc=0;
380
                String text = null;
381
                JComponent jc1 = null, jc2 = null;
382
                try {
383
                        jc1 = (JComponent) c1;
384
                        text = jc1.getToolTipText();
385
                        ++jc;
386
                        if (text != null && text.length() > 0) tooltips++;
387
                }
388
                catch (Exception e) {}
389
                try {
390
                        jc2 = (JComponent) c2;
391
                        String text2 = jc2.getToolTipText();
392
                        ++jc;
393
                        if (text2 != null && text2.length() > 0) {
394
                                text = text2;
395
                                tooltips++;
396
                        }
397
                }
398
                catch (Exception e) {}
399
                if (tooltips == 1 && jc == 2) {
400
                        jc1.setToolTipText(text);
401
                        jc2.setToolTipText(text);
402
                }
403
        }
404

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

    
428
        /**
429
         * Adds an empty row to the form. It can be used as a separator to
430
         * improve panel appearance and comprehension.
431
         */
432
        public void addBlank() {
433
                addComponent(new JBlank(1,1));
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
}