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);
}
}
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;
}
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);
}
}
}
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();
}
}
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);
}
Aggregations