svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / frameworks / _fwAndami / src / com / iver / andami / PluginServices.java @ 5222
History | View | Annotate | Download (11.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 |
package com.iver.andami; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.awt.event.ActionListener; |
45 |
import java.io.File; |
46 |
import java.lang.reflect.InvocationTargetException; |
47 |
import java.util.Iterator; |
48 |
import java.util.Locale; |
49 |
import java.util.MissingResourceException; |
50 |
import java.util.PropertyResourceBundle; |
51 |
import java.util.ResourceBundle; |
52 |
|
53 |
import javax.swing.SwingUtilities; |
54 |
|
55 |
import org.apache.log4j.Logger; |
56 |
|
57 |
import com.iver.andami.messages.Messages; |
58 |
import com.iver.andami.messages.NotificationManager; |
59 |
import com.iver.andami.plugins.Extension; |
60 |
import com.iver.andami.plugins.ExtensionDecorator; |
61 |
import com.iver.andami.plugins.PluginClassLoader; |
62 |
import com.iver.andami.ui.mdiFrame.MDIFrame; |
63 |
import com.iver.andami.ui.mdiFrame.MainFrame; |
64 |
import com.iver.andami.ui.mdiManager.MDIManager; |
65 |
import com.iver.utiles.XMLEntity; |
66 |
|
67 |
|
68 |
/**
|
69 |
* Clase que proporciona servicios a los plugins.
|
70 |
*
|
71 |
* @author Fernando Gonz?lez Cort?s
|
72 |
*/
|
73 |
public class PluginServices { |
74 |
private static Logger logger = Logger.getLogger(PluginServices.class.getName()); |
75 |
|
76 |
private static String[] arguments; |
77 |
|
78 |
private PluginClassLoader loader;
|
79 |
private PropertyResourceBundle resourceBundle; |
80 |
private XMLEntity persistentXML;
|
81 |
|
82 |
/**
|
83 |
* Crea un nuevo PluginServices
|
84 |
*
|
85 |
* @param loader ClassLoader del plugin
|
86 |
*/
|
87 |
public PluginServices(PluginClassLoader loader) {
|
88 |
this.loader = loader;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Devuelve el mensaje en el idioma del locale actual del texto
|
93 |
* correspondiente a la clave que se pasa como par?metro
|
94 |
*
|
95 |
* @param key Clave del texto que se quiere obtener
|
96 |
*
|
97 |
* @return Texto en el idioma que toca o la propia clave si no se encuentra
|
98 |
* en el fichero
|
99 |
*/
|
100 |
public String getText(String key) { |
101 |
if (resourceBundle == null) { |
102 |
return key;
|
103 |
} |
104 |
|
105 |
if (key == null) { |
106 |
return null; |
107 |
} |
108 |
|
109 |
try {
|
110 |
return resourceBundle.getString(key);
|
111 |
} catch (MissingResourceException e) { |
112 |
logger.warn(getPluginName()+": "+Messages.getString("PluginServices.No_se_encontro_la_traduccion_para") + " " + key); |
113 |
|
114 |
return key;
|
115 |
} |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Obtiene el classloader del plugin
|
120 |
*
|
121 |
* @return Returns the loader.
|
122 |
*/
|
123 |
public PluginClassLoader getClassLoader() {
|
124 |
return loader;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Obtiene el nombre del plugin
|
129 |
*
|
130 |
* @return String
|
131 |
*/
|
132 |
String getPluginName() {
|
133 |
return loader.getPluginName();
|
134 |
} |
135 |
|
136 |
/**
|
137 |
* Establece el paquete de traducciones del objeto
|
138 |
* pluginservices asociado a un plugin
|
139 |
*
|
140 |
* @param name nombre del paquete
|
141 |
* @param locale locale para obtener los recursos del paquete
|
142 |
*/
|
143 |
void setResourceBundle(String name, Locale locale) { |
144 |
try {
|
145 |
resourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle(name, |
146 |
locale, loader); |
147 |
} catch (MissingResourceException e) { |
148 |
logger.error(Messages.getString("PluginServices.No_se_encontro_el_recurso_de_traducciones") +
|
149 |
" '"+name+"' en "+getPluginName(), e); |
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Obtienen una referencia al PluginServices del plugin cuyo nombre se pasa
|
155 |
* como par?metro
|
156 |
*
|
157 |
* @param pluginClassInstance Instancia de una clase propia del plugin a
|
158 |
* cuyos servicios se quiere acceder
|
159 |
*
|
160 |
* @return Objeto PluginServices asociado al plugin
|
161 |
*
|
162 |
* @throws RuntimeException Si el par?metro no es un objeto cargado desde un plugin
|
163 |
*/
|
164 |
public static PluginServices getPluginServices(Object pluginClassInstance) { |
165 |
try {
|
166 |
PluginClassLoader loader = (PluginClassLoader) pluginClassInstance.getClass() |
167 |
.getClassLoader(); |
168 |
|
169 |
return Launcher.getPluginServices(loader.getPluginName());
|
170 |
} catch (ClassCastException e) { |
171 |
/* throw new RuntimeException(
|
172 |
"Parameter is not a plugin class instance"); */
|
173 |
return null; |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Obtienen una referencia al PluginServices del plugin cuyo nombre se pasa
|
179 |
* como par?metro
|
180 |
*
|
181 |
* @param pluginName Instancia de una clase propia del plugin a cuyos
|
182 |
* servicios se quiere acceder
|
183 |
*
|
184 |
* @return Objeto PluginServices asociado al plugin
|
185 |
*/
|
186 |
public static PluginServices getPluginServices(String pluginName) { |
187 |
return Launcher.getPluginServices(pluginName);
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Obtiene una referencia al gestor de ventanas
|
192 |
*
|
193 |
* @return MDIManager
|
194 |
*/
|
195 |
public static MDIManager getMDIManager() { |
196 |
return Launcher.getFrame().getMDIManager();
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Obtiene una referencia al marco principal de la
|
201 |
* aplicaci?n
|
202 |
*
|
203 |
* @return MainFrame
|
204 |
*/
|
205 |
public static MainFrame getMainFrame() { |
206 |
return Launcher.getFrame();
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* Obtiene una referencia a la instancia de la extensi?n
|
211 |
* cuya clase se pasa como par?metro
|
212 |
*
|
213 |
* @param extensionClass Clase de la extensi?n cuya instancia se quiere obtener
|
214 |
*
|
215 |
* @return Instancia de la extensi?n o null en caso de que no haya
|
216 |
* una extensi?n con esa clase
|
217 |
*/
|
218 |
public static Extension getExtension(Class extensionClass) { |
219 |
ExtensionDecorator extAux = (ExtensionDecorator) Launcher.getClassesExtensions().get(extensionClass); |
220 |
return extAux.getExtension();
|
221 |
// return (Extension) Launcher.getClassesExtensions().get(extensionClass);
|
222 |
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* Gets a reference to the Extension Decorator with support for several more options
|
227 |
* @param extensionClass
|
228 |
* @return
|
229 |
*/
|
230 |
public static ExtensionDecorator getDecoratedExtension(Class extensionClass) { |
231 |
return (ExtensionDecorator) Launcher.getClassesExtensions().get(extensionClass);
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* Obtiene un iterador sobre las extensiones
|
236 |
*
|
237 |
* @return Iterator
|
238 |
*/
|
239 |
public static Iterator getExtensions() { |
240 |
return Launcher.getClassesExtensions().values().iterator();
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Obtiene una traducci?n de un plugin
|
245 |
*
|
246 |
* @param pluginObject Objeto cargado desde un plugin
|
247 |
* @param key Nombre de la clave cuyo valor se quiere obtener
|
248 |
*
|
249 |
* @return El valor, si existe. La clave si no existe. Si el objeto que
|
250 |
* se pasa como par?metro no ha sido cargado desde un plugin
|
251 |
* se devuelve la clave.
|
252 |
*/
|
253 |
public static String getText(Object pluginObject, String key) { |
254 |
try {
|
255 |
PluginServices ps = getPluginServices(pluginObject); |
256 |
if (ps == null) return key; |
257 |
return ps.getText(key);
|
258 |
} catch (RuntimeException e) { |
259 |
logger.error("GetText: Clave = " + key + ". ", e); |
260 |
|
261 |
return key;
|
262 |
} |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Establece los datos del plugin que deber?n persistir entre ejecuciones
|
267 |
* en formato xml
|
268 |
*
|
269 |
* @param entity DOCUMENT ME!
|
270 |
*/
|
271 |
public void setPersistentXML(XMLEntity entity) { |
272 |
persistentXML = entity; |
273 |
} |
274 |
|
275 |
/**
|
276 |
* Obtiene un objeto que representa la persistencia del plugin en formato
|
277 |
* xml.
|
278 |
*
|
279 |
* @return Devuelve null hasta que se invoca el m?todo setPersistentXML
|
280 |
*/
|
281 |
public XMLEntity getPersistentXML() {
|
282 |
if (persistentXML == null){ |
283 |
persistentXML = new XMLEntity();
|
284 |
} |
285 |
return persistentXML;
|
286 |
} |
287 |
|
288 |
/**
|
289 |
* A?ade un listener a un popup menu registrado en el
|
290 |
* config.xml de alg?n plugin
|
291 |
*
|
292 |
* @param name Nombre del men? contextual
|
293 |
* @param c Componente que desplegar? el men? cuando
|
294 |
* se haga click con el bot?n derecho
|
295 |
* @param listener Listener que se ejecutar? cuando se
|
296 |
* seleccione cualquier entrada del men?
|
297 |
*
|
298 |
* @throws RuntimeException Si la interfaz no est? preparada
|
299 |
* todav?a. S?lo puede darse durante el arranque
|
300 |
*/
|
301 |
public void addPopupMenuListener(String name, Component c, |
302 |
ActionListener listener) {
|
303 |
MDIFrame frame = Launcher.getFrame(); |
304 |
|
305 |
if (frame == null) { |
306 |
throw new RuntimeException("MDIFrame not loaded yet"); |
307 |
} |
308 |
|
309 |
frame.addPopupMenuListener(name, c, listener, loader); |
310 |
} |
311 |
|
312 |
/**
|
313 |
* Obtiene una referencia al directorio del plugin
|
314 |
*
|
315 |
* @return File
|
316 |
*/
|
317 |
public File getPluginDirectory() { |
318 |
return new File(Launcher.getPluginsDir() + File.separator + |
319 |
getPluginName()); |
320 |
} |
321 |
|
322 |
/**
|
323 |
* Ejecuta una tarea en segundo plano, dejando a la
|
324 |
* interfaz responder pero inhibiendo los eventos
|
325 |
*
|
326 |
* @param r Tarea a ejecutar
|
327 |
*
|
328 |
* @return Thread en el que se ejecuta la tarea
|
329 |
*/
|
330 |
public static Thread backgroundExecution(Runnable r) { |
331 |
Thread t = new Thread(new RunnableDecorator(r)); |
332 |
t.start(); |
333 |
|
334 |
return t;
|
335 |
} |
336 |
|
337 |
public static void closeApplication(){ |
338 |
Launcher.closeApplication(); |
339 |
} |
340 |
|
341 |
/**
|
342 |
* DOCUMENT ME!
|
343 |
*
|
344 |
* @author Fernando Gonz?lez Cort?s
|
345 |
*/
|
346 |
private static class RunnableDecorator implements Runnable { |
347 |
private Runnable r; |
348 |
|
349 |
/**
|
350 |
* Crea un nuevo RunnableDecorator.
|
351 |
*
|
352 |
* @param r DOCUMENT ME!
|
353 |
*/
|
354 |
public RunnableDecorator(Runnable r) { |
355 |
this.r = r;
|
356 |
} |
357 |
|
358 |
/**
|
359 |
* @see java.lang.Runnable#run()
|
360 |
*/
|
361 |
public void run() { |
362 |
/* try {
|
363 |
SwingUtilities.invokeAndWait(new Runnable() {
|
364 |
public void run() {
|
365 |
getMDIManager().setWaitCursor();
|
366 |
}
|
367 |
});
|
368 |
} catch (InterruptedException e) {
|
369 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
370 |
e);
|
371 |
} catch (InvocationTargetException e) {
|
372 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
373 |
e);
|
374 |
} */
|
375 |
|
376 |
try {
|
377 |
SwingUtilities.invokeAndWait(new Runnable() { |
378 |
public void run() { |
379 |
try{
|
380 |
r.run(); |
381 |
} catch(RuntimeException e){ |
382 |
NotificationManager.addError(Messages.getString("PluginServices.Bug_en_el_codigo"), e);
|
383 |
} catch (Error e){ |
384 |
NotificationManager.addError(Messages.getString("PluginServices.Error_grave_de_la_aplicaci?n"), e);
|
385 |
} |
386 |
} |
387 |
}); |
388 |
} catch (InterruptedException e) { |
389 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
390 |
e); |
391 |
} catch (InvocationTargetException e) { |
392 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
393 |
e); |
394 |
} |
395 |
|
396 |
/* try {
|
397 |
SwingUtilities.invokeAndWait(new Runnable() {
|
398 |
public void run() {
|
399 |
getMDIManager().restoreCursor();
|
400 |
}
|
401 |
});
|
402 |
} catch (InterruptedException e) {
|
403 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_restaurar_el_cursor_del_raton"),
|
404 |
e);
|
405 |
} catch (InvocationTargetException e) {
|
406 |
NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_restaurar_el_cursor_del_raton"),
|
407 |
e);
|
408 |
} */
|
409 |
} |
410 |
} |
411 |
|
412 |
/**
|
413 |
* Usually appName plugins-directory [locale] [other args]
|
414 |
*
|
415 |
* @return the original arguments that Andami received. (app-name
|
416 |
* plugins-directory, locale, etc)
|
417 |
*/
|
418 |
public static String[] getArguments() { |
419 |
return arguments;
|
420 |
} |
421 |
|
422 |
public static void setArguments(String[] arguments) { |
423 |
PluginServices.arguments = arguments; |
424 |
} |
425 |
|
426 |
} |