svn-gvsig-desktop / trunk / extensions / extNormalization / src / org / gvsig / normalization / gui / NormPanelModel.java @ 22982
History | View | Annotate | Download (25.1 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.FileNotFoundException; |
33 |
import java.io.FileReader; |
34 |
import java.io.FileWriter; |
35 |
import java.text.DecimalFormatSymbols; |
36 |
import java.util.ArrayList; |
37 |
import java.util.List; |
38 |
import java.util.Locale; |
39 |
|
40 |
import javax.swing.DefaultListModel; |
41 |
import javax.swing.JFileChooser; |
42 |
import javax.swing.JOptionPane; |
43 |
import javax.swing.event.ChangeListener; |
44 |
|
45 |
import org.apache.log4j.Logger; |
46 |
import org.gvsig.normalization.operations.JoinedTableNormalization; |
47 |
import org.gvsig.normalization.operations.NormAlgorithm; |
48 |
import org.gvsig.normalization.operations.Normalization; |
49 |
import org.gvsig.normalization.operations.NormalizationNewTable; |
50 |
import org.gvsig.normalization.operations.StringListNormalization; |
51 |
import org.gvsig.normalization.operations.TableNormalization; |
52 |
import org.gvsig.patterns.normalization.Element; |
53 |
import org.gvsig.patterns.normalization.Fieldseparator; |
54 |
import org.gvsig.patterns.normalization.Fieldtype; |
55 |
import org.gvsig.patterns.normalization.Infieldseparators; |
56 |
import org.gvsig.patterns.normalization.Patternnormalization; |
57 |
import org.gvsig.patterns.normalization.Stringvalue; |
58 |
import org.gvsig.tools.storage.IStorage; |
59 |
import org.gvsig.tools.storage.StorageXML; |
60 |
|
61 |
import sun.text.Normalizer; |
62 |
|
63 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
64 |
import com.hardcode.gdbms.engine.data.DataSource; |
65 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
66 |
import com.iver.andami.PluginServices; |
67 |
import com.iver.cit.gvsig.ProjectExtension; |
68 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
69 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
70 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
71 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
72 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
73 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
74 |
import com.iver.cit.gvsig.project.Project; |
75 |
import com.iver.cit.gvsig.project.ProjectFactory; |
76 |
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory; |
77 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
78 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
79 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
80 |
import com.iver.utiles.GenericFileFilter; |
81 |
import com.iver.utiles.XMLEntity; |
82 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
83 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
84 |
|
85 |
/**
|
86 |
* Model of the Normalization panel
|
87 |
*
|
88 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
89 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
90 |
*/
|
91 |
|
92 |
public class NormPanelModel implements INormPanelModel { |
93 |
|
94 |
/* ATTRIBUTES */
|
95 |
|
96 |
private static final Logger log = PluginServices.getLogger(); |
97 |
|
98 |
private static final String ERROR = "ERROR: "; |
99 |
|
100 |
private static final String INFO = "INFO: "; |
101 |
|
102 |
private static StringBuffer infob = new StringBuffer(); |
103 |
|
104 |
private String fieldToNormalize; |
105 |
|
106 |
private String[] samples; |
107 |
|
108 |
private Patternnormalization pattern;
|
109 |
|
110 |
private Table tab;
|
111 |
|
112 |
private boolean inNewTable; |
113 |
|
114 |
private int selectedFieldInList = 0; |
115 |
|
116 |
private int contador = 1; |
117 |
|
118 |
private String[] relateNames; |
119 |
|
120 |
private ArrayList<String> fileChains; |
121 |
|
122 |
private boolean isFile; |
123 |
|
124 |
private String nameFile; |
125 |
|
126 |
/* SINGLETON DEFINITION */
|
127 |
private volatile static INormPanelModel uniqueInstance; |
128 |
|
129 |
/**
|
130 |
* Get the instance of panel model
|
131 |
*
|
132 |
* @return panel model
|
133 |
*/
|
134 |
public static INormPanelModel getInstance() { |
135 |
if (uniqueInstance == null) { |
136 |
synchronized (NormPanelModel.class) {
|
137 |
if (uniqueInstance == null) { |
138 |
uniqueInstance = new NormPanelModel();
|
139 |
} |
140 |
} |
141 |
} |
142 |
return uniqueInstance;
|
143 |
} |
144 |
|
145 |
/* BUILDERS */
|
146 |
|
147 |
private NormPanelModel() {
|
148 |
initPattern(); |
149 |
} |
150 |
|
151 |
// Constructors prior to the singleton pattern adoption
|
152 |
|
153 |
// private NormPanelModel(Table tab2) {
|
154 |
// this();
|
155 |
// isFile = false;
|
156 |
// /* Table */
|
157 |
// this.tab = tab2;
|
158 |
// }
|
159 |
//
|
160 |
// private NormPanelModel(ArrayList<String> arr, String _nameFile) {
|
161 |
// this();
|
162 |
// isFile = true;
|
163 |
// /* all strings from file */
|
164 |
// fileChains = arr;
|
165 |
// /* file name */
|
166 |
// nameFile = _nameFile;
|
167 |
// /* Samples of strings */
|
168 |
// getSamplesFromFile();
|
169 |
// }
|
170 |
|
171 |
/* METHODS */
|
172 |
|
173 |
/**
|
174 |
* This method fills the GUI sample table with the samples
|
175 |
*
|
176 |
* @return Object[][]
|
177 |
*/
|
178 |
@SuppressWarnings("unchecked") |
179 |
public Object[][] normalizeSamples() { |
180 |
|
181 |
int numFields = pattern.getElements().size();
|
182 |
Object[][] results = new Object[NormalizationPanel.SAMPLES][numFields]; |
183 |
|
184 |
NormAlgorithm na = new NormAlgorithm(this.pattern); |
185 |
List<String> chains; |
186 |
for (int i = 0; i < NormalizationPanel.SAMPLES; i++) { |
187 |
chains = na.splitChain(samples[i]); |
188 |
numFields = chains.size(); |
189 |
for (int j = 0; j < numFields; j++) { |
190 |
results[i][j] = chains.get(j); |
191 |
} |
192 |
} |
193 |
return results;
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* This method updates the model of the names FieldList
|
198 |
*
|
199 |
* @return list model
|
200 |
*/
|
201 |
public DefaultListModel updateListModel() { |
202 |
|
203 |
DefaultListModel dlmodel = new DefaultListModel(); |
204 |
Element[] adr = this.pattern.getArrayElements(); |
205 |
String name = ""; |
206 |
for (int i = 0; i < adr.length; i++) { |
207 |
name = adr[i].getFieldname(); |
208 |
dlmodel.add(i, name); |
209 |
} |
210 |
return dlmodel;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Run the process of normalization
|
215 |
*
|
216 |
* @return process ok
|
217 |
*/
|
218 |
public boolean runModel(ChangeListener listener) { |
219 |
|
220 |
Normalization normAction = null;
|
221 |
// REQUEST FILE TO OUPUT THE DBF
|
222 |
File thefile = null; |
223 |
if (this.isFile || (!this.isFile && this.inNewTable)) { |
224 |
thefile = getDBFFile(); |
225 |
if (thefile == null) { |
226 |
addInfo(INFO + PluginServices.getText(null, "endnormalizing") |
227 |
+ " \n");
|
228 |
return false; |
229 |
} |
230 |
if (thefile.exists() ) {
|
231 |
addInfo(ERROR + PluginServices.getText(null, "fileexists") |
232 |
+ " \n");
|
233 |
addInfo(INFO + PluginServices.getText(null, "endnormalizing") |
234 |
+ " \n");
|
235 |
return false; |
236 |
} |
237 |
} |
238 |
|
239 |
// FILE NORMALIZATION
|
240 |
if (this.isFile) { |
241 |
normAction = new StringListNormalization(pattern, fileChains,
|
242 |
thefile); |
243 |
} else {
|
244 |
IEditableSource source = tab.getModel().getModelo(); |
245 |
int index = tab.getSelectedFieldIndices().nextSetBit(0); |
246 |
|
247 |
if (!this.inNewTable) { |
248 |
// ONE TALBE
|
249 |
log.debug("New fields in the original table");
|
250 |
normAction = new TableNormalization(source, index, pattern);
|
251 |
} else {
|
252 |
// JOINED TABLE
|
253 |
normAction = new JoinedTableNormalization(source, index,
|
254 |
pattern, relateNames, thefile); |
255 |
} |
256 |
} |
257 |
|
258 |
normAction.registerListener(listener); |
259 |
boolean pre = normAction.preProcess();
|
260 |
|
261 |
if (pre) {
|
262 |
PluginServices.cancelableBackgroundExecution(new NormalizationTask(
|
263 |
normAction, this));
|
264 |
} else {
|
265 |
log.error("Error preprocessing tables");
|
266 |
return false; |
267 |
} |
268 |
|
269 |
if (normAction instanceof NormalizationNewTable) { |
270 |
NormalizationNewTable normTable = (NormalizationNewTable) normAction; |
271 |
this.loadTable(normTable);
|
272 |
} |
273 |
|
274 |
return true; |
275 |
} |
276 |
|
277 |
/**
|
278 |
* This method up the selected element one position in the list of
|
279 |
* Elements
|
280 |
*
|
281 |
* @param pos
|
282 |
*/
|
283 |
public void fieldUp(int pos) { |
284 |
|
285 |
int nu = pattern.getElements().size();
|
286 |
|
287 |
if (pos > 0 && nu > 1) { |
288 |
int newpos = pos - 1; |
289 |
Element[] ad = pattern.getArrayElements(); |
290 |
Element ele21 = ad[pos];
|
291 |
Element ele12 = ad[newpos];
|
292 |
|
293 |
ad[newpos] = ele21; |
294 |
ad[pos] = ele12; |
295 |
List<Element> elems = new ArrayList<Element>(); |
296 |
for (int i = 0; i < ad.length; i++) { |
297 |
elems.add(ad[i]); |
298 |
} |
299 |
|
300 |
pattern.setElements(elems); |
301 |
} |
302 |
} |
303 |
|
304 |
/**
|
305 |
* This method down the selected element one position in the list of
|
306 |
* Elements
|
307 |
*
|
308 |
* @param pos
|
309 |
*/
|
310 |
public void fieldDown(int pos) { |
311 |
int nu = pattern.getElements().size();
|
312 |
|
313 |
if (pos != (nu - 1) && nu > 1) { |
314 |
int newpos = pos + 1; |
315 |
Element[] ad = pattern.getArrayElements(); |
316 |
Element ele21 = ad[pos];
|
317 |
Element ele12 = ad[newpos];
|
318 |
|
319 |
ad[newpos] = ele21; |
320 |
ad[pos] = ele12; |
321 |
List<Element> elems = new ArrayList<Element>(); |
322 |
for (int i = 0; i < ad.length; i++) { |
323 |
elems.add(ad[i]); |
324 |
} |
325 |
|
326 |
pattern.setElements(elems); |
327 |
} |
328 |
} |
329 |
|
330 |
/**
|
331 |
* This method adds a new element to the pattern
|
332 |
*/
|
333 |
@SuppressWarnings("unchecked") |
334 |
public void addField() { |
335 |
|
336 |
contador++; |
337 |
int tam = pattern.getElements().size();
|
338 |
Element elem = new Element(); |
339 |
|
340 |
Fieldseparator fsep = new Fieldseparator();
|
341 |
fsep.setSemicolonsep(true);
|
342 |
fsep.setJoinsep(false);
|
343 |
fsep.setColonsep(false);
|
344 |
fsep.setSpacesep(false);
|
345 |
fsep.setTabsep(false);
|
346 |
|
347 |
String nam = ""; |
348 |
boolean isOkName = true; |
349 |
do {
|
350 |
isOkName = true;
|
351 |
nam = "NewField" + contador;
|
352 |
for (int i = 0; i < tam; i++) { |
353 |
String napat = ((Element) pattern.getElements().get(i)) |
354 |
.getFieldname(); |
355 |
if (napat.compareToIgnoreCase(nam) == 0) { |
356 |
isOkName = false;
|
357 |
break;
|
358 |
} |
359 |
} |
360 |
if (!isOkName) {
|
361 |
contador++; |
362 |
} |
363 |
|
364 |
} while (!isOkName);
|
365 |
|
366 |
// validate the new field name
|
367 |
elem.setFieldname(validateFieldName(nam)); |
368 |
elem.setFieldseparator(fsep); |
369 |
elem.setInfieldseparators(getDefaultInfieldseparators()); |
370 |
elem.setFieldtype(getDefaultNewFieldType()); |
371 |
elem.setFieldwidth(0);
|
372 |
elem.setImportfield(true);
|
373 |
|
374 |
pattern.getElements().add(tam, elem); |
375 |
} |
376 |
|
377 |
/**
|
378 |
* This method erases a selected element to the list
|
379 |
*
|
380 |
* @param pos
|
381 |
* position
|
382 |
*/
|
383 |
public void deleteField(int pos) { |
384 |
int conta = pattern.getElements().size();
|
385 |
if (conta > 1) { |
386 |
pattern.getElements().remove(pos); |
387 |
} |
388 |
} |
389 |
|
390 |
/**
|
391 |
* This method saves a Normalization pattern to XML file *
|
392 |
*/
|
393 |
public void savePatternXML() { |
394 |
|
395 |
JFileChooser jfc = new JFileChooser(); |
396 |
jfc.setDialogTitle(PluginServices.getText(this, "save_norm_pattern")); |
397 |
String[] extensions = { "xml" }; |
398 |
jfc.setCurrentDirectory(new File(getFolderPattern())); |
399 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
400 |
PluginServices.getText(this, "pattern_norm_file"))); |
401 |
int returnval = jfc.showSaveDialog((Component) PluginServices |
402 |
.getMainFrame()); |
403 |
|
404 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
405 |
File thefile = jfc.getSelectedFile();
|
406 |
// Check if the file has extension
|
407 |
if (!(thefile.getPath().toLowerCase().endsWith(".xml"))) { |
408 |
thefile = new File(thefile.getPath() + ".xml"); |
409 |
} |
410 |
|
411 |
if (thefile.exists()) {
|
412 |
|
413 |
int n = JOptionPane.showConfirmDialog(null, PluginServices |
414 |
.getText(null, "file_exists"), PluginServices.getText( |
415 |
null, "save_norm_pattern"), JOptionPane.YES_NO_OPTION); |
416 |
if (n == JOptionPane.YES_OPTION) { |
417 |
writeSaveFile(thefile); |
418 |
log.debug("overwrite file");
|
419 |
} else if (n == JOptionPane.NO_OPTION) { |
420 |
log.debug("Don't save the file");
|
421 |
addInfo(INFO |
422 |
+ PluginServices.getText(null, "filenotoverwrite") |
423 |
+ " \n");
|
424 |
} else {
|
425 |
log.debug("Don't save the file");
|
426 |
addInfo(INFO + PluginServices.getText(null, "filenotsave") |
427 |
+ " \n");
|
428 |
} |
429 |
} else {
|
430 |
writeSaveFile(thefile); |
431 |
} |
432 |
} else {
|
433 |
addInfo(INFO + PluginServices.getText(null, "nosave") + " \n"); |
434 |
} |
435 |
} |
436 |
|
437 |
/**
|
438 |
* This method loads a Normalization pattern from a XML file and return the
|
439 |
* pattern and the String info
|
440 |
*
|
441 |
* @return pattern
|
442 |
*/
|
443 |
public Patternnormalization loadPatternXML() {
|
444 |
|
445 |
Patternnormalization pat = new Patternnormalization();
|
446 |
File thefile = null; |
447 |
StringBuffer infob = new StringBuffer(); |
448 |
|
449 |
// Show the FileChooser to select a pattern
|
450 |
JFileChooser jfc = new JFileChooser(); |
451 |
jfc.setCurrentDirectory(new File(getFolderPattern())); |
452 |
jfc.setDialogTitle(PluginServices.getText(this, "load_norm_pattern")); |
453 |
String[] extensions = { "xml" }; |
454 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
455 |
PluginServices.getText(this, "pattern_norm_file"))); |
456 |
|
457 |
int returnval = jfc.showOpenDialog((Component) PluginServices |
458 |
.getMainFrame()); |
459 |
|
460 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
461 |
thefile = jfc.getSelectedFile(); |
462 |
log.debug("file to open: " + thefile);
|
463 |
} else {
|
464 |
addInfo(INFO + PluginServices.getText(null, "noload") + " \n"); |
465 |
return null; |
466 |
} |
467 |
FileReader red;
|
468 |
IStorage storage; |
469 |
|
470 |
try {
|
471 |
red = new FileReader(thefile); |
472 |
storage = new StorageXML();
|
473 |
storage.load(red); |
474 |
pat.setstate(storage); |
475 |
addInfo(INFO + thefile.getName() + " "
|
476 |
+ PluginServices.getText(null, "loadsuccess") + " \n"); |
477 |
} catch (FileNotFoundException e) { |
478 |
pat = null;
|
479 |
addInfo(ERROR + PluginServices.getText(null, "filenotfound") |
480 |
+ " \n");
|
481 |
log.error("File not found", e);
|
482 |
} catch (Exception e) { |
483 |
pat = null;
|
484 |
addInfo(ERROR + PluginServices.getText(null, "parsingpatternxml") |
485 |
+ " \n");
|
486 |
log.error("ERROR Parsing xml", e);
|
487 |
} |
488 |
|
489 |
addInfo(infob.toString()); |
490 |
return pat;
|
491 |
} |
492 |
|
493 |
/* GETTERS Y SETTERS */
|
494 |
|
495 |
/**
|
496 |
* Return the name of the field to normalize
|
497 |
*
|
498 |
* @return field name
|
499 |
*/
|
500 |
public String getFieldToNormalize() { |
501 |
return fieldToNormalize;
|
502 |
} |
503 |
|
504 |
/**
|
505 |
* Set the name of the field to normalize
|
506 |
*
|
507 |
* @param fieldToNormalize
|
508 |
*/
|
509 |
public void setFieldToNormalize(String fieldToNormalize) { |
510 |
|
511 |
this.fieldToNormalize = fieldToNormalize;
|
512 |
|
513 |
/* Get samples to put in the GUI table */
|
514 |
getSamplesFromTable(); |
515 |
} |
516 |
|
517 |
/**
|
518 |
* This method returns the names of the fields from the pattern
|
519 |
*
|
520 |
* @return new fields names
|
521 |
*/
|
522 |
public String[] getNewFieldNames() { |
523 |
|
524 |
int numFields = pattern.getElements().size();
|
525 |
String[] res = new String[numFields]; |
526 |
|
527 |
for (int i = 0; i < numFields; i++) { |
528 |
res[i] = ((Element) pattern.getElements().get(i)).getFieldname();
|
529 |
} |
530 |
return res;
|
531 |
} |
532 |
|
533 |
/**
|
534 |
* Get the first or random samples from data source (FILE)
|
535 |
*/
|
536 |
public void getSamplesFromFile(int numNoRows) { |
537 |
|
538 |
int num = NormalizationPanel.SAMPLES;
|
539 |
String[] res = new String[num]; |
540 |
for (int i = 0; i < num; i++) { |
541 |
try {
|
542 |
res[i] = (String) fileChains.get(i + numNoRows);
|
543 |
} catch (Exception e) { |
544 |
res[i] = "";
|
545 |
} |
546 |
} |
547 |
this.samples = res;
|
548 |
} |
549 |
|
550 |
/**
|
551 |
* Return the samples Table
|
552 |
*
|
553 |
* @return samples from table
|
554 |
*/
|
555 |
public String[] getSamplesTable() { |
556 |
return samples;
|
557 |
} |
558 |
|
559 |
/**
|
560 |
* Return the samples File
|
561 |
*
|
562 |
* @return samples from file
|
563 |
*/
|
564 |
public String[] getSamplesFile() { |
565 |
return samples;
|
566 |
} |
567 |
|
568 |
/**
|
569 |
* This method sets the original table to the model
|
570 |
*
|
571 |
* @param _table
|
572 |
*/
|
573 |
public void setTable(Table _table) { |
574 |
tab = _table; |
575 |
} |
576 |
|
577 |
/**
|
578 |
* This method says if the process will be in a new table (true) or in the
|
579 |
* original table (false)
|
580 |
*
|
581 |
* @return
|
582 |
*/
|
583 |
public boolean isInNewTable() { |
584 |
return inNewTable;
|
585 |
} |
586 |
|
587 |
/**
|
588 |
* Assign value the flag
|
589 |
*
|
590 |
* @param inNewTable
|
591 |
*/
|
592 |
public void setInNewTable(boolean inNewTable) { |
593 |
this.inNewTable = inNewTable;
|
594 |
} |
595 |
|
596 |
/**
|
597 |
* This method returns a Element of one position
|
598 |
*
|
599 |
* @param index
|
600 |
* @return
|
601 |
*/
|
602 |
public Element getElement(int index) { |
603 |
Element[] adrs = pattern.getArrayElements(); |
604 |
return adrs[index];
|
605 |
} |
606 |
|
607 |
/**
|
608 |
* This method gets the position of the selected element in the List of
|
609 |
* names
|
610 |
*
|
611 |
* @return position in the list
|
612 |
*/
|
613 |
public int getSelectedFieldInList() { |
614 |
return selectedFieldInList;
|
615 |
} |
616 |
|
617 |
/**
|
618 |
* This method set the position of the selected element in the List
|
619 |
*
|
620 |
* @param selectedFieldInList
|
621 |
*/
|
622 |
public void setSelectedFieldInList(int selectedFieldInList) { |
623 |
this.selectedFieldInList = selectedFieldInList;
|
624 |
} |
625 |
|
626 |
/**
|
627 |
* This method returns the fields names of the original table
|
628 |
*
|
629 |
* @return list model
|
630 |
*/
|
631 |
public DefaultListModel getAllOriginalFields() { |
632 |
|
633 |
FieldDescription[] fields = tab.getModel().getModelo()
|
634 |
.getFieldsDescription(); |
635 |
int num = fields.length;
|
636 |
String names = ""; |
637 |
DefaultListModel dlm = new DefaultListModel(); |
638 |
dlm.add(0, PluginServices.getText(this, "none_field")); |
639 |
dlm.add(1, PluginServices.getText(this, "all_fields")); |
640 |
for (int i = 0; i < num; i++) { |
641 |
names = fields[i].getFieldName(); |
642 |
dlm.addElement(names); |
643 |
} |
644 |
return dlm;
|
645 |
} |
646 |
|
647 |
/**
|
648 |
* This method generates and returns field separators
|
649 |
*
|
650 |
* @return default separators between fields
|
651 |
*/
|
652 |
public Fieldseparator getDefaultFieldseparators() {
|
653 |
|
654 |
Fieldseparator filsep = new Fieldseparator();
|
655 |
filsep.setSemicolonsep(true);
|
656 |
filsep.setJoinsep(false);
|
657 |
filsep.setColonsep(false);
|
658 |
filsep.setSpacesep(false);
|
659 |
filsep.setTabsep(false);
|
660 |
|
661 |
return filsep;
|
662 |
} |
663 |
|
664 |
/**
|
665 |
* Set the names of the relate Fields
|
666 |
*
|
667 |
* @param list
|
668 |
* of the names
|
669 |
*/
|
670 |
|
671 |
public void setNameRelateFields(String[] names) { |
672 |
relateNames = names; |
673 |
} |
674 |
|
675 |
/**
|
676 |
* This method returns the fields names of the original table
|
677 |
*
|
678 |
* @return fields names of the main table
|
679 |
*/
|
680 |
public String[] getFieldNamesMainTable() { |
681 |
FieldDescription[] fds = tab.getModel().getModelo()
|
682 |
.getFieldsDescription(); |
683 |
String[] na = new String[fds.length]; |
684 |
for (int i = 0; i < fds.length; i++) { |
685 |
na[i] = fds[i].getFieldName(); |
686 |
} |
687 |
return na;
|
688 |
} |
689 |
|
690 |
/**
|
691 |
* This method sets the pattern
|
692 |
*
|
693 |
* @pat pattern
|
694 |
*/
|
695 |
public void setPattern(Patternnormalization pat) { |
696 |
pattern = pat; |
697 |
} |
698 |
|
699 |
/**
|
700 |
* This method returns the debug info
|
701 |
*
|
702 |
* @return info about the process
|
703 |
*/
|
704 |
public String getInfo() { |
705 |
return infob.toString();
|
706 |
} |
707 |
|
708 |
/**
|
709 |
* This method adds info about the process
|
710 |
*
|
711 |
* @param chain
|
712 |
* message
|
713 |
*/
|
714 |
public void addInfo(String chain) { |
715 |
infob.append(chain); |
716 |
} |
717 |
|
718 |
/**
|
719 |
* This method returns the File name to normalize
|
720 |
*
|
721 |
* @return file name
|
722 |
*/
|
723 |
public String getFileName() { |
724 |
return nameFile;
|
725 |
} |
726 |
|
727 |
/**
|
728 |
* set the number of first rows that not normalize
|
729 |
*
|
730 |
* @param first
|
731 |
* select the first row
|
732 |
*/
|
733 |
public void setFirstRows(int first) { |
734 |
pattern.setNofirstrows(first); |
735 |
|
736 |
} |
737 |
|
738 |
/**
|
739 |
* Set the table
|
740 |
*
|
741 |
* @param tab
|
742 |
* the tab to set
|
743 |
*/
|
744 |
public void setTab(Table tab) { |
745 |
this.tab = tab;
|
746 |
} |
747 |
|
748 |
/**
|
749 |
* Set the file strings
|
750 |
*
|
751 |
* @param fileChains
|
752 |
* the fileChains to set
|
753 |
*/
|
754 |
public void setFileChains(ArrayList<String> fileChains) { |
755 |
this.fileChains = fileChains;
|
756 |
this.isFile = true; |
757 |
getSamplesFromFile(getFirstRows()); |
758 |
} |
759 |
|
760 |
/**
|
761 |
* Get the file strings
|
762 |
*
|
763 |
* @return the file strings
|
764 |
*/
|
765 |
public ArrayList<String> getFileChains() { |
766 |
return fileChains;
|
767 |
} |
768 |
|
769 |
/**
|
770 |
* Get the file name
|
771 |
*
|
772 |
* @return the nameFile
|
773 |
*/
|
774 |
public String getNameFile() { |
775 |
return nameFile;
|
776 |
} |
777 |
|
778 |
/**
|
779 |
* Get the table
|
780 |
*
|
781 |
* @return the tab
|
782 |
*/
|
783 |
public Table getTab() {
|
784 |
return tab;
|
785 |
} |
786 |
|
787 |
/**
|
788 |
* Set the file name
|
789 |
*
|
790 |
* @param nameFile
|
791 |
* the nameFile to set
|
792 |
*/
|
793 |
public void setNameFile(String nameFile) { |
794 |
this.nameFile = nameFile;
|
795 |
} |
796 |
|
797 |
/**
|
798 |
* get Normalize the first row
|
799 |
*
|
800 |
* @return normalize first row
|
801 |
*/
|
802 |
public int getFirstRows() { |
803 |
return pattern.getNofirstrows();
|
804 |
|
805 |
} |
806 |
|
807 |
/**
|
808 |
* Set the file type
|
809 |
*
|
810 |
* @param file
|
811 |
*/
|
812 |
public void isFile(boolean file) { |
813 |
this.isFile = file;
|
814 |
} |
815 |
|
816 |
/**
|
817 |
* This method cleans the information about the process
|
818 |
*/
|
819 |
public void clearConsole() { |
820 |
int con = infob.length() > 0 ? infob.length() - 1 : 0; |
821 |
if (con > 0) { |
822 |
infob.delete(0, con);
|
823 |
} |
824 |
} |
825 |
|
826 |
/**
|
827 |
* This method creates the default Normalization pattern
|
828 |
*/
|
829 |
public void initPattern() { |
830 |
|
831 |
Patternnormalization pat = new Patternnormalization();
|
832 |
|
833 |
pat.setPatternname("defaultPattern");
|
834 |
pat.setPatternurl("\\");
|
835 |
pat.setNofirstrows(0);
|
836 |
|
837 |
/* Create the first Address Element */
|
838 |
Element elem = new Element(); |
839 |
|
840 |
elem.setFieldname("NewField");
|
841 |
elem.setFieldseparator(getDefaultFieldseparators()); |
842 |
elem.setInfieldseparators(getDefaultInfieldseparators()); |
843 |
elem.setFieldtype(getDefaultNewFieldType()); |
844 |
elem.setFieldwidth(0);
|
845 |
elem.setImportfield(true);
|
846 |
|
847 |
List<Element> elems = new ArrayList<Element>(); |
848 |
elems.add(elem); |
849 |
|
850 |
pat.setElements(elems); |
851 |
|
852 |
this.pattern = pat;
|
853 |
} |
854 |
|
855 |
/* METHODS PRIVATES */
|
856 |
|
857 |
/**
|
858 |
* This method loads the new table
|
859 |
*
|
860 |
* @param normTable
|
861 |
*/
|
862 |
private void loadTable(NormalizationNewTable normTable) { |
863 |
String name = normTable.getOuputFile().getName();
|
864 |
|
865 |
LayerFactory.getDataSourceFactory().addDataSource( |
866 |
normTable.getDriver(), name); |
867 |
|
868 |
EditableAdapter editAdapterSecond = null;
|
869 |
|
870 |
DataSource dtSecond;
|
871 |
try {
|
872 |
dtSecond = LayerFactory.getDataSourceFactory() |
873 |
.createRandomDataSource(name, |
874 |
DataSourceFactory.AUTOMATIC_OPENING); |
875 |
|
876 |
editAdapterSecond = new EditableAdapter();
|
877 |
editAdapterSecond.setOriginalDataSource(new SelectableDataSource(
|
878 |
dtSecond)); |
879 |
|
880 |
// add to doc factory
|
881 |
ProjectTable ptsec = ProjectFactory.createTable(name, |
882 |
editAdapterSecond); |
883 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton |
884 |
.getInstance(); |
885 |
|
886 |
extensionPoints.get("Documents");
|
887 |
|
888 |
ProjectDocumentFactory documentFactory = null;
|
889 |
|
890 |
documentFactory = new ProjectTableFactory();
|
891 |
|
892 |
ptsec.setProjectDocumentFactory(documentFactory); |
893 |
Table secondTable = new Table();
|
894 |
secondTable.setModel(ptsec); |
895 |
PluginServices.getMDIManager().addWindow(secondTable); |
896 |
ProjectExtension pe = (ProjectExtension) PluginServices |
897 |
.getExtension(ProjectExtension.class); |
898 |
Project theProject = pe.getProject(); |
899 |
theProject.addDocument(ptsec); |
900 |
pe.getProjectFrame().refreshControls(); |
901 |
} catch (Exception e) { |
902 |
log.error("Error loading the new table", e);
|
903 |
} |
904 |
} |
905 |
|
906 |
/**
|
907 |
* This method validates the name of a new field
|
908 |
*
|
909 |
* @param text
|
910 |
* @return field name formatted
|
911 |
*/
|
912 |
private String validateFieldName(String text) { |
913 |
|
914 |
String temp = Normalizer.normalize(text, Normalizer.DECOMP, 0); |
915 |
temp = temp.replaceAll("[^\\p{ASCII}]", ""); |
916 |
temp = temp.replaceAll("[\\s]+", "_"); |
917 |
temp = temp.toUpperCase(); |
918 |
|
919 |
return temp;
|
920 |
} |
921 |
|
922 |
/**
|
923 |
* Get the first or random samples from data source (TABLE)
|
924 |
*/
|
925 |
private void getSamplesFromTable() { |
926 |
int num = NormalizationPanel.SAMPLES;
|
927 |
String[] res = new String[num]; |
928 |
|
929 |
ProjectTable pt = tab.getModel(); |
930 |
IEditableSource ies = pt.getModelo(); |
931 |
try {
|
932 |
|
933 |
long tamTab = ies.getRowCount();
|
934 |
if (tamTab > num)
|
935 |
tamTab = num; |
936 |
|
937 |
for (int i = 0; i < (int) tamTab; i++) { |
938 |
IRowEdited rowVals = ies.getRow(i); |
939 |
|
940 |
res[i] = rowVals.getAttribute( |
941 |
tab.getSelectedFieldIndices().nextSetBit(0)).toString()
|
942 |
.trim(); |
943 |
} |
944 |
} catch (ReadDriverException e) {
|
945 |
log.error("ERROR al obtener el recorset de la tabla");
|
946 |
e.printStackTrace(); |
947 |
} |
948 |
|
949 |
this.samples = res;
|
950 |
} |
951 |
|
952 |
/**
|
953 |
* This method returns the dbf file
|
954 |
*
|
955 |
* @return dbf file
|
956 |
*/
|
957 |
private File getDBFFile() { |
958 |
File thefile = null; |
959 |
JFileChooser jfc = new JFileChooser(); |
960 |
jfc.setAcceptAllFileFilterUsed(false);
|
961 |
jfc.addChoosableFileFilter(new GenericFileFilter("dbf", PluginServices |
962 |
.getText(this, "gdbms dbf drver"))); |
963 |
jfc.setDialogTitle(PluginServices.getText(this, "new_table")); |
964 |
int returnval = jfc.showSaveDialog((Component) PluginServices |
965 |
.getMainFrame()); |
966 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
967 |
thefile = jfc.getSelectedFile(); |
968 |
if (!(thefile.getPath().toLowerCase().endsWith(".dbf"))) { |
969 |
thefile = new File(thefile.getPath() + ".dbf"); |
970 |
} |
971 |
} |
972 |
return thefile;
|
973 |
} |
974 |
|
975 |
/**
|
976 |
* This method generates and returns in field separators
|
977 |
*
|
978 |
* @return special characters within one string
|
979 |
*/
|
980 |
|
981 |
private Infieldseparators getDefaultInfieldseparators() {
|
982 |
|
983 |
/* create the default in-field separators */
|
984 |
Locale loc = Locale.getDefault(); |
985 |
DecimalFormatSymbols dfs = new DecimalFormatSymbols(loc); |
986 |
Infieldseparators infilsep = new Infieldseparators();
|
987 |
infilsep.setThousandseparator(Character.toString(dfs
|
988 |
.getGroupingSeparator())); |
989 |
infilsep.setDecimalseparator(Character.toString(dfs
|
990 |
.getDecimalSeparator())); |
991 |
infilsep.setTextseparator("\"");
|
992 |
|
993 |
return infilsep;
|
994 |
} |
995 |
|
996 |
/**
|
997 |
* This method generates and returns a new field type of type Stringvalue
|
998 |
*
|
999 |
* @return field type
|
1000 |
*/
|
1001 |
private Fieldtype getDefaultNewFieldType() {
|
1002 |
|
1003 |
Fieldtype newtype = new Fieldtype();
|
1004 |
Stringvalue strval = new Stringvalue();
|
1005 |
strval.setStringvaluewidth(50);
|
1006 |
newtype.setStringvalue(strval); |
1007 |
|
1008 |
return newtype;
|
1009 |
} |
1010 |
|
1011 |
/**
|
1012 |
* This method write the save xml file
|
1013 |
*
|
1014 |
* @param file
|
1015 |
*/
|
1016 |
private void writeSaveFile(File thefile) { |
1017 |
|
1018 |
StringBuffer infob = new StringBuffer(); |
1019 |
|
1020 |
/* insert the name and the path */
|
1021 |
pattern.setPatternname(thefile.getName()); |
1022 |
pattern.setPatternurl(thefile.getAbsolutePath()); |
1023 |
|
1024 |
FileWriter writer;
|
1025 |
IStorage storage; |
1026 |
try {
|
1027 |
storage = new StorageXML();
|
1028 |
pattern.getstate(storage); |
1029 |
|
1030 |
writer = new FileWriter(thefile); |
1031 |
// Parser the pattern
|
1032 |
storage.dump(writer); |
1033 |
infob.append(INFO + thefile.getName() + " "
|
1034 |
+ PluginServices.getText(null, "savesuccess") + " \n"); |
1035 |
writer.close(); |
1036 |
} catch (Exception e) { |
1037 |
log.error("Error al parsear el patron", e);
|
1038 |
infob.append(ERROR + PluginServices.getText(null, "savingpat") |
1039 |
+ " \n");
|
1040 |
} |
1041 |
|
1042 |
addInfo(infob.toString()); |
1043 |
} |
1044 |
|
1045 |
|
1046 |
/**
|
1047 |
* This method return the folder where gvSIG stores the patterns
|
1048 |
* @return
|
1049 |
*/
|
1050 |
private String getFolderPattern() { |
1051 |
XMLEntity xml = PluginServices.getPluginServices(this)
|
1052 |
.getPersistentXML(); |
1053 |
String pathFolder = String.valueOf(xml |
1054 |
.getStringProperty("Normalization_pattern_folder"));
|
1055 |
return pathFolder;
|
1056 |
} |
1057 |
|
1058 |
} |