root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / GridBagLayoutPanel.java @ 13136
History | View | Annotate | Download (11.8 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 13136 2007-08-20 08:38:34Z evercher $
|
45 |
* $Log$
|
46 |
* Revision 1.1 2007-08-20 08:34:46 evercher
|
47 |
* He fusionado LibUI con LibUIComponents
|
48 |
*
|
49 |
* Revision 1.11 2006/10/23 09:53:07 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.10 2006/10/18 07:29:21 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.9 2006/08/23 07:33:32 jaume
|
56 |
* *** empty log message ***
|
57 |
*
|
58 |
* Revision 1.8 2006/08/22 07:38:58 jaume
|
59 |
* added support for detecting changes keyboard and mouse changes over the componenets added via addComponent methods
|
60 |
*
|
61 |
* Revision 1.7 2006/08/11 16:36:15 azabala
|
62 |
* *** empty log message ***
|
63 |
*
|
64 |
* Revision 1.6 2006/08/07 14:45:14 azabala
|
65 |
* added a convenience method to specify insets and gridbagconstraints
|
66 |
*
|
67 |
* Revision 1.5 2006/07/28 15:17:43 azabala
|
68 |
* added the posibility to customize insets of the panel which has each row
|
69 |
*
|
70 |
* Revision 1.4 2006/07/12 11:23:56 jaume
|
71 |
* *** empty log message ***
|
72 |
*
|
73 |
* Revision 1.3 2006/07/04 16:56:10 azabala
|
74 |
* new method to add three componentes
|
75 |
*
|
76 |
* Revision 1.2 2006/07/03 09:29:09 jaume
|
77 |
* javadoc
|
78 |
*
|
79 |
* Revision 1.1 2006/06/29 06:27:10 jaume
|
80 |
* *** empty log message ***
|
81 |
*
|
82 |
*
|
83 |
*/
|
84 |
package org.gvsig.gui.beans.swing; |
85 |
|
86 |
import java.awt.Component; |
87 |
import java.awt.GridBagConstraints; |
88 |
import java.awt.GridBagLayout; |
89 |
import java.awt.Insets; |
90 |
import java.awt.event.KeyEvent; |
91 |
import java.awt.event.KeyListener; |
92 |
import java.awt.event.MouseEvent; |
93 |
import java.awt.event.MouseListener; |
94 |
|
95 |
import javax.swing.JComponent; |
96 |
import javax.swing.JLabel; |
97 |
import javax.swing.JPanel; |
98 |
import javax.swing.border.EmptyBorder; |
99 |
|
100 |
/**
|
101 |
* A panel designed for make the production of forms easier and faster.<br><br>
|
102 |
*
|
103 |
* It is a JPanel with a GridBagLayout that allows easily adding new components
|
104 |
* in rows that are automatically added and aligned by using the
|
105 |
* addComponent(...) methods.
|
106 |
*
|
107 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
108 |
*
|
109 |
*/
|
110 |
public class GridBagLayoutPanel extends JPanel{ |
111 |
private GridBagLayout gridBag; |
112 |
private boolean changed; |
113 |
/**
|
114 |
* The number of components already added to the layout manager.
|
115 |
*/
|
116 |
protected int y; |
117 |
private MyValueChangeListener lst;
|
118 |
|
119 |
|
120 |
public GridBagLayoutPanel() {
|
121 |
setLayout(gridBag = new GridBagLayout()); |
122 |
lst = new MyValueChangeListener();
|
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 |
*/
|
132 |
public void addComponent(String label, Component comp) |
133 |
{ |
134 |
JLabel l = newLabel(label, comp);
|
135 |
l.setBorder(new EmptyBorder(0,0,0,12)); |
136 |
addComponent(l,comp,GridBagConstraints.BOTH);
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* Adds a labeled component to the option pane. Components are
|
141 |
* added in a vertical fashion, one per row. The label is
|
142 |
* displayed to the left of the component.
|
143 |
* @param label The label
|
144 |
* @param comp The component
|
145 |
* @param fill Fill parameter to GridBagConstraints for the right
|
146 |
* component
|
147 |
*/
|
148 |
public void addComponent(String label, Component comp, int fill) |
149 |
{ |
150 |
JLabel l = newLabel(label, comp);
|
151 |
l.setBorder(new EmptyBorder(0,0,0,12)); |
152 |
addComponent(l,comp,fill); |
153 |
} |
154 |
|
155 |
|
156 |
public void addComponent(String label, Component comp, Insets insets) |
157 |
{ |
158 |
addComponent(label, comp, GridBagConstraints.BOTH, insets);
|
159 |
} |
160 |
|
161 |
public void addComponent(String label, Component comp, int fill, Insets insets){ |
162 |
JLabel l = newLabel(label, comp);
|
163 |
l.setBorder(new EmptyBorder(0,0,0,12)); |
164 |
addComponent(l,comp, fill, insets); |
165 |
} |
166 |
|
167 |
|
168 |
/**
|
169 |
* Adds a labeled component to the option pane. Components are
|
170 |
* added in a vertical fashion, one per row. The label is
|
171 |
* displayed to the left of the component.
|
172 |
* @param comp1 The label
|
173 |
* @param comp2 The component
|
174 |
*
|
175 |
* @since jEdit 4.1pre3
|
176 |
*/
|
177 |
public void addComponent(Component comp1, Component comp2) |
178 |
{ |
179 |
addComponent(comp1,comp2,GridBagConstraints.BOTH);
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Adds two components in a single line using the default inset (borders of margin)
|
184 |
* @param comp1
|
185 |
* @param comp2
|
186 |
* @param fill
|
187 |
*/
|
188 |
public void addComponent(Component comp1, Component comp2, int fill){ |
189 |
addComponent(comp1, comp2, fill, new Insets(1,0,1,0)); |
190 |
} |
191 |
/**
|
192 |
* Adds a labeled component to the option pane. Components are
|
193 |
* added in a vertical fashion, one per row. The label is
|
194 |
* displayed to the left of the component.
|
195 |
* @param comp1 The label
|
196 |
* @param comp2 The component
|
197 |
* @param fill Fill parameter to GridBagConstraints for the right
|
198 |
* component
|
199 |
*
|
200 |
* @since jEdit 4.1pre3
|
201 |
*/
|
202 |
public void addComponent(Component comp1, Component comp2, int fill, Insets insets) |
203 |
{ |
204 |
copyToolTips(comp1, comp2); |
205 |
GridBagConstraints cons = new GridBagConstraints(); |
206 |
cons.gridy = y++; |
207 |
cons.gridheight = 1;
|
208 |
cons.gridwidth = 1;
|
209 |
cons.weightx = 0.0f;
|
210 |
cons.insets = insets; |
211 |
cons.fill = GridBagConstraints.BOTH;
|
212 |
|
213 |
gridBag.setConstraints(comp1,cons); |
214 |
add(comp1); |
215 |
|
216 |
cons.fill = fill; |
217 |
cons.gridx = 1;
|
218 |
cons.weightx = 1.0f;
|
219 |
gridBag.setConstraints(comp2,cons); |
220 |
add(comp2); |
221 |
comp1.addKeyListener(lst); |
222 |
comp1.addMouseListener(lst); |
223 |
comp2.addKeyListener(lst); |
224 |
comp2.addMouseListener(lst); |
225 |
} |
226 |
|
227 |
/**
|
228 |
* Adds three components in a line using the default insets
|
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 |
addComponent(comp1, comp2, comp3, fill, new Insets(1, 0, 1, 0)); |
239 |
} |
240 |
/**
|
241 |
* Adds three components (azabala)
|
242 |
*
|
243 |
* @param comp1
|
244 |
* @param comp2
|
245 |
* @param comp3
|
246 |
* @param fill
|
247 |
*/
|
248 |
public void addComponent(Component comp1, |
249 |
Component comp2,
|
250 |
Component comp3,
|
251 |
int fill,
|
252 |
Insets insets)
|
253 |
{ |
254 |
copyToolTips(comp1, comp2); |
255 |
copyToolTips(comp1, comp3); |
256 |
|
257 |
GridBagConstraints cons = new GridBagConstraints(); |
258 |
cons.gridy = y++; |
259 |
cons.gridheight = 1;
|
260 |
cons.gridwidth = 1;
|
261 |
cons.weightx = 0.0f;
|
262 |
cons.insets = insets; |
263 |
cons.fill = GridBagConstraints.BOTH;
|
264 |
|
265 |
gridBag.setConstraints(comp1,cons); |
266 |
add(comp1); |
267 |
|
268 |
cons.gridx = 1;
|
269 |
cons.weightx = 1.0f;
|
270 |
gridBag.setConstraints(comp2,cons); |
271 |
add(comp2); |
272 |
|
273 |
//FIXME. REVISAR ESTO QUE SEGURAMENTE ESTE MAL (AZABALA)
|
274 |
cons.fill = GridBagConstraints.NONE;
|
275 |
cons.gridx = 2;
|
276 |
cons.weightx = 1.0f;
|
277 |
gridBag.setConstraints(comp3, cons); |
278 |
add(comp3); |
279 |
comp1.addKeyListener(lst); |
280 |
comp1.addMouseListener(lst); |
281 |
comp2.addKeyListener(lst); |
282 |
comp2.addMouseListener(lst); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Adds three components in a single line using the default BOTH fill constraint
|
287 |
* @param comp1
|
288 |
* @param comp2
|
289 |
* @param comp3
|
290 |
*/
|
291 |
public void addComponent(Component comp1, |
292 |
Component comp2,
|
293 |
Component comp3) {
|
294 |
addComponent(comp1, comp2, comp3, GridBagConstraints.BOTH);
|
295 |
} |
296 |
|
297 |
|
298 |
|
299 |
public void addComponent(Component comp){ |
300 |
addComponent(comp, new Insets(1, 0, 1, 0)); |
301 |
} |
302 |
/**
|
303 |
* Adds a component to the option pane. Components are
|
304 |
* added in a vertical fashion, one per row.
|
305 |
* @param comp The component
|
306 |
*/
|
307 |
public void addComponent(Component comp, Insets insets) |
308 |
{ |
309 |
GridBagConstraints cons = new GridBagConstraints(); |
310 |
cons.gridy = y++; |
311 |
cons.gridheight = 1;
|
312 |
cons.gridwidth = GridBagConstraints.REMAINDER;
|
313 |
cons.fill = GridBagConstraints.NONE;
|
314 |
cons.anchor = GridBagConstraints.WEST;
|
315 |
cons.weightx = 1.0f;
|
316 |
cons.insets = insets; |
317 |
|
318 |
gridBag.setConstraints(comp,cons); |
319 |
add(comp); |
320 |
comp.addKeyListener(lst); |
321 |
comp.addMouseListener(lst); |
322 |
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* (azabala)
|
327 |
* Adds a component which is going to fill many rows of the grid
|
328 |
* (useful to add scrollpanes with list, etc.)
|
329 |
*
|
330 |
* */
|
331 |
public void addComponent(Component comp, Insets insets, int numRows){ |
332 |
GridBagConstraints cons = new GridBagConstraints(); |
333 |
cons.gridy = y++; |
334 |
cons.gridheight = numRows; |
335 |
//REVISAR
|
336 |
y += numRows; |
337 |
cons.gridwidth = GridBagConstraints.REMAINDER;
|
338 |
cons.fill = GridBagConstraints.HORIZONTAL;
|
339 |
cons.anchor = GridBagConstraints.WEST;
|
340 |
cons.weightx = 1.0f;
|
341 |
cons.insets = insets; |
342 |
cons.weighty = 0.0f;
|
343 |
|
344 |
gridBag.setConstraints(comp,cons); |
345 |
add(comp); |
346 |
comp.addKeyListener(lst); |
347 |
comp.addMouseListener(lst); |
348 |
|
349 |
} |
350 |
|
351 |
|
352 |
///////ADDING SEPARATORS
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
public void addComponent(Component comp, int fill){ |
365 |
addComponent(comp, fill, new Insets(1, 0, 1, 0)); |
366 |
} |
367 |
|
368 |
/**
|
369 |
* Adds a component to the option pane. Components are
|
370 |
* added in a vertical fashion, one per row.
|
371 |
* @param comp The component
|
372 |
* @param fill Fill parameter to GridBagConstraints
|
373 |
*/
|
374 |
public void addComponent(Component comp, int fill, Insets insets) |
375 |
{ |
376 |
GridBagConstraints cons = new GridBagConstraints(); |
377 |
cons.gridy = y++; |
378 |
cons.gridheight = 1;
|
379 |
cons.gridwidth = GridBagConstraints.REMAINDER;
|
380 |
cons.fill = fill; |
381 |
cons.anchor = GridBagConstraints.WEST;
|
382 |
cons.weightx = 1.0f;
|
383 |
cons.insets = insets; |
384 |
|
385 |
gridBag.setConstraints(comp,cons); |
386 |
add(comp); |
387 |
comp.addKeyListener(lst); |
388 |
comp.addMouseListener(lst); |
389 |
} |
390 |
|
391 |
private void copyToolTips (Component c1, Component c2) { |
392 |
int tooltips = 0; |
393 |
int jc=0; |
394 |
String text = null; |
395 |
JComponent jc1 = null, jc2 = null; |
396 |
try {
|
397 |
jc1 = (JComponent) c1;
|
398 |
text = jc1.getToolTipText(); |
399 |
++jc; |
400 |
if (text != null && text.length() > 0) tooltips++; |
401 |
} |
402 |
catch (Exception e) {} |
403 |
try {
|
404 |
jc2 = (JComponent) c2;
|
405 |
String text2 = jc2.getToolTipText();
|
406 |
++jc; |
407 |
if (text2 != null && text2.length() > 0) { |
408 |
text = text2; |
409 |
tooltips++; |
410 |
} |
411 |
} |
412 |
catch (Exception e) {} |
413 |
if (tooltips == 1 && jc == 2) { |
414 |
jc1.setToolTipText(text); |
415 |
jc2.setToolTipText(text); |
416 |
} |
417 |
} |
418 |
|
419 |
/**
|
420 |
* @return a label which has the same tooltiptext as the Component
|
421 |
* that it is a label for. This is used to create labels from inside
|
422 |
* AbstractPreferencePage.
|
423 |
*/
|
424 |
public JLabel newLabel(String label, Component comp) |
425 |
{ |
426 |
JLabel retval = new JLabel(label); |
427 |
try /* to get the tooltip of the component */ |
428 |
{ |
429 |
JComponent jc = (JComponent) comp; |
430 |
String tttext = jc.getToolTipText();
|
431 |
retval.setToolTipText(tttext); |
432 |
} |
433 |
catch (Exception e) |
434 |
{ |
435 |
/* There probably wasn't a tooltip,
|
436 |
* or it wasn't a JComponent.
|
437 |
We don't care. */
|
438 |
} |
439 |
return retval;
|
440 |
} |
441 |
|
442 |
/**
|
443 |
* Adds an empty row to the form. It can be used as a separator to
|
444 |
* improve panel appearance and comprehension.
|
445 |
*/
|
446 |
public void addBlank() { |
447 |
addComponent(new JBlank(1,1)); |
448 |
} |
449 |
|
450 |
public boolean hasChanged() { |
451 |
return changed;
|
452 |
} |
453 |
|
454 |
public void setChanged(boolean changed) { |
455 |
this.changed = changed;
|
456 |
} |
457 |
|
458 |
private class MyValueChangeListener implements KeyListener, MouseListener { |
459 |
public void keyPressed(KeyEvent e) { changed = true; } |
460 |
public void keyReleased(KeyEvent e) { changed = true; } |
461 |
public void keyTyped(KeyEvent e) { changed = true; } |
462 |
public void mouseClicked(MouseEvent e) { changed = true; } |
463 |
public void mouseEntered(MouseEvent e) { changed = true; } |
464 |
public void mouseExited(MouseEvent e) { changed = true; } |
465 |
public void mousePressed(MouseEvent e) { changed = true; } |
466 |
public void mouseReleased(MouseEvent e) { changed = true; } |
467 |
} |
468 |
} |