ok, ti rispondo per punti.
per quanto riguarda la gestione delle azioni, ok.
adesso raggruppo in metodi privati come nel tuo esempio.
per quanto riguarda le eccezioni, potre anche farle gestire ai metodi privati direttamente, invece che nell'actionPerformed.
poi magari, messo a posto il resto, sistemo anche la gestione delle eccezioni con una customizzata.
per quanto riguarda il FileChooser:
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);
}
}
}
sicuramente lo devo mettere a posto.
il problema è che all'epoca avevo fretta e non ne uscivo fuori!