Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Consiglio desgin FileChooser

    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??

  2. #2
    ho già fatto qualche modifica.
    vi metto qui quello nuovo:
    codice:
    public class FileChooser {
    
        private ArrayList<ArrayList<String>> map = null;
        private JTable table = null;
    
        public FileChooser(JTable table, ArrayList<ArrayList<String>> map) {
            this.map = map;
            this.table = table;
        }
    
        public void salva(String tipo) 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) {
                File 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;
                    default:
                        break;
                }
            }
        }
    }

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    come punto di partenza potrebbe andare?
    Ehm .. non proprio ....
    Innanzitutto è comunque un gran bel "mix" di cose, è legata a JTable ed ha conoscenza di tutte le classi di export. E inoltre deve comunque prendere in considerazione, almeno a livello dichiarativo, tutte le eccezioni relative agli export.

    E se si guarda alla logica del file browsing, il tuo messaggio che il file esiste e si chiede di sovrascriverlo, non è di per sé sbagliato. Ma compare solo dopo che la dialog del JFileChooser si è chiusa. Mentre invece sarebbe bello se l'avvertimento comparisse quando il file chooser è ancora visibile.
    Questo è fattibile, si può fare ma bisogna estendere JFileChooser. Avevo fatto un esempio qui

    E se vuoi forzare il controllo della estensione basandosi sul filter selezionato, si può anche fare (sempre prima che la dialog si chiuda).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  4. #4
    ok, vediamo se ho capito meglio:
    -creo un filechooser che in pratica fa solo quello, impostando tutto il necessario
    -allo scatensarsi dell'evento, lancio il filechooser
    -al momento in l'utente sceglie il nome del file (scegliendo tra le estensioni supportate) il filechooser ritorna il file
    -nel jframe riprendo il file, controllo l'estensione e di conseguenza lancio l'export appropriato

    se corretto, ho pensato di prendere l'esempio che mi hai indicato, ed aggiungerci i filtri per le estensioni supportate e i set/get per impostare e ritornare il percorso del file da salvare.
    codice:
    public class FileChooserExtension extends JFileChooser {
    
        private static final String DEFAULT_CONFIRM_TITLE = "Conferma";
        private static final String DEFAULT_CONFIRM_MESSAGE = "Il file ''{0}'' già esiste.\nVuoi sovrascriverlo??";
        private String confirmTitle;
        private String confirmMessage;
        private File finalFile = null;
    
        public FileChooserExtension() {
            this(DEFAULT_CONFIRM_TITLE, DEFAULT_CONFIRM_MESSAGE);
        }
    
        public FileChooserExtension(String confirmTitle, String confirmMessage) {
            this.confirmTitle = confirmTitle;
            this.confirmMessage = confirmMessage;
            setDialogTitle("Salva");
            setApproveButtonText("Salva");
            setApproveButtonToolTipText("Salva");
            String[] extensions = {"pdf", "txt", "xls", "xml"};
            for (String extension : extensions) {
                FileFilter ff = new FileNameExtensionFilter("File *." + extension, extension);
                addChoosableFileFilter(ff);
            }
        }
    
        @Override
        public void approveSelection() {
            if (getDialogType() == SAVE_DIALOG) {
                File f = getSelectedFile();
                if (f.exists()) {
                    String msg = MessageFormat.format(confirmMessage, new Object[]{f});
                    int r = JOptionPane.showConfirmDialog(this, msg, confirmTitle, JOptionPane.YES_NO_OPTION);
                    if (r == JOptionPane.NO_OPTION) {
                        return;
                    }
                }
                String ext = ((FileNameExtensionFilter) getFileFilter()).getExtensions()[0];
                setFinalFile(f.getAbsolutePath() + "." + ext);
            }
            super.approveSelection();
        }
    
        public void setFinalFile(String f) {
            finalFile = new File(f);
        }
    
        public File getFinalFile() {
            return finalFile;
        }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.