svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / extensionPointsOld / ExtensionPoint.java @ 40561
History | View | Annotate | Download (8.45 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.utils.extensionPointsOld; |
25 |
|
26 |
import java.lang.reflect.InvocationTargetException; |
27 |
import java.security.KeyException; |
28 |
import java.util.*; |
29 |
|
30 |
/**
|
31 |
* Esta clase permite registrar extensiones para un punto de extension. <br>
|
32 |
* <br>
|
33 |
* La clase se comporta como un Map que mantiene el orden de insercion de los
|
34 |
* elementos, para que puedan ser recorridos en ese orden. <br>
|
35 |
* Ademas de registrar las extensiones para un punto de extension, presenta
|
36 |
* metodos para facilitar la creacion de la extension. <br>
|
37 |
* A la hora de registrar una extension, mediante el metodo <i>put</i>, podremos
|
38 |
* suministrarle una clase o una instancia que implemente el interface
|
39 |
* IExtensionBuilder. Si le suministramos una clase, cuando queramos crear la
|
40 |
* extension mediante el metodo <i>create</i>, se creara una instancia de la
|
41 |
* clase y se retornara. Si lo que se suministro fue una instancia que
|
42 |
* implementa el interface IExtensionBuilder, se invocara al metodo
|
43 |
* <i>create</i> de esta para crear la extension. <br>
|
44 |
*
|
45 |
* @author jjdelcerro
|
46 |
*
|
47 |
* @deprecated @see org.gvsig.tools.extensionPoint.ExtensionPoint
|
48 |
*/
|
49 |
public class ExtensionPoint extends LinkedHashMap { |
50 |
|
51 |
private static final long serialVersionUID = -5908427725588553371L; |
52 |
|
53 |
private String name; |
54 |
private String description; |
55 |
private Hashtable extensionDescriptions = new Hashtable(); |
56 |
private Hashtable aliases = new Hashtable(); |
57 |
|
58 |
/**
|
59 |
* Construye un punto de extension.
|
60 |
* <br>
|
61 |
* @param extensionPointName Nombre del punto de extension.
|
62 |
*/
|
63 |
public ExtensionPoint(String extensionPointName) { |
64 |
this.name = extensionPointName;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* Construye un punto de extension.
|
69 |
* <br>
|
70 |
* @param extensionPointName Nombre del punto de extension
|
71 |
* @param description Descripcion del punto de extension
|
72 |
*/
|
73 |
public ExtensionPoint(String extensionPointName, String description) { |
74 |
this.name = extensionPointName;
|
75 |
this.description = description;
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* Retorna el nombre de punto de extension.
|
80 |
* <br>
|
81 |
* @return Nombre del punto de extension
|
82 |
*/
|
83 |
public String getName() { |
84 |
return this.name; |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Retorna la descripcion asociada al punto de extension.
|
89 |
* <br>
|
90 |
* @return descripcion del punto de extension
|
91 |
*/
|
92 |
public String getDescription() { |
93 |
return this.description; |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Asocia una descripcion al punto de extension.
|
98 |
* <br>
|
99 |
*
|
100 |
* @param description
|
101 |
*/
|
102 |
public void setDescripcion(String description) { |
103 |
this.description = description;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Retorna la descripcion asociada a una extension.
|
108 |
* <br>
|
109 |
* @param key
|
110 |
* <br>
|
111 |
* @return descripcion del punto de extension
|
112 |
*/
|
113 |
public String getExtensionDescription(String key) { |
114 |
return (String)this.extensionDescriptions.get(key); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Asocia una descripcion a una extension registrada.
|
119 |
* <br>
|
120 |
*
|
121 |
* @param key Nombre de la extension
|
122 |
* @param description
|
123 |
*/
|
124 |
public void setExtensionDescription(String key,String description) { |
125 |
if (this.containsKey(key)) { |
126 |
this.extensionDescriptions.put(key,description);
|
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* A?ade una extension con su descripcion asociada
|
132 |
*
|
133 |
* @param key clave de la extension
|
134 |
* @param decription descripcion de la extension
|
135 |
* @param value extension
|
136 |
*/
|
137 |
|
138 |
public Object put(String key, String description,Object value) { |
139 |
this.extensionDescriptions.put(key,description);
|
140 |
return super.put(key,value); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* A?ade una extension antes de la indicada con beforeKey,
|
145 |
* con su descripcion asociada
|
146 |
*
|
147 |
* @param key clave de la extension
|
148 |
* @param decription descripcion de la extension
|
149 |
* @param value extension
|
150 |
*/
|
151 |
|
152 |
public Object insert(String beforeKey, String key, String description,Object value) { |
153 |
boolean mover = false; |
154 |
Map tmp = new LinkedHashMap(); |
155 |
|
156 |
for (Iterator i = this.entrySet().iterator(); i.hasNext(); ) { |
157 |
Map.Entry e = (Map.Entry)i.next(); |
158 |
if ( e.getKey().equals(beforeKey) ) {
|
159 |
mover = true;
|
160 |
} |
161 |
if( mover ) {
|
162 |
tmp.put(e.getKey(), e.getValue()); |
163 |
} |
164 |
} |
165 |
for (Iterator i = tmp.keySet().iterator(); i.hasNext(); ) { |
166 |
String key1 = (String)i.next(); |
167 |
this.remove(key1);
|
168 |
} |
169 |
if ( description!= null ) { |
170 |
this.extensionDescriptions.put(key,description);
|
171 |
} |
172 |
Object returnValue = super.put(key,value); |
173 |
this.putAll(tmp);
|
174 |
return returnValue;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Crea una extension.
|
179 |
* <br>
|
180 |
* Dado un nombre de extension asociada a este punto de extension, crea
|
181 |
* el objeto registrado para manejar la extension.
|
182 |
* <br>
|
183 |
* Si el objeto registrado para esa extension implementa el interface
|
184 |
* <i>IExtensionBuilder</i>, se invoca al metodo create para crear la instancia
|
185 |
* de la extension.
|
186 |
* <br>
|
187 |
* Si no implementa este interface, debera ser una clase, y se creara una
|
188 |
* instancia de esa clase.
|
189 |
* <br>
|
190 |
* @param name Nombre de la extension a crear.
|
191 |
* @return La instancia creada de la extension.
|
192 |
*
|
193 |
* @throws InstantiationException
|
194 |
* @throws IllegalAccessException
|
195 |
*/
|
196 |
public Object create(String name) throws InstantiationException, IllegalAccessException { |
197 |
Object extension = this.get(name); |
198 |
if (extension == null) { |
199 |
extension = this.get(this.aliases.get(name)); |
200 |
} |
201 |
|
202 |
if( extension instanceof IExtensionBuilder ) { |
203 |
return ((IExtensionBuilder)extension).create();
|
204 |
} |
205 |
return ExtensionBuilder.create((Class) extension); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Crea una extension.
|
210 |
* <br>
|
211 |
* Dado un nombre de extension asociada a este punto de extension, crea
|
212 |
* el objeto registrado para manejar la extension.
|
213 |
* <br>
|
214 |
* A la hora de crear la instancia de la extension, le pasara los parametros
|
215 |
* indicados en <i>args</i>.
|
216 |
* <br>
|
217 |
* Debido a que los argumentos se pasan como un array de objetos, no es posible
|
218 |
* pasar al constructor de la extension parametros de tipos basicos como <i>int</i>
|
219 |
* o <i>long</i>. Se deberan pasar como objetos y existir un constructor de la clase
|
220 |
* que los pueda recibir de esta manera.
|
221 |
* <br>
|
222 |
* @param name Nombre de la extension a crear.
|
223 |
* @param args Array de objetos a pasar como parametros en la construccion de la instancia de la extension.
|
224 |
* @return La instancia creada de la extension.
|
225 |
*
|
226 |
* @throws SecurityException
|
227 |
* @throws NoSuchMethodException
|
228 |
* @throws IllegalArgumentException
|
229 |
* @throws InstantiationException
|
230 |
* @throws IllegalAccessException
|
231 |
* @throws InvocationTargetException
|
232 |
*/
|
233 |
public Object create(String name, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
234 |
Object extension = this.get(name); |
235 |
if (extension == null) { |
236 |
extension = this.get(this.aliases.get(name)); |
237 |
} |
238 |
|
239 |
if( extension instanceof IExtensionBuilder ) { |
240 |
return ((IExtensionBuilder)extension).create(args);
|
241 |
} |
242 |
return ExtensionBuilder.create((Class) extension, args); |
243 |
} |
244 |
|
245 |
public Object create(String name, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
246 |
Object extension = this.get(name); |
247 |
if (extension == null) { |
248 |
extension = this.get(this.aliases.get(name)); |
249 |
} |
250 |
|
251 |
if( extension instanceof IExtensionBuilder ) { |
252 |
return ((IExtensionBuilder)extension).create(args);
|
253 |
} |
254 |
return ExtensionBuilder.create((Class) extension, args); |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Crea un alias para una extension registrada.
|
259 |
* <br>
|
260 |
* @param item Nombre de la extension registrada.
|
261 |
* @param alias alias a a?adir.
|
262 |
*
|
263 |
**/
|
264 |
public void addAlias(String item, String alias) throws KeyException{ |
265 |
if (!this.containsKey(item)) { |
266 |
throw new KeyException(item); |
267 |
} |
268 |
this.aliases.put(alias,item);
|
269 |
} |
270 |
} |