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