Example usage for javax.swing.filechooser FileFilter FileFilter

List of usage examples for javax.swing.filechooser FileFilter FileFilter

Introduction

In this page you can find the example usage for javax.swing.filechooser FileFilter FileFilter.

Prototype

FileFilter

Source Link

Usage

From source file:org.martin.ftp.gui.GUIClient.java

private FileFilter getAllFileFilter() {
    return new FileFilter() {

        @Override/*from  www.ja v  a2s . co  m*/
        public boolean accept(File f) {
            return true;
        }

        @Override
        public String getDescription() {
            return "Escoja el archivo";
        }
    };
}

From source file:org.n52.ifgicopter.spf.input.GpxInputPlugin.java

/**
 * // w  w  w . ja v  a2s.  c om
 */
protected void selectGpxFileAction() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFileChooser fc = new JFileChooser();
            fc.setDialogTitle("Select GPX File");
            fc.setFileFilter(new FileFilter() {

                @Override
                public boolean accept(File f) {
                    if (f.getName().toLowerCase().endsWith("gpx") || f.getName().toLowerCase().endsWith("xml"))
                        return true;
                    if (f.isDirectory())
                        return true;

                    return false;
                }

                @Override
                public String getDescription() {
                    return "GPX file";
                }
            });
            File gpxfile = new File(getGpxFilePath());
            if (gpxfile.isFile()) {
                fc.setCurrentDirectory(gpxfile.getParentFile());
            }

            int returnVal = fc.showOpenDialog(getUserInterface().getGui());

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                setGpxFilePath(file.getAbsolutePath());
            } else {
                getLog().debug("Open command cancelled by user.");
            }

            fc = null;
        }
    });
}

From source file:org.n52.ifgicopter.spf.output.GpxOutputPlugin.java

/**
 * //from   w  w  w  .j  a v  a 2  s .  c o  m
 */
protected void selectGpxFileAction() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFileChooser fc = new JFileChooser();
            fc.setDialogTitle("Select GPX File");
            fc.setFileFilter(new FileFilter() {

                @Override
                public boolean accept(File f) {
                    if (f.getName().toLowerCase().endsWith("gpx") || f.getName().toLowerCase().endsWith("xml"))
                        return true;
                    if (f.isDirectory())
                        return true;

                    return false;
                }

                @Override
                public String getDescription() {
                    return "GPX file";
                }
            });

            File gpxfile = new File(getGpxFilePath());
            if (gpxfile.isFile()) {
                fc.setCurrentDirectory(gpxfile.getParentFile());
            }

            int returnVal = fc.showOpenDialog(getUserInterface().getGui());

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                if (!file.getName().endsWith(".gpx") || !file.getName().endsWith(".xml"))
                    setGpxFilePath(file.getAbsolutePath() + ".gpx");
                else
                    setGpxFilePath(file.getAbsolutePath());
            } else {
                log.debug("Open command cancelled by user.");
            }

            fc = null;
        }
    });
}

From source file:org.parosproxy.paros.control.MenuFileControl.java

private void openFileBasedSession() {
    JFileChooser chooser = new JFileChooser(model.getOptionsParam().getUserDirectory());
    chooser.setFileHidingEnabled(false); // By default ZAP on linux puts timestamped sessions under a 'dot' directory 
    File file = null;/*  ww  w .j av  a  2  s  .com*/
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".session")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            // ZAP: Rebrand
            return Constant.messages.getString("file.format.zap.session");
        }
    });
    int rc = chooser.showOpenDialog(view.getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            if (file == null) {
                return;
            }
            model.getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
            log.info("opening session file " + file.getAbsolutePath());
            waitMessageDialog = view.getWaitMessageDialog(Constant.messages.getString("menu.file.loadSession"));
            control.openSession(file, this);
            waitMessageDialog.setVisible(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.control.MenuFileControl.java

public void saveAsSession() {
    if (!informStopActiveActions()) {
        return;/*from  w  w w.  ja  va 2s .c o  m*/
    }

    Session session = model.getSession();

    JFileChooser chooser = new JFileChooser(model.getOptionsParam().getUserDirectory());
    // ZAP: set session name as file name proposal
    File fileproposal = new File(session.getSessionName());
    if (session.getFileName() != null && session.getFileName().trim().length() > 0) {
        // if there is already a file name, use it
        fileproposal = new File(session.getFileName());
    }
    chooser.setSelectedFile(fileproposal);
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".session")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            // ZAP: Rebrand
            return Constant.messages.getString("file.format.zap.session");
        }
    });
    File file = null;
    int rc = chooser.showSaveDialog(view.getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }
        model.getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileName = file.getAbsolutePath();
        if (!fileName.endsWith(".session")) {
            fileName += ".session";
        }

        try {
            waitMessageDialog = view
                    .getWaitMessageDialog(Constant.messages.getString("menu.file.savingSession")); // ZAP: i18n
            control.saveSession(fileName, this);
            log.info("save as session file " + session.getFileName());
            waitMessageDialog.setVisible(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.control.MenuFileControl.java

public void saveSnapshot() {
    String activeActions = wrapEntriesInLiTags(control.getExtensionLoader().getActiveActions());
    if (!activeActions.isEmpty()) {
        view.showMessageDialog(Constant.messages.getString("menu.file.snapshot.activeactions", activeActions));
        return;/* w  ww . ja  va  2s . co m*/
    }

    Session session = model.getSession();

    JFileChooser chooser = new JFileChooser(model.getOptionsParam().getUserDirectory());
    // ZAP: set session name as file name proposal
    File fileproposal = new File(session.getSessionName());
    if (session.getFileName() != null && session.getFileName().trim().length() > 0) {
        String proposedFileName;
        // if there is already a file name, use it and add a timestamp
        proposedFileName = StringUtils.removeEnd(session.getFileName(), ".session");
        proposedFileName += "-" + dateFormat.format(new Date()) + ".session";
        fileproposal = new File(proposedFileName);
    }
    chooser.setSelectedFile(fileproposal);
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".session")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("file.format.zap.session");
        }
    });
    File file = null;
    int rc = chooser.showSaveDialog(view.getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }
        model.getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileName = file.getAbsolutePath();
        if (!fileName.endsWith(".session")) {
            fileName += ".session";
        }

        try {
            waitMessageDialog = view
                    .getWaitMessageDialog(Constant.messages.getString("menu.file.savingSnapshot")); // ZAP: i18n
            control.snapshotSession(fileName, this);
            log.info("Snapshotting: " + session.getFileName() + " as " + fileName);
            waitMessageDialog.setVisible(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.control.MenuFileControl.java

/**
 * Prompt the user to export a context/*from www . jav a 2s. c om*/
 */
public void importContext() {
    JFileChooser chooser = new JFileChooser(Constant.getContextsDir());
    File file = null;
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".context")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("file.format.zap.context");
        }
    });

    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            if (file == null || !file.exists()) {
                return;
            }
            // Import the context
            Model.getSingleton().getSession().importContext(file);

            // Show the dialog
            View.getSingleton().showSessionDialog(Model.getSingleton().getSession(),
                    Constant.messages.getString("context.list"), true);

        } catch (IllegalContextNameException e) {
            String detailError;
            if (e.getReason() == IllegalContextNameException.Reason.EMPTY_NAME) {
                detailError = Constant.messages.getString("context.error.name.empty");
            } else if (e.getReason() == IllegalContextNameException.Reason.DUPLICATED_NAME) {
                detailError = Constant.messages.getString("context.error.name.duplicated");
            } else {
                detailError = Constant.messages.getString("context.error.name.unknown");
            }
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("context.import.error", detailError));
        } catch (Exception e1) {
            log.debug(e1.getMessage(), e1);
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("context.import.error", e1.getMessage()));
        }
    }
}

From source file:org.parosproxy.paros.extension.history.PopupMenuExportMessage.java

private File getOutputFile() {

    JFileChooser chooser = new JFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
    chooser.setFileFilter(new FileFilter() {
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".txt")) {
                return true;
            }/*from   w w  w. ja v a  2 s. co  m*/
            return false;
        }

        public String getDescription() {
            return "ASCII text file";
        }
    });
    File file = null;
    int rc = chooser.showSaveDialog(extension.getView().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return file;
        }
        extension.getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileName = file.getAbsolutePath();
        if (!fileName.endsWith(".txt")) {
            fileName += ".txt";
            file = new File(fileName);
        }
        return file;

    }
    return file;
}

From source file:org.rdv.viz.image.HighResImageViz.java

/**
 * If an image is currently being displayed, save it to a file. This will
 * bring up a file chooser to select the file.
 * //from  w ww .j  ava  2 s. c  o  m
 * @param directory the directory to start the file chooser in
 */
private void saveImage(File directory) {
    if (displayedImageData != null) {
        // create default file name
        String channelName = (String) seriesList_.getChannels().iterator().next();
        String fileName = channelName.replace("/", " - ");
        if (!fileName.endsWith(".jpg")) {
            fileName += ".jpg";
        }
        File selectedFile = new File(directory, fileName);

        // create filter to only show files that end with '.jpg'
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File f) {
                return (f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg"));
            }

            public String getDescription() {
                return "JPEG Image Files";
            }
        };

        // show dialog
        File outFile = UIUtilities.getFile(fileFilter, "Save", null, selectedFile);
        if (outFile != null) {
            // prompt for overwrite if file already exists
            if (outFile.exists()) {
                int overwriteReturn = JOptionPane.showConfirmDialog(null,
                        outFile.getName() + " already exists. Do you want to replace it?", "Replace image?",
                        JOptionPane.YES_NO_OPTION);
                if (overwriteReturn == JOptionPane.NO_OPTION) {
                    saveImage(outFile.getParentFile());
                    return;
                }
            }

            // write image file
            try {
                FileOutputStream out = new FileOutputStream(outFile);
                out.write(displayedImageData);
                out.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Filed to write image file.", "Save Image Error",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        }
    }
}

From source file:org.rdv.viz.image.ImageViz.java

/**
 * If an image is currently being displayed, save it to a file. This will
 * bring up a file chooser to select the file.
 * /*  w  w  w.j  a  va  2 s . c  o m*/
 * @param directory the directory to start the file chooser in
 */
private void saveImage(File directory) {
    if (displayedImageData != null) {
        // create default file name
        String channelName = (String) channels.iterator().next();
        String fileName = channelName.replace("/", " - ");
        if (!fileName.endsWith(".jpg")) {
            fileName += ".jpg";
        }
        File selectedFile = new File(directory, fileName);

        // create filter to only show files that end with '.jpg'
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File f) {
                return (f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg"));
            }

            public String getDescription() {
                return "JPEG Image Files";
            }
        };

        // show dialog
        File outFile = UIUtilities.getFile(fileFilter, "Save", null, selectedFile);
        if (outFile != null) {
            // prompt for overwrite if file already exists
            if (outFile.exists()) {
                int overwriteReturn = JOptionPane.showConfirmDialog(null,
                        outFile.getName() + " already exists. Do you want to replace it?", "Replace image?",
                        JOptionPane.YES_NO_OPTION);
                if (overwriteReturn == JOptionPane.NO_OPTION) {
                    saveImage(outFile.getParentFile());
                    return;
                }
            }

            // write image file
            try {
                FileOutputStream out = new FileOutputStream(outFile);
                out.write(displayedImageData);
                out.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Filed to write image file.", "Save Image Error",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        }
    }
}