svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / ApplicationLibrary.java @ 43983
History | View | Annotate | Download (5.18 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.app; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
|
29 |
import javax.swing.JOptionPane; |
30 |
|
31 |
import org.apache.commons.io.FileUtils; |
32 |
import org.gvsig.andami.PluginServices; |
33 |
import org.gvsig.andami.PluginsLocator; |
34 |
import org.gvsig.app.extension.InitializeApplicationExtension; |
35 |
import org.gvsig.app.gui.preferencespage.AppSymbolPreferences; |
36 |
import org.gvsig.app.project.DefaultProject; |
37 |
import org.gvsig.fmap.mapcontext.MapContextLibrary; |
38 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
39 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
40 |
import org.gvsig.fmap.mapcontrol.MapControlLibrary; |
41 |
import org.gvsig.i18n.Messages; |
42 |
import org.gvsig.installer.lib.api.InstallerLibrary; |
43 |
import org.gvsig.tools.library.AbstractLibrary; |
44 |
import org.gvsig.tools.library.LibraryException; |
45 |
import org.gvsig.tools.locator.ReferenceNotRegisteredException; |
46 |
import org.slf4j.Logger; |
47 |
import org.slf4j.LoggerFactory; |
48 |
|
49 |
/**
|
50 |
* @author 2008-2009 jmvivo
|
51 |
* @author 2009- jjdelcerro
|
52 |
*
|
53 |
*/
|
54 |
public class ApplicationLibrary extends AbstractLibrary { |
55 |
|
56 |
private static final Logger logger = |
57 |
LoggerFactory.getLogger(ApplicationLibrary.class); |
58 |
|
59 |
@Override
|
60 |
public void doRegistration() { |
61 |
registerAsAPI(ApplicationLibrary.class); |
62 |
require(MapContextLibrary.class); |
63 |
require(MapControlLibrary.class); |
64 |
require(InstallerLibrary.class); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
protected void doInitialize() throws LibraryException { |
69 |
// Do nothing
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
protected void doPostInitialize() throws LibraryException { |
74 |
ApplicationManager manager = ApplicationLocator.getManager(); |
75 |
if (manager == null) { |
76 |
throw new ReferenceNotRegisteredException( |
77 |
ApplicationLocator.APPGVSIG_MANAGER_NAME, ApplicationLocator |
78 |
.getInstance()); |
79 |
} |
80 |
DefaultProject.registerPersistent(); |
81 |
|
82 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
83 |
|
84 |
PluginServices plugin = PluginsLocator.getManager().getPlugin(InitializeApplicationExtension.class); |
85 |
mapContextManager.setColorTableLibraryFolder(new File(plugin.getPluginHomeFolder(),"colortable")); |
86 |
|
87 |
MapContextLocator.getSymbolManager().setSymbolPreferences(new AppSymbolPreferences());
|
88 |
|
89 |
installBaseSymbols(); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Copy symbol folders from this plugin's 'Symbols' folder
|
94 |
* to user's 'Symbols' folder unless a folder with same name
|
95 |
* exists.
|
96 |
*/
|
97 |
private void installBaseSymbols() { |
98 |
PluginServices ps = PluginsLocator.getManager().getPlugin(InitializeApplicationExtension.class); |
99 |
File from_folder = new File( ps.getPluginDirectory(), "Symbols"); |
100 |
String to_folder = MapContextLocator.getSymbolManager().
|
101 |
getSymbolPreferences().getSymbolLibraryPath(); |
102 |
try {
|
103 |
copyMissingFolders(from_folder, new File(to_folder)); |
104 |
} catch (IOException ioe) { |
105 |
ApplicationLocator.getManager().message( |
106 |
Messages.getText("_Unable_to_copy_symbols_from_main_plugin"),
|
107 |
JOptionPane.ERROR_MESSAGE);
|
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
private void copyMissingFolders(File fromf, File tof) |
113 |
throws IOException { |
114 |
|
115 |
if (fromf == null || tof == null) { |
116 |
return;
|
117 |
} |
118 |
|
119 |
if (!fromf.exists() || !fromf.isDirectory() ||
|
120 |
(tof.exists() && tof.isFile())) { |
121 |
return;
|
122 |
} |
123 |
|
124 |
if (!tof.exists()) {
|
125 |
tof.mkdirs(); |
126 |
} |
127 |
|
128 |
File[] from_subs = fromf.listFiles(); |
129 |
for (int i=0; i<from_subs.length; i++) { |
130 |
if (!folderIsOneIn(from_subs[i], tof)) {
|
131 |
logger.info("Copying symbols subfolder: " + from_subs[i].getName());
|
132 |
FileUtils.copyDirectoryToDirectory(from_subs[i], tof); |
133 |
} else {
|
134 |
logger.info("Symbols subfolder already exists: " + from_subs[i].getName());
|
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
|
140 |
/**
|
141 |
* Tells whether <folder> is a folder and <tofolder>
|
142 |
* has a file or subfolder with the same short name as
|
143 |
* <folder>
|
144 |
*
|
145 |
*/
|
146 |
private boolean folderIsOneIn(File folder, File tofolder) { |
147 |
|
148 |
if (!folder.isDirectory()) {
|
149 |
return false; |
150 |
} |
151 |
|
152 |
String name = folder.getName();
|
153 |
String tname = tofolder.getAbsolutePath() + File.separator + name; |
154 |
File tfile = new File(tname); |
155 |
return tfile.exists();
|
156 |
} |
157 |
|
158 |
|
159 |
} |