Search in sources :

Example 1 with SimpleFileFilter

use of org.knime.core.util.SimpleFileFilter in project knime-core by knime.

the class CSVFilesHistoryPanel method getOutputFileName.

private String getOutputFileName() {
    // file chooser triggered by choose button
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setFileFilter(new SimpleFileFilter(".csv"));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    String f = m_textBox.getEditor().getItem().toString();
    File dirOrFile = getFile(f);
    if (dirOrFile.isDirectory()) {
        fileChooser.setCurrentDirectory(dirOrFile);
    } else {
        fileChooser.setSelectedFile(dirOrFile);
    }
    int r = fileChooser.showDialog(CSVFilesHistoryPanel.this, "Save");
    if (r == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (file.exists() && file.isDirectory()) {
            JOptionPane.showMessageDialog(this, "Error: Please specify " + "a file, not a directory.");
            return null;
        }
        return file.getAbsolutePath();
    }
    return null;
}
Also used : JFileChooser(javax.swing.JFileChooser) SimpleFileFilter(org.knime.core.util.SimpleFileFilter) File(java.io.File)

Example 2 with SimpleFileFilter

use of org.knime.core.util.SimpleFileFilter in project knime-core by knime.

the class FilesHistoryPanel method getOutputFileName.

private String getOutputFileName() {
    // file chooser triggered by choose button
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(true);
    List<SimpleFileFilter> filters = createFiltersFromSuffixes(m_suffixes);
    for (SimpleFileFilter filter : filters) {
        fileChooser.addChoosableFileFilter(filter);
    }
    if (filters.size() > 0) {
        fileChooser.setFileFilter(filters.get(0));
    }
    fileChooser.setFileSelectionMode(m_selectMode);
    fileChooser.setDialogType(m_dialogType);
    // AP-2562
    // It seems only resized event is happening when showing the dialog
    // Grabbing the focus then makes two clicks to single click selection.
    fileChooser.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(final ComponentEvent e) {
            fileChooser.grabFocus();
        }
    });
    try {
        URL url = FileUtil.toURL(getSelectedFileWithPropertiesReplaced());
        Path localPath = FileUtil.resolveToPath(url);
        if (localPath != null) {
            if (Files.isDirectory(localPath)) {
                fileChooser.setCurrentDirectory(localPath.toFile());
            } else {
                fileChooser.setSelectedFile(localPath.toFile());
            }
        }
    } catch (IOException | URISyntaxException | InvalidPathException ex) {
    // ignore
    }
    int r;
    /* This if construct is result of a fix for bug 5841.
        * showDialog does not resolve localized folder names correctly under Mac OS,
        * so we use the methods showSaveDialog and showOpenDialog if possible.
        */
    if (this.m_dialogType == JFileChooser.SAVE_DIALOG) {
        r = fileChooser.showSaveDialog(FilesHistoryPanel.this);
    } else if (this.m_dialogType == JFileChooser.OPEN_DIALOG) {
        r = fileChooser.showOpenDialog(FilesHistoryPanel.this);
    } else {
        r = fileChooser.showDialog(FilesHistoryPanel.this, "OK");
    }
    if (r == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (m_dialogType == JFileChooser.SAVE_DIALOG) {
            String forceFileExtension = StringUtils.defaultString(m_forcedFileExtensionOnSave);
            final String fileName = file.getName();
            if (!(StringUtils.endsWithAny(fileName, m_suffixes) || StringUtils.endsWithIgnoreCase(fileName, m_forcedFileExtensionOnSave))) {
                file = new File(file.getParentFile(), fileName.concat(forceFileExtension));
            }
        }
        if (file.exists() && (m_selectMode == JFileChooser.FILES_ONLY) && file.isDirectory()) {
            JOptionPane.showMessageDialog(this, "Error: Please select a file, not a directory.");
            return null;
        }
        return file.getAbsolutePath();
    }
    return null;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) InvalidPathException(java.nio.file.InvalidPathException) JFileChooser(javax.swing.JFileChooser) SimpleFileFilter(org.knime.core.util.SimpleFileFilter) ComponentEvent(java.awt.event.ComponentEvent) File(java.io.File) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 3 with SimpleFileFilter

use of org.knime.core.util.SimpleFileFilter in project knime-core by knime.

the class JavaScriptingJarListPanel method onJarAdd.

private void onJarAdd() {
    DefaultListModel model = (DefaultListModel) m_addJarList.getModel();
    Set<Object> hash = new HashSet<Object>();
    for (Enumeration<?> e = model.elements(); e.hasMoreElements(); ) {
        hash.add(e.nextElement());
    }
    StringHistory history = StringHistory.getInstance("java_snippet_jar_dirs");
    if (m_jarFileChooser == null) {
        File dir = null;
        for (String h : history.getHistory()) {
            File temp = new File(h);
            if (temp.isDirectory()) {
                dir = temp;
                break;
            }
        }
        m_jarFileChooser = new JFileChooser(dir);
        m_jarFileChooser.setFileFilter(new SimpleFileFilter(".zip", ".jar"));
        m_jarFileChooser.setMultiSelectionEnabled(true);
    }
    int result = m_jarFileChooser.showDialog(m_addJarList, "Select");
    if (result == JFileChooser.APPROVE_OPTION) {
        for (File f : m_jarFileChooser.getSelectedFiles()) {
            String s = f.getAbsolutePath();
            if (hash.add(s)) {
                model.addElement(s);
            }
        }
        history.add(m_jarFileChooser.getCurrentDirectory().getAbsolutePath());
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) DefaultListModel(javax.swing.DefaultListModel) StringHistory(org.knime.core.node.util.StringHistory) SimpleFileFilter(org.knime.core.util.SimpleFileFilter) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with SimpleFileFilter

use of org.knime.core.util.SimpleFileFilter in project knime-core by knime.

the class JarListPanel method onJarFileAdd.

private void onJarFileAdd() {
    final Set<String> hash = new HashSet<>(Collections.list(m_listModel.elements()));
    final StringHistory history = StringHistory.getInstance("java_snippet_jar_dirs");
    if (m_jarFileChooser == null) {
        File dir = null;
        for (final String h : history.getHistory()) {
            final File temp = new File(h);
            if (temp.isDirectory()) {
                dir = temp;
                break;
            }
        }
        m_jarFileChooser = new JFileChooser(dir);
        m_jarFileChooser.setFileFilter(new SimpleFileFilter(".zip", ".jar"));
        m_jarFileChooser.setMultiSelectionEnabled(true);
    }
    final int result = m_jarFileChooser.showDialog(m_addJarList, "Select");
    if (result == JFileChooser.APPROVE_OPTION) {
        for (final File f : m_jarFileChooser.getSelectedFiles()) {
            final String s = f.getAbsolutePath();
            if (hash.add(s)) {
                m_listModel.addElement(s);
            }
        }
        history.add(m_jarFileChooser.getCurrentDirectory().getAbsolutePath());
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) StringHistory(org.knime.core.node.util.StringHistory) SimpleFileFilter(org.knime.core.util.SimpleFileFilter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)4 JFileChooser (javax.swing.JFileChooser)4 SimpleFileFilter (org.knime.core.util.SimpleFileFilter)4 HashSet (java.util.HashSet)2 StringHistory (org.knime.core.node.util.StringHistory)2 ComponentAdapter (java.awt.event.ComponentAdapter)1 ComponentEvent (java.awt.event.ComponentEvent)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 DefaultListModel (javax.swing.DefaultListModel)1