svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extNormalization / src / org / gvsig / normalization / gui / NormalizationPanelController.java @ 29217
History | View | Annotate | Download (14.7 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. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.normalization.gui; |
29 |
|
30 |
import java.awt.Component; |
31 |
import java.io.File; |
32 |
import java.io.FileReader; |
33 |
import java.io.FileWriter; |
34 |
import java.io.IOException; |
35 |
import java.io.Writer; |
36 |
import java.text.DecimalFormatSymbols; |
37 |
import java.util.ArrayList; |
38 |
import java.util.Iterator; |
39 |
import java.util.List; |
40 |
import java.util.Locale; |
41 |
|
42 |
import javax.swing.JFileChooser; |
43 |
import javax.swing.JOptionPane; |
44 |
import javax.swing.table.DefaultTableModel; |
45 |
|
46 |
import org.gvsig.fmap.dal.feature.Feature; |
47 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
48 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
49 |
import org.gvsig.normalization.algorithm.NormalizationAlgorithm; |
50 |
import org.gvsig.normalization.algorithm.impl.DefaultNormalizationAlgorithm; |
51 |
import org.gvsig.normalization.pattern.Element; |
52 |
import org.gvsig.normalization.pattern.Fieldseparator; |
53 |
import org.gvsig.normalization.pattern.Fieldtype; |
54 |
import org.gvsig.normalization.pattern.Infieldseparators; |
55 |
import org.gvsig.normalization.pattern.Patternnormalization; |
56 |
import org.gvsig.normalization.pattern.Stringvalue; |
57 |
import org.gvsig.normalization.pattern.impl.DefaultElement; |
58 |
import org.gvsig.normalization.pattern.impl.DefaultFieldseparator; |
59 |
import org.gvsig.normalization.pattern.impl.DefaultFieldtype; |
60 |
import org.gvsig.normalization.pattern.impl.DefaultInfieldseparators; |
61 |
import org.gvsig.normalization.pattern.impl.DefaultPatternnormalization; |
62 |
import org.gvsig.normalization.pattern.impl.DefaultStringvalue; |
63 |
import org.gvsig.tools.ToolsLocator; |
64 |
import org.gvsig.tools.persistence.PersistenceException; |
65 |
import org.gvsig.tools.persistence.PersistenceManager; |
66 |
import org.gvsig.tools.persistence.PersistentState; |
67 |
import org.slf4j.Logger; |
68 |
import org.slf4j.LoggerFactory; |
69 |
|
70 |
import sun.text.Normalizer; |
71 |
|
72 |
import com.iver.andami.PluginServices; |
73 |
import com.iver.utiles.GenericFileFilter; |
74 |
import com.iver.utiles.XMLEntity; |
75 |
|
76 |
/**
|
77 |
* Controller of the Normalization panel
|
78 |
*
|
79 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
80 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
81 |
*/
|
82 |
|
83 |
public class NormalizationPanelController { |
84 |
|
85 |
private static final Logger log = LoggerFactory |
86 |
.getLogger(NormalizationPanelController.class); |
87 |
|
88 |
public static final int SAMPLES = 3; |
89 |
|
90 |
private int selectedField; |
91 |
private Patternnormalization pattern;
|
92 |
private FeatureStore store = null; |
93 |
private int contador = 1; |
94 |
private String[] samples = new String[SAMPLES]; |
95 |
|
96 |
/* SINGLETON DEFINITION */
|
97 |
private volatile static NormalizationPanelController uniqueInstance; |
98 |
|
99 |
/**
|
100 |
* Get the instance of panel controller
|
101 |
*
|
102 |
* @return panel model
|
103 |
*/
|
104 |
public static NormalizationPanelController getInstance() { |
105 |
if (uniqueInstance == null) { |
106 |
synchronized (NormalizationPanelController.class) {
|
107 |
if (uniqueInstance == null) { |
108 |
uniqueInstance = new NormalizationPanelController();
|
109 |
} |
110 |
} |
111 |
} |
112 |
return uniqueInstance;
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* Private constructor
|
117 |
*/
|
118 |
private NormalizationPanelController() {
|
119 |
this.setPattern(initializeDefaultPattern());
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* This method fills the GUI sample table with the samples
|
124 |
*
|
125 |
* @return Object[][]
|
126 |
*/
|
127 |
protected Object[][] normalizeOutSamples() { |
128 |
|
129 |
int numFields = pattern.getElements().size();
|
130 |
Object[][] results = new Object[SAMPLES][numFields]; |
131 |
|
132 |
NormalizationAlgorithm na = new DefaultNormalizationAlgorithm(
|
133 |
this.pattern);
|
134 |
List<String> chains = null; |
135 |
for (int i = 0; i < SAMPLES; i++) { |
136 |
chains = na.splitChain(samples[i]); |
137 |
numFields = chains.size(); |
138 |
for (int j = 0; j < numFields; j++) { |
139 |
results[i][j] = chains.get(j); |
140 |
} |
141 |
} |
142 |
return results;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* This method up the selected element one position in the list of Elements
|
147 |
*
|
148 |
* @param pos
|
149 |
*/
|
150 |
protected void fieldUp(int pos) { |
151 |
|
152 |
int nu = pattern.getElements().size();
|
153 |
|
154 |
if (pos > 0 && nu > 1) { |
155 |
int newpos = pos - 1; |
156 |
Element[] ad = pattern.getArrayElements(); |
157 |
Element ele21 = ad[pos];
|
158 |
Element ele12 = ad[newpos];
|
159 |
|
160 |
ad[newpos] = ele21; |
161 |
ad[pos] = ele12; |
162 |
List<Element> elems = new ArrayList<Element>(); |
163 |
for (int i = 0; i < ad.length; i++) { |
164 |
elems.add(ad[i]); |
165 |
} |
166 |
pattern.setElements(elems); |
167 |
} |
168 |
} |
169 |
|
170 |
/**
|
171 |
* This method down the selected element one position in the list of
|
172 |
* Elements
|
173 |
*
|
174 |
* @param pos
|
175 |
*/
|
176 |
protected void fieldDown(int pos) { |
177 |
int nu = pattern.getElements().size();
|
178 |
|
179 |
if (pos != (nu - 1) && nu > 1) { |
180 |
int newpos = pos + 1; |
181 |
Element[] ad = pattern.getArrayElements(); |
182 |
Element ele21 = ad[pos];
|
183 |
Element ele12 = ad[newpos];
|
184 |
|
185 |
ad[newpos] = ele21; |
186 |
ad[pos] = ele12; |
187 |
List<Element> elems = new ArrayList<Element>(); |
188 |
for (int i = 0; i < ad.length; i++) { |
189 |
elems.add(ad[i]); |
190 |
} |
191 |
pattern.setElements(elems); |
192 |
} |
193 |
} |
194 |
|
195 |
/**
|
196 |
* This method adds a new element to the pattern
|
197 |
*/
|
198 |
|
199 |
protected void addField() { |
200 |
|
201 |
contador++; |
202 |
int tam = pattern.getElements().size();
|
203 |
Element eleme = new DefaultElement(); |
204 |
|
205 |
Fieldseparator fsep = new DefaultFieldseparator();
|
206 |
fsep.setSemicolonsep(true);
|
207 |
fsep.setJoinsep(false);
|
208 |
fsep.setColonsep(false);
|
209 |
fsep.setSpacesep(false);
|
210 |
fsep.setTabsep(false);
|
211 |
|
212 |
String nam = ""; |
213 |
boolean isOkName = true; |
214 |
do {
|
215 |
isOkName = true;
|
216 |
nam = "NewField" + contador;
|
217 |
for (int i = 0; i < tam; i++) { |
218 |
String napat = ((Element) pattern.getElements().get(i)) |
219 |
.getFieldname(); |
220 |
if (napat.compareToIgnoreCase(nam) == 0) { |
221 |
isOkName = false;
|
222 |
break;
|
223 |
} |
224 |
} |
225 |
if (!isOkName) {
|
226 |
contador++; |
227 |
} |
228 |
|
229 |
} while (!isOkName);
|
230 |
|
231 |
// validate the new field name
|
232 |
String vname = validateFieldName(nam);
|
233 |
eleme.setFieldname(vname); |
234 |
eleme.setFieldseparator(fsep); |
235 |
eleme.setInfieldseparators(getDefaultInfieldseparators()); |
236 |
eleme.setFieldtype(getDefaultNewFieldType()); |
237 |
eleme.setFieldwidth(0);
|
238 |
eleme.setImportfield(true);
|
239 |
|
240 |
List<Element> elems = pattern.getElements(); |
241 |
elems.add(tam, eleme); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* This method erases a selected element to the list
|
246 |
*
|
247 |
* @param pos
|
248 |
* position
|
249 |
*/
|
250 |
protected void deleteField(int pos) { |
251 |
int conta = pattern.getElements().size();
|
252 |
if (conta > 1) { |
253 |
pattern.getElements().remove(pos); |
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
258 |
* This method saves a Normalization pattern to XML file *
|
259 |
*/
|
260 |
protected void savePatternXML() { |
261 |
|
262 |
JFileChooser jfc = new JFileChooser(); |
263 |
jfc.setDialogTitle(PluginServices.getText(this, "save_norm_pattern")); |
264 |
String[] extensions = { "xml" }; |
265 |
jfc.setCurrentDirectory(new File(getFolderPattern())); |
266 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
267 |
PluginServices.getText(this, "pattern_norm_file"))); |
268 |
int returnval = jfc.showSaveDialog((Component) PluginServices |
269 |
.getMainFrame()); |
270 |
|
271 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
272 |
File thefile = jfc.getSelectedFile();
|
273 |
// Check if the file has extension .xml
|
274 |
if (!(thefile.getPath().toLowerCase().endsWith(".xml"))) { |
275 |
thefile = new File(thefile.getPath() + ".xml"); |
276 |
} |
277 |
try {
|
278 |
// the file exists
|
279 |
if (thefile.exists()) {
|
280 |
|
281 |
int n = JOptionPane.showConfirmDialog(null, PluginServices |
282 |
.getText(null, "file_exists"), PluginServices |
283 |
.getText(null, "save_norm_pattern"), |
284 |
JOptionPane.YES_NO_OPTION);
|
285 |
if (n == JOptionPane.YES_OPTION) { |
286 |
writeSaveFile(thefile); |
287 |
} |
288 |
} |
289 |
// the file not exists
|
290 |
else {
|
291 |
writeSaveFile(thefile); |
292 |
} |
293 |
} catch (Exception e) { |
294 |
log.error("Error saving the pattern", e);
|
295 |
} |
296 |
} |
297 |
} |
298 |
|
299 |
/**
|
300 |
* This method loads a Normalization pattern from a XML file and return the
|
301 |
* pattern and the String info
|
302 |
*
|
303 |
* @return pattern
|
304 |
*/
|
305 |
protected Patternnormalization loadPatternXML() {
|
306 |
|
307 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
308 |
|
309 |
Patternnormalization pat = new DefaultPatternnormalization();
|
310 |
File thefile = null; |
311 |
|
312 |
// Show the FileChooser to select a pattern
|
313 |
JFileChooser jfc = new JFileChooser(); |
314 |
jfc.setCurrentDirectory(new File(getFolderPattern())); |
315 |
jfc.setDialogTitle(PluginServices.getText(this, "load_norm_pattern")); |
316 |
String[] extensions = { "xml" }; |
317 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
318 |
PluginServices.getText(this, "pattern_norm_file"))); |
319 |
|
320 |
int returnval = jfc.showOpenDialog((Component) PluginServices |
321 |
.getMainFrame()); |
322 |
|
323 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
324 |
thefile = jfc.getSelectedFile(); |
325 |
} else {
|
326 |
return null; |
327 |
} |
328 |
|
329 |
FileReader reader = null; |
330 |
PersistentState state = null;
|
331 |
try {
|
332 |
reader = new FileReader(thefile); |
333 |
state = manager.loadState(reader); |
334 |
pat.loadFromState(state); |
335 |
reader.close(); |
336 |
} catch (Exception e) { |
337 |
log.error("Error loading the pattern", e);
|
338 |
return null; |
339 |
} |
340 |
|
341 |
return pat;
|
342 |
} |
343 |
|
344 |
/**
|
345 |
* This method returns a Element of one position
|
346 |
*
|
347 |
* @param index
|
348 |
* @return
|
349 |
*/
|
350 |
protected Element getElement(int index) { |
351 |
Element[] adrs = pattern.getArrayElements(); |
352 |
return adrs[index];
|
353 |
} |
354 |
|
355 |
/**
|
356 |
* This method generates and returns field separators
|
357 |
*
|
358 |
* @return default separators between fields
|
359 |
*/
|
360 |
protected Fieldseparator getDefaultFieldseparators() {
|
361 |
|
362 |
Fieldseparator filsep = new DefaultFieldseparator();
|
363 |
filsep.setSemicolonsep(true);
|
364 |
filsep.setJoinsep(false);
|
365 |
filsep.setColonsep(false);
|
366 |
filsep.setSpacesep(false);
|
367 |
filsep.setTabsep(false);
|
368 |
|
369 |
return filsep;
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* @return the pattern
|
374 |
*/
|
375 |
protected Patternnormalization getPattern() {
|
376 |
return pattern;
|
377 |
} |
378 |
|
379 |
/**
|
380 |
* This method sets the pattern
|
381 |
*
|
382 |
* @pat pattern
|
383 |
*/
|
384 |
protected void setPattern(Patternnormalization pat) { |
385 |
pattern = pat; |
386 |
} |
387 |
|
388 |
/**
|
389 |
*
|
390 |
* @return
|
391 |
*/
|
392 |
protected int getSelectedField() { |
393 |
return selectedField;
|
394 |
} |
395 |
|
396 |
/**
|
397 |
*
|
398 |
* @param selectedField
|
399 |
*/
|
400 |
protected void setSelectedField(int selectedField) { |
401 |
this.selectedField = selectedField;
|
402 |
} |
403 |
|
404 |
/**
|
405 |
*
|
406 |
* @return
|
407 |
*/
|
408 |
protected FeatureStore getStore() {
|
409 |
return store;
|
410 |
} |
411 |
|
412 |
/**
|
413 |
*
|
414 |
* @param store
|
415 |
*/
|
416 |
protected void setStore(FeatureStore store) { |
417 |
this.store = store;
|
418 |
} |
419 |
|
420 |
/**
|
421 |
* set the number of first rows that not normalize
|
422 |
*
|
423 |
* @param first
|
424 |
* select the first row
|
425 |
*/
|
426 |
protected void setFirstRows(int first) { |
427 |
pattern.setNofirstrows(first); |
428 |
|
429 |
} |
430 |
|
431 |
/**
|
432 |
* get Normalize the first row
|
433 |
*
|
434 |
* @return normalize first row
|
435 |
*/
|
436 |
protected int getFirstRows() { |
437 |
return pattern.getNofirstrows();
|
438 |
|
439 |
} |
440 |
|
441 |
/**
|
442 |
* This method creates the default Normalization pattern
|
443 |
*/
|
444 |
private Patternnormalization initializeDefaultPattern() {
|
445 |
|
446 |
Patternnormalization pat = new DefaultPatternnormalization();
|
447 |
|
448 |
pat.setPatternname("defaultPattern");
|
449 |
pat.setNofirstrows(0);
|
450 |
|
451 |
/* Create the first Address Element */
|
452 |
Element elem = new DefaultElement(); |
453 |
|
454 |
elem.setFieldname("NewField");
|
455 |
elem.setFieldseparator(getDefaultFieldseparators()); |
456 |
elem.setInfieldseparators(getDefaultInfieldseparators()); |
457 |
elem.setFieldtype(getDefaultNewFieldType()); |
458 |
elem.setFieldwidth(0);
|
459 |
elem.setImportfield(true);
|
460 |
|
461 |
List<Element> elems = new ArrayList<Element>(); |
462 |
elems.add(elem); |
463 |
|
464 |
pat.setElements(elems); |
465 |
|
466 |
return pat;
|
467 |
} |
468 |
|
469 |
/**
|
470 |
* This method validates the name of a new field
|
471 |
*
|
472 |
* @param text
|
473 |
* @return field name formatted
|
474 |
*/
|
475 |
private String validateFieldName(String text) { |
476 |
|
477 |
String temp = Normalizer.normalize(text, Normalizer.DECOMP, 0); |
478 |
temp = temp.replaceAll("[^\\p{ASCII}]", ""); |
479 |
temp = temp.replaceAll("[\\s]+", "_"); |
480 |
temp = temp.toUpperCase(); |
481 |
|
482 |
return temp;
|
483 |
} |
484 |
|
485 |
/**
|
486 |
* This method generates and returns in field separators
|
487 |
*
|
488 |
* @return special characters within one string
|
489 |
*/
|
490 |
|
491 |
private Infieldseparators getDefaultInfieldseparators() {
|
492 |
/* create the default in-field separators */
|
493 |
Locale loc = Locale.getDefault(); |
494 |
DecimalFormatSymbols dfs = new DecimalFormatSymbols(loc); |
495 |
Infieldseparators infilsep = new DefaultInfieldseparators();
|
496 |
infilsep.setThousandseparator(Character.toString(dfs
|
497 |
.getGroupingSeparator())); |
498 |
infilsep.setDecimalseparator(Character.toString(dfs
|
499 |
.getDecimalSeparator())); |
500 |
infilsep.setTextseparator("\"");
|
501 |
return infilsep;
|
502 |
} |
503 |
|
504 |
/**
|
505 |
* This method generates and returns a new field type of type Stringvalue
|
506 |
*
|
507 |
* @return field type
|
508 |
*/
|
509 |
private Fieldtype getDefaultNewFieldType() {
|
510 |
Fieldtype newtype = new DefaultFieldtype();
|
511 |
Stringvalue strval = new DefaultStringvalue();
|
512 |
strval.setStringvaluewidth(50);
|
513 |
newtype.setStringvalue(strval); |
514 |
return newtype;
|
515 |
} |
516 |
|
517 |
/**
|
518 |
* This method write the save xml file
|
519 |
*
|
520 |
* @param file
|
521 |
* @throws IOException
|
522 |
* @throws PersistenceException
|
523 |
*/
|
524 |
private void writeSaveFile(File thefile) throws IOException, |
525 |
PersistenceException { |
526 |
|
527 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
528 |
|
529 |
pattern.setPatternname(thefile.getName()); |
530 |
|
531 |
Writer writer = new FileWriter(thefile); |
532 |
PersistentState state = manager.getState(pattern); |
533 |
state.save(writer); |
534 |
writer.close(); |
535 |
} |
536 |
|
537 |
/**
|
538 |
* This method return the folder where gvSIG stores the patterns
|
539 |
*
|
540 |
* @return
|
541 |
*/
|
542 |
private String getFolderPattern() { |
543 |
XMLEntity xml = PluginServices.getPluginServices(this)
|
544 |
.getPersistentXML(); |
545 |
String pathFolder = String.valueOf(xml |
546 |
.getStringProperty("Normalization_pattern_folder"));
|
547 |
return pathFolder;
|
548 |
} |
549 |
|
550 |
/**
|
551 |
* Gets the strings sample for the table of samples
|
552 |
*
|
553 |
* @return table model
|
554 |
*/
|
555 |
protected DefaultTableModel getSamplesDataStore() { |
556 |
|
557 |
FeatureSet features = (FeatureSet) store.getDataSet(); |
558 |
Iterator<Feature> it = features.iterator();
|
559 |
int contador = 0; |
560 |
while (it.hasNext()) {
|
561 |
Feature feature = (Feature) it.next(); |
562 |
if(contador<SAMPLES){
|
563 |
String sample = feature.get(selectedField).toString();
|
564 |
samples[contador] = sample; |
565 |
} |
566 |
} |
567 |
|
568 |
Object[][] data = new Object[samples.length][1]; |
569 |
for (int i = 0; i < samples.length; i++) { |
570 |
data[i][0] = samples[i];
|
571 |
} |
572 |
DefaultTableModel tablemodel = new DefaultTableModel(data, new String[] { this.controller |
573 |
.getFieldToNormalize() }) { |
574 |
|
575 |
private static final long serialVersionUID = -7429493540158414622L; |
576 |
|
577 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
578 |
return false; |
579 |
} |
580 |
}; |
581 |
} else {
|
582 |
Object[] names = { controller.getFileName() }; |
583 |
dtm1 = new DefaultTableModel(data, names); |
584 |
} |
585 |
|
586 |
return tablemodel;
|
587 |
} |
588 |
|
589 |
} |