Search in sources :

Example 16 with FileFilter

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

the class Initiative method saveToFile.

//** End Preferences Functions **
//** IO Functions **
/**  Calls up a file save dialog, and if a file is selected/created, will then save the combatants out to disk. */
public void saveToFile() {
    JFileChooser fLoad = new JFileChooser();
    File defaultFile = new File(PCGenSettings.getPcgDir());
    if (defaultFile.exists()) {
        fLoad.setCurrentDirectory(defaultFile);
    }
    String[] fileExt = { "gmi", "init" };
    FileFilter sff = new FileNameExtensionFilter("GMGen Initiative/Encounter Export", fileExt);
    fLoad.addChoosableFileFilter(sff);
    fLoad.setFileFilter(sff);
    final int returnVal = fLoad.showSaveDialog(this);
    try {
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String fileName = fLoad.getSelectedFile().getName();
            String ext = "";
            if (!fileName.endsWith(".gmi")) {
                ext = ".gmi";
            }
            File xml = new File(fLoad.getSelectedFile().getParent() + File.separator + fileName + ext);
            if (xml.exists()) {
                int choice = JOptionPane.showConfirmDialog(this, "File Exists, Overwrite?", "File Exists", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (choice == JOptionPane.YES_OPTION) {
                    SettingsHandler.ensurePathExists(xml.getParentFile());
                    saveToDocument(xml);
                }
            } else {
                SettingsHandler.ensurePathExists(xml.getParentFile());
                saveToDocument(xml);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Error Writing File");
        Logging.errorPrint("Error Writing File");
        Logging.errorPrint(e.getMessage(), e);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) IOException(java.io.IOException)

Example 17 with FileFilter

use of javax.swing.filechooser.FileFilter 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 18 with FileFilter

use of javax.swing.filechooser.FileFilter 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 19 with FileFilter

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

the class OptionsLangPanel method browseButtonActionPerformed.

private void browseButtonActionPerformed(ActionEvent evt) {
    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return Constant.messages.getString("options.lang.file.chooser.description");
        }

        @Override
        public boolean accept(java.io.File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".zaplang");
        }
    });
    final int state = fc.showOpenDialog(null);
    if (state == JFileChooser.APPROVE_OPTION) {
        fileTextField.setText(fc.getSelectedFile().toString());
        fileTextField.discardAllEdits();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter)

Example 20 with FileFilter

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

the class BrowserDialog method capture.

private void capture() {
    try {
        //	        this.setAlwaysOnTop(true);
        BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(this.getX(), this.getY(), this.getWidth(), this.getHeight() - this.jPanelBottom.getHeight()));
        // Save as JPEG
        JFileChooser chooser = new JFileChooser();
        chooser.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(File file) {
                String filename = file.getName();
                return filename.endsWith(".png");
            }

            @Override
            public String getDescription() {
                return "*.png";
            }
        });
        chooser.showSaveDialog(this);
        File file = chooser.getSelectedFile();
        if (file != null)
            ImageIO.write(screencapture, "png", file);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
//			 this.setAlwaysOnTop(false);
}
Also used : JFileChooser(javax.swing.JFileChooser) Rectangle(java.awt.Rectangle) FileFilter(javax.swing.filechooser.FileFilter) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

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