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