root / trunk / frameworks / _fwAndami / src / com / iver / andami / PluginServices.java @ 10999
History | View | Annotate | Download (16 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.Frame; |
45 |
import java.awt.KeyEventDispatcher; |
46 |
import java.awt.Toolkit; |
47 |
import java.awt.datatransfer.DataFlavor; |
48 |
import java.awt.datatransfer.StringSelection; |
49 |
import java.awt.datatransfer.UnsupportedFlavorException; |
50 |
import java.awt.event.ActionListener; |
51 |
import java.io.File; |
52 |
import java.io.IOException; |
53 |
import java.lang.reflect.InvocationTargetException; |
54 |
import java.util.ArrayList; |
55 |
import java.util.HashMap; |
56 |
import java.util.Iterator; |
57 |
|
58 |
import javax.swing.KeyStroke; |
59 |
import javax.swing.SwingUtilities; |
60 |
import javax.swing.Timer; |
61 |
|
62 |
import org.apache.log4j.Logger; |
63 |
|
64 |
import com.iver.andami.authentication.IAuthentication; |
65 |
import com.iver.andami.messages.Messages; |
66 |
import com.iver.andami.messages.NotificationManager; |
67 |
import com.iver.andami.plugins.ExtensionDecorator; |
68 |
import com.iver.andami.plugins.IExtension; |
69 |
import com.iver.andami.plugins.PluginClassLoader; |
70 |
import com.iver.andami.preferences.DlgPreferences; |
71 |
import com.iver.andami.ui.mdiFrame.MDIFrame; |
72 |
import com.iver.andami.ui.mdiFrame.MainFrame; |
73 |
import com.iver.andami.ui.mdiManager.MDIManager; |
74 |
import com.iver.utiles.XMLEntity; |
75 |
import com.iver.utiles.swing.threads.IMonitorableTask; |
76 |
import com.iver.utiles.swing.threads.IProgressMonitorIF; |
77 |
import com.iver.utiles.swing.threads.TaskMonitorTimerListener; |
78 |
import com.iver.utiles.swing.threads.UndefinedProgressMonitor; |
79 |
|
80 |
/**
|
81 |
* Clase que proporciona servicios a los plugins.
|
82 |
*
|
83 |
* @author Fernando Gonz?lez Cort?s
|
84 |
*/
|
85 |
public class PluginServices { |
86 |
private static Logger logger = Logger.getLogger(PluginServices.class |
87 |
.getName()); |
88 |
|
89 |
private static String[] arguments; |
90 |
|
91 |
private static IAuthentication authentication; |
92 |
|
93 |
private static IExtension exclusiveUIExtension = null; |
94 |
|
95 |
private PluginClassLoader loader;
|
96 |
|
97 |
private XMLEntity persistentXML;
|
98 |
|
99 |
/**
|
100 |
* Crea un nuevo PluginServices
|
101 |
*
|
102 |
* @param loader
|
103 |
* ClassLoader del plugin
|
104 |
*/
|
105 |
public PluginServices(PluginClassLoader loader) {
|
106 |
this.loader = loader;
|
107 |
} |
108 |
|
109 |
/**
|
110 |
* Devuelve el mensaje en el idioma del locale actual del texto
|
111 |
* correspondiente a la clave que se pasa como par?metro
|
112 |
*
|
113 |
* @param key
|
114 |
* Clave del texto que se quiere obtener
|
115 |
*
|
116 |
* @return Texto en el idioma que toca o la propia clave si no se encuentra
|
117 |
* en el fichero
|
118 |
*/
|
119 |
public String getText(String key) { |
120 |
if (key == null) return null; |
121 |
String translation = org.gvsig.i18n.Messages.getText(key, false); |
122 |
if (translation != null) |
123 |
return translation;
|
124 |
else
|
125 |
{ |
126 |
logger.warn("Plugin "+getPluginName()+" -- "+org.gvsig.i18n.Messages.getText("No_se_encontro_la_traduccion_para", false)+ " " + key); |
127 |
return key;
|
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Obtiene el classloader del plugin
|
133 |
*
|
134 |
* @return Returns the loader.
|
135 |
*/
|
136 |
public PluginClassLoader getClassLoader() {
|
137 |
return loader;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Obtiene el nombre del plugin
|
142 |
*
|
143 |
* @return String
|
144 |
*/
|
145 |
String getPluginName() {
|
146 |
return loader.getPluginName();
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* Obtienen una referencia al PluginServices del plugin cuyo nombre se pasa
|
151 |
* como par?metro
|
152 |
*
|
153 |
* @param pluginClassInstance
|
154 |
* Instancia de una clase propia del plugin a cuyos servicios se
|
155 |
* quiere acceder
|
156 |
*
|
157 |
* @return Objeto PluginServices asociado al plugin
|
158 |
*
|
159 |
* @throws RuntimeException
|
160 |
* Si el par?metro no es un objeto cargado desde un plugin
|
161 |
*/
|
162 |
public static PluginServices getPluginServices(Object pluginClassInstance) { |
163 |
try {
|
164 |
PluginClassLoader loader = (PluginClassLoader) pluginClassInstance |
165 |
.getClass().getClassLoader(); |
166 |
|
167 |
return Launcher.getPluginServices(loader.getPluginName());
|
168 |
} catch (ClassCastException e) { |
169 |
/*
|
170 |
* throw new RuntimeException( "Parameter is not a plugin class
|
171 |
* instance");
|
172 |
*/
|
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
|
182 |
* Instancia de una clase propia del plugin a cuyos servicios se
|
183 |
* quiere acceder
|
184 |
*
|
185 |
* @return Objeto PluginServices asociado al plugin
|
186 |
*/
|
187 |
public static PluginServices getPluginServices(String pluginName) { |
188 |
return Launcher.getPluginServices(pluginName);
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* Obtiene una referencia al gestor de ventanas
|
193 |
*
|
194 |
* @return MDIManager
|
195 |
*/
|
196 |
public static MDIManager getMDIManager() { |
197 |
return Launcher.getFrame().getMDIManager();
|
198 |
} |
199 |
|
200 |
/**
|
201 |
* Obtiene una referencia al marco principal de la aplicaci?n
|
202 |
*
|
203 |
* @return MainFrame
|
204 |
*/
|
205 |
public static MainFrame getMainFrame() { |
206 |
return Launcher.getFrame();
|
207 |
} |
208 |
|
209 |
public static void registerKeyStroke(KeyStroke key, KeyEventDispatcher a) |
210 |
{ |
211 |
GlobalKeyEventDispatcher.getInstance().registerKeyStroke(key, a); |
212 |
} |
213 |
|
214 |
public static void unRegisterKeyStroke(KeyStroke key) |
215 |
{ |
216 |
GlobalKeyEventDispatcher.getInstance().removeKeyStrokeBinding(key); |
217 |
} |
218 |
|
219 |
/**
|
220 |
* Obtiene una referencia a la instancia de la extensi?n cuya clase se pasa
|
221 |
* como par?metro
|
222 |
*
|
223 |
* @param extensionClass
|
224 |
* Clase de la extensi?n cuya instancia se quiere obtener
|
225 |
*
|
226 |
* @return Instancia de la extensi?n o null en caso de que no haya una
|
227 |
* extensi?n con esa clase
|
228 |
*/
|
229 |
public static IExtension getExtension(Class extensionClass) { |
230 |
ExtensionDecorator extAux = (ExtensionDecorator) Launcher |
231 |
.getClassesExtensions().get(extensionClass); |
232 |
return extAux.getExtension();
|
233 |
// return (Extension)
|
234 |
// Launcher.getClassesExtensions().get(extensionClass);
|
235 |
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Gets a reference to the Extension Decorator with support for several more
|
240 |
* options
|
241 |
*
|
242 |
* @param extensionClass
|
243 |
* @return
|
244 |
*/
|
245 |
public static ExtensionDecorator getDecoratedExtension(Class extensionClass) { |
246 |
return (ExtensionDecorator) Launcher.getClassesExtensions().get(
|
247 |
extensionClass); |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Returns an array containing references to all the loaded extensions.
|
252 |
* @return ExtensionDecorator[]
|
253 |
*/
|
254 |
public static ExtensionDecorator[] getDecoratedExtensions() { |
255 |
HashMap map = Launcher.getClassesExtensions();
|
256 |
ExtensionDecorator[] extensions = (ExtensionDecorator[]) map. |
257 |
values().toArray(new ExtensionDecorator[0]); |
258 |
return extensions;
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* Obtiene un iterador sobre las extensiones
|
263 |
*
|
264 |
* @return Iterator
|
265 |
*/
|
266 |
public static Iterator getExtensions() { |
267 |
return Launcher.getClassesExtensions().values().iterator();
|
268 |
} |
269 |
|
270 |
/**
|
271 |
* Obtiene una traducci?n de un plugin
|
272 |
*
|
273 |
* @param pluginObject
|
274 |
* Objeto cargado desde un plugin
|
275 |
* @param key
|
276 |
* Nombre de la clave cuyo valor se quiere obtener
|
277 |
*
|
278 |
* @return El valor, si existe. La clave si no existe. Si el objeto que se
|
279 |
* pasa como par?metro no ha sido cargado desde un plugin se
|
280 |
* devuelve la clave.
|
281 |
*/
|
282 |
public static String getText(Object pluginObject, String key) { |
283 |
if (key == null) return null; |
284 |
String translation = org.gvsig.i18n.Messages.getText(key, false); |
285 |
if (translation != null) |
286 |
return translation;
|
287 |
else
|
288 |
{ |
289 |
logger.warn(org.gvsig.i18n.Messages.getText("No_se_encontro_la_traduccion_para",false) + " "+ key); |
290 |
return key;
|
291 |
} |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Establece los datos del plugin que deber?n persistir entre ejecuciones en
|
296 |
* formato xml
|
297 |
*
|
298 |
* @param entity
|
299 |
* DOCUMENT ME!
|
300 |
*/
|
301 |
public void setPersistentXML(XMLEntity entity) { |
302 |
persistentXML = entity; |
303 |
} |
304 |
|
305 |
/**
|
306 |
* Obtiene un objeto que representa la persistencia del plugin en formato
|
307 |
* xml.
|
308 |
*
|
309 |
* @return Devuelve null hasta que se invoca el m?todo setPersistentXML
|
310 |
*/
|
311 |
public XMLEntity getPersistentXML() {
|
312 |
if (persistentXML == null) { |
313 |
persistentXML = new XMLEntity();
|
314 |
} |
315 |
return persistentXML;
|
316 |
} |
317 |
|
318 |
/**
|
319 |
* A?ade un listener a un popup menu registrado en el config.xml de alg?n
|
320 |
* plugin
|
321 |
*
|
322 |
* @param name
|
323 |
* Nombre del men? contextual
|
324 |
* @param c
|
325 |
* Componente que desplegar? el men? cuando se haga click con el
|
326 |
* bot?n derecho
|
327 |
* @param listener
|
328 |
* Listener que se ejecutar? cuando se seleccione cualquier
|
329 |
* entrada del men?
|
330 |
*
|
331 |
* @throws RuntimeException
|
332 |
* Si la interfaz no est? preparada todav?a. S?lo puede darse
|
333 |
* durante el arranque
|
334 |
*/
|
335 |
public void addPopupMenuListener(String name, Component c, |
336 |
ActionListener listener) {
|
337 |
MDIFrame frame = Launcher.getFrame(); |
338 |
|
339 |
if (frame == null) { |
340 |
throw new RuntimeException("MDIFrame not loaded yet"); |
341 |
} |
342 |
|
343 |
frame.addPopupMenuListener(name, c, listener, loader); |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Obtiene una referencia al directorio del plugin
|
348 |
*
|
349 |
* @return File
|
350 |
*/
|
351 |
public File getPluginDirectory() { |
352 |
return new File(Launcher.getPluginsDir() + File.separator |
353 |
+ getPluginName()); |
354 |
} |
355 |
|
356 |
/**
|
357 |
* Ejecuta una tarea en segundo plano, dejando a la interfaz responder pero
|
358 |
* inhibiendo los eventos
|
359 |
*
|
360 |
* @param r
|
361 |
* Tarea a ejecutar
|
362 |
*
|
363 |
* @return Thread en el que se ejecuta la tarea
|
364 |
*/
|
365 |
public static Thread backgroundExecution(Runnable r) { |
366 |
Thread t = new Thread(new RunnableDecorator(r)); |
367 |
t.start(); |
368 |
|
369 |
return t;
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* Runs a task in background. This task could be monitored and canceled, and
|
374 |
* doesnt inhibed any event.
|
375 |
*
|
376 |
* @param task
|
377 |
* IMonitorableTask
|
378 |
*/
|
379 |
public static void cancelableBackgroundExecution(final IMonitorableTask task) { |
380 |
final com.iver.utiles.swing.threads.SwingWorker worker = new com.iver.utiles.swing.threads.SwingWorker() { |
381 |
public Object construct() { |
382 |
try {
|
383 |
task.run(); |
384 |
return task;
|
385 |
} catch (Exception e) { |
386 |
NotificationManager.addError(null, e);
|
387 |
} |
388 |
return null; |
389 |
} |
390 |
}; |
391 |
|
392 |
Component mainFrame = (Component) PluginServices.getMainFrame(); |
393 |
|
394 |
IProgressMonitorIF progressMonitor = null;
|
395 |
String title = getText(null, "PluginServices.Procesando"); |
396 |
progressMonitor = new UndefinedProgressMonitor((Frame) mainFrame, title); |
397 |
progressMonitor.setIndeterminated(!task.isDefined()); |
398 |
progressMonitor.setInitialStep(task.getInitialStep()); |
399 |
progressMonitor.setLastStep(task.getFinishStep()); |
400 |
progressMonitor.setCurrentStep(task.getCurrentStep()); |
401 |
progressMonitor.setMainTitleLabel(task.getStatusMessage()); |
402 |
progressMonitor.setNote(task.getNote()); |
403 |
progressMonitor.open(); |
404 |
int delay = 500; |
405 |
TaskMonitorTimerListener timerListener = new TaskMonitorTimerListener(
|
406 |
progressMonitor, task); |
407 |
Timer timer = new Timer(delay, timerListener); |
408 |
timerListener.setTimer(timer); |
409 |
timer.start(); |
410 |
|
411 |
worker.start(); |
412 |
|
413 |
} |
414 |
|
415 |
public static void closeApplication() { |
416 |
Launcher.closeApplication(); |
417 |
} |
418 |
|
419 |
/**
|
420 |
* DOCUMENT ME!
|
421 |
*
|
422 |
* @author Fernando Gonz?lez Cort?s
|
423 |
*/
|
424 |
private static class RunnableDecorator implements Runnable { |
425 |
private Runnable r; |
426 |
|
427 |
/**
|
428 |
* Crea un nuevo RunnableDecorator.
|
429 |
*
|
430 |
* @param r
|
431 |
* DOCUMENT ME!
|
432 |
*/
|
433 |
public RunnableDecorator(Runnable r) { |
434 |
this.r = r;
|
435 |
} |
436 |
|
437 |
/**
|
438 |
* @see java.lang.Runnable#run()
|
439 |
*/
|
440 |
public void run() { |
441 |
/*
|
442 |
* try { SwingUtilities.invokeAndWait(new Runnable() { public void
|
443 |
* run() { getMDIManager().setWaitCursor(); } }); } catch
|
444 |
* (InterruptedException e) {
|
445 |
* NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
446 |
* e); } catch (InvocationTargetException e) {
|
447 |
* NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
448 |
* e); }
|
449 |
*/
|
450 |
|
451 |
try {
|
452 |
SwingUtilities.invokeAndWait(new Runnable() { |
453 |
public void run() { |
454 |
try {
|
455 |
r.run(); |
456 |
} catch (RuntimeException e) { |
457 |
NotificationManager |
458 |
.addError( |
459 |
Messages |
460 |
.getString("PluginServices.Bug_en_el_codigo"),
|
461 |
e); |
462 |
} catch (Error e) { |
463 |
NotificationManager |
464 |
.addError( |
465 |
Messages |
466 |
.getString("PluginServices.Error_grave_de_la_aplicaci?n"),
|
467 |
e); |
468 |
} |
469 |
} |
470 |
}); |
471 |
} catch (InterruptedException e) { |
472 |
NotificationManager |
473 |
.addWarning( |
474 |
Messages |
475 |
.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
476 |
e); |
477 |
} catch (InvocationTargetException e) { |
478 |
NotificationManager |
479 |
.addWarning( |
480 |
Messages |
481 |
.getString("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
482 |
e); |
483 |
} |
484 |
|
485 |
/*
|
486 |
* try { SwingUtilities.invokeAndWait(new Runnable() { public void
|
487 |
* run() { getMDIManager().restoreCursor(); } }); } catch
|
488 |
* (InterruptedException e) {
|
489 |
* NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_restaurar_el_cursor_del_raton"),
|
490 |
* e); } catch (InvocationTargetException e) {
|
491 |
* NotificationManager.addWarning(Messages.getString("PluginServices.No_se_pudo_restaurar_el_cursor_del_raton"),
|
492 |
* e); }
|
493 |
*/
|
494 |
} |
495 |
} |
496 |
|
497 |
/**
|
498 |
* Usually appName plugins-directory [locale] [other args]
|
499 |
*
|
500 |
* @return the original arguments that Andami received. (app-name
|
501 |
* plugins-directory, locale, etc)
|
502 |
*/
|
503 |
public static String[] getArguments() { |
504 |
return arguments;
|
505 |
} |
506 |
|
507 |
|
508 |
public static void setArguments(String[] arguments) { |
509 |
PluginServices.arguments = arguments; |
510 |
} |
511 |
|
512 |
/**
|
513 |
* Return the value of a command line named argument.
|
514 |
*<br>
|
515 |
*<br>
|
516 |
* The argument format is:
|
517 |
*<br>
|
518 |
* -{argumentName}={argumentValue}
|
519 |
*<br>
|
520 |
*<br>
|
521 |
*example:
|
522 |
* <br>
|
523 |
* ' -language=en '
|
524 |
* @return String value
|
525 |
*/
|
526 |
public static String getArgumentByName(String name) { |
527 |
for (int i=2; i < PluginServices.arguments.length; i++) |
528 |
{ |
529 |
int index = PluginServices.arguments[i].indexOf(name +"="); |
530 |
if (index != -1) |
531 |
return PluginServices.arguments[i].substring(index+name.length()+1); |
532 |
} |
533 |
return null; |
534 |
} |
535 |
|
536 |
public static Logger getLogger() { |
537 |
return logger;
|
538 |
} |
539 |
|
540 |
public static IAuthentication getAuthentication() { |
541 |
return authentication;
|
542 |
} |
543 |
|
544 |
public static void setAuthentication(IAuthentication authen) { |
545 |
authentication = authen; |
546 |
} |
547 |
public static DlgPreferences getDlgPreferences() { |
548 |
return DlgPreferences.getInstance();
|
549 |
} |
550 |
|
551 |
public static void putInClipboard(String data) { |
552 |
StringSelection ss = new StringSelection(data); |
553 |
|
554 |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
|
555 |
} |
556 |
|
557 |
public static String getFromClipboard() { |
558 |
|
559 |
try {
|
560 |
return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); |
561 |
} catch (UnsupportedFlavorException e) { |
562 |
return null; |
563 |
} catch (IOException e) { |
564 |
// TODO Auto-generated catch block
|
565 |
return null; |
566 |
} |
567 |
} |
568 |
|
569 |
|
570 |
/**
|
571 |
* Gets the extesion that have to take the control
|
572 |
* of the state of action controls of the UI of all extensions.
|
573 |
* <br>
|
574 |
* <br>
|
575 |
* -exclusiveUI={pathToExtensionClass}
|
576 |
* <br>
|
577 |
* @see com.iver.andami.Launcher#initializeExclusiveUIExtension()
|
578 |
* @see com.iver.andami.plugins.IExtension#isEnabled(IExtension extension)
|
579 |
* @see com.iver.andami.plugins.IExtension#isVisible(IExtension extension)
|
580 |
*/
|
581 |
|
582 |
public static IExtension getExclusiveUIExtension() { |
583 |
return PluginServices.exclusiveUIExtension;
|
584 |
} |
585 |
|
586 |
static void setExclusiveUIExtension(IExtension extension) { |
587 |
PluginServices.exclusiveUIExtension = extension; |
588 |
} |
589 |
|
590 |
public static void addLoaders(ArrayList classLoaders) { |
591 |
PluginClassLoader.addLoaders(classLoaders); |
592 |
} |
593 |
} |