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