示例#1
0
 @Override
 public void actionPerformed(ActionEvent e) {
   JFileChooser fc = new JFileChooser();
   fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
   FileFilter ff = new FileNameExtensionFilter(I18n.get("CSV FILE"), "csv");
   fc.setFileFilter(ff);
   if (currentDirectory != null) {
     fc.setCurrentDirectory(currentDirectory);
   }
   fc.setSelectedFile(new File(name + ".csv"));
   if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
     File file = fc.getSelectedFile();
     if (file != null) {
       if (file.getName().indexOf('.') == -1) {
         file = new File(file.getPath() + ".csv");
       }
       currentDirectory = file.getParentFile();
       if (file.exists()) {
         int confirm =
             JOptionPane.showConfirmDialog(
                 null, file, I18n.get("FILE EXISTS! OVERWRITE?"), JOptionPane.OK_CANCEL_OPTION);
         if (JOptionPane.YES_OPTION != confirm) {
           return;
         }
       }
       try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
         for (int col = 0; col < model.getColumnCount(); col++) {
           if (col > 0) {
             pw.print(',');
           }
           pw.print('"');
           pw.print(model.getColumnName(col));
           pw.print('"');
         }
         pw.println();
         for (int row = 0; row < model.getRowCount(); row++) {
           for (int col = 0; col < model.getColumnCount(); col++) {
             if (col > 0) {
               pw.print(',');
             }
             Object valueAt = model.getValueAt(row, col);
             pw.print('"');
             if (valueAt != null) {
               pw.print(GObjectHelper.getString(valueAt));
             }
             pw.print('"');
           }
           pw.println();
         }
       } catch (IOException ex) {
         JOptionPane.showMessageDialog(null, ex.getLocalizedMessage());
       }
     }
   }
 }
示例#2
0
 @Override
 public void propertyChange(PropertyChangeEvent evt) {
   if (FetchResultHandler.ModelPropertyName.equals(evt.getPropertyName())) {
     model = (FetchResultTableModel) evt.getNewValue();
     if (model != null) {
       if (model.getRowCount() > 0) {
         setEnabled(true);
       } else {
         setEnabled(false);
       }
     } else {
       setEnabled(false);
     }
   }
 }