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