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