ciao!
su consiglio di andbin, sto cercando di sistemare questo file chooser:
codice:
public class FileChooser {
private static File f = null;
public void salvaPdf(ArrayList<ArrayList<String>> map) throws FileNotFoundException, DocumentException {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Save PDF");
fc.setApproveButtonText("Save");
fc.setApproveButtonToolTipText("Approve file");
FileFilter pdfFilter = new GenericFileFilter("File *.pdf", "pdf");
//fc.addChoosableFileFilter(pdfFilter);
fc.setFileFilter(pdfFilter);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (f.getName().indexOf('.') == -1) {
if (selectedFilter == pdfFilter) {
f = new File(f.getPath() + ".pdf");
} else {
f = new File(f.getPath() + ".pdf");
}
}
if (f.exists()) {
String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{f});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
ExportPdf pdf = new ExportPdf();
pdf.createPdf(map, f.toString());
}
}
public void salvaXls(JTable table) throws IOException, WriteException {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Save XLS");
fc.setApproveButtonText("Save");
fc.setApproveButtonToolTipText("Approve file");
FileFilter xlsFilter = new GenericFileFilter("File *.xls", "xls");
fc.setFileFilter(xlsFilter);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (f.getName().indexOf('.') == -1) {
if (selectedFilter == xlsFilter) {
f = new File(f.getPath() + ".xls");
} else {
f = new File(f.getPath() + ".xls");
}
}
if (f.exists()) {
String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{f});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
ExportXls test = new ExportXls();
test.create(table, f.toString());
}
}
public void salvaTxt(JTable table) throws IOException, WriteException {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Save TXT");
fc.setApproveButtonText("Save");
fc.setApproveButtonToolTipText("Approve file");
FileFilter txtFilter = new GenericFileFilter("File *.txt", "txt");
//fc.addChoosableFileFilter(txtFilter);
fc.setFileFilter(txtFilter);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (f.getName().indexOf('.') == -1) {
if (selectedFilter == txtFilter) {
f = new File(f.getPath() + ".txt");
} else {
f = new File(f.getPath() + ".txt");
}
}
if (f.exists()) {
String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{f});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
ExportTxt txt = new ExportTxt();
txt.salva(table, f);
}
}
public void salvaXml(JTable table) throws JAXBException {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Save XML");
fc.setApproveButtonText("Save");
fc.setApproveButtonToolTipText("Approve file");
FileFilter xlsFilter = new GenericFileFilter("File *.xml", "xml");
fc.setFileFilter(xlsFilter);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (f.getName().indexOf('.') == -1) {
if (selectedFilter == xlsFilter) {
f = new File(f.getPath() + ".xml");
} else {
f = new File(f.getPath() + ".xml");
}
}
if (f.exists()) {
String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{f});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
ExportXml ex = new ExportXml();
ex.createXml(table, f);
}
}
}
in pratica dalla mia applicazione, devo poter salvare in 4 formati differenti.
ogni formato ha la sua classe di export:
- pdf
-xls
-txt
-xml
i dati li prendo o dalla JTable o da un ArrayList<ArrayList<String>>.
facendo uno step alla volta, per cercare di raggruppare il tutto ho fato un file chooser con un unico metodo salva.
al costruttore passo la JTable, l'ArrayList<ArrayList<String>> e un "flag" con cui indico il formato in cui salvare.
ecco qua:
codice:
public class FileChooser1 {
private File f = null;
private ArrayList<ArrayList<String>> map = null;
private JTable table = null;
private String tipo;
public FileChooser1(JTable table, ArrayList<ArrayList<String>> map, String tipo) {
this.map = map;
this.table = table;
this.tipo = tipo;
}
public void salva() throws IOException, JAXBException, WriteException, FileNotFoundException, DocumentException {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Salva " + tipo.toUpperCase());
fc.setApproveButtonText("Salva");
fc.setApproveButtonToolTipText("Salva");
FileFilter ff = new GenericFileFilter("File *." + tipo, tipo);
fc.setFileFilter(ff);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (f.getName().indexOf('.') == -1) {
if (selectedFilter == ff) {
f = new File(f.getPath() + "." + tipo);
} else {
f = new File(f.getPath() + "." + tipo);
}
}
if (f.exists()) {
String msg = MessageFormat.format("Il file ''{0}'' già esiste.\nVuoi sovrascriverlo?", new Object[]{f});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
switch (tipo) {
case "pdf":
ExportPdf pdf = new ExportPdf();
pdf.createPdf(map, f.toString());
break;
case "xls":
ExportXls xls = new ExportXls();
xls.create(table, f.toString());
break;
case "txt":
ExportTxt txt = new ExportTxt();
txt.salva(table, f);
break;
case "xml":
ExportXml ex = new ExportXml();
ex.createXml(table, f);
break;
}
}
}
}
come punto di partenza potrebbe andare?
altri consigli??