List of usage examples for javax.swing JFileChooser getFileFilter
public FileFilter getFileFilter()
From source file:org.signserver.admin.gui.ViewCertificateFrame.java
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed if (certificate != null) { final JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setAcceptAllFileFilterUsed(false); final FileFilter pemFilter = new FileNameExtensionFilter("X.509 Certificate (PEM)", "pem"); final FileFilter pemChainFilter = new FileNameExtensionFilter("X.509 Certificate with chain (PEM)", "pem"); final FileFilter derFilter = new FileNameExtensionFilter("X.509 Certificate (DER)", "cer", "crt", "der"); chooser.addChoosableFileFilter(pemFilter); chooser.addChoosableFileFilter(derFilter); chooser.addChoosableFileFilter(pemChainFilter); chooser.setDialogTitle("Save certificate as file"); if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); try { if (chooser.getFileFilter() == derFilter) { // DER file FileUtils.writeByteArrayToFile(file, certificate.getEncoded()); } else { // PEM file BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); PEMWriter pemWriter = new PEMWriter(new OutputStreamWriter(out)); if (chooser.getFileFilter() == pemFilter) { pemWriter.writeObject(certificate); } else { for (X509Certificate cert : certificates) { pemWriter.writeObject(cert); }/* w ww .j a va 2 s. c om*/ } pemWriter.close(); } finally { IOUtils.closeQuietly(out); } } } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Exporting certificate failed:\n" + ex.getLocalizedMessage(), "Export", JOptionPane.ERROR_MESSAGE); } catch (CertificateEncodingException ex) { JOptionPane.showMessageDialog(this, "Unable to encode certificate:\n" + ex.getLocalizedMessage(), "Export", JOptionPane.ERROR_MESSAGE); } } } }
From source file:org.yccheok.jstock.gui.Utils.java
public static FileEx promptSavePortfolioCSVAndExcelJFileChooser(final String suggestedFileName) { final JStockOptions jStockOptions = JStock.instance().getJStockOptions(); final JFileChooser chooser = new JFileChooser(jStockOptions.getLastFileIODirectory()); final FileNameExtensionFilter csvFilter = new FileNameExtensionFilter("CSV Documents (*.csv)", "csv"); final FileNameExtensionFilter xlsFilter = new FileNameExtensionFilter("Microsoft Excel (*.xls)", "xls"); chooser.setAcceptAllFileFilterUsed(false); chooser.addChoosableFileFilter(csvFilter); chooser.addChoosableFileFilter(xlsFilter); final org.yccheok.jstock.gui.file.PortfolioSelectionJPanel portfolioSelectionJPanel = new org.yccheok.jstock.gui.file.PortfolioSelectionJPanel(); chooser.setAccessory(portfolioSelectionJPanel); chooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() { @Override// w w w . j a va2 s. c o m public void propertyChange(PropertyChangeEvent evt) { final boolean flag = ((FileNameExtensionFilter) evt.getNewValue()).equals(csvFilter); portfolioSelectionJPanel.setEnabled(flag); chooser.setSelectedFile(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription()) ? new File(portfolioSelectionJPanel.getSuggestedFileName()) : new File(suggestedFileName)); } }); portfolioSelectionJPanel.addJRadioButtonsActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { chooser.setSelectedFile(new File(portfolioSelectionJPanel.getSuggestedFileName())); } }); final java.util.Map<String, FileNameExtensionFilter> map = new HashMap<String, FileNameExtensionFilter>(); map.put(csvFilter.getDescription(), csvFilter); map.put(xlsFilter.getDescription(), xlsFilter); final FileNameExtensionFilter filter = map.get(jStockOptions.getLastSavedFileNameExtensionDescription()); if (filter != null) { chooser.setFileFilter(filter); } // Only enable portfolioSelectionJPanel, if CSV is being selected. portfolioSelectionJPanel .setEnabled(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription())); chooser.setSelectedFile(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription()) ? new File(portfolioSelectionJPanel.getSuggestedFileName()) : new File(suggestedFileName)); while (true) { final int returnVal = chooser.showSaveDialog(JStock.instance()); if (returnVal != JFileChooser.APPROVE_OPTION) { return null; } File file = chooser.getSelectedFile(); if (file == null) { return null; } // Ensure the saved file is in correct extension. If user provide correct // file extension explicitly, leave it as is. If not, mutate the filename. final String extension = Utils.getFileExtension(file); if (extension.equals("csv") == false && extension.equals("xls") == false) { if (chooser.getFileFilter().getDescription().equals(csvFilter.getDescription())) { file = new File(file.getAbsolutePath() + ".csv"); } else if (chooser.getFileFilter().getDescription().equals(xlsFilter.getDescription())) { file = new File(file.getAbsolutePath() + ".xls"); } else { // Impossible. return null; } } if (file.exists()) { final String output = MessageFormat .format(MessagesBundle.getString("question_message_replace_old_template"), file.getName()); final int result = javax.swing.JOptionPane.showConfirmDialog(JStock.instance(), output, MessagesBundle.getString("question_title_replace_old"), javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE); if (result != javax.swing.JOptionPane.YES_OPTION) { continue; } } final String parent = chooser.getSelectedFile().getParent(); if (parent != null) { jStockOptions.setLastFileIODirectory(parent); } if (Utils.getFileExtension(file).equals("csv")) { jStockOptions.setLastFileNameExtensionDescription(csvFilter.getDescription()); } else if (Utils.getFileExtension(file).equals("xls")) { jStockOptions.setLastFileNameExtensionDescription(xlsFilter.getDescription()); } else { // Impossible. return null; } return new FileEx(file, portfolioSelectionJPanel.getType()); } }
From source file:org.yccheok.jstock.gui.Utils.java
private static File promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters) { final JStockOptions jStockOptions = JStock.instance().getJStockOptions(); final JFileChooser chooser = new JFileChooser(jStockOptions.getLastFileIODirectory()); chooser.setAcceptAllFileFilterUsed(false); for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) { chooser.addChoosableFileFilter(fileNameExtensionFilter); }/*from w w w . j ava 2 s . co m*/ chooser.setSelectedFile(new File(suggestedFileName)); final java.util.Map<String, FileNameExtensionFilter> map = new HashMap<String, FileNameExtensionFilter>(); for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) { map.put(fileNameExtensionFilter.getDescription(), fileNameExtensionFilter); } final FileNameExtensionFilter filter = map.get(jStockOptions.getLastSavedFileNameExtensionDescription()); if (filter != null) { chooser.setFileFilter(filter); } while (true) { final int returnVal = chooser.showSaveDialog(JStock.instance()); if (returnVal != JFileChooser.APPROVE_OPTION) { return null; } File file = chooser.getSelectedFile(); if (file == null) { return null; } // Ensure the saved file is in correct extension. If user provide correct // file extension explicitly, leave it as is. If not, mutate the filename. final String extension = Utils.getFileExtension(file); boolean found = false; root: for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) { String[] extensions = fileNameExtensionFilter.getExtensions(); for (String e : extensions) { if (e.equals(extension)) { found = true; break root; } } } if (!found) { for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) { String[] extensions = fileNameExtensionFilter.getExtensions(); if (extensions.length <= 0) { continue; } final String e = extensions[0]; if (chooser.getFileFilter().getDescription().equals(fileNameExtensionFilter.getDescription())) { if (e.startsWith(".")) { file = new File(file.getAbsolutePath() + e); } else { file = new File(file.getAbsolutePath() + "." + e); } break; } } } if (file.exists()) { final String output = MessageFormat .format(MessagesBundle.getString("question_message_replace_old_template"), file.getName()); final int result = javax.swing.JOptionPane.showConfirmDialog(JStock.instance(), output, MessagesBundle.getString("question_title_replace_old"), javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE); if (result != javax.swing.JOptionPane.YES_OPTION) { continue; } } final String parent = chooser.getSelectedFile().getParent(); if (parent != null) { jStockOptions.setLastFileIODirectory(parent); } final String e = Utils.getFileExtension(file); for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) { String[] extensions = fileNameExtensionFilter.getExtensions(); if (extensions.length <= 0) { continue; } if (e.equals(extensions[0])) { jStockOptions.setLastFileNameExtensionDescription(fileNameExtensionFilter.getDescription()); break; } } return file; } }
From source file:us.daveread.basicquery.BasicQuery.java
/** * Open or create a SQL statement file./* w w w. jav a 2 s . c om*/ */ private void openSQLFile() { JFileChooser fileMenu; FileFilter defaultFileFilter = null; FileFilter preferredFileFilter = null; File chosenSQLFile; int returnVal; chosenSQLFile = null; // Save current information, including SQL Statements saveConfig(); // Allow user to choose/create new file for SQL Statements fileMenu = new JFileChooser(new File(queryFilename)); for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) { if (filterDefinition.name().startsWith("QUERY")) { final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(), filterDefinition.acceptedSuffixes()); if (filterDefinition.isPreferredOption()) { preferredFileFilter = fileFilter; } fileMenu.addChoosableFileFilter(fileFilter); if (filterDefinition.description().equals(latestChosenQueryFileFilterDescription)) { defaultFileFilter = fileFilter; } } } if (defaultFileFilter != null) { fileMenu.setFileFilter(defaultFileFilter); } else if (latestChosenQueryFileFilterDescription != null && latestChosenQueryFileFilterDescription.startsWith("All")) { fileMenu.setFileFilter(fileMenu.getAcceptAllFileFilter()); } else if (preferredFileFilter != null) { fileMenu.setFileFilter(preferredFileFilter); } fileMenu.setSelectedFile(new File(queryFilename)); fileMenu.setDialogTitle(Resources.getString("dlgSQLFileTitle")); fileMenu.setDialogType(JFileChooser.OPEN_DIALOG); fileMenu.setFileSelectionMode(JFileChooser.FILES_ONLY); fileMenu.setMultiSelectionEnabled(false); if (fileMenu.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { chosenSQLFile = fileMenu.getSelectedFile(); // Adjust file suffix if necessary final FileFilter fileFilter = fileMenu.getFileFilter(); if (fileFilter != null && fileFilter instanceof SuffixFileFilter && !fileMenu.getFileFilter().accept(chosenSQLFile)) { chosenSQLFile = ((SuffixFileFilter) fileFilter).makeWithPrimarySuffix(chosenSQLFile); } if (!chosenSQLFile.exists()) { returnVal = JOptionPane.showConfirmDialog(this, Resources.getString("dlgNewSQLFileText", chosenSQLFile.getName()), Resources.getString("dlgNewSQLFileTitle"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (returnVal == JOptionPane.NO_OPTION) { querySelection.removeAllItems(); queryText.setText(""); QueryHistory.getInstance().clearAllQueries(); // Update GUI setPrevNextIndication(); } else if (returnVal == JOptionPane.CANCEL_OPTION) { chosenSQLFile = null; } } else { setQueryFilename(chosenSQLFile.getAbsolutePath()); querySelection.removeAllItems(); queryText.setText(""); loadCombo(querySelection, queryFilename); QueryHistory.getInstance().clearAllQueries(); // Update GUI setPrevNextIndication(); } } try { latestChosenQueryFileFilterDescription = fileMenu.getFileFilter().getDescription(); } catch (Throwable throwable) { LOGGER.warn("Unable to determine which ontology file filter was chosen", throwable); } if (chosenSQLFile != null) { setQueryFilename(chosenSQLFile.getAbsolutePath()); saveConfig(); } }
From source file:xtrememp.PlaylistManager.java
public boolean savePlaylistDialog() { JFileChooser fileChooser = new JFileChooser(Settings.getLastDir()); M3uPlaylistFileFilter m3uFileFilter = new M3uPlaylistFileFilter(); XspfPlaylistFileFilter xspfFileFilter = new XspfPlaylistFileFilter(); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.addChoosableFileFilter(m3uFileFilter); fileChooser.addChoosableFileFilter(xspfFileFilter); fileChooser.setMultiSelectionEnabled(false); if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); FileFilter fileFilter = fileChooser.getFileFilter(); String fileName = file.getName().toLowerCase(); if (fileFilter == m3uFileFilter) { if (!fileName.endsWith(".m3u")) { fileName = fileName.concat(".m3u"); }//from w ww .java 2 s.c o m try { return PlaylistIO.saveM3U(playlist, file.getParent() + File.separator + fileName); } catch (PlaylistException ex) { logger.error("Can't save playlist in M3U format", ex); } } if (fileFilter == xspfFileFilter) { if (!fileName.endsWith(".xspf")) { fileName = fileName.concat(".xspf"); } try { return PlaylistIO.saveXSPF(playlist, file.getParent() + File.separator + fileName); } catch (PlaylistException ex) { logger.error("Can't save playlist in XSPF format", ex); } } Settings.setLastDir(file.getParent()); } return false; }