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