appgvSIG-Added-new-feature-to-filter-dialog-Export-selected-t.patch

Pablo Sanxiao, 02/16/2012 10:53 AM

Download (6.18 KB)

View differences:

src/com/iver/cit/gvsig/FiltroExtension.java
41 41
package com.iver.cit.gvsig;
42 42

  
43 43
import java.awt.Component;
44
import java.io.File;
45
import java.io.FileOutputStream;
44 46
import java.io.IOException;
47
import java.io.PrintStream;
45 48

  
49
import javax.swing.JFileChooser;
46 50
import javax.swing.JOptionPane;
51
import javax.swing.filechooser.FileNameExtensionFilter;
47 52

  
48 53
import com.hardcode.driverManager.DriverLoadException;
49 54
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
......
377 382
		else
378 383
			return false;
379 384
	}
380
}
385

  
386
	@Override
387
	public void exportSet(String expression) {
388
	    File file = null;
389
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
390
	    PluginServices.getText(this, "CSV_file"), "csv");
391
	    newSet(expression);
392
	     
393
	    JFileChooser fc = new JFileChooser();
394
	    fc.setFileFilter(filter);
395
	    int returnVal = fc.showSaveDialog(vista);
396

  
397
	    do {
398
		if (returnVal == JFileChooser.CANCEL_OPTION) {
399
		    break;
400
	    	} else {
401
	    	    file = fc.getSelectedFile();
402
	    	    if (!file.getName().toLowerCase().endsWith(".csv")) {
403
	    		file = new File(file.getAbsolutePath() + ".csv");
404
	    	    }
405
	    	    if (file.exists()) {
406
	    		int overwriteFile = JOptionPane.showConfirmDialog(null, PluginServices.getText(
407
	    			this, "file_already_exists"),
408
	    			PluginServices.getText(this, "warning"),
409
	    			JOptionPane.YES_NO_OPTION);
410
	    		 if (overwriteFile == JOptionPane.NO_OPTION) {
411
	    		     continue;
412
	    		 }
413
	    	    }
414

  
415
	    	    if (writeSelectionToFile(getCSVContentsFromSelectedRecords(), file)) {
416
	    		NotificationManager.showMessageError("error_saving_file", null);
417
	    	    }
418
	    	}
419
	    
420
	    } while (file == null);
421
	 }
422
	    
423
	 private boolean writeSelectionToFile(String contents, File file) {
424
	     FileOutputStream fos = null;
425
	     PrintStream ps = null;
426
	     boolean error = false;
427
	     try {
428
		 fos = new FileOutputStream(file);
429
	    	 ps = new PrintStream(fos);
430
	    	 ps.println(contents);
431
	     } catch (IOException e1) {
432
	    	 error = true;
433
	    	 e1.printStackTrace();
434
	     } finally {
435
	    	 if (ps != null) {
436
	    	     ps.close();
437
	    	 }
438
	    	 if (fos != null) {
439
	    	     try {
440
	    		 fos.close();
441
	    	     } catch (IOException e) {
442
	    		 error = true;
443
	    	     }
444
	    	 }
445
	     }
446
	    	return error;
447
	    } 
448

  
449
	 private String getCSVContentsFromSelectedRecords() {
450
	     String contents = "";
451
	     try {
452
		 String[] fieldNames = dataSource.getFieldNames();
453
		 for (int i = 0; i < fieldNames.length; i++) {
454
		     contents = contents + fieldNames[i] + ";";
455
	    	 }
456
	    	 contents = contents + "\n";
457
	    	 for (int i = 0; i < dataSource.getRowCount(); i++) {
458
	    	     if (dataSource.isSelected(i)) {
459
	    		 for (int j = 0; j < dataSource.getFieldCount(); j++) {
460
	    		     contents = contents + dataSource.getFieldValue(i, j) + ";";
461
	    		 }
462
	    		 contents = contents + "\n";
463
	    	     }
464
	    	 }
465
	    	} catch (ReadDriverException e1) {
466
	    	    // TODO Auto-generated catch block
467
	    	    e1.printStackTrace();
468
	     	}
469
	    	return contents;
470
	 }
471
}	
src/com/iver/cit/gvsig/TableOperations.java
518 518
		return fieldName.replaceAll("\\W", "_"); // replace any non-word character by an underscore
519 519
	}
520 520

  
521
	@Override
522
	public void exportSet(String expression) {
523
	}
521 524
}
src/com/iver/cit/gvsig/gui/filter/ExpressionListener.java
44 44
	public void newSet(String expression);
45 45
	public void addToSet(String expression);
46 46
	public void fromSet(String expression);
47
	public void exportSet(String expression);
47 48
}
src/com/iver/cit/gvsig/gui/filter/FilterDialog.java
196 196
				});
197 197
			}
198 198
		});
199
		
200
		// Listener for "btnExportSet"
201
		// Export selected elements in the table to csv file that are according
202
		// the current filter
203
		// condition
204
		getBtnExportSet().addActionListener(new java.awt.event.ActionListener() {
205
			public void actionPerformed(java.awt.event.ActionEvent e) {
206
				final String expr = "select * from '" + 
207
				model.getDataSourceName() + "' where " + 
208
				getTxtExpression().getText() + ";";
209
		
210
				logger.debug(expr);
211
			
212
				PluginServices.backgroundExecution(new Runnable() {
213
					public void run() {
214
						for (int i = 0; i < expressionListeners.size(); i++) {
215
							ExpressionListener l = (ExpressionListener) expressionListeners.get(i);
216
							l.exportSet(expr);
217
						}
218
					}
219
				});
220
			}
221
		});
199 222

  
200 223
		// Listener for "fieldsJTree"
201 224
		getFieldsJTree().addMouseListener(new MouseAdapter() {
202
-