Search in sources :

Example 6 with FileNameExtensionFilter

use of javax.swing.filechooser.FileNameExtensionFilter in project pcgen by PCGen.

the class DiceBagPluginController method fileOpen.

/**
	 * <p>
	 * Displays a file-open dialog box and processes the selected values.
	 * </p>
	 *
	 * @return {@code boolean} indicating success/failure of operation.
	 */
public boolean fileOpen() {
    boolean returnValue = false;
    String sFile = SettingsHandler.getGMGenOption(DiceBagPlugin.LOG_NAME + ".LastFile", System.getProperty("user.dir"));
    JFileChooser open = new JFileChooser();
    if (sFile != null) {
        File defaultFile = new File(sFile);
        if (defaultFile.exists()) {
            open.setCurrentDirectory(defaultFile);
        }
    }
    String fileExt = "dbg";
    FileFilter ff = new FileNameExtensionFilter("GMGen Dice Bag", fileExt);
    open.addChoosableFileFilter(ff);
    open.setFileFilter(ff);
    if (open.showOpenDialog(GMGenSystem.inst) == JFileChooser.APPROVE_OPTION) {
        openFile(open.getSelectedFile());
        returnValue = true;
    }
    return returnValue;
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File)

Example 7 with FileNameExtensionFilter

use of javax.swing.filechooser.FileNameExtensionFilter in project pcgen by PCGen.

the class NotesView method getImageFromChooser.

/**
	 *  obtains an Image for input using a custom JFileChooser dialog
	 *
	 *@param  startDir  Directory to open JFielChooser to
	 *@param  exts      Extensions to search for
	 *@param  desc      Description for files
	 *@return           File pointing to the selected image
	 */
private File getImageFromChooser(String startDir, String[] exts, String desc) {
    JFileChooser jImageDialog = new JFileChooser();
    jImageDialog.setCurrentDirectory(new File(startDir));
    jImageDialog.setAccessory(new ImageFileChooserPreview(jImageDialog));
    jImageDialog.setDialogType(JFileChooser.CUSTOM_DIALOG);
    jImageDialog.setFileFilter(new FileNameExtensionFilter(desc, exts));
    jImageDialog.setDialogTitle("Select an Image to Insert");
    int optionSelected = jImageDialog.showDialog(this, "Insert");
    if (optionSelected == JFileChooser.APPROVE_OPTION) {
        return jImageDialog.getSelectedFile();
    }
    return null;
}
Also used : ImageFileChooserPreview(gmgen.gui.ImageFileChooserPreview) JFileChooser(javax.swing.JFileChooser) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) Point(java.awt.Point)

Example 8 with FileNameExtensionFilter

use of javax.swing.filechooser.FileNameExtensionFilter in project ACS by ACS-Community.

the class TablePopupMenu method saveSelectedLogs.

/**
	 * Save the selected logs into a file in the passed format.
	 * 
	 * @param converter The converter to desired format
	 * @param fileIcon The icon of the file type
	 */
private void saveSelectedLogs(LogConverter converter, ImageIcon fileIcon, String extension) {
    if (converter == null) {
        throw new IllegalArgumentException("The converter can't be null");
    }
    if (extension == null || extension.isEmpty()) {
        throw new IllegalArgumentException("Invalid file extension");
    }
    converter.setCols(buildFields());
    // Build the text to save in the file
    StringBuilder strBuffer = new StringBuilder();
    if (!selectionModel.isSelectionEmpty()) {
        for (int i = selectionModel.getMinSelectionIndex(); i <= selectionModel.getMaxSelectionIndex(); i++) {
            if (!selectionModel.isSelectedIndex(i)) {
                continue;
            } else {
                ILogEntry log = model.getVisibleLogEntry(sorter.convertRowIndexToModel(i));
                strBuffer.append(converter.convert(log));
            }
        }
        if (strBuffer.length() == 0) {
            // Nothing to save
            return;
        }
    }
    FileFilter fileFilter = new FileNameExtensionFilter("File " + extension, extension);
    CustomFileChooser fc = new CustomFileChooser(fileIcon, fileFilter);
    fc.addChoosableFileFilter(fileFilter);
    if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        // If not present, add the xml extension
        if (!file.getAbsolutePath().toLowerCase().endsWith("." + extension)) {
            file = new File(file.getAbsolutePath() + "." + extension);
        }
        FileOutputStream outFStream = null;
        try {
            outFStream = new FileOutputStream(file);
        } catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(null, "Error creating " + file.getAbsolutePath() + ":\n" + fnfe.getMessage(), "Error saving logs", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            outFStream.write(strBuffer.toString().getBytes());
            outFStream.flush();
            outFStream.close();
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, "Error saving " + file.getAbsolutePath() + ":\n" + ioe.getMessage(), "Error saving logs", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : ILogEntry(com.cosylab.logging.engine.log.ILogEntry) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File)

Example 9 with FileNameExtensionFilter

use of javax.swing.filechooser.FileNameExtensionFilter in project zaproxy by zaproxy.

the class PopupMenuExportURLs method getOutputFile.

protected File getOutputFile() {
    WritableFileChooser chooser = new WritableFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
    FileNameExtensionFilter textFilesFilter = new FileNameExtensionFilter(Constant.messages.getString("file.format.ascii"), "txt");
    FileNameExtensionFilter htmlFilesFilter = new FileNameExtensionFilter(Constant.messages.getString("file.format.html"), "html", "htm");
    chooser.addChoosableFileFilter(textFilesFilter);
    chooser.addChoosableFileFilter(htmlFilesFilter);
    chooser.setFileFilter(textFilesFilter);
    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 fileNameLc = file.getAbsolutePath().toLowerCase();
        if (!fileNameLc.endsWith(".txt") && !fileNameLc.endsWith(".htm") && !fileNameLc.endsWith(".html")) {
            String ext;
            if (htmlFilesFilter.equals(chooser.getFileFilter())) {
                ext = ".html";
            } else {
                ext = ".txt";
            }
            file = new File(file.getAbsolutePath() + ext);
        }
        return file;
    }
    return file;
}
Also used : FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) WritableFileChooser(org.zaproxy.zap.view.widgets.WritableFileChooser)

Example 10 with FileNameExtensionFilter

use of javax.swing.filechooser.FileNameExtensionFilter in project DistributedFractalNetwork by Budder21.

the class Display method menus.

private JMenuBar menus() {
    ToolTipManager.sharedInstance().setInitialDelay(0);
    JMenuBar bar = new JMenuBar();
    JMenu view = new JMenu("View");
    JMenuItem serverLog = new JMenuItem("Server Log");
    serverLog.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JFrame f = new JFrame("Server Log");
            JTextArea textArea = new JTextArea(35, 55);
            JScrollPane scroll = new JScrollPane(textArea);
            textArea.setText(log.getLog());
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            f.setLayout(new BorderLayout());
            f.add(scroll, BorderLayout.CENTER);
            JButton b = new JButton("Save");
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JFileChooser fileChooser = new JFileChooser();
                    FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
                    fileChooser.setFileFilter(filter);
                    fileChooser.setCurrentDirectory(new File("fractals/logs"));
                    fileChooser.showSaveDialog(null);
                    String dir = fileChooser.getSelectedFile().getPath();
                    if (!dir.substring(dir.lastIndexOf(".") + 1).equals("txt"))
                        dir = dir.substring(0, dir.lastIndexOf(".")) + ".txt";
                    System.out.println("\n\n" + dir + "\n\n");
                    try {
                        PrintWriter out = new PrintWriter(dir);
                        Scanner s = new Scanner(textArea.getText());
                        while (s.hasNextLine()) out.println(s.nextLine());
                        out.flush();
                        out.close();
                    } catch (FileNotFoundException e1) {
                        log.addError(e1);
                    }
                }
            });
            JPanel tempPanel = new JPanel();
            tempPanel.add(b);
            f.add(tempPanel, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setResizable(true);
            f.setVisible(true);
        }
    });
    serverLog.setToolTipText("Displays the log of the current server.");
    view.add(serverLog);
    bar.add(view);
    return bar;
}
Also used : JScrollPane(javax.swing.JScrollPane) Scanner(java.util.Scanner) JPanel(javax.swing.JPanel) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) FileNotFoundException(java.io.FileNotFoundException) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) JFileChooser(javax.swing.JFileChooser) JFrame(javax.swing.JFrame) JMenuItem(javax.swing.JMenuItem) File(java.io.File) JMenuBar(javax.swing.JMenuBar) JMenu(javax.swing.JMenu) PrintWriter(java.io.PrintWriter)

Aggregations

FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)52 File (java.io.File)32 JFileChooser (javax.swing.JFileChooser)30 FileFilter (javax.swing.filechooser.FileFilter)15 IOException (java.io.IOException)11 Preferences (java.util.prefs.Preferences)7 FileNotFoundException (java.io.FileNotFoundException)6 PCGFile (pcgen.io.PCGFile)6 ResourceBundle (java.util.ResourceBundle)5 SwingWorker (javax.swing.SwingWorker)5 FileOutputStream (java.io.FileOutputStream)3 PCGenSettings (pcgen.system.PCGenSettings)3 ImportException (dr.evolution.io.Importer.ImportException)2 MissingBlockException (dr.evolution.io.NexusImporter.MissingBlockException)2 java.awt (java.awt)2 BorderLayout (java.awt.BorderLayout)2 Cursor (java.awt.Cursor)2 Dimension (java.awt.Dimension)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2