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