svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geocoding.extension / src / org / gvsig / geocoding / preferences / GeocodingPreferences.java @ 32526
History | View | Annotate | Download (7.26 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 Prodevelop S.L. vsanjaime Programador
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.preferences; |
29 |
|
30 |
import java.io.BufferedReader; |
31 |
import java.io.File; |
32 |
import java.io.FileInputStream; |
33 |
import java.io.FileOutputStream; |
34 |
import java.io.FileReader; |
35 |
import java.io.FileWriter; |
36 |
import java.io.IOException; |
37 |
import java.net.URL; |
38 |
import java.nio.channels.FileChannel; |
39 |
import java.util.ArrayList; |
40 |
import java.util.Locale; |
41 |
|
42 |
import javax.swing.ImageIcon; |
43 |
import javax.swing.JPanel; |
44 |
|
45 |
import org.gvsig.andami.PluginServices; |
46 |
import org.gvsig.andami.preferences.AbstractPreferencePage; |
47 |
import org.gvsig.andami.preferences.StoreException; |
48 |
import org.gvsig.utils.XMLEntity; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
/**
|
53 |
* Geocoding preferences
|
54 |
*
|
55 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
56 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
57 |
*/
|
58 |
|
59 |
public class GeocodingPreferences extends AbstractPreferencePage { |
60 |
|
61 |
private static final long serialVersionUID = 1L; |
62 |
|
63 |
private ImageIcon icon; |
64 |
|
65 |
private String lang = ""; |
66 |
|
67 |
private String tag = ""; |
68 |
|
69 |
private static final Logger log = LoggerFactory |
70 |
.getLogger(GeocodingPreferences.class); |
71 |
|
72 |
static String id = GeocodingPreferences.class.getName(); |
73 |
|
74 |
public static final String GEOCODINGELEMENTS = "GeocodingElements"; |
75 |
|
76 |
public static final String ES = "es"; |
77 |
public static final String CA = "ca"; |
78 |
public static final String EN = "en"; |
79 |
|
80 |
private static final String TEMPLATES_FOLDER = "templates"; |
81 |
|
82 |
private PreferencesGeocoPanel panel;
|
83 |
|
84 |
private String andamiConfigPath = System.getProperty("user.home") |
85 |
+ File.separator + "gvSIG" + File.separator; |
86 |
|
87 |
private String geocoPath = System.getProperty("user.home") + File.separator |
88 |
+ "gvSIG" + File.separator + "geocoding"; |
89 |
|
90 |
private String geocoFolder = System.getProperty("user.home") |
91 |
+ File.separator + "gvSIG" + File.separator + "geocoding" |
92 |
+ File.separator;
|
93 |
|
94 |
private File persistenceFile; |
95 |
|
96 |
private FileReader reader; |
97 |
|
98 |
private FileWriter writer; |
99 |
|
100 |
/**
|
101 |
* Constructor
|
102 |
*/
|
103 |
public GeocodingPreferences() {
|
104 |
super();
|
105 |
|
106 |
icon = new ImageIcon(this.getClass().getClassLoader().getResource( |
107 |
"images/preferences.png"));
|
108 |
|
109 |
/* Create Panel and add to gvSIG */
|
110 |
panel = new PreferencesGeocoPanel();
|
111 |
addComponent(panel); |
112 |
|
113 |
/* Get the gvSIG Language */
|
114 |
lang = this.getLanguage();
|
115 |
tag = GEOCODINGELEMENTS + "_" + lang.trim();
|
116 |
|
117 |
/* Create the geocoding folder in the persistence */
|
118 |
this.createGeocoFolder();
|
119 |
|
120 |
/* PERSISTENCE */
|
121 |
PluginServices ps = PluginServices.getPluginServices(this);
|
122 |
XMLEntity xml = ps.getPersistentXML(); |
123 |
|
124 |
// TAG exists in the persistence
|
125 |
if (xml.contains(tag)) {
|
126 |
|
127 |
String nam = String.valueOf(xml.getStringProperty(tag)); |
128 |
persistenceFile = new File(nam); |
129 |
BufferedReader br = null; |
130 |
String str = ""; |
131 |
StringBuffer arr = new StringBuffer(); |
132 |
|
133 |
try {
|
134 |
reader = new FileReader(persistenceFile); |
135 |
br = new BufferedReader(reader); |
136 |
while ((str = br.readLine()) != null) { |
137 |
arr.append(str + "\n");
|
138 |
} |
139 |
} catch (IOException e) { |
140 |
log.error("Reading the geocoding elements file");
|
141 |
e.printStackTrace(); |
142 |
} |
143 |
|
144 |
/* Set the name file and its contents */
|
145 |
panel.setFileName(nam); |
146 |
panel.setFileText(arr.toString()); |
147 |
} |
148 |
// TAG doesn?t exist in the persistence
|
149 |
else {
|
150 |
|
151 |
URL extensionURL = this.getClass().getClassLoader().getResource( |
152 |
TEMPLATES_FOLDER + "/geocoding_" + lang + ".txt"); |
153 |
String path = extensionURL.getPath();
|
154 |
|
155 |
String topath = geocoFolder + "geocoding_" + lang + ".txt"; |
156 |
|
157 |
/* save in the persistence */
|
158 |
xml.putProperty(tag, topath); |
159 |
ps.setPersistentXML(xml); |
160 |
|
161 |
/* Copy the address elements file in the persistence */
|
162 |
FileChannel ic;
|
163 |
try {
|
164 |
ic = new FileInputStream(path).getChannel(); |
165 |
|
166 |
FileChannel oc = new FileOutputStream(topath).getChannel(); |
167 |
ic.transferTo(0, ic.size(), oc);
|
168 |
ic.close(); |
169 |
oc.close(); |
170 |
} catch (Exception e1) { |
171 |
e1.printStackTrace(); |
172 |
} |
173 |
|
174 |
BufferedReader br = null; |
175 |
persistenceFile = new File(topath); |
176 |
String str = ""; |
177 |
StringBuffer arr = new StringBuffer(); |
178 |
try {
|
179 |
reader = new FileReader(persistenceFile); |
180 |
br = new BufferedReader(reader); |
181 |
while ((str = br.readLine()) != null) { |
182 |
arr.append(str + "\n");
|
183 |
} |
184 |
} catch (IOException e) { |
185 |
log.error("Reading the geocoding elements file");
|
186 |
e.printStackTrace(); |
187 |
} |
188 |
/* Set the name file and its contents */
|
189 |
panel.setFileName(topath); |
190 |
panel.setFileText(arr.toString()); |
191 |
} |
192 |
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Store values in the persistence
|
197 |
*/
|
198 |
@Override
|
199 |
public void storeValues() throws StoreException { |
200 |
|
201 |
String txt = panel.getFileText();
|
202 |
/* Overwrite the text in the persistence file */
|
203 |
if (txt.compareToIgnoreCase("") != 0) { |
204 |
|
205 |
try {
|
206 |
writer = new FileWriter(persistenceFile); |
207 |
writer.write(txt); |
208 |
writer.close(); |
209 |
|
210 |
} catch (IOException e) { |
211 |
log.error("Reading the geocoding elements file");
|
212 |
e.printStackTrace(); |
213 |
} |
214 |
} |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Get Id
|
219 |
*
|
220 |
* @return id
|
221 |
*/
|
222 |
public String getID() { |
223 |
return id;
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Get panel
|
228 |
*/
|
229 |
public JPanel getPanel() { |
230 |
return this; |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Get name panel
|
235 |
*/
|
236 |
public String getTitle() { |
237 |
return PluginServices.getText(this, "geocoding"); |
238 |
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* Initialize default values
|
243 |
*/
|
244 |
public void initializeDefaults() { |
245 |
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* initialize values
|
250 |
*/
|
251 |
public void initializeValues() { |
252 |
|
253 |
} |
254 |
|
255 |
/*
|
256 |
* (non-Javadoc)
|
257 |
*
|
258 |
* @see com.iver.andami.preferences.IPreference#getIcon()
|
259 |
*/
|
260 |
public ImageIcon getIcon() { |
261 |
return icon;
|
262 |
} |
263 |
|
264 |
/**
|
265 |
*
|
266 |
*/
|
267 |
public boolean isValueChanged() { |
268 |
return super.hasChanged(); |
269 |
} |
270 |
|
271 |
/**
|
272 |
*
|
273 |
*/
|
274 |
public void setChangesApplied() { |
275 |
setChanged(false);
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* Get the language of gvSIG
|
280 |
*
|
281 |
* @return
|
282 |
*/
|
283 |
private String getLanguage() { |
284 |
|
285 |
ArrayList<Locale> myLocs = org.gvsig.i18n.Messages |
286 |
.getPreferredLocales(); |
287 |
Locale myLoc = myLocs.size() == 0 ? Locale.ENGLISH : myLocs.get(0); |
288 |
String lang = myLoc.getLanguage().toLowerCase();
|
289 |
return lang;
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* Create the geocoding persistence folder
|
294 |
*/
|
295 |
private void createGeocoFolder() { |
296 |
File geocoFile = new File(geocoPath); |
297 |
if (!geocoFile.exists()) {
|
298 |
geocoFile.mkdir(); |
299 |
} |
300 |
} |
301 |
|
302 |
} |