Revision 47372
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
3 |
|
|
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
<artifactId>org.gvsig.desktop.framework</artifactId> |
|
6 |
<packaging>pom</packaging> |
|
7 |
<name>${project.artifactId}</name> |
|
8 |
<parent> |
|
9 |
<groupId>org.gvsig</groupId> |
|
10 |
<artifactId>org.gvsig.desktop</artifactId> |
|
11 |
<version>2.0.424</version> |
|
12 |
</parent> |
|
13 |
|
|
14 |
<modules> |
|
15 |
<module>org.gvsig.andami</module> |
|
16 |
<module>org.gvsig.andami.updater</module> |
|
17 |
</modules> |
|
18 |
|
|
19 |
<description>Contains the launcher and plugin framework used in gvSIG.</description> |
|
20 |
</project> |
|
21 |
|
|
0 | 22 |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/test/java/org/gvsig/andami/AllTests.java | ||
---|---|---|
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 junit.framework.Test; |
|
27 |
import junit.framework.TestSuite; |
|
28 |
|
|
29 |
public class AllTests { |
|
30 |
|
|
31 |
public static Test suite() { |
|
32 |
TestSuite suite = new TestSuite("Test for com.iver.andami"); |
|
33 |
//$JUnit-BEGIN$ |
|
34 |
|
|
35 |
//$JUnit-END$ |
|
36 |
return suite; |
|
37 |
} |
|
38 |
|
|
39 |
} |
|
0 | 40 |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultPluginsManager.java | ||
---|---|---|
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.impl; |
|
25 |
|
|
26 |
import java.io.File; |
|
27 |
import java.io.IOException; |
|
28 |
import java.lang.reflect.InvocationTargetException; |
|
29 |
import java.net.URI; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Arrays; |
|
32 |
import java.util.Collections; |
|
33 |
import java.util.Date; |
|
34 |
import java.util.Enumeration; |
|
35 |
import java.util.Iterator; |
|
36 |
import java.util.List; |
|
37 |
|
|
38 |
import javax.swing.SwingUtilities; |
|
39 |
|
|
40 |
import org.apache.commons.io.FileUtils; |
|
41 |
import org.gvsig.andami.Arguments; |
|
42 |
|
|
43 |
import org.gvsig.andami.Launcher; |
|
44 |
import org.gvsig.andami.PluginServices; |
|
45 |
import org.gvsig.andami.PluginsManager; |
|
46 |
import org.gvsig.andami.Utilities; |
|
47 |
import org.gvsig.andami.config.generate.AndamiConfig; |
|
48 |
import org.gvsig.andami.config.generate.Plugin; |
|
49 |
import org.gvsig.andami.firewall.FirewallConfiguration; |
|
50 |
import org.gvsig.andami.plugins.ExclusiveUIExtension; |
|
51 |
import org.gvsig.andami.plugins.Extension; |
|
52 |
import org.gvsig.andami.plugins.IExtension; |
|
53 |
import org.gvsig.andami.plugins.PluginClassLoader; |
|
54 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
|
55 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
|
56 |
import org.gvsig.installer.lib.api.PackageInfo; |
|
57 |
import org.gvsig.installer.lib.api.Version; |
|
58 |
import org.gvsig.tools.ToolsLocator; |
|
59 |
import org.gvsig.tools.packageutils.PackageManager; |
|
60 |
|
|
61 |
import org.slf4j.Logger; |
|
62 |
import org.slf4j.LoggerFactory; |
|
63 |
|
|
64 |
public class DefaultPluginsManager implements PluginsManager { |
|
65 |
|
|
66 |
private class Task implements Comparable, Runnable { |
|
67 |
|
|
68 |
private String type = ""; |
|
69 |
private Runnable task = null; |
|
70 |
private boolean in_event_thread = false; |
|
71 |
private int priority = 0; |
|
72 |
private String name = null; |
|
73 |
|
|
74 |
public Task(String type, String name, Runnable task, boolean in_event_thread, int priority) { |
|
75 |
this.type = type; |
|
76 |
this.in_event_thread = in_event_thread; |
|
77 |
this.task = task; |
|
78 |
this.priority = priority; |
|
79 |
this.name = name; |
|
80 |
} |
|
81 |
|
|
82 |
public int compareTo(Object t) { |
|
83 |
return this.priority - ((Task) t).priority; |
|
84 |
} |
|
85 |
|
|
86 |
public boolean equals(Object t) { |
|
87 |
return this.compareTo(t) == 0; |
|
88 |
} |
|
89 |
|
|
90 |
public void run() { |
|
91 |
if ( this.in_event_thread ) { |
|
92 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
|
93 |
try { |
|
94 |
SwingUtilities.invokeAndWait(new Runnable() { |
|
95 |
public void run() { |
|
96 |
Task.this.run(); |
|
97 |
} |
|
98 |
}); |
|
99 |
} catch (InterruptedException ex) { |
|
100 |
// Do nothing |
|
101 |
} catch (InvocationTargetException ex) { |
|
102 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
|
103 |
|
|
104 |
} |
|
105 |
return; |
|
106 |
} |
|
107 |
} |
|
108 |
logger.info("Running " + type + " task '" + name + "' (priority " + priority + ")."); |
|
109 |
try { |
|
110 |
task.run(); |
|
111 |
logger.info("Terminated " + type + " task '" + name + "'."); |
|
112 |
} catch (Throwable ex) { |
|
113 |
// Catch Exceptions and Errors (class not found) |
|
114 |
logger.warn("Errors in execution of " + type + " task '" + name + "'.", ex); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
} |
|
119 |
|
|
120 |
private static Logger logger |
|
121 |
= LoggerFactory.getLogger(DefaultPluginsManager.class); |
|
122 |
|
|
123 |
private List<File> pluginsFolders = null; |
|
124 |
private List<Task> startupTasks = new ArrayList<Task>(); |
|
125 |
private List<Task> shutdownTasks = new ArrayList<Task>(); |
|
126 |
|
|
127 |
public ExclusiveUIExtension getExclusiveUIExtension() { |
|
128 |
return PluginServices.getExclusiveUIExtension(); |
|
129 |
} |
|
130 |
|
|
131 |
public IExtension getExtension(Class<? extends IExtension> extension) { |
|
132 |
return PluginServices.getExtension(extension); |
|
133 |
} |
|
134 |
|
|
135 |
public IExtension getExtension(String extension) { |
|
136 |
return Launcher.getExtensionByName(extension); |
|
137 |
} |
|
138 |
|
|
139 |
@SuppressWarnings("unchecked") |
|
140 |
public Iterator<IExtension> getExtensions() { |
|
141 |
return PluginServices.getExtensions(); |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* Return the associated pluginServices to the extension class passed as |
|
146 |
* parameter. |
|
147 |
* |
|
148 |
*/ |
|
149 |
public PluginServices getPlugin(Class<? extends IExtension> extension) { |
|
150 |
if (!(extension.getClassLoader() instanceof PluginClassLoader)) { |
|
151 |
return null; |
|
152 |
} |
|
153 |
String pluginName = ((PluginClassLoader) extension.getClassLoader()).getPluginName(); |
|
154 |
return this.getPlugin(pluginName); |
|
155 |
} |
|
156 |
|
|
157 |
public PluginServices getPlugin(Object obj) { |
|
158 |
if ( obj instanceof Extension ) { |
|
159 |
return ((Extension)obj).getPlugin(); |
|
160 |
} |
|
161 |
if ( obj instanceof IExtension ) { |
|
162 |
Class<? extends IExtension> klass = (Class<? extends IExtension>) obj.getClass(); |
|
163 |
return this.getPlugin(klass); |
|
164 |
} |
|
165 |
ClassLoader loader = obj.getClass().getClassLoader(); |
|
166 |
if( loader instanceof PluginClassLoader ) { |
|
167 |
String pluginName = ((PluginClassLoader)loader).getPluginName(); |
|
168 |
return this.getPlugin(pluginName); |
|
169 |
} |
|
170 |
return null; |
|
171 |
} |
|
172 |
|
|
173 |
@Override |
|
174 |
public PluginServices getPlugin(String pluginName) { |
|
175 |
return Launcher.getPluginServices(pluginName); |
|
176 |
} |
|
177 |
|
|
178 |
@Override |
|
179 |
public File getPluginHomeFolder(String pluginName) { |
|
180 |
File folder = FileUtils.getFile( |
|
181 |
this.getApplicationHomeFolder(), "plugins", pluginName |
|
182 |
); |
|
183 |
if (!folder.exists()) { |
|
184 |
folder.mkdirs(); |
|
185 |
} |
|
186 |
return folder; |
|
187 |
} |
|
188 |
|
|
189 |
@Override |
|
190 |
public PackageInfo getPackageInfo(Class<? extends IExtension> extension) { |
|
191 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
|
192 |
File pinfo_file = new File(this.getPlugin(extension).getPluginDirectory(), "package.info"); |
|
193 |
|
|
194 |
PackageInfo packageInfo = null; |
|
195 |
try { |
|
196 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
|
197 |
} catch (Exception e) { |
|
198 |
logger.info("Error while reading package info file from " |
|
199 |
+ pinfo_file.toString(), e); |
|
200 |
} |
|
201 |
return packageInfo; |
|
202 |
} |
|
203 |
|
|
204 |
public PackageInfo getPackageInfo(String pluginName) { |
|
205 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
|
206 |
File pinfo_file = new File(this.getPlugin(pluginName) |
|
207 |
.getPluginDirectory(), "package.info"); |
|
208 |
|
|
209 |
PackageInfo packageInfo = null; |
|
210 |
try { |
|
211 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
|
212 |
} catch (Exception e) { |
|
213 |
logger.info("Error while reading package info file from " |
|
214 |
+ pinfo_file.toString(), e); |
|
215 |
} |
|
216 |
return packageInfo; |
|
217 |
} |
|
218 |
|
|
219 |
public PackageInfo getPackageInfo() { |
|
220 |
PackageManager pkgmgr = ToolsLocator.getPackageManager(); |
|
221 |
File pinfo_file = new File( |
|
222 |
this.getApplicationFolder(), "package.info"); |
|
223 |
PackageInfo packageInfo = null; |
|
224 |
try { |
|
225 |
packageInfo = pkgmgr.createPackageInfo(pinfo_file); |
|
226 |
} catch (Exception e) { |
|
227 |
logger.info("Error while reading package info file from " |
|
228 |
+ pinfo_file.toString(), e); |
|
229 |
} |
|
230 |
return packageInfo; |
|
231 |
} |
|
232 |
|
|
233 |
@SuppressWarnings("unchecked") |
|
234 |
public List<PluginServices> getPlugins() { |
|
235 |
List<PluginServices> pluginServices = new ArrayList<PluginServices>(); |
|
236 |
|
|
237 |
AndamiConfig config = Launcher.getAndamiConfig(); |
|
238 |
Enumeration<Plugin> plugins = config.enumeratePlugin(); |
|
239 |
while ( plugins.hasMoreElements() ) { |
|
240 |
Plugin plugin = plugins.nextElement(); |
|
241 |
pluginServices.add(PluginServices.getPluginServices(plugin.getName())); |
|
242 |
} |
|
243 |
return pluginServices; |
|
244 |
} |
|
245 |
|
|
246 |
public void setExclusiveUIExtension(ExclusiveUIExtension extension) { |
|
247 |
PluginServices.setExclusiveUIExtension(extension); |
|
248 |
} |
|
249 |
|
|
250 |
public String getText(Object obj, String msg) { |
|
251 |
return PluginServices.getText(obj, msg); |
|
252 |
} |
|
253 |
|
|
254 |
public String translate(String msg) { |
|
255 |
return org.gvsig.i18n.Messages.translate(msg); |
|
256 |
} |
|
257 |
|
|
258 |
public File getApplicationFolder() { |
|
259 |
return Launcher.getApplicationFolder(); |
|
260 |
} |
|
261 |
|
|
262 |
/** |
|
263 |
* @deprecated use {@link #getPluginsFolders()} |
|
264 |
*/ |
|
265 |
public File getPluginsDirectory() { |
|
266 |
return getPluginsFolder(); |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* @deprecated use {@link #getPluginsFolders()} |
|
271 |
*/ |
|
272 |
public File getPluginsFolder() { |
|
273 |
List<File> l = this.getPluginsFolders(); |
|
274 |
if ( l == null || l.size() < 1 ) { |
|
275 |
return null; |
|
276 |
} |
|
277 |
return l.get(0); |
|
278 |
} |
|
279 |
|
|
280 |
public List<File> getPluginsFolders() { |
|
281 |
if ( this.pluginsFolders != null ) { |
|
282 |
return this.pluginsFolders; |
|
283 |
} |
|
284 |
File folder; |
|
285 |
String folderPath = "gvSIG/extensiones"; |
|
286 |
if ( !(Launcher.getAndamiConfig() == null || Launcher.getAndamiConfig().getPluginsDirectory() == null) ) { |
|
287 |
folderPath = Launcher.getAndamiConfig().getPluginsDirectory(); |
|
288 |
} |
|
289 |
|
|
290 |
this.pluginsFolders = new ArrayList<File>(); |
|
291 |
|
|
292 |
folder = new File(this.getApplicationFolder(), folderPath); |
|
293 |
if( !folder.exists() ) { |
|
294 |
try { |
|
295 |
FileUtils.forceMkdir(folder); |
|
296 |
} catch (IOException ex) { |
|
297 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
|
298 |
} |
|
299 |
} |
|
300 |
this.pluginsFolders.add(folder); |
|
301 |
|
|
302 |
folder = new File(this.getApplicationHomeFolder(), "installation"); |
|
303 |
folder = new File(folder, folderPath); |
|
304 |
if( !folder.exists() ) { |
|
305 |
try { |
|
306 |
FileUtils.forceMkdir(folder); |
|
307 |
} catch (IOException ex) { |
|
308 |
logger.warn("The plugins folder '"+folder.getAbsolutePath()+"' don't exist and can't create.",ex); |
|
309 |
} |
|
310 |
} |
|
311 |
this.pluginsFolders.add(folder); |
|
312 |
|
|
313 |
return this.pluginsFolders; |
|
314 |
} |
|
315 |
|
|
316 |
public File getInstallFolder() { |
|
317 |
return new File(getApplicationFolder(), "install"); |
|
318 |
} |
|
319 |
|
|
320 |
public File getApplicationHomeFolder() { |
|
321 |
return Launcher.getApplicationHomeFolder(); |
|
322 |
} |
|
323 |
|
|
324 |
public void addStartupTask(String name, Runnable task, boolean in_event_thread, int priority) { |
|
325 |
this.startupTasks.add(new Task("startup", name, task, in_event_thread, priority)); |
|
326 |
} |
|
327 |
|
|
328 |
public void addShutdownTask(String name, Runnable task, boolean in_event_thread, int priority) { |
|
329 |
this.shutdownTasks.add(new Task("shutdown", name, task, in_event_thread, priority)); |
|
330 |
} |
|
331 |
|
|
332 |
public void executeStartupTasks() { |
|
333 |
logger.info("Executing startup tasks."); |
|
334 |
Thread th = new Thread(new Runnable() { |
|
335 |
public void run() { |
|
336 |
try { |
|
337 |
Thread.sleep(3); |
|
338 |
} catch (Exception exc) { |
|
339 |
// Ignore error |
|
340 |
} |
|
341 |
Collections.sort(startupTasks); |
|
342 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
|
343 |
Task task = startupTasks.get(i); |
|
344 |
logger.info("Task ["+i+"] "+task.priority+", "+task.name+", in event thread "+task.in_event_thread); |
|
345 |
} |
|
346 |
for ( int i = startupTasks.size() - 1; i >= 0; i-- ) { |
|
347 |
Task task = startupTasks.get(i); |
|
348 |
task.run(); |
|
349 |
} |
|
350 |
logger.info("Running startup tasks terminated."); |
|
351 |
} |
|
352 |
}); |
|
353 |
th.start(); |
|
354 |
} |
|
355 |
|
|
356 |
public void executeShutdownTasks() { |
|
357 |
logger.info("Executing shutdown tasks."); |
|
358 |
Collections.sort(shutdownTasks); |
|
359 |
for ( int i = shutdownTasks.size() - 1; i >= 0; i-- ) { |
|
360 |
Task task = shutdownTasks.get(i); |
|
361 |
task.run(); |
|
362 |
} |
|
363 |
} |
|
364 |
|
|
365 |
public File getApplicationI18nFolder() { |
|
366 |
return new File(this.getApplicationFolder(), "i18n"); |
|
367 |
} |
|
368 |
|
|
369 |
public FirewallConfiguration getFirewallConfiguration() { |
|
370 |
return (FirewallConfiguration) ToolsLocator.getFirewallManager(); |
|
371 |
} |
|
372 |
|
|
373 |
public Version getApplicationVersion() { |
|
374 |
PackageInfo pinfo = this.getPackageInfo(); |
|
375 |
Version version = pinfo.getVersion(); |
|
376 |
return version; |
|
377 |
} |
|
378 |
|
|
379 |
public List<IUnsavedData> getUnsavedData() { |
|
380 |
List<IUnsavedData> unsavedDatas = new ArrayList<IUnsavedData>(); |
|
381 |
Iterator<IExtension> extensions = getExtensions(); |
|
382 |
while(extensions.hasNext()){ |
|
383 |
IExtension extension = extensions.next(); |
|
384 |
IExtensionStatus status = extension.getStatus(); |
|
385 |
if(status != null && status.hasUnsavedData()){ |
|
386 |
IUnsavedData[] unsavedData = status.getUnsavedData(); |
|
387 |
if (unsavedData != null){ |
|
388 |
unsavedDatas.addAll(Arrays.asList(unsavedData)); |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
return unsavedDatas; |
|
393 |
} |
|
394 |
|
|
395 |
public void saveUnsavedData(List<IUnsavedData> unsavedData) throws UnsavedDataException { |
|
396 |
List<IUnsavedData> errors = new ArrayList<IUnsavedData>(); |
|
397 |
|
|
398 |
for (Iterator iterator = unsavedData.iterator(); iterator.hasNext();) { |
|
399 |
IUnsavedData itemUnsavedData = (IUnsavedData) iterator.next(); |
|
400 |
try { |
|
401 |
itemUnsavedData.saveData(); |
|
402 |
} catch (Exception e) { |
|
403 |
errors.add(itemUnsavedData); |
|
404 |
logger.warn("Can't save '"+itemUnsavedData.getResourceName()+"'.",e); |
|
405 |
} |
|
406 |
} |
|
407 |
if(!errors.isEmpty()){ |
|
408 |
throw new UnsavedDataException(errors); |
|
409 |
} |
|
410 |
} |
|
411 |
|
|
412 |
@Override |
|
413 |
public String getApplicationName() { |
|
414 |
return Launcher.getApplicationName(); |
|
415 |
} |
|
416 |
|
|
417 |
@Override |
|
418 |
public File getTempFolder() { |
|
419 |
return new File(Utilities.TEMPDIRECTORYPATH); |
|
420 |
} |
|
421 |
|
|
422 |
@Override |
|
423 |
public File getTempFile(String name) { |
|
424 |
return new File(Utilities.TEMPDIRECTORYPATH, name); |
|
425 |
} |
|
426 |
|
|
427 |
@Override |
|
428 |
public File getTempFile(String name, String sufix) { |
|
429 |
File tempFolder = new File(Utilities.TEMPDIRECTORYPATH); |
|
430 |
if( !tempFolder.exists() ) { |
|
431 |
try { |
|
432 |
FileUtils.forceMkdir(tempFolder); |
|
433 |
} catch(Throwable th) { |
|
434 |
throw new RuntimeException(th); |
|
435 |
} |
|
436 |
} |
|
437 |
long t = new Date().getTime(); |
|
438 |
String fname = String.format("%s-%x%x%s", name,t,sufix); |
|
439 |
return new File(tempFolder,fname); |
|
440 |
} |
|
441 |
|
|
442 |
@Override |
|
443 |
public boolean desktopBrowse(URI uri){ |
|
444 |
return DesktopApi.browse(uri); |
|
445 |
} |
|
446 |
|
|
447 |
@Override |
|
448 |
public boolean desktopOpen(File file){ |
|
449 |
return DesktopApi.open(file); |
|
450 |
} |
|
451 |
|
|
452 |
@Override |
|
453 |
public Arguments getArguments() { |
|
454 |
return Launcher.getArguments(); |
|
455 |
} |
|
456 |
} |
|
0 | 457 |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DesktopApi.java | ||
---|---|---|
1 |
package org.gvsig.andami.impl; |
|
2 |
|
|
3 |
/* |
|
4 |
* Based on portions of code of MightyPork, http://www.ondrovo.com/ |
|
5 |
* extracted from : |
|
6 |
* http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform |
|
7 |
*/ |
|
8 |
|
|
9 |
import java.awt.Desktop; |
|
10 |
import java.io.File; |
|
11 |
import java.io.IOException; |
|
12 |
import java.net.URI; |
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.List; |
|
15 |
import org.slf4j.Logger; |
|
16 |
import org.slf4j.LoggerFactory; |
|
17 |
|
|
18 |
public class DesktopApi { |
|
19 |
|
|
20 |
private static Logger logger = LoggerFactory.getLogger(DesktopApi.class); |
|
21 |
|
|
22 |
public static boolean browse(URI uri) { |
|
23 |
|
|
24 |
if ( browseDESKTOP(uri) ) { |
|
25 |
return true; |
|
26 |
} |
|
27 |
|
|
28 |
if ( openSystemSpecific(uri.toString()) ) { |
|
29 |
return true; |
|
30 |
} |
|
31 |
|
|
32 |
return false; |
|
33 |
} |
|
34 |
|
|
35 |
public static boolean open(File file) { |
|
36 |
|
|
37 |
if ( openDESKTOP(file) ) { |
|
38 |
return true; |
|
39 |
} |
|
40 |
|
|
41 |
if ( openSystemSpecific(file.getPath()) ) { |
|
42 |
return true; |
|
43 |
} |
|
44 |
|
|
45 |
return false; |
|
46 |
} |
|
47 |
|
|
48 |
public static boolean edit(File file) { |
|
49 |
|
|
50 |
if ( editDESKTOP(file) ) { |
|
51 |
return true; |
|
52 |
} |
|
53 |
|
|
54 |
if ( openSystemSpecific(file.getPath()) ) { |
|
55 |
return true; |
|
56 |
} |
|
57 |
|
|
58 |
return false; |
|
59 |
} |
|
60 |
|
|
61 |
private static boolean openSystemSpecific(String what) { |
|
62 |
|
|
63 |
EnumOS os = getOs(); |
|
64 |
|
|
65 |
if (os.isLinux()) { |
|
66 |
// if (runCommand("gnome-open", "%s", what)) { |
|
67 |
// return true; |
|
68 |
// } |
|
69 |
// if (runCommand("kde-open", "%s", what)) { |
|
70 |
// return true; |
|
71 |
// } |
|
72 |
if (runCommand("xdg-open", "%s", what)) { |
|
73 |
return true; |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
if ( os.isMac() ) { |
|
78 |
if ( runCommand("open", "%s", what) ) { |
|
79 |
return true; |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
if ( os.isWindows() ) { |
|
84 |
if ( runCommand("explorer", "%s", what) ) { |
|
85 |
return true; |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
return false; |
|
90 |
} |
|
91 |
|
|
92 |
private static boolean browseDESKTOP(URI uri) { |
|
93 |
|
|
94 |
logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString()); |
|
95 |
try { |
|
96 |
if ( !Desktop.isDesktopSupported() ) { |
|
97 |
logErr("Platform is not supported."); |
|
98 |
return false; |
|
99 |
} |
|
100 |
|
|
101 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE) ) { |
|
102 |
logErr("BROWSE is not supported."); |
|
103 |
return false; |
|
104 |
} |
|
105 |
|
|
106 |
Desktop.getDesktop().browse(uri); |
|
107 |
|
|
108 |
return true; |
|
109 |
} catch (Throwable t) { |
|
110 |
logErr("Error using desktop browse.", t); |
|
111 |
return false; |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
private static boolean openDESKTOP(File file) { |
|
116 |
|
|
117 |
logOut("Trying to use Desktop.getDesktop().open() with " + file.toString()); |
|
118 |
try { |
|
119 |
if ( !Desktop.isDesktopSupported() ) { |
|
120 |
logErr("Platform is not supported."); |
|
121 |
return false; |
|
122 |
} |
|
123 |
|
|
124 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.OPEN) ) { |
|
125 |
logErr("OPEN is not supported."); |
|
126 |
return false; |
|
127 |
} |
|
128 |
|
|
129 |
Desktop.getDesktop().open(file); |
|
130 |
|
|
131 |
return true; |
|
132 |
} catch (Throwable t) { |
|
133 |
logErr("Error using desktop open.", t); |
|
134 |
return false; |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
private static boolean editDESKTOP(File file) { |
|
139 |
|
|
140 |
logOut("Trying to use Desktop.getDesktop().edit() with " + file); |
|
141 |
try { |
|
142 |
if ( !Desktop.isDesktopSupported() ) { |
|
143 |
logErr("Platform is not supported."); |
|
144 |
return false; |
|
145 |
} |
|
146 |
|
|
147 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.EDIT) ) { |
|
148 |
logErr("EDIT is not supported."); |
|
149 |
return false; |
|
150 |
} |
|
151 |
|
|
152 |
Desktop.getDesktop().edit(file); |
|
153 |
|
|
154 |
return true; |
|
155 |
} catch (Throwable t) { |
|
156 |
logErr("Error using desktop edit.", t); |
|
157 |
return false; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
private static boolean runCommand(String command, String args, String file) { |
|
162 |
|
|
163 |
logOut("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file); |
|
164 |
|
|
165 |
String[] parts = prepareCommand(command, args, file); |
|
166 |
|
|
167 |
try { |
|
168 |
Process p = Runtime.getRuntime().exec(parts); |
|
169 |
if ( p == null ) { |
|
170 |
return false; |
|
171 |
} |
|
172 |
|
|
173 |
try { |
|
174 |
int retval = p.exitValue(); |
|
175 |
if ( retval == 0 ) { |
|
176 |
logErr("Process ended immediately."); |
|
177 |
return false; |
|
178 |
} else { |
|
179 |
logErr("Process crashed."); |
|
180 |
return false; |
|
181 |
} |
|
182 |
} catch (IllegalThreadStateException itse) { |
|
183 |
logErr("Process is running."); |
|
184 |
return true; |
|
185 |
} |
|
186 |
} catch (IOException e) { |
|
187 |
logErr("Error running command.", e); |
|
188 |
return false; |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
private static String[] prepareCommand(String command, String args, String file) { |
|
193 |
|
|
194 |
List<String> parts = new ArrayList<String>(); |
|
195 |
parts.add(command); |
|
196 |
|
|
197 |
if ( args != null ) { |
|
198 |
for ( String s : args.split(" ") ) { |
|
199 |
s = String.format(s, file); // put in the filename thing |
|
200 |
|
|
201 |
parts.add(s.trim()); |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
return parts.toArray(new String[parts.size()]); |
|
206 |
} |
|
207 |
|
|
208 |
private static void logErr(String msg, Throwable t) { |
|
209 |
logger.warn(msg,t); |
|
210 |
} |
|
211 |
|
|
212 |
private static void logErr(String msg) { |
|
213 |
logger.warn(msg); |
|
214 |
} |
|
215 |
|
|
216 |
private static void logOut(String msg) { |
|
217 |
logger.info(msg); |
|
218 |
} |
|
219 |
|
|
220 |
public static enum EnumOS { |
|
221 |
|
|
222 |
linux, macos, solaris, unknown, windows; |
|
223 |
|
|
224 |
public boolean isLinux() { |
|
225 |
|
|
226 |
return this == linux || this == solaris; |
|
227 |
} |
|
228 |
|
|
229 |
public boolean isMac() { |
|
230 |
|
|
231 |
return this == macos; |
|
232 |
} |
|
233 |
|
|
234 |
public boolean isWindows() { |
|
235 |
|
|
236 |
return this == windows; |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
public static EnumOS getOs() { |
|
241 |
|
|
242 |
String s = System.getProperty("os.name").toLowerCase(); |
|
243 |
|
|
244 |
if ( s.contains("win") ) { |
|
245 |
return EnumOS.windows; |
|
246 |
} |
|
247 |
|
|
248 |
if ( s.contains("mac") ) { |
|
249 |
return EnumOS.macos; |
|
250 |
} |
|
251 |
|
|
252 |
if ( s.contains("solaris") ) { |
|
253 |
return EnumOS.solaris; |
|
254 |
} |
|
255 |
|
|
256 |
if ( s.contains("sunos") ) { |
|
257 |
return EnumOS.solaris; |
|
258 |
} |
|
259 |
|
|
260 |
if ( s.contains("linux") ) { |
|
261 |
return EnumOS.linux; |
|
262 |
} |
|
263 |
|
|
264 |
if ( s.contains("unix") ) { |
|
265 |
return EnumOS.linux; |
|
266 |
} else { |
|
267 |
return EnumOS.unknown; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultArguments.java | ||
---|---|---|
1 |
package org.gvsig.andami.impl; |
|
2 |
|
|
3 |
import java.time.LocalDate; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.Collections; |
|
6 |
import java.util.Date; |
|
7 |
import java.util.HashMap; |
|
8 |
import java.util.Iterator; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
import org.apache.commons.lang3.BooleanUtils; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
import org.gvsig.andami.Arguments; |
|
14 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
15 |
|
|
16 |
public class DefaultArguments implements Arguments { |
|
17 |
|
|
18 |
private final String[] args; |
|
19 |
protected Map<String, String> flags; |
|
20 |
protected List<String> parameters; |
|
21 |
|
|
22 |
public DefaultArguments(String args[]) { |
|
23 |
this.args = args; |
|
24 |
this.flags = new HashMap<>(); |
|
25 |
this.parameters = new ArrayList<>(); |
|
26 |
this.parse(); |
|
27 |
} |
|
28 |
|
|
29 |
private void parse() { |
|
30 |
String name; |
|
31 |
String value; |
|
32 |
boolean acceptFlags = true; |
|
33 |
for( String arg : args ) { |
|
34 |
if( acceptFlags ) { |
|
35 |
if( arg.equals("--") ) { |
|
36 |
acceptFlags = false; |
|
37 |
continue; |
|
38 |
} |
|
39 |
if( arg.startsWith("--") ) { |
|
40 |
int n = arg.indexOf("="); |
|
41 |
if( n < 0 ) { |
|
42 |
name = arg.substring(2); |
|
43 |
value = "true"; |
|
44 |
} else { |
|
45 |
name = arg.substring(2, n); |
|
46 |
value = arg.substring(n + 1); |
|
47 |
} |
|
48 |
this.flags.put(name, value); |
|
49 |
} else { |
|
50 |
this.parameters.add(arg); |
|
51 |
} |
|
52 |
} else { |
|
53 |
this.parameters.add(arg); |
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public String get(String name) { |
|
60 |
return this.flags.get(name); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public Object get(String name, Object defaultValue) { |
|
65 |
String value = this.get(name); |
|
66 |
if( defaultValue == null ) { |
|
67 |
return value; |
|
68 |
} |
|
69 |
if( StringUtils.isBlank(value) ) { |
|
70 |
return defaultValue; |
|
71 |
} |
|
72 |
if( defaultValue instanceof CharSequence ) { |
|
73 |
return value; |
|
74 |
} |
|
75 |
if( defaultValue instanceof Integer ) { |
|
76 |
return DataTypeUtils.toInteger(value, (Integer) defaultValue); |
|
77 |
} |
|
78 |
if( defaultValue instanceof Long ) { |
|
79 |
return DataTypeUtils.toLong(value, (Long) defaultValue); |
|
80 |
} |
|
81 |
if( defaultValue instanceof Boolean ) { |
|
82 |
try { |
|
83 |
return DataTypeUtils.toBoolean(value, (Boolean) defaultValue); |
|
84 |
} catch(Throwable th) { |
|
85 |
return BooleanUtils.toBoolean(value); |
|
86 |
} |
|
87 |
} |
|
88 |
if( defaultValue instanceof Double ) { |
|
89 |
return DataTypeUtils.toDouble(value, (Double) defaultValue); |
|
90 |
} |
|
91 |
if( defaultValue instanceof Float ) { |
|
92 |
return DataTypeUtils.toDouble(value, (Float) defaultValue); |
|
93 |
} |
|
94 |
if( defaultValue instanceof Date ) { |
|
95 |
return DataTypeUtils.toDate(value, (Date) defaultValue); |
|
96 |
} |
|
97 |
return value; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public boolean contains(String name) { |
|
102 |
return this.flags.containsKey(name); |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public boolean contains(String name, String value) { |
|
107 |
String x = this.flags.get(name); |
|
108 |
if( x==null ) { |
|
109 |
return false; |
|
110 |
} |
|
111 |
return StringUtils.equalsIgnoreCase(value, x); |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public List<String> parameters() { |
|
116 |
return Collections.unmodifiableList(parameters); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public Map<String, String> flags() { |
|
121 |
return Collections.unmodifiableMap(flags); |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public int size() { |
|
126 |
return this.parameters.size(); |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public String get(int n) { |
|
131 |
return this.parameters.get(n); |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public Iterator<String> iterator() { |
|
136 |
return this.parameters.iterator(); |
|
137 |
} |
|
138 |
} |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/UnsavedDataException.java | ||
---|---|---|
1 |
package org.gvsig.andami.impl; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
|
6 |
import org.gvsig.tools.exception.BaseException; |
|
7 |
|
|
8 |
public class UnsavedDataException extends BaseException { |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
*/ |
|
13 |
private static final long serialVersionUID = 7764186865062216316L; |
|
14 |
private final static String MESSAGE_FORMAT = "Has not been able to save the following data: %(errors)\n"; |
|
15 |
private final static String MESSAGE_KEY = "_UnsavedDataException"; |
|
16 |
private List<IUnsavedData> errors; |
|
17 |
|
|
18 |
public UnsavedDataException(List<IUnsavedData> errors) { |
|
19 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID); |
|
20 |
this.errors = errors; |
|
21 |
|
|
22 |
StringBuilder errorsString = new StringBuilder(); |
|
23 |
for (IUnsavedData error : errors) { |
|
24 |
errorsString.append(" - "); |
|
25 |
errorsString.append(error.getResourceName()); |
|
26 |
errorsString.append(" -- "); |
|
27 |
errorsString.append(error.getDescription()); |
|
28 |
errorsString.append("\n"); |
|
29 |
} |
|
30 |
setValue("errors", errorsString); |
|
31 |
} |
|
32 |
|
|
33 |
public List<IUnsavedData> getUnsavedData(){ |
|
34 |
return errors; |
|
35 |
} |
|
36 |
} |
tags/org.gvsig.desktop-2.0.424/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultLocaleManager.java | ||
---|---|---|
1 |
package org.gvsig.andami.impl; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.net.URL; |
|
5 |
import java.security.AccessController; |
|
6 |
import java.security.PrivilegedAction; |
|
7 |
import java.util.Collections; |
|
8 |
import java.util.Comparator; |
|
9 |
import java.util.HashSet; |
|
10 |
import java.util.Iterator; |
|
11 |
import java.util.List; |
|
12 |
import java.util.Locale; |
|
13 |
import java.util.Set; |
|
14 |
import java.util.TreeSet; |
|
15 |
|
|
16 |
import javax.swing.JComponent; |
|
17 |
import javax.swing.SwingUtilities; |
|
18 |
|
|
19 |
import org.apache.commons.configuration.PropertiesConfiguration; |
|
20 |
import org.apache.commons.lang3.LocaleUtils; |
|
21 |
import org.apache.commons.lang3.StringUtils; |
|
22 |
import org.slf4j.Logger; |
|
23 |
import org.slf4j.LoggerFactory; |
|
24 |
|
|
25 |
import org.gvsig.andami.Launcher; |
|
26 |
import org.gvsig.andami.LocaleManager; |
|
27 |
import org.gvsig.andami.PluginServices; |
|
28 |
import org.gvsig.andami.PluginsLocator; |
|
29 |
import org.gvsig.andami.PluginsManager; |
|
30 |
import org.gvsig.andami.config.generate.AndamiConfig; |
|
31 |
import org.gvsig.andami.installer.translations.TranslationsInstallerFactory; |
|
32 |
import org.gvsig.andami.ui.mdiFrame.MDIFrame; |
|
33 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
34 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
35 |
import org.gvsig.utils.XMLEntity; |
|
36 |
|
|
37 |
public class DefaultLocaleManager implements LocaleManager { |
|
38 |
|
|
39 |
private static final Logger logger |
|
40 |
= LoggerFactory.getLogger(DefaultLocaleManager.class); |
|
41 |
|
|
42 |
private static final String LOCALE_CODE_VALENCIANO = "vl"; |
|
43 |
|
|
44 |
private static final String I18N_EXTENSION = "org.gvsig.i18n.extension"; |
|
45 |
private static final String INSTALLED_TRANSLATIONS_HOME_FOLDER = "i18n"; |
|
46 |
private static final String VARIANT = "variant"; |
|
47 |
private static final String COUNTRY = "country"; |
|
48 |
private static final String LANGUAGE = "language"; |
|
49 |
private static final String REGISTERED_LOCALES_PERSISTENCE = "RegisteredLocales"; |
|
50 |
|
|
51 |
private static final String LOCALE_CONFIG_FILENAME = "locale.config"; |
|
52 |
|
|
53 |
private final Locale[] hardcoredLocales = new Locale[]{ |
|
54 |
// Default supported locales |
|
55 |
SPANISH, // Spanish |
|
56 |
ENGLISH, // English |
|
57 |
new Locale("en", "US"), // English US |
|
58 |
new Locale("ca"), // Catalan |
|
59 |
new Locale("gl"), // Galician |
|
60 |
new Locale("eu"), // Basque |
|
61 |
new Locale("de"), // German |
|
62 |
new Locale("cs"), // Czech |
|
63 |
new Locale("fr"), // French |
|
64 |
new Locale("it"), // Italian |
|
65 |
new Locale("pl"), // Polish |
|
66 |
new Locale("pt"), // Portuguese |
|
67 |
new Locale("pt", "BR"), // Portuguese Brazil |
|
68 |
new Locale("ro"), // Romanian |
|
69 |
new Locale("zh"), // Chinese |
|
70 |
new Locale("ru"), // Russian |
|
71 |
new Locale("el"), // Greek |
|
72 |
new Locale("ro"), // Romanian |
|
73 |
new Locale("pl"), // Polish |
|
74 |
new Locale("tr"), // Turkish |
|
75 |
new Locale("sr"), // Serbio |
|
76 |
new Locale("sw") // Swahili |
|
77 |
}; |
|
78 |
|
|
79 |
private Set installedLocales = null; |
|
80 |
private Set defaultLocales = null; |
|
81 |
|
|
82 |
private boolean storedInstalledLocales = false; |
|
83 |
|
|
84 |
public DefaultLocaleManager() { |
|
85 |
Locale currentLocale = org.gvsig.i18n.Messages.getCurrentLocale(); |
|
86 |
if( currentLocale==null ) { |
|
87 |
// Is run from test, currentLocale is null. |
|
88 |
currentLocale = Locale.getDefault(); |
|
89 |
} |
|
90 |
this.setCurrentLocale(currentLocale); |
|
91 |
} |
|
92 |
|
|
93 |
public Locale getCurrentLocale() { |
|
94 |
return org.gvsig.i18n.Messages.getCurrentLocale(); |
|
95 |
} |
|
96 |
|
|
97 |
public void setCurrentLocale(final Locale locale) { |
|
98 |
Locale nearestLocale = getNearestLocale(locale); |
|
99 |
if(nearestLocale == null){ |
|
100 |
nearestLocale = this.getLocaleAlternatives(locale)[0]; |
|
101 |
} |
|
102 |
org.gvsig.i18n.Messages.setCurrentLocale(nearestLocale, this.getLocaleAlternatives(nearestLocale)); |
|
103 |
|
|
104 |
String localeStr = nearestLocale.getLanguage(); |
|
105 |
if (localeStr.equalsIgnoreCase(LOCALE_CODE_VALENCIANO)) { |
|
106 |
Locale.setDefault(new Locale("ca")); |
|
107 |
} else { |
|
108 |
Locale.setDefault(nearestLocale); |
|
109 |
} |
|
110 |
|
|
111 |
AndamiConfig config = Launcher.getAndamiConfig(); |
|
112 |
if( config != null ) { |
|
113 |
config.setLocaleLanguage(nearestLocale.getLanguage()); |
|
114 |
config.setLocaleCountry(nearestLocale.getCountry()); |
|
115 |
config.setLocaleVariant(nearestLocale.getVariant()); |
|
116 |
} |
|
117 |
setCurrentLocaleUI(nearestLocale); |
|
118 |
} |
|
119 |
|
|
120 |
public Locale getDefaultSystemLocale() { |
|
121 |
String language, region, country, variant; |
|
122 |
language = getSystemProperty("user.language", "en"); |
|
123 |
// for compatibility, check for old user.region property |
|
124 |
region = getSystemProperty("user.region", null); |
|
125 |
|
|
126 |
if (region != null) { |
|
127 |
// region can be of form country, country_variant, or _variant |
|
128 |
int i = region.indexOf('_'); |
|
129 |
if (i >= 0) { |
|
130 |
country = region.substring(0, i); |
|
131 |
variant = region.substring(i + 1); |
|
132 |
} else { |
|
133 |
country = region; |
|
134 |
variant = ""; |
|
135 |
} |
|
136 |
} else { |
|
137 |
country = getSystemProperty("user.country", ""); |
|
138 |
variant = getSystemProperty("user.variant", ""); |
|
139 |
} |
|
140 |
return new Locale(language, country, variant); |
|
141 |
} |
|
142 |
|
|
143 |
private Set<Locale> loadLocalesFromConfing(File localesFile) { |
|
144 |
PropertiesConfiguration config = null; |
|
145 |
if (!localesFile.canRead()) { |
|
146 |
return null; |
|
147 |
} |
|
148 |
try { |
|
149 |
config = new PropertiesConfiguration(localesFile); |
|
150 |
} catch (Exception ex) { |
|
151 |
logger.warn("Can't open locale configuration '" + localesFile + "'.", ex); |
|
152 |
return null; |
|
153 |
} |
|
154 |
|
|
155 |
Set<Locale> locales = new HashSet<Locale>(); |
|
156 |
List localeCodes = config.getList("locale"); |
|
157 |
for (Object localeCode : localeCodes) { |
|
158 |
try { |
|
159 |
Locale locale = LocaleUtils.toLocale((String) localeCode); |
|
160 |
locales.add(locale); |
|
161 |
} catch(IllegalArgumentException ex) { |
|
162 |
logger.warn("Can't load locale '"+localeCode+"' from config '"+localesFile.getAbsolutePath()+"'.",ex); |
|
163 |
} |
|
164 |
} |
|
165 |
return locales; |
|
166 |
} |
|
167 |
|
|
168 |
public Set<Locale> getDefaultLocales() { |
|
169 |
if (this.defaultLocales == null) { |
|
170 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
|
171 |
File localesFile = new File(pluginsManager.getApplicationI18nFolder(), LOCALE_CONFIG_FILENAME); |
|
172 |
|
|
173 |
Set<Locale> locales = loadLocalesFromConfing(localesFile); |
|
174 |
defaultLocales = new HashSet<Locale>(); |
|
175 |
if( locales == null ) { |
|
176 |
for (int i = 0; i < hardcoredLocales.length; i++) { |
|
177 |
defaultLocales.add(hardcoredLocales[i]); |
|
178 |
} |
|
179 |
} else { |
|
180 |
defaultLocales.addAll(locales); |
|
181 |
} |
|
182 |
} |
|
183 |
return this.defaultLocales; |
|
184 |
} |
|
185 |
|
|
186 |
public boolean installLocales(URL localesFile) { |
|
187 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
|
188 |
PropertiesConfiguration config = null; |
|
189 |
try { |
|
190 |
config = new PropertiesConfiguration(localesFile); |
|
191 |
} catch (Exception ex) { |
|
192 |
logger.warn("Can't open configuration '" + localesFile + "'.", ex); |
|
193 |
} |
|
194 |
if (config == null) { |
|
195 |
return false; |
|
196 |
} |
|
197 |
List localeCodes = config.getList("locale"); |
|
198 |
for (Object localeCode : localeCodes) { |
|
199 |
Locale locale = LocaleUtils.toLocale((String) localeCode); |
|
200 |
if (!getInstalledLocales().contains(locale)) { |
|
201 |
this.installedLocales.add(locale); |
|
202 |
} |
|
203 |
} |
|
204 |
storeInstalledLocales(); |
|
205 |
return true; |
|
206 |
} |
|
207 |
|
|
208 |
public Set<Locale> getInstalledLocales() { |
|
209 |
if (installedLocales == null) { |
|
210 |
installedLocales = new TreeSet<Locale>(new Comparator<Locale>() { |
|
211 |
public int compare(Locale t0, Locale t1) { |
|
212 |
if (t1 == null || t0 == null) { |
|
213 |
return 0; |
|
214 |
} |
|
215 |
// return getLanguageDisplayName(t0).compareToIgnoreCase(getLanguageDisplayName(t1)); |
|
216 |
return t0.toString().compareToIgnoreCase(t1.toString()); |
|
217 |
} |
|
218 |
}); |
|
219 |
|
|
220 |
XMLEntity child = getRegisteredLocalesPersistence(); |
|
221 |
|
|
222 |
// If the list of registered locales is not already persisted, |
|
223 |
// this should be the first time gvSIG is run with the I18nPlugin |
|
224 |
// so we will take the list of default locales |
|
225 |
if (child == null) { |
|
226 |
installedLocales.addAll(getDefaultLocales()); |
|
227 |
} else { |
|
228 |
XMLEntity localesEntity = child; |
|
229 |
for (int i = 0; i < localesEntity.getChildrenCount(); i++) { |
|
230 |
Locale locale = null; |
|
231 |
XMLEntity localeEntity = localesEntity.getChild(i); |
|
232 |
String language = localeEntity.getStringProperty(LANGUAGE); |
|
233 |
String country = localeEntity.getStringProperty(COUNTRY); |
|
234 |
String variant = localeEntity.getStringProperty(VARIANT); |
|
235 |
locale = new Locale(language, country, variant); |
|
236 |
installedLocales.add(locale); |
|
237 |
} |
|
238 |
} |
|
239 |
Set<Locale> lfp = getAllLocalesFromPackages(); |
|
240 |
installedLocales.addAll(lfp); |
|
241 |
} |
|
242 |
storeInstalledLocales(); |
|
243 |
return Collections.unmodifiableSet(installedLocales); |
|
244 |
} |
|
245 |
|
|
246 |
public void updateInstalledLocales(){ |
|
247 |
installedLocales.addAll(getAllLocalesFromPackages()); |
|
248 |
storedInstalledLocales = false; |
|
249 |
storeInstalledLocales(); |
|
250 |
} |
|
251 |
|
|
252 |
public boolean installLocale(Locale locale) { |
|
253 |
// Add the locale to the list of installed ones, if it |
|
254 |
// is new, otherwise, update the texts. |
|
255 |
if (!getInstalledLocales().contains(locale)) { |
|
256 |
getInstalledLocales().add(locale); |
|
257 |
storeInstalledLocales(); |
|
258 |
} |
|
259 |
return true; |
|
260 |
} |
|
261 |
|
|
262 |
public boolean uninstallLocale(Locale locale) { |
|
263 |
if (!getInstalledLocales().remove(locale)) { |
|
264 |
return false; |
|
265 |
} |
|
266 |
storeInstalledLocales(); |
|
267 |
return true; |
|
268 |
} |
|
269 |
|
|
270 |
public String getLanguageDisplayName(Locale locale) { |
|
271 |
|
|
272 |
String displayName; |
|
273 |
|
|
274 |
if ("eu".equals(locale.getLanguage()) |
|
275 |
&& "vascuence".equals(locale.getDisplayLanguage())) { |
|
276 |
// Correction for the Basque language display name, |
|
277 |
// show it in Basque, not in Spanish |
|
278 |
displayName = "Euskera"; |
|
279 |
} else if (LOCALE_CODE_VALENCIANO.equals(locale.getLanguage())) { |
|
280 |
// Patch for Valencian/Catalan |
|
281 |
displayName = "Valenci?"; |
|
282 |
} else { |
|
283 |
displayName = locale.getDisplayLanguage(locale); |
|
284 |
} |
|
285 |
|
|
286 |
return StringUtils.capitalize(displayName); |
|
287 |
} |
|
288 |
|
|
289 |
public String getLocaleDisplayName(Locale locale) { |
|
290 |
String displayName = this.getLanguageDisplayName(locale); |
|
291 |
|
|
292 |
if( !StringUtils.isBlank(locale.getCountry()) ) { |
|
293 |
if( !StringUtils.isBlank(locale.getVariant()) ) { |
|
294 |
displayName = displayName + " (" + |
|
295 |
StringUtils.capitalize(locale.getDisplayCountry(locale)) + "/" + |
|
296 |
locale.getDisplayVariant(locale) + ")"; |
|
297 |
} else { |
|
298 |
displayName = displayName + " ("+locale.getDisplayCountry(locale)+")"; |
|
299 |
} |
|
300 |
} |
|
301 |
|
|
302 |
return displayName; |
|
303 |
} |
|
304 |
|
|
305 |
public Locale[] getLocaleAlternatives(Locale locale) { |
|
306 |
Locale alternatives[] = null; |
|
307 |
|
|
308 |
String localeStr = locale.getLanguage(); |
|
309 |
if (localeStr.equals("es") |
|
310 |
|| localeStr.equals("ca") |
|
311 |
|| localeStr.equals("gl") |
|
312 |
|| localeStr.equals("eu") |
|
313 |
|| localeStr.equals(LOCALE_CODE_VALENCIANO)) { |
|
314 |
alternatives = new Locale[2]; |
|
315 |
alternatives[0] = new Locale("es"); |
|
316 |
alternatives[1] = new Locale("en"); |
|
317 |
} else { |
|
318 |
// prefer English for the rest |
|
319 |
alternatives = new Locale[2]; |
|
320 |
alternatives[0] = new Locale("en"); |
|
321 |
alternatives[1] = new Locale("es"); |
|
322 |
} |
|
323 |
return alternatives; |
|
324 |
} |
|
325 |
|
|
326 |
private void setCurrentLocaleUI(final Locale locale) { |
|
327 |
if (!SwingUtilities.isEventDispatchThread()) { |
|
328 |
try { |
|
329 |
SwingUtilities.invokeAndWait(new Runnable() { |
|
330 |
public void run() { |
|
331 |
setCurrentLocaleUI(locale); |
|
332 |
} |
|
333 |
}); |
|
334 |
} catch (Exception ex) { |
|
335 |
// Ignore |
|
336 |
} |
|
337 |
} |
|
338 |
try { |
|
339 |
JComponent.setDefaultLocale(locale); |
|
340 |
} catch (Exception ex) { |
|
341 |
logger.warn("Problems setting locale to JComponent.", ex); |
|
342 |
} |
|
343 |
if (MDIFrame.isInitialized()) { |
|
344 |
try { |
|
345 |
MDIFrame.getInstance().setLocale(locale); |
|
346 |
} catch (Exception ex) { |
|
347 |
logger.warn("Problems settings locale to MDIFrame.", ex); |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
|
|
352 |
public File getResourcesFolder() { |
|
353 |
File i18nFolder = new File( |
|
354 |
PluginsLocator.getManager().getApplicationHomeFolder(), |
|
355 |
INSTALLED_TRANSLATIONS_HOME_FOLDER |
|
356 |
); |
|
357 |
if (!i18nFolder.exists()) { |
|
358 |
i18nFolder.mkdirs(); |
|
359 |
} |
|
360 |
return i18nFolder; |
|
361 |
} |
|
362 |
|
|
363 |
/** |
|
364 |
* Returns the child XMLEntity with the RegisteredLocales. |
|
365 |
*/ |
|
366 |
private XMLEntity getRegisteredLocalesPersistence() { |
|
367 |
XMLEntity entity = getI18nPersistence(); |
|
368 |
if (entity == null) { |
|
369 |
return null; |
|
370 |
} |
|
371 |
XMLEntity child = null; |
|
372 |
for (int i = entity.getChildrenCount() - 1; i >= 0; i--) { |
|
373 |
XMLEntity tmpchild = entity.getChild(i); |
|
374 |
if (tmpchild.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) { |
|
375 |
child = tmpchild; |
|
376 |
break; |
|
377 |
} |
|
378 |
} |
|
379 |
return child; |
|
380 |
} |
|
381 |
|
|
382 |
/** |
|
383 |
* Stores the list of installed locales into the plugin persistence. |
|
384 |
*/ |
|
385 |
private void storeInstalledLocales() { |
|
386 |
if(this.storedInstalledLocales ){ |
|
387 |
return; |
|
388 |
} |
|
389 |
XMLEntity i18nPersistence = getI18nPersistence(); |
|
390 |
if(i18nPersistence == null){ |
|
391 |
return; |
|
392 |
} |
|
393 |
XMLEntity localesEntity = getRegisteredLocalesPersistence(); |
|
394 |
|
|
395 |
// Remove the previous list of registered languages |
|
396 |
if (localesEntity != null) { |
|
397 |
for (int i = i18nPersistence.getChildrenCount() - 1; i >= 0; i--) { |
|
398 |
XMLEntity child = i18nPersistence.getChild(i); |
|
399 |
if (child.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) { |
|
400 |
i18nPersistence.removeChild(i); |
|
401 |
break; |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
406 |
// Create the new persistence for the registered languages |
|
407 |
localesEntity = new XMLEntity(); |
|
408 |
|
|
409 |
localesEntity.setName(REGISTERED_LOCALES_PERSISTENCE); |
|
410 |
|
|
411 |
Set<Locale> locales = this.installedLocales; //getInstalledLocales(); |
|
412 |
for (Iterator iterator = locales.iterator(); iterator.hasNext();) { |
|
413 |
Locale locale = (Locale) iterator.next(); |
|
414 |
XMLEntity localeEntity = new XMLEntity(); |
|
415 |
localeEntity.setName(locale.getDisplayName()); |
|
416 |
localeEntity.putProperty(LANGUAGE, locale.getLanguage()); |
|
417 |
localeEntity.putProperty(COUNTRY, locale.getCountry()); |
|
418 |
localeEntity.putProperty(VARIANT, locale.getVariant()); |
|
419 |
|
|
420 |
localesEntity.addChild(localeEntity); |
|
421 |
} |
|
422 |
|
|
423 |
getI18nPersistence().addChild(localesEntity); |
|
424 |
this.storedInstalledLocales = true; |
|
425 |
} |
|
426 |
|
|
427 |
/** |
|
428 |
* Returns the I18n Plugin persistence. |
|
429 |
*/ |
|
430 |
private XMLEntity getI18nPersistence() { |
|
431 |
try { |
|
432 |
XMLEntity entity = PluginServices.getPluginServices(I18N_EXTENSION).getPersistentXML(); |
|
433 |
return entity; |
|
434 |
} catch (Throwable t) { |
|
435 |
// logger.warn("Can't get I18nPersistence",t); |
|
436 |
return null; |
|
437 |
} |
|
438 |
} |
|
439 |
|
|
440 |
@SuppressWarnings("unchecked") |
|
441 |
private String getSystemProperty(final String property, String defaultValue) { |
|
442 |
String value |
|
443 |
= (String) AccessController.doPrivileged(new PrivilegedAction() { |
|
444 |
public Object run() { |
|
445 |
return System.getProperty(property); |
|
446 |
} |
|
447 |
}); |
|
448 |
return value == null ? defaultValue : value; |
|
449 |
} |
|
450 |
|
|
451 |
private Set<Locale> getAllLocalesFromPackages() { |
|
452 |
Set<Locale> locales = new HashSet<Locale>(); |
|
453 |
InstallerManager installerManager = InstallerLocator.getInstallerManager(); |
|
454 |
List<File> folders = installerManager.getAddonFolders(TranslationsInstallerFactory.PROVIDER_NAME); |
|
455 |
for( File folder : folders ) { |
|
456 |
File file = new File(folder,LOCALE_CONFIG_FILENAME); |
|
457 |
Set<Locale> l = this.loadLocalesFromConfing(file); |
|
458 |
locales.addAll(l); |
|
459 |
} |
|
460 |
return locales; |
|
461 |
} |
|
462 |
|
|
463 |
public Locale getNearestLocale(Locale locale) { |
|
464 |
if( locale == null ) { |
|
465 |
return null; |
|
466 |
} |
|
467 |
String language = locale.getLanguage(); |
|
468 |
String country = locale.getCountry(); |
|
469 |
String variant = locale.getVariant(); |
|
470 |
Locale nearestLocale; |
|
471 |
if (language != null && !language.isEmpty()) { |
|
472 |
if (country != null && !country.isEmpty()) { |
|
473 |
if(variant != null && !variant.isEmpty()){ |
|
474 |
nearestLocale = new Locale(language, country, variant); |
|
475 |
if (getInstalledLocales().contains(nearestLocale)) { |
|
476 |
return nearestLocale; |
|
477 |
} |
|
478 |
} |
|
479 |
nearestLocale = new Locale(language, country); |
|
480 |
if (getInstalledLocales().contains(nearestLocale)) { |
|
481 |
return nearestLocale; |
|
482 |
} |
|
483 |
} |
|
484 |
nearestLocale = new Locale(language); |
|
485 |
if (getInstalledLocales().contains(nearestLocale)) { |
|
486 |
return nearestLocale; |
Also available in: Unified diff