svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / PluginServices.java @ 42102
History | View | Annotate | Download (30.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.andami; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.Frame; |
28 |
import java.awt.KeyEventDispatcher; |
29 |
import java.awt.Toolkit; |
30 |
import java.awt.datatransfer.DataFlavor; |
31 |
import java.awt.datatransfer.StringSelection; |
32 |
import java.awt.datatransfer.UnsupportedFlavorException; |
33 |
import java.awt.event.ActionListener; |
34 |
import java.io.File; |
35 |
import java.io.FileInputStream; |
36 |
import java.io.FileNotFoundException; |
37 |
import java.io.FileOutputStream; |
38 |
import java.io.IOException; |
39 |
import java.lang.reflect.InvocationTargetException; |
40 |
import java.util.ArrayList; |
41 |
import java.util.Arrays; |
42 |
import java.util.HashMap; |
43 |
import java.util.Iterator; |
44 |
import java.util.Properties; |
45 |
|
46 |
import javax.swing.JOptionPane; |
47 |
import javax.swing.KeyStroke; |
48 |
import javax.swing.SwingUtilities; |
49 |
import javax.swing.Timer; |
50 |
|
51 |
import org.gvsig.andami.messages.NotificationManager; |
52 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
53 |
import org.gvsig.andami.plugins.ExtensionDecorator; |
54 |
import org.gvsig.andami.plugins.IExtension; |
55 |
import org.gvsig.andami.plugins.PluginClassLoader; |
56 |
import org.gvsig.andami.preferences.DlgPreferences; |
57 |
import org.gvsig.andami.ui.mdiFrame.MDIFrame; |
58 |
import org.gvsig.andami.ui.mdiFrame.MainFrame; |
59 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
60 |
import org.gvsig.i18n.Messages; |
61 |
import org.gvsig.tools.ToolsLocator; |
62 |
import org.gvsig.tools.dynobject.DynObject; |
63 |
import org.gvsig.tools.dynobject.DynStruct; |
64 |
import org.gvsig.tools.exception.BaseRuntimeException; |
65 |
import org.gvsig.tools.persistence.PersistenceManager; |
66 |
import org.gvsig.tools.persistence.PersistentState; |
67 |
import org.gvsig.tools.persistence.exception.AddDefinitionException; |
68 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
69 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
70 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
71 |
import org.gvsig.utils.XMLEntity; |
72 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
73 |
import org.gvsig.utils.swing.threads.IProgressMonitorIF; |
74 |
import org.gvsig.utils.swing.threads.TaskMonitorTimerListener; |
75 |
import org.gvsig.utils.swing.threads.UndefinedProgressMonitor; |
76 |
import org.slf4j.Logger; |
77 |
import org.slf4j.LoggerFactory; |
78 |
|
79 |
/**
|
80 |
* Provides services to Plugins. Each plugin has an associated PluginServices
|
81 |
* object, which provides specific services. Main of them: translations,
|
82 |
* logging, access to plugin's resources, background tasks, clipboard access
|
83 |
* and data persistence.
|
84 |
*
|
85 |
* @author Fernando Gonz�lez Cort�s
|
86 |
*/
|
87 |
public class PluginServices { |
88 |
|
89 |
private static Logger logger = |
90 |
LoggerFactory.getLogger(PluginServices.class); |
91 |
|
92 |
private static String[] arguments; |
93 |
|
94 |
private static ExclusiveUIExtension exclusiveUIExtension = null; |
95 |
|
96 |
private PluginClassLoader loader;
|
97 |
|
98 |
private XMLEntity persistentXML;
|
99 |
|
100 |
private DynObject pluginPersistence = null; |
101 |
|
102 |
private String[] alternativeNames = null; |
103 |
/**
|
104 |
* Creates a new PluginServices objetct.
|
105 |
*
|
106 |
* @param loader
|
107 |
* The Plugin's ClassLoader.
|
108 |
*/
|
109 |
public PluginServices(PluginClassLoader loader) {
|
110 |
this.loader = loader;
|
111 |
} |
112 |
|
113 |
public PluginServices(PluginClassLoader loader, String[] alternativeNames) { |
114 |
this(loader);
|
115 |
this.alternativeNames = alternativeNames;
|
116 |
} |
117 |
|
118 |
public String[] getAlternativeNames() { |
119 |
return this.alternativeNames; |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Returns the message in the current's locale language
|
124 |
* corresponding to the provided translation key.
|
125 |
* The key-message pairs are obtained from the plugin's
|
126 |
* translation files (text_xx.properties files).
|
127 |
*
|
128 |
* @param key
|
129 |
* Translation key whose associated message is to be obtained
|
130 |
*
|
131 |
* @return The message associated with the provided key, in one of the
|
132 |
* current locale languages, or the key if the translation is not
|
133 |
* found.
|
134 |
*/
|
135 |
public String getText(String key) { |
136 |
if (key == null) |
137 |
return null; |
138 |
String translation = org.gvsig.i18n.Messages.getText(key, false); |
139 |
if (translation != null) |
140 |
return translation;
|
141 |
else {
|
142 |
logger.debug("Can't find translation for ''{}'' in plugin ''{}''.",
|
143 |
key, |
144 |
getPluginName()); |
145 |
return key;
|
146 |
} |
147 |
} |
148 |
|
149 |
/**
|
150 |
* Gets the plugin's classloader.
|
151 |
*
|
152 |
* @return Returns the loader.
|
153 |
*/
|
154 |
public PluginClassLoader getClassLoader() {
|
155 |
return loader;
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* Gets the plugin's name
|
160 |
*
|
161 |
* @return The plugin's name
|
162 |
*/
|
163 |
public String getPluginName() { |
164 |
return loader.getPluginName();
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Gets a reference to the PluginServices object associated with the
|
169 |
* plugin containing the provided class.
|
170 |
*
|
171 |
* Obtienen una referencia al PluginServices del plugin cuyo nombre se pasa
|
172 |
* como par�metro
|
173 |
*
|
174 |
* @param pluginClassInstance
|
175 |
* An instance of a class. This class is contained in a plugin,
|
176 |
* whose
|
177 |
* services are to be obtained
|
178 |
*
|
179 |
* @return The PluginServices object associated to the containing plugin
|
180 |
*
|
181 |
* @throws RuntimeException
|
182 |
* If the parameter was not loaded from a plugin
|
183 |
*/
|
184 |
public static PluginServices getPluginServices(Object pluginClassInstance) { |
185 |
try {
|
186 |
PluginClassLoader loader; |
187 |
if( pluginClassInstance instanceof Class ) { |
188 |
loader = (PluginClassLoader) ((Class) pluginClassInstance).getClassLoader();
|
189 |
} else {
|
190 |
loader = (PluginClassLoader) pluginClassInstance.getClass().getClassLoader(); |
191 |
} |
192 |
return Launcher.getPluginServices(loader.getPluginName());
|
193 |
} catch (ClassCastException e) { |
194 |
/*
|
195 |
* throw new RuntimeException( "Parameter is not a plugin class
|
196 |
* instance");
|
197 |
*/
|
198 |
return null; |
199 |
} |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Gets a reference to the PluginServices object associated with the
|
204 |
* provided plugin.
|
205 |
*
|
206 |
* @param pluginName
|
207 |
* Plugin's name whose services are going to be used
|
208 |
*
|
209 |
* @return The PluginServices object associated with the provided plugin.
|
210 |
*/
|
211 |
public static PluginServices getPluginServices(String pluginName) { |
212 |
return Launcher.getPluginServices(pluginName);
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Gets the window manager (MDIManager).
|
217 |
*
|
218 |
* @return A reference to the window manager (MDIManager).
|
219 |
*/
|
220 |
public static MDIManager getMDIManager() { |
221 |
return Launcher.getFrame().getMDIManager();
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Gets the application's main frame.
|
226 |
*
|
227 |
* @return A reference to the application's main window
|
228 |
*/
|
229 |
public static MainFrame getMainFrame() { |
230 |
return Launcher.getFrame();
|
231 |
} |
232 |
|
233 |
public static void registerKeyStroke(KeyStroke key, KeyEventDispatcher a) { |
234 |
GlobalKeyEventDispatcher.getInstance().registerKeyStroke(key, a); |
235 |
} |
236 |
|
237 |
public static void unRegisterKeyStroke(KeyStroke key) { |
238 |
GlobalKeyEventDispatcher.getInstance().removeKeyStrokeBinding(key); |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Gets the instance of the extension whose class is provided.
|
243 |
*
|
244 |
* @param extensionClass
|
245 |
* Class of the extension whose instance is to be returned
|
246 |
*
|
247 |
* @return The instance of the extension, or null if the instance does
|
248 |
* not exist.Instancia de la extensi�n o null en caso de que no haya
|
249 |
* una
|
250 |
*/
|
251 |
public static IExtension getExtension(Class extensionClass) { |
252 |
ExtensionDecorator extAux = |
253 |
(ExtensionDecorator) Launcher.getClassesExtensions() |
254 |
.get(extensionClass); |
255 |
try {
|
256 |
return extAux.getExtension();
|
257 |
} catch (NullPointerException ex) { |
258 |
return null; |
259 |
} |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Gets a reference to the Extension Decorator which adds some extra options
|
264 |
* to the basic extension interface.
|
265 |
*
|
266 |
* @param extensionClass
|
267 |
* The class of the extension whose decorator is to be returned
|
268 |
* @return The ExtensionDecorator associated with the provided extension,
|
269 |
* or null if the extension does not exist.
|
270 |
*/
|
271 |
public static ExtensionDecorator getDecoratedExtension(Class extensionClass) { |
272 |
return (ExtensionDecorator) Launcher.getClassesExtensions()
|
273 |
.get(extensionClass); |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Returns an array containing references to all the loaded extensions.
|
278 |
*
|
279 |
* @return ExtensionDecorator[] An array of ExtensionDecorators (each
|
280 |
* Decorator contains one extension).
|
281 |
*/
|
282 |
public static ExtensionDecorator[] getDecoratedExtensions() { |
283 |
HashMap map = Launcher.getClassesExtensions();
|
284 |
ExtensionDecorator[] extensions =
|
285 |
(ExtensionDecorator[]) map.values()
|
286 |
.toArray(new ExtensionDecorator[0]); |
287 |
return extensions;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* Gets an iterator with all the loaded Extensions.
|
292 |
*
|
293 |
* @return Iterator over the decorated extensions (each member of
|
294 |
* the iterator is an ExtensionDecorator, which in turn contains
|
295 |
* one IExtension object).
|
296 |
*/
|
297 |
public static Iterator getExtensions() { |
298 |
return Launcher.getClassesExtensions().values().iterator();
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* Returns the message in the current's locale language
|
303 |
* corresponding to the provided translation key.
|
304 |
* The key-message pairs are obtained from the plugin's
|
305 |
* translation files (text_xx.properties files).
|
306 |
*
|
307 |
* @param pluginObject
|
308 |
* Any object which was loaded from a plugin
|
309 |
*
|
310 |
* @param key
|
311 |
* Translation key whose associated message is to be obtained
|
312 |
*
|
313 |
* @return The message associated with the provided key, in one of the
|
314 |
* current locale languages, or the key if the translation is not
|
315 |
* found.
|
316 |
*/
|
317 |
public static String getText(Object pluginObject, String key) { |
318 |
if (key == null) |
319 |
return null; |
320 |
String translation = org.gvsig.i18n.Messages.getText(key, false); |
321 |
if (translation != null) |
322 |
return translation;
|
323 |
else {
|
324 |
logger.debug("Can't find translation for ''{}''.", key);
|
325 |
return key;
|
326 |
} |
327 |
} |
328 |
|
329 |
/**
|
330 |
* Sets the XML data which should be saved on disk for this plugin. This
|
331 |
* data can be retrieved on following sessions.
|
332 |
*
|
333 |
* @param The
|
334 |
* XMLEntity object containing the data to be persisted.
|
335 |
*
|
336 |
* @see PluginServices.getPersistentXML()
|
337 |
* @see XMLEntity
|
338 |
*/
|
339 |
public void setPersistentXML(XMLEntity entity) { |
340 |
persistentXML = entity; |
341 |
} |
342 |
|
343 |
/**
|
344 |
* Gets the XML data which was saved on previous sessions for this
|
345 |
* plugins.
|
346 |
*
|
347 |
* @return An XMLEntity object containing the persisted data
|
348 |
*/
|
349 |
public XMLEntity getPersistentXML() {
|
350 |
if (persistentXML == null) { |
351 |
persistentXML = new XMLEntity();
|
352 |
} |
353 |
return persistentXML;
|
354 |
} |
355 |
|
356 |
/**
|
357 |
* A�ade un listener a un popup menu registrado en el config.xml de alg�n
|
358 |
* plugin
|
359 |
*
|
360 |
* @param name
|
361 |
* Nombre del men� contextual
|
362 |
* @param c
|
363 |
* Componente que desplegar� el men� cuando se haga click con el
|
364 |
* bot�n derecho
|
365 |
* @param listener
|
366 |
* Listener que se ejecutar� cuando se seleccione cualquier
|
367 |
* entrada del men�
|
368 |
*
|
369 |
* @throws RuntimeException
|
370 |
* Si la interfaz no est� preparada todav�a. S�lo puede darse
|
371 |
* durante el arranque
|
372 |
*/
|
373 |
public void addPopupMenuListener(String name, |
374 |
Component c,
|
375 |
ActionListener listener) {
|
376 |
MDIFrame frame = Launcher.getFrame(); |
377 |
|
378 |
if (frame == null) { |
379 |
throw new RuntimeException("MDIFrame not loaded yet"); |
380 |
} |
381 |
|
382 |
frame.addPopupMenuListener(name, c, listener, loader); |
383 |
} |
384 |
|
385 |
/**
|
386 |
* Gets the plugin's root directory.
|
387 |
*
|
388 |
* @return A File pointing to the plugin's root directory.
|
389 |
*/
|
390 |
public File getPluginDirectory() { |
391 |
return Launcher.getPluginFolder(this.getPluginName()); |
392 |
} |
393 |
|
394 |
/**
|
395 |
* Runs a background task. The events produced on the main frame will
|
396 |
* be inhibited.
|
397 |
*
|
398 |
* @param r
|
399 |
* The task to run.
|
400 |
*
|
401 |
* @return The Thread on which the task is executed.
|
402 |
*/
|
403 |
public static Thread backgroundExecution(Runnable r) { |
404 |
Thread t = new Thread(new RunnableDecorator(r)); |
405 |
t.start(); |
406 |
|
407 |
return t;
|
408 |
} |
409 |
|
410 |
/**
|
411 |
* Runs a backbround task. This task may be monitored and canceled, and
|
412 |
* does not inhibit any event.
|
413 |
*
|
414 |
* @param task
|
415 |
* The task to run.
|
416 |
*/
|
417 |
public static void cancelableBackgroundExecution(final IMonitorableTask task) { |
418 |
final org.gvsig.utils.swing.threads.SwingWorker worker =
|
419 |
new org.gvsig.utils.swing.threads.SwingWorker() {
|
420 |
|
421 |
public Object construct() { |
422 |
try {
|
423 |
task.run(); |
424 |
return task;
|
425 |
} catch (Exception e) { |
426 |
NotificationManager.addError(null, e);
|
427 |
} |
428 |
return null; |
429 |
} |
430 |
|
431 |
/**
|
432 |
* Called on the event dispatching thread (not on the worker
|
433 |
* thread)
|
434 |
* after the <code>construct</code> method has returned.
|
435 |
*/
|
436 |
public void finished() { |
437 |
task.finished(); |
438 |
} |
439 |
}; |
440 |
|
441 |
Component mainFrame = (Component) PluginServices.getMainFrame(); |
442 |
|
443 |
IProgressMonitorIF progressMonitor = null;
|
444 |
String title = getText(null, "PluginServices.Procesando"); |
445 |
progressMonitor = |
446 |
new UndefinedProgressMonitor((Frame) mainFrame, title); |
447 |
progressMonitor.setIndeterminated(!task.isDefined()); |
448 |
progressMonitor.setInitialStep(task.getInitialStep()); |
449 |
progressMonitor.setLastStep(task.getFinishStep()); |
450 |
progressMonitor.setCurrentStep(task.getCurrentStep()); |
451 |
progressMonitor.setMainTitleLabel(task.getStatusMessage()); |
452 |
progressMonitor.setNote(task.getNote()); |
453 |
progressMonitor.open(); |
454 |
int delay = 500; |
455 |
TaskMonitorTimerListener timerListener = |
456 |
new TaskMonitorTimerListener(progressMonitor, task);
|
457 |
Timer timer = new Timer(delay, timerListener); |
458 |
timerListener.setTimer(timer); |
459 |
timer.start(); |
460 |
|
461 |
worker.start(); |
462 |
|
463 |
} |
464 |
|
465 |
/**
|
466 |
* Closes the application. Cleanly exits from the application:
|
467 |
* terminates all the extensions, then exits.
|
468 |
*
|
469 |
*/
|
470 |
public static void closeApplication() { |
471 |
Launcher.closeApplication(); |
472 |
} |
473 |
|
474 |
/**
|
475 |
* DOCUMENT ME!
|
476 |
*
|
477 |
* @author Fernando Gonz�lez Cort�s
|
478 |
*/
|
479 |
private static class RunnableDecorator implements Runnable { |
480 |
|
481 |
private Runnable r; |
482 |
|
483 |
/**
|
484 |
* Crea un nuevo RunnableDecorator.
|
485 |
*
|
486 |
* @param r
|
487 |
* DOCUMENT ME!
|
488 |
*/
|
489 |
public RunnableDecorator(Runnable r) { |
490 |
this.r = r;
|
491 |
} |
492 |
|
493 |
/**
|
494 |
* @see java.lang.Runnable#run()
|
495 |
*/
|
496 |
public void run() { |
497 |
try {
|
498 |
SwingUtilities.invokeAndWait(new Runnable() { |
499 |
|
500 |
public void run() { |
501 |
try {
|
502 |
r.run(); |
503 |
} catch (RuntimeException e) { |
504 |
NotificationManager.addError(Messages.getText("PluginServices.Bug_en_el_codigo"),
|
505 |
e); |
506 |
} catch (Error e) { |
507 |
NotificationManager.addError(Messages.getText("PluginServices.Error_grave_de_la_aplicaci�n"),
|
508 |
e); |
509 |
} |
510 |
} |
511 |
}); |
512 |
} catch (InterruptedException e) { |
513 |
NotificationManager.addWarning(Messages.getText("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
514 |
e); |
515 |
} catch (InvocationTargetException e) { |
516 |
NotificationManager.addWarning(Messages.getText("PluginServices.No_se_pudo_poner_el_reloj_de_arena"),
|
517 |
e); |
518 |
} |
519 |
} |
520 |
} |
521 |
|
522 |
/**
|
523 |
* Gets an array containing the application's startup arguments. <br>
|
524 |
* <br>
|
525 |
* Usually: <code>appName plugins-directory [locale] [other args]</code>
|
526 |
*
|
527 |
* @return the original arguments that Andami received. (app-name
|
528 |
* plugins-directory, locale, etc)
|
529 |
*/
|
530 |
public static String[] getArguments() { |
531 |
return arguments;
|
532 |
} |
533 |
|
534 |
/**
|
535 |
* Replaces the original Andami arguments with the provided arguments.
|
536 |
*
|
537 |
* @param arguments
|
538 |
* An array of String, each element of the array
|
539 |
* represents one
|
540 |
* argument.
|
541 |
*/
|
542 |
public static void setArguments(String[] arguments) { |
543 |
PluginServices.arguments = arguments; |
544 |
} |
545 |
|
546 |
/**
|
547 |
* Returns the value of a command line named argument. <br>
|
548 |
* <br>
|
549 |
* The argument format is: <br>
|
550 |
* -{argumentName}={argumentValue} <br>
|
551 |
* <br>
|
552 |
* example: <br>
|
553 |
* ' -language=en '
|
554 |
*
|
555 |
* @return String The value of the argument
|
556 |
*/
|
557 |
public static String getArgumentByName(String name) { |
558 |
for (int i = 2; i < PluginServices.arguments.length; i++) { |
559 |
String arg = PluginServices.arguments[i];
|
560 |
if( arg != null ) { |
561 |
int index = arg.indexOf(name + "="); |
562 |
if (index != -1) |
563 |
return arg.substring(index
|
564 |
+ name.length() + 1);
|
565 |
} |
566 |
} |
567 |
return null; |
568 |
} |
569 |
|
570 |
/**
|
571 |
* Gets the logger. The logger is used to register important
|
572 |
* events (errors, etc), which are stored on a file which
|
573 |
* can be checked later.
|
574 |
*
|
575 |
* @return A Logger object.
|
576 |
* @see Logger object from the Log4j library.
|
577 |
*
|
578 |
*/
|
579 |
public static Logger getLogger() { |
580 |
return logger;
|
581 |
} |
582 |
|
583 |
public static DlgPreferences getDlgPreferences() { |
584 |
return DlgPreferences.getInstance();
|
585 |
} |
586 |
|
587 |
/**
|
588 |
* Stores the provided text data on the clipboard.
|
589 |
*
|
590 |
* @param data
|
591 |
* An String containing the data to be stored
|
592 |
* on the clipboard.
|
593 |
*/
|
594 |
public static void putInClipboard(String data) { |
595 |
StringSelection ss = new StringSelection(data); |
596 |
|
597 |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
|
598 |
} |
599 |
|
600 |
/**
|
601 |
* Gets text data from the Clipboard, if available.
|
602 |
*
|
603 |
* @return An String containing the clipboard's data, or <code>null</code>
|
604 |
* if the data was not available.
|
605 |
*/
|
606 |
public static String getFromClipboard() { |
607 |
|
608 |
try {
|
609 |
return (String) Toolkit.getDefaultToolkit() |
610 |
.getSystemClipboard() |
611 |
.getContents(null)
|
612 |
.getTransferData(DataFlavor.stringFlavor);
|
613 |
} catch (UnsupportedFlavorException e) { |
614 |
return null; |
615 |
} catch (IOException e) { |
616 |
// TODO Auto-generated catch block
|
617 |
return null; |
618 |
} |
619 |
} |
620 |
|
621 |
/**
|
622 |
* Gets the ExclusiveUIExtension, an special extension which
|
623 |
* will take
|
624 |
* control over the UI and will decide which extensions may be
|
625 |
* enable/disabled or visible/hidden.
|
626 |
*
|
627 |
* @return If an ExclusiveUIExtension was set, return this extension.
|
628 |
* Otherwise, return <code>null</code>.
|
629 |
*
|
630 |
* @see org.gvsig.andami.Launcher#initializeExclusiveUIExtension()
|
631 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled(IExtension extension)
|
632 |
* @see org.gvsig.andami.plugins.IExtension#isVisible(IExtension extension)
|
633 |
*/
|
634 |
public static ExclusiveUIExtension getExclusiveUIExtension() { |
635 |
return PluginServices.exclusiveUIExtension;
|
636 |
} |
637 |
|
638 |
/**
|
639 |
* Sets the ExclusiveUIExtension, an special extension which
|
640 |
* will take
|
641 |
* control over the UI and will decide which extensions may be
|
642 |
* enable/disabled or visible/hidden. <br>
|
643 |
* <br>
|
644 |
* The ExclusiveUIExtension is normally set by the following
|
645 |
* Andami startup argument: <br>
|
646 |
* <br>
|
647 |
* <code>ExclusiveUIExtension=ExtensionName</code>
|
648 |
*
|
649 |
* @see org.gvsig.andami.Launcher#initializeExclusiveUIExtension()
|
650 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled(IExtension extension)
|
651 |
* @see org.gvsig.andami.plugins.IExtension#isVisible(IExtension extension)
|
652 |
*/
|
653 |
public static void setExclusiveUIExtension(ExclusiveUIExtension extension) { |
654 |
PluginServices.exclusiveUIExtension = extension; |
655 |
} |
656 |
|
657 |
public static void addLoaders(ArrayList classLoaders) { |
658 |
PluginClassLoader.addLoaders(classLoaders); |
659 |
} |
660 |
|
661 |
/**
|
662 |
* @deprecated use ToolsSwingLocator.getIconThemeManager()
|
663 |
*/
|
664 |
public static IconThemeManager getIconThemeManager() { |
665 |
return ToolsSwingLocator.getIconThemeManager();
|
666 |
} |
667 |
|
668 |
/**
|
669 |
* @deprecated use ToolsSwingLocator.getIconThemeManager().getCurrent()
|
670 |
*/
|
671 |
public static IconTheme getIconTheme() { |
672 |
return getIconThemeManager().getCurrent();
|
673 |
} |
674 |
|
675 |
/**
|
676 |
* Try to detect if the application is running in a development
|
677 |
* environment. <br>
|
678 |
* This look for <b>.project</b> and <b>.classpath</b> files in the starts
|
679 |
* path of the application.
|
680 |
*
|
681 |
* @return true if <b>.project</b> and <b>.classpath</b> are in the
|
682 |
* development path
|
683 |
*/
|
684 |
public static boolean runningInDevelopment() { |
685 |
String andamiPath;
|
686 |
Properties props = System.getProperties(); |
687 |
|
688 |
try {
|
689 |
try {
|
690 |
andamiPath = |
691 |
(new File(Launcher.class.getResource(".").getFile() |
692 |
+ File.separator + ".." + File.separator + ".." |
693 |
+ File.separator + ".." + File.separator + "..")).getCanonicalPath(); |
694 |
} catch (IOException e) { |
695 |
andamiPath = |
696 |
(new File(Launcher.class.getResource(".").getFile() |
697 |
+ File.separator + ".." + File.separator + ".." |
698 |
+ File.separator + ".." + File.separator + "..")).getAbsolutePath(); |
699 |
} |
700 |
} catch (Exception e1) { |
701 |
andamiPath = (String) props.get("user.dir"); |
702 |
} |
703 |
|
704 |
File andamiJar = new File(andamiPath + File.separator + "andami.jar"); |
705 |
if (!andamiJar.exists())
|
706 |
return false; |
707 |
File projectFile = new File(andamiPath + File.separator + ".project"); |
708 |
File classpathFile =
|
709 |
new File(andamiPath + File.separator + ".classpath"); |
710 |
return projectFile.exists() && classpathFile.exists();
|
711 |
|
712 |
} |
713 |
|
714 |
public PluginsManager getManager() {
|
715 |
return PluginsLocator.getManager();
|
716 |
} |
717 |
|
718 |
private String[] getAllPluginNames() { |
719 |
String[] names = new String[this.alternativeNames.length+1]; |
720 |
names[0] = this.getPluginName(); |
721 |
for( int n=0; n<this.alternativeNames.length; n++ ) { |
722 |
names[n+1] = this.alternativeNames[n]; |
723 |
} |
724 |
return names;
|
725 |
} |
726 |
|
727 |
public DynObject getPluginProperties() {
|
728 |
if (this.pluginPersistence == null) { |
729 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
730 |
DynStruct dynStruct = manager.getDynObjectDefinition(getPluginName()); |
731 |
if ( dynStruct == null) { |
732 |
File persistenceDefinitionFile =
|
733 |
new File(this.getPluginDirectory(), "plugin-persistence.def"); |
734 |
String[] names = getAllPluginNames(); |
735 |
for( int i=0; i<names.length ; i++ ) { |
736 |
try {
|
737 |
dynStruct = manager.addDefinition(DynObject.class, |
738 |
names[i], |
739 |
new FileInputStream(persistenceDefinitionFile), |
740 |
this.getClassLoader(),
|
741 |
null,
|
742 |
null);
|
743 |
break;
|
744 |
} catch (FileNotFoundException e) { |
745 |
throw new PluginPersistenceNeedDefinitionException(this.getPluginName(), e); |
746 |
} catch (AddDefinitionException e) {
|
747 |
if( i+1 >= names.length ) { // Si ya no hay mas nombres peta |
748 |
throw new PluginPersistenceAddDefinitionException(this.getPluginName(), e); |
749 |
} |
750 |
} |
751 |
} |
752 |
} |
753 |
|
754 |
File persistenceFile = getPluginPersistenceFile();
|
755 |
if (persistenceFile.exists()) {
|
756 |
PersistentState state; |
757 |
|
758 |
try {
|
759 |
state = manager.loadState(new FileInputStream(persistenceFile)); |
760 |
pluginPersistence = (DynObject) manager.create(state); |
761 |
} catch (Exception e) { |
762 |
|
763 |
logger.info("Can't retrieve persistent values from plugin "+
|
764 |
getPluginName(), e); |
765 |
showMessageDialogDelayed(Messages.getText("_Unable_to_read_persistence_for_plugin")
|
766 |
+ ": " + getPluginName() + "\n" + |
767 |
Messages.getText("_Perhaps_saved_data_refers_to_missing_plugin"),
|
768 |
Messages.getText("_Persistence"),
|
769 |
JOptionPane.WARNING_MESSAGE);
|
770 |
} |
771 |
} |
772 |
if (this.pluginPersistence == null) { |
773 |
logger.info("Creating default values for plugin persistence ("+this.getPluginName()+")."); |
774 |
this.pluginPersistence =
|
775 |
ToolsLocator.getDynObjectManager() |
776 |
.createDynObject(dynStruct); |
777 |
} |
778 |
} |
779 |
return pluginPersistence;
|
780 |
} |
781 |
|
782 |
private void showMessageDialogDelayed(final String msg, final String title, final int type) { |
783 |
PluginsManager pluginManger = PluginsLocator.getManager(); |
784 |
pluginManger.addStartupTask("Persistence_"+getPluginName(), new Runnable() { |
785 |
public void run() { |
786 |
JOptionPane.showMessageDialog(
|
787 |
(Component) PluginServices.getMainFrame(),
|
788 |
msg, |
789 |
title, |
790 |
type); |
791 |
} |
792 |
}, true, 100); |
793 |
} |
794 |
|
795 |
public void savePluginProperties() { |
796 |
if (this.pluginPersistence == null) { |
797 |
return;
|
798 |
} |
799 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
800 |
File persistenceFile = getPluginPersistenceFile();
|
801 |
PersistentState state; |
802 |
FileOutputStream fos;
|
803 |
try {
|
804 |
state = manager.getState(pluginPersistence); |
805 |
manager.saveState(state, new FileOutputStream(persistenceFile)); |
806 |
} catch (Exception e) { |
807 |
throw new PluginSaveDataException(this.getPluginName(), e); |
808 |
} |
809 |
|
810 |
|
811 |
} |
812 |
|
813 |
/**
|
814 |
* Returns the plugin persistence file.
|
815 |
*
|
816 |
* @return the plugin persistence file
|
817 |
*/
|
818 |
private File getPluginPersistenceFile() { |
819 |
return new File(getPluginHomeFolder(), "plugin-persistence.dat"); |
820 |
} |
821 |
|
822 |
/**
|
823 |
* Returns the folder where the plugin stores its resources. That folder
|
824 |
* will be usually a subfolder into the application folder in the user home
|
825 |
* folder.
|
826 |
*
|
827 |
* @return the folder where the plugin stores its resources
|
828 |
*/
|
829 |
public File getPluginHomeFolder() { |
830 |
File persistenceFolder = new File(Launcher.getAppHomeDir() |
831 |
+ File.separator + "plugins" + File.separator |
832 |
+ this.getPluginName());
|
833 |
|
834 |
if (!persistenceFolder.exists()) {
|
835 |
persistenceFolder.mkdirs(); |
836 |
} |
837 |
|
838 |
return persistenceFolder;
|
839 |
} |
840 |
|
841 |
public class PluginPersistenceNeedDefinitionException extends |
842 |
BaseRuntimeException { |
843 |
|
844 |
/**
|
845 |
*
|
846 |
*/
|
847 |
private static final long serialVersionUID = -2036279527440882712L; |
848 |
|
849 |
PluginPersistenceNeedDefinitionException(String name, Throwable cause) { |
850 |
super("Can't load persistence definition of plugin %(name).", |
851 |
"_cant_load_persistence_definition_of_plugin_XnameX",
|
852 |
serialVersionUID); |
853 |
initCause(cause); |
854 |
setValue("name", name);
|
855 |
} |
856 |
} |
857 |
|
858 |
public class PluginPersistenceAddDefinitionException extends |
859 |
BaseRuntimeException { |
860 |
|
861 |
/**
|
862 |
*
|
863 |
*/
|
864 |
private static final long serialVersionUID = 2227722796640780361L; |
865 |
|
866 |
PluginPersistenceAddDefinitionException(String name, Throwable cause) { |
867 |
super("Can't add persistence definition of plugin %(name).", |
868 |
"_cant_add_persistence_definition_of_plugin_XnameX",
|
869 |
serialVersionUID); |
870 |
this.initCause(cause);
|
871 |
setValue("name", name);
|
872 |
} |
873 |
} |
874 |
|
875 |
public class PluginLoadDataException extends |
876 |
BaseRuntimeException { |
877 |
|
878 |
/**
|
879 |
*
|
880 |
*/
|
881 |
private static final long serialVersionUID = 1168749231143949111L; |
882 |
|
883 |
PluginLoadDataException(String name) {
|
884 |
super("Can't load data of plugin %(name).", |
885 |
"_cant_load_data_of_plugin_XnameX",
|
886 |
serialVersionUID); |
887 |
setValue("name", name);
|
888 |
} |
889 |
} |
890 |
|
891 |
public class PluginSaveDataException extends |
892 |
BaseRuntimeException { |
893 |
|
894 |
/**
|
895 |
*
|
896 |
*/
|
897 |
private static final long serialVersionUID = 4893241183911774542L; |
898 |
private final static String MESSAGE_FORMAT = "Can't save data of plugin %(name)."; |
899 |
private final static String MESSAGE_KEY = "_cant_save_data_of_plugin_XnameX"; |
900 |
|
901 |
PluginSaveDataException(String name) {
|
902 |
super(MESSAGE_FORMAT,
|
903 |
MESSAGE_KEY, |
904 |
serialVersionUID); |
905 |
setValue("name", name);
|
906 |
} |
907 |
|
908 |
public PluginSaveDataException(String name, Throwable cause) { |
909 |
super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
910 |
setValue("name", name);
|
911 |
|
912 |
} |
913 |
} |
914 |
|
915 |
@Override
|
916 |
public String toString() { |
917 |
return super.toString()+" "+this.getPluginName(); |
918 |
} |
919 |
|
920 |
public void addDependencyWithPlugin(PluginServices otherPlugin) { |
921 |
this.getClassLoader().addPluginClassLoader(otherPlugin.getClassLoader());
|
922 |
} |
923 |
} |