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