gvsig-desktop-customize / trunk / org.gvsig.customize.app / org.gvsig.customize.app.mainplugin / src / main / java / org / gvsig / customize / Configuration.java @ 56
History | View | Annotate | Download (10.5 KB)
1 |
package org.gvsig.customize; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileOutputStream; |
7 |
import java.io.IOException; |
8 |
import java.io.InputStream; |
9 |
import java.util.Map; |
10 |
import javax.swing.JOptionPane; |
11 |
import org.apache.commons.io.FileUtils; |
12 |
import org.apache.commons.io.IOUtils; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.andami.PluginsLocator; |
16 |
import org.gvsig.andami.PluginsManager; |
17 |
import org.gvsig.app.ApplicationLocator; |
18 |
import org.gvsig.app.ApplicationManager; |
19 |
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.dynobject.DynClass; |
22 |
import org.gvsig.tools.dynobject.DynObject; |
23 |
import org.gvsig.tools.dynobject.DynObjectManager; |
24 |
import org.gvsig.tools.persistence.PersistenceManager; |
25 |
import org.gvsig.tools.persistence.PersistentState; |
26 |
import org.gvsig.tools.persistence.xml.XMLPersistenceManager; |
27 |
import org.gvsig.tools.service.ServiceException; |
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
public class Configuration { |
32 |
|
33 |
private final static Logger logger = LoggerFactory.getLogger(CustomizeExtension.class); |
34 |
private Map<String, DynClass> definitions; |
35 |
|
36 |
public Configuration() { |
37 |
this.definitions = null; |
38 |
} |
39 |
|
40 |
public void edit() { |
41 |
ApplicationManager application = ApplicationLocator.getManager(); |
42 |
final DynObject config = load();
|
43 |
if (config == null) { |
44 |
application.messageDialog("Can't show the configuration dialog.\nSee the error log for more information.", "Warning", JOptionPane.WARNING_MESSAGE); |
45 |
return;
|
46 |
} |
47 |
try {
|
48 |
final DynObjectEditor editor = new DynObjectEditor(config, false); |
49 |
editor.setPreferredSize(new Dimension(800, 400)); |
50 |
editor.setOnClose(new Runnable() { |
51 |
public void run() { |
52 |
if (editor.isCanceled()) {
|
53 |
return;
|
54 |
} |
55 |
save(config); |
56 |
} |
57 |
}); |
58 |
editor.editObject(false);
|
59 |
|
60 |
} catch (ServiceException ex) {
|
61 |
logger.warn("Can't edit customize configuration", ex);
|
62 |
application.messageDialog("Can't show the configuration dialog.\nSee the error log for more information.", "Warning", JOptionPane.WARNING_MESSAGE); |
63 |
} |
64 |
|
65 |
} |
66 |
|
67 |
private File getConfigurationFile() { |
68 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
69 |
PluginServices plugin = pluginManager.getPlugin(CustomizeExtension.class); |
70 |
//
|
71 |
// El fichero de configuracion NO puede estar en el home del usuario.
|
72 |
// Siempre estara en la carpeta de la instalacion del plugin.
|
73 |
File configFile = new File(plugin.getPluginDirectory(), "configure.dat"); |
74 |
return configFile;
|
75 |
} |
76 |
|
77 |
public DynObject load() {
|
78 |
FileInputStream is = null; |
79 |
try {
|
80 |
Map<String, DynClass> definitions = getDefinitions(); |
81 |
if (definitions == null) { |
82 |
return null; |
83 |
} |
84 |
DynClass def = definitions.get("Configuration");
|
85 |
if (def == null) { |
86 |
logger.warn("Can't find definition 'Configuration'.");
|
87 |
return null; |
88 |
} |
89 |
DynObject config = null;
|
90 |
File configFile = getConfigurationFile();
|
91 |
if (configFile.exists()) {
|
92 |
is = new FileInputStream(configFile); |
93 |
PersistenceManager pman = ToolsLocator.getPersistenceManager();//new XMLPersistenceManager();
|
94 |
config = (DynObject) pman.getObject(is); |
95 |
} else {
|
96 |
config = this.create();
|
97 |
} |
98 |
return config;
|
99 |
} catch (Exception ex) { |
100 |
logger.warn("Can't load configuration file.", ex);
|
101 |
return null; |
102 |
} finally {
|
103 |
IOUtils.closeQuietly(is); |
104 |
} |
105 |
} |
106 |
|
107 |
public Map<String, DynClass> getDefinitions() { |
108 |
if (this.definitions == null) { |
109 |
this.definitions = this.loadDefinitions(); |
110 |
} |
111 |
return this.definitions; |
112 |
} |
113 |
|
114 |
private Map<String, DynClass> loadDefinitions() { |
115 |
InputStream formIs = null; |
116 |
|
117 |
try {
|
118 |
DynObjectManager dynObjectManager = null;
|
119 |
String resourceName = "configure.form"; |
120 |
Map<String, DynClass> definitions = null; |
121 |
try {
|
122 |
dynObjectManager = ToolsLocator.getDynObjectManager(); |
123 |
formIs = this.getClass().getResourceAsStream("/" + resourceName); |
124 |
definitions = dynObjectManager.importDynClassDefinitions(formIs, this.getClass().getClassLoader());
|
125 |
} catch (Exception ex) { |
126 |
logger.warn("Can't load form definition from '" + resourceName + "'.", ex); |
127 |
return null; |
128 |
} |
129 |
for (DynClass definition : definitions.values()) {
|
130 |
if (!dynObjectManager.has(definition.getFullName())) {
|
131 |
dynObjectManager.add(definition); |
132 |
} |
133 |
} |
134 |
return definitions;
|
135 |
} catch (Exception ex) { |
136 |
logger.warn("Can't load definitions for the configuration file.", ex);
|
137 |
return null; |
138 |
} finally {
|
139 |
IOUtils.closeQuietly(formIs); |
140 |
} |
141 |
} |
142 |
|
143 |
public DynObject create() {
|
144 |
DynObjectManager dynObjectManager = null;
|
145 |
dynObjectManager = ToolsLocator.getDynObjectManager(); |
146 |
|
147 |
Map<String, DynClass> definitions = getDefinitions(); |
148 |
if (definitions == null) { |
149 |
return null; |
150 |
} |
151 |
DynObject config = dynObjectManager.createDynObject(definitions.get("Configuration"));
|
152 |
DynObject crs = dynObjectManager.createDynObject(definitions.get("CRSConfiguration"));
|
153 |
DynObject httpproxy = dynObjectManager.createDynObject(definitions.get("ProxyConfiguration"));
|
154 |
DynObject theme = dynObjectManager.createDynObject(definitions.get("ThemeConfiguration"));
|
155 |
|
156 |
config.setDynValue("crs", crs);
|
157 |
config.setDynValue("httpproxy", httpproxy);
|
158 |
config.setDynValue("theme", theme);
|
159 |
|
160 |
return config;
|
161 |
} |
162 |
|
163 |
public void save(DynObject config) { |
164 |
FileOutputStream os = null; |
165 |
try {
|
166 |
os = new FileOutputStream(getConfigurationFile()); |
167 |
PersistenceManager pman = ToolsLocator.getPersistenceManager();//new XMLPersistenceManager();
|
168 |
PersistentState state = pman.getState(config); |
169 |
pman.saveState(state, os); |
170 |
updateAndamiTheme((DynObject) config.getDynValue("theme"));
|
171 |
} catch (Exception ex) { |
172 |
logger.warn("Can't save configuration file.", ex);
|
173 |
} finally {
|
174 |
IOUtils.closeQuietly(os); |
175 |
} |
176 |
} |
177 |
|
178 |
private void updateAndamiTheme(DynObject config) { |
179 |
try {
|
180 |
StringBuffer writer = new StringBuffer(); |
181 |
writer.append("<AndamiProperties>\n");
|
182 |
writer.append(" <ApplicationImages>\n");
|
183 |
writer.append(" <SplashImages>\n");
|
184 |
writer.append(" <Splash\n");
|
185 |
writer.append(" path=\"splash.png\"\n");
|
186 |
writer.append(" timer=\"10000\" \n");
|
187 |
writer.append(" x=\"").append(config.getDynValue("splash_x")).append("\" \n"); |
188 |
writer.append(" y=\"").append(config.getDynValue("splash_y")).append("\" \n"); |
189 |
writer.append(" fontsize=\"").append(config.getDynValue("splash_font_size")).append("\" \n"); |
190 |
writer.append(" color=\"").append(config.getDynValue("splash_font_color")).append("\" \n"); |
191 |
writer.append(" version=\"").append(config.getDynValue("splash_version")).append("\"/>\n"); |
192 |
writer.append(" </SplashImages>\n");
|
193 |
|
194 |
if (config.getDynValue("wallpaper_image") == null) { |
195 |
writer.append("<!--\n");
|
196 |
writer.append(" <BackgroundImage path=\"wallpaper.png\"/>\n");
|
197 |
writer.append("-->\n");
|
198 |
} else {
|
199 |
writer.append(" <BackgroundImage path=\"wallpaper.png\"/>\n");
|
200 |
} |
201 |
|
202 |
if (StringUtils.isEmpty((String) config.getDynValue("background_color"))) { |
203 |
writer.append("<!--\n");
|
204 |
writer.append(" <BackgroundColor color=\"0,0,0\"/>\n");
|
205 |
writer.append("-->\n");
|
206 |
} else {
|
207 |
writer.append(" <BackgroundColor color=\"").append(config.getDynValue("background_color")).append("\"/>\n"); |
208 |
} |
209 |
|
210 |
writer.append(" <WallpaperType value=\"").append(config.getDynValue("wallpaper_mode")).append("\"/>\n"); |
211 |
writer.append(" <Icon path=\"icon16x16.png\"/>\n");
|
212 |
writer.append(" </ApplicationImages>\n");
|
213 |
writer.append("<ApplicationName value=\"").append(config.getDynValue("applicationname")).append("\"/>\n"); |
214 |
writer.append("<priority value=\"1000\"/>\n");
|
215 |
writer.append("</AndamiProperties>\n");
|
216 |
|
217 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
218 |
PluginServices plugin = pluginManager.getPlugin(CustomizeExtension.class); |
219 |
File andamiThemeFolder = new File(plugin.getPluginDirectory(), "theme").getAbsoluteFile(); |
220 |
File andamiThemeFile = new File(andamiThemeFolder, "andami-theme.xml"); |
221 |
FileUtils.write(andamiThemeFile, writer.toString()); |
222 |
|
223 |
File file = (File) config.getDynValue("splash_image"); |
224 |
if (!file.isAbsolute()) {
|
225 |
file = new File(andamiThemeFolder, file.getPath()); |
226 |
} |
227 |
File targetFile = new File(andamiThemeFolder, "splash.png"); |
228 |
if (!file.equals(targetFile)) {
|
229 |
FileUtils.copyFile(file, targetFile); |
230 |
} |
231 |
|
232 |
file = (File) config.getDynValue("wallpaper_image"); |
233 |
if (file != null) { |
234 |
if (!file.isAbsolute()) {
|
235 |
file = new File(andamiThemeFolder, file.getPath()); |
236 |
} |
237 |
targetFile = new File(andamiThemeFolder, "wallpaper.png"); |
238 |
if (!file.equals(targetFile)) {
|
239 |
FileUtils.copyFile(file, targetFile); |
240 |
} |
241 |
} |
242 |
file = (File) config.getDynValue("icon"); |
243 |
if (!file.isAbsolute()) {
|
244 |
file = new File(andamiThemeFolder, file.getPath()); |
245 |
} |
246 |
targetFile = new File(andamiThemeFolder, "icon16x16.png"); |
247 |
if (!file.equals(targetFile)) {
|
248 |
FileUtils.copyFile(file, targetFile); |
249 |
} |
250 |
} catch (IOException ex) { |
251 |
logger.warn("Can't update gvSIG theme", ex);
|
252 |
} |
253 |
} |
254 |
|
255 |
} |