Search in sources :

Example 26 with FileFilter

use of javax.swing.filechooser.FileFilter in project hid-serial by rayshobby.

the class G4P method selectImpl.

/**
	 * The implementation of the select input and output methods.
	 * @param prompt
	 * @param mode
	 * @param types
	 * @param typeDesc
	 * @return the absolute path name for the selected folder, or null if action 
	 * cancelled.
	 */
private static String selectImpl(String prompt, int mode, String types, String typeDesc) {
    // If no initial selection made then use last selection	
    // Assume that a file will not be selected
    String selectedFile = null;
    // Get the owner
    Frame owner = (sketchApplet == null) ? null : sketchApplet.frame;
    // Create a file filter
    if (PApplet.useNativeSelect) {
        FileDialog dialog = new FileDialog(owner, prompt, mode);
        FilenameFilter filter = null;
        if (types != null && types.length() > 0) {
            filter = new FilenameChooserFilter(types);
            dialog.setFilenameFilter(filter);
        }
        dialog.setVisible(true);
        String directory = dialog.getDirectory();
        if (directory != null) {
            selectedFile = dialog.getFile();
            if (selectedFile != null) {
                try {
                    selectedFile = (new File(directory, selectedFile)).getCanonicalPath();
                } catch (IOException e) {
                    selectedFile = null;
                }
            }
        }
    } else {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(prompt);
        FileFilter filter = null;
        if (types != null && types.length() > 0) {
            filter = new FileChooserFilter(types, typeDesc);
            chooser.setFileFilter(filter);
        }
        int result = JFileChooser.ERROR_OPTION;
        if (mode == FileDialog.SAVE) {
            result = chooser.showSaveDialog(owner);
        } else if (mode == FileDialog.LOAD) {
            result = chooser.showOpenDialog(owner);
        }
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                selectedFile = chooser.getSelectedFile().getCanonicalPath();
            } catch (IOException e) {
                selectedFile = null;
            }
        }
    }
    return selectedFile;
}
Also used : FilenameFilter(java.io.FilenameFilter) Frame(java.awt.Frame) JFileChooser(javax.swing.JFileChooser) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 27 with FileFilter

use of javax.swing.filechooser.FileFilter in project jgnash by ccavanaugh.

the class DynamicJasperReportPanel method saveAction.

private void saveAction() {
    Preferences p = Preferences.userNodeForPackage(DynamicJasperReportPanel.class);
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setMultiSelectionEnabled(false);
    saveContributors.forEach(fileChooser::addChoosableFileFilter);
    // restore the last save format
    if (p.get(LAST_CONTRIBUTOR, null) != null) {
        String last = p.get(LAST_CONTRIBUTOR, null);
        for (JRSaveContributor saveContributor : saveContributors) {
            if (saveContributor.getDescription().equals(last)) {
                fileChooser.setFileFilter(saveContributor);
                break;
            }
        }
    } else if (!saveContributors.isEmpty()) {
        fileChooser.setFileFilter(saveContributors.get(0));
    }
    if (p.get(LAST_DIRECTORY, null) != null) {
        fileChooser.setCurrentDirectory(new File(p.get(LAST_DIRECTORY, null)));
    }
    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();
        p.put(LAST_DIRECTORY, file.getParent());
        JRSaveContributor contributor = null;
        if (fileFilter instanceof JRSaveContributor) {
            // save format chosen from the list
            contributor = (JRSaveContributor) fileFilter;
        } else {
            for (JRSaveContributor saveContributor : saveContributors) {
                // need to determine the best match
                if (saveContributor.accept(file)) {
                    contributor = saveContributor;
                    break;
                }
            }
            if (contributor == null) {
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
        if (contributor != null) {
            p.put(LAST_CONTRIBUTOR, contributor.getDescription());
            try {
                if (contributor instanceof JRSingleSheetXlsSaveContributor) {
                    LOG.info("Formatting for xls file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else if (contributor instanceof JRCsvSaveContributor) {
                    LOG.info("Formatting for csv file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else {
                    contributor.save(jasperPrint, file);
                }
            } catch (final JRException ex) {
                LOG.log(Level.SEVERE, ex.getMessage(), ex);
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
    }
}
Also used : JRSingleSheetXlsSaveContributor(net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor) JFileChooser(javax.swing.JFileChooser) JRException(net.sf.jasperreports.engine.JRException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSaveContributor(net.sf.jasperreports.view.JRSaveContributor) Preferences(java.util.prefs.Preferences) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRCsvSaveContributor(net.sf.jasperreports.view.save.JRCsvSaveContributor)

Example 28 with FileFilter

use of javax.swing.filechooser.FileFilter in project jgnash by ccavanaugh.

the class AttachmentPanel method attachmentAction.

private void attachmentAction() {
    final Preferences pref = Preferences.userNodeForPackage(AbstractBankTransactionPanel.class);
    final String baseFile = EngineFactory.getActiveDatabase();
    final String[] fileSuffixes = ImageIO.getReaderFileSuffixes();
    StringBuilder description = new StringBuilder(rb.getString("Title.ImageFiles")).append(" (");
    for (int i = 0; i < fileSuffixes.length; i++) {
        description.append("*.");
        description.append(fileSuffixes[i]);
        if (i < fileSuffixes.length - 1) {
            description.append(", ");
        }
    }
    description.append(")");
    FileFilter fileFilter = new FileNameExtensionFilter(description.toString(), fileSuffixes);
    final JFileChooser chooser = new JFileChooser(pref.get(LAST_DIR, null));
    chooser.addChoosableFileFilter(fileFilter);
    chooser.setFileFilter(fileFilter);
    chooser.setMultiSelectionEnabled(false);
    chooser.setAcceptAllFileFilterUsed(false);
    if (attachment != null) {
        chooser.setSelectedFile(attachment.toFile());
    }
    if (chooser.showOpenDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(LAST_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        File selectedFile = chooser.getSelectedFile();
        if (selectedFile != null) {
            boolean result = true;
            final Path attachmentDirectory = AttachmentUtils.getAttachmentDirectory(Paths.get(baseFile));
            if (baseFile.startsWith(EngineFactory.REMOTE_PREFIX)) {
                // working remotely
                moveAttachment = true;
            } else if (attachmentDirectory != null && !attachmentDirectory.toString().equals(selectedFile.getParent())) {
                String message = ResourceUtils.getString("Message.Warn.MoveFile", selectedFile.toString(), attachmentDirectory.toString());
                result = YesNoDialog.showYesNoDialog(UIApplication.getFrame(), new JLabel(message), rb.getString("Title.MoveFile"));
                if (result) {
                    moveAttachment = true;
                    Path newPath = Paths.get(AttachmentUtils.getAttachmentDirectory(Paths.get(baseFile)) + FileUtils.separator + selectedFile.getName());
                    if (newPath.toFile().exists()) {
                        message = ResourceUtils.getString("Message.Warn.SameFile", selectedFile.toString(), attachmentDirectory.toString());
                        StaticUIMethods.displayWarning(message);
                        moveAttachment = false;
                        result = false;
                    }
                }
            }
            if (result) {
                attachment = selectedFile.toPath();
            }
        }
    }
}
Also used : Path(java.nio.file.Path) JFileChooser(javax.swing.JFileChooser) JLabel(javax.swing.JLabel) Preferences(java.util.prefs.Preferences) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File)

Example 29 with FileFilter

use of javax.swing.filechooser.FileFilter in project jdk8u_jdk by JetBrains.

the class FileChooserDemo method resetFileFilters.

private void resetFileFilters(boolean enableFilters, boolean showExtensionInDescription) {
    chooser.resetChoosableFileFilters();
    if (enableFilters) {
        FileFilter jpgFilter = createFileFilter("JPEG Compressed Image Files", showExtensionInDescription, "jpg");
        FileFilter gifFilter = createFileFilter("GIF Image Files", showExtensionInDescription, "gif");
        FileFilter bothFilter = createFileFilter("JPEG and GIF Image Files", showExtensionInDescription, "jpg", "gif");
        chooser.addChoosableFileFilter(bothFilter);
        chooser.addChoosableFileFilter(jpgFilter);
        chooser.addChoosableFileFilter(gifFilter);
    }
}
Also used : FileFilter(javax.swing.filechooser.FileFilter)

Example 30 with FileFilter

use of javax.swing.filechooser.FileFilter in project JMRI by JMRI.

the class ScriptFileChooser method init.

private void init() {
    List<String> allExtensions = new ArrayList<>();
    HashMap<String, FileFilter> filters = new HashMap<>();
    List<String> filterNames = new ArrayList<>();
    JmriScriptEngineManager.getDefault().getManager().getEngineFactories().stream().forEach((ScriptEngineFactory factory) -> {
        List<String> extensions = factory.getExtensions();
        allExtensions.addAll(extensions);
        String name = this.fileForLanguage(factory.getLanguageName());
        filterNames.add(name);
        filters.put(name, new FileNameExtensionFilter(name, extensions.toArray(new String[extensions.size()])));
    });
    FileFilter allScripts = new FileNameExtensionFilter(Bundle.getMessage("allScripts"), allExtensions.toArray(new String[allExtensions.size()]));
    this.addChoosableFileFilter(allScripts);
    filterNames.stream().sorted().forEach((filter) -> {
        this.addChoosableFileFilter(filters.get(filter));
    });
    this.setFileFilter(allScripts);
    this.setFileSelectionMode(JFileChooser.FILES_ONLY);
}
Also used : HashMap(java.util.HashMap) ScriptEngineFactory(javax.script.ScriptEngineFactory) ArrayList(java.util.ArrayList) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter)

Aggregations

FileFilter (javax.swing.filechooser.FileFilter)59 File (java.io.File)48 JFileChooser (javax.swing.JFileChooser)39 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)15 IOException (java.io.IOException)10 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)5 DatabaseException (org.parosproxy.paros.db.DatabaseException)5 PCGFile (pcgen.io.PCGFile)5 IllegalContextNameException (org.zaproxy.zap.model.IllegalContextNameException)4 TopLevelWindowManager (cbit.vcell.client.TopLevelWindowManager)3 UserPreferences (cbit.vcell.client.server.UserPreferences)3 ArrayList (java.util.ArrayList)3 Preferences (java.util.prefs.Preferences)3 JButton (javax.swing.JButton)3 Session (org.parosproxy.paros.model.Session)3 Component (java.awt.Component)2 Cursor (java.awt.Cursor)2 Dimension (java.awt.Dimension)2 FileDialog (java.awt.FileDialog)2