svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / util / StatusComponent.java @ 13589
History | View | Annotate | Download (5.62 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 |
package org.gvsig.gui.util; |
20 |
|
21 |
import java.util.ArrayList; |
22 |
|
23 |
import javax.swing.JComponent; |
24 |
/**
|
25 |
* Clase para poder cambiar el estado de un componente y sus componentes hijos.
|
26 |
*
|
27 |
* Tiene dos formas de uso:
|
28 |
*
|
29 |
* 1.- Desactivar un componente y todos sus hijos sin necesidad de guardar su
|
30 |
* estado. Para este caso solo es necesario usar el m?todo est?tico
|
31 |
* setDisabled(componente). El hecho de que no exista un activar es que para
|
32 |
* desactivar esta claro que queremos desactivar un componente y sus hijos,
|
33 |
* pero a la hora de activar no todos los hijos deben estar activos, para
|
34 |
* estos casos es necesario ver la segunda opci?n.
|
35 |
*
|
36 |
* 2.- Desactivar un componente guardando todos sus estados y volver a recuperar
|
37 |
* sus estados como estaba inicialmente. Ejemplo:
|
38 |
*
|
39 |
* // Creamos el StatusComponent asoci?ndolo al componente en cuesti?n
|
40 |
* StatusComponent statusComponent = new StatusComponent(miControl);
|
41 |
*
|
42 |
* // Desactivamos el componente y sus hijos guardando todos los estados.
|
43 |
* statusComponent.setEnabled(false);
|
44 |
*
|
45 |
* ......
|
46 |
* // Activamos el componente recuperando su estado inicial
|
47 |
* statusComponent.setEnabled(true);
|
48 |
*
|
49 |
* @version 07/09/2007
|
50 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
51 |
*/
|
52 |
public class StatusComponent { |
53 |
private ArrayList statusList = new ArrayList(); |
54 |
private boolean oldEnabled = true; |
55 |
private JComponent component = null; |
56 |
|
57 |
/**
|
58 |
* Estructura de datos para poder tener el estado de un componente
|
59 |
* @version 07/09/2007
|
60 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
61 |
*/
|
62 |
public class StatusComponentStruct { |
63 |
private JComponent object; |
64 |
private boolean enabled; |
65 |
|
66 |
public boolean isEnabled() { |
67 |
return enabled;
|
68 |
} |
69 |
|
70 |
public void setEnabled(boolean enabled) { |
71 |
this.enabled = enabled;
|
72 |
} |
73 |
|
74 |
public JComponent getObject() { |
75 |
return object;
|
76 |
} |
77 |
|
78 |
public void setObject(JComponent object) { |
79 |
this.object = object;
|
80 |
} |
81 |
} |
82 |
|
83 |
/**
|
84 |
* Construye un StatusComponent. Es necesario pasarle el componente que
|
85 |
* queremos tratar.
|
86 |
* @param component
|
87 |
*/
|
88 |
public StatusComponent(JComponent component) { |
89 |
this.component = component;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Recupera el estado de un componente y todos sus hijos, vaciando la pila de
|
94 |
* estados. Eso quiere decir que no se podra volver a recuperar su estado sin
|
95 |
* haberlo guardado previamente.
|
96 |
* @param component
|
97 |
*/
|
98 |
private void restoreStatus(JComponent component) { |
99 |
boolean auxEnabled = false; |
100 |
boolean finded = false; |
101 |
// Buscar estado de dicho componente
|
102 |
for (int i = 0; i < statusList.size(); i++) { |
103 |
StatusComponentStruct auxStatus = (StatusComponentStruct) statusList.get(i); |
104 |
if (auxStatus.getObject() == component) {
|
105 |
auxEnabled = auxStatus.isEnabled(); |
106 |
statusList.remove(i); |
107 |
finded = true;
|
108 |
break;
|
109 |
} |
110 |
} |
111 |
|
112 |
// Asignar su estado
|
113 |
if (finded)
|
114 |
component.setEnabled(auxEnabled); |
115 |
|
116 |
for (int i = 0; i < component.getComponentCount(); i++) |
117 |
if (component.getComponent(i) instanceof JComponent) |
118 |
restoreStatus((JComponent) component.getComponent(i));
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* Desactivar el componente y todos sus hijos sin guardar los estados. Hay que
|
123 |
* tener cuidado con no confundirlo con setEnabled(false). Este metodo nunca
|
124 |
* guardara el estado, asi que no se podra recuperar despues dicho estado.
|
125 |
* @param component
|
126 |
*/
|
127 |
static public void setDisabled(JComponent component) { |
128 |
component.setEnabled(false);
|
129 |
for (int i = 0; i < component.getComponentCount(); i++) |
130 |
if (component.getComponent(i) instanceof JComponent) |
131 |
setDisabled((JComponent) component.getComponent(i));
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Guarda el estado de un componente. Este proceso es recursivo. El estado
|
136 |
* se guarda en un array y este array no es vaciado inicialmente. La idea es
|
137 |
* guardar en un disabled y recuperar en un enabled y asegurarse que no puede
|
138 |
* ocurrir un disabled o un enabled dos veces.
|
139 |
* @param component
|
140 |
*/
|
141 |
private void saveComponentsStatus(JComponent component) { |
142 |
// Guardar estado
|
143 |
StatusComponentStruct auxStatus = new StatusComponentStruct();
|
144 |
auxStatus.setEnabled(component.isEnabled()); |
145 |
auxStatus.setObject(component); |
146 |
statusList.add(auxStatus); |
147 |
|
148 |
for (int i = 0; i < component.getComponentCount(); i++) |
149 |
if (component.getComponent(i) instanceof JComponent) |
150 |
saveComponentsStatus((JComponent) component.getComponent(i));
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Activa o desactiva un componente y todos sus componentes hijos. No se puede
|
155 |
* activar o desactivar dos veces seguidas. Siendo ignoradas las peticiones
|
156 |
* repetitivas.
|
157 |
* @param enabled
|
158 |
*/
|
159 |
public void setEnabled(boolean enabled) { |
160 |
// Si el estado ha cambiado, hacemos algo
|
161 |
if (oldEnabled != enabled) {
|
162 |
if (enabled) {
|
163 |
restoreStatus(component); |
164 |
} else {
|
165 |
saveComponentsStatus(component); |
166 |
setDisabled(component); |
167 |
} |
168 |
oldEnabled = enabled; |
169 |
} |
170 |
} |
171 |
} |