use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class PCGTrackerPlugin method handleOpen.
/**
* Handles the clicking of the <b>Add</b> button on the GUI.
*/
public void handleOpen() {
File defaultFile = new File(PCGenSettings.getPcgDir());
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(defaultFile);
String[] pcgs = { FILENAME_PCG, FILENAME_PCP };
FileFilter ff = new FileNameExtensionFilter(LanguageBundle.getString("in_pcgen_file"), //$NON-NLS-1$
pcgs);
chooser.addChoosableFileFilter(ff);
chooser.setFileFilter(ff);
chooser.setMultiSelectionEnabled(true);
Component component = GMGenSystem.inst;
Cursor originalCursor = component.getCursor();
component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
int option = chooser.showOpenDialog(GMGenSystem.inst);
if (option == JFileChooser.APPROVE_OPTION) {
for (File selectedFile : chooser.getSelectedFiles()) {
if (PCGFile.isPCGenCharacterOrPartyFile(selectedFile)) {
messageHandler.handleMessage(new RequestOpenPlayerCharacterMessage(this, selectedFile, false));
}
}
} else {
/* this means the file is invalid */
}
GMGenSystem.inst.setCursor(originalCursor);
}
use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class PCGTrackerPlugin method savePC.
/**
* Checks whether a character can be saved, and if so, calls
* it's {@code save} method.
*
* @param aPC The PlayerCharacter to save
* @param saveas boolean if {@code true}, ask for file name
*
* @return {@code true} if saved; {@code false} if save as cancelled
*/
// TODO use pcgen save methods rather than implementing it again
private boolean savePC(PlayerCharacter aPC, boolean saveas) {
boolean newPC = false;
File prevFile;
File file = null;
String aPCFileName = aPC.getFileName();
if (aPCFileName.isEmpty()) {
prevFile = new File(PCGenSettings.getPcgDir(), aPC.getDisplay().getDisplayName() + Constants.EXTENSION_CHARACTER_FILE);
aPCFileName = prevFile.getAbsolutePath();
newPC = true;
} else {
prevFile = new File(aPCFileName);
}
if (saveas || newPC) {
JFileChooser fc = ImagePreview.decorateWithImagePreview(new JFileChooser());
String[] pcgs = { FILENAME_PCG };
FileFilter ff = new FileNameExtensionFilter(LanguageBundle.getString("in_pcgen_file_char"), //$NON-NLS-1$
pcgs);
fc.setFileFilter(ff);
fc.setSelectedFile(prevFile);
PropertyChangeListener listener = new FilenameChangeListener(aPCFileName, fc);
fc.addPropertyChangeListener(listener);
int returnVal = fc.showSaveDialog(GMGenSystem.inst);
fc.removePropertyChangeListener(listener);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (!PCGFile.isPCGenCharacterFile(file)) {
file = new File(file.getParent(), file.getName() + Constants.EXTENSION_CHARACTER_FILE);
}
if (file.isDirectory()) {
JOptionPane.showMessageDialog(null, //$NON-NLS-1$
LanguageBundle.getString("in_savePcDirOverwrite"), Constants.APPLICATION_NAME, JOptionPane.ERROR_MESSAGE);
return false;
}
if (file.exists() && (newPC || !file.getName().equals(prevFile.getName()))) {
int reallyClose = JOptionPane.showConfirmDialog(GMGenSystem.inst, //$NON-NLS-1$
LanguageBundle.getFormattedString(//$NON-NLS-1$
"in_savePcConfirmOverMsg", file.getName()), //$NON-NLS-1$
LanguageBundle.getFormattedString("in_savePcConfirmOverTitle", file.getName()), JOptionPane.YES_NO_OPTION);
if (reallyClose != JOptionPane.YES_OPTION) {
return false;
}
}
aPC.setFileName(file.getAbsolutePath());
} else {
return false;
}
} else {
// simple save
file = prevFile;
}
try {
(new PCGIOHandler()).write(aPC, null, null, file);
} catch (Exception ex) {
//$NON-NLS-1$
String formattedString = LanguageBundle.getFormattedString("in_saveFailMsg", aPC.getDisplay().getDisplayName());
JOptionPane.showMessageDialog(null, formattedString, Constants.APPLICATION_NAME, JOptionPane.ERROR_MESSAGE);
Logging.errorPrint(formattedString);
Logging.errorPrint(ex.getMessage(), ex);
return false;
}
return true;
}
use of javax.swing.filechooser.FileFilter in project jabref by JabRef.
the class ExportAction method getExportAction.
/**
* Create an AbstractAction for performing an export operation.
*
* @param frame
* The JabRefFrame of this JabRef instance.
* @param selectedOnly
* true indicates that only selected entries should be exported,
* false indicates that all entries should be exported.
* @return The action.
*/
public static AbstractAction getExportAction(JabRefFrame frame, boolean selectedOnly) {
class InternalExportAction extends MnemonicAwareAction {
private final JabRefFrame frame;
private final boolean selectedOnly;
public InternalExportAction(JabRefFrame frame, boolean selectedOnly) {
this.frame = frame;
this.selectedOnly = selectedOnly;
putValue(Action.NAME, selectedOnly ? Localization.menuTitle("Export selected entries") : Localization.menuTitle("Export"));
}
@Override
public void actionPerformed(ActionEvent e) {
Map<String, ExportFormat> customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs, Globals.journalAbbreviationLoader);
LayoutFormatterPreferences layoutPreferences = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs);
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
JFileChooser fc = ExportAction.createExportFileChooser(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY));
fc.showSaveDialog(frame);
File file = fc.getSelectedFile();
if (file == null) {
return;
}
FileFilter ff = fc.getFileFilter();
if (ff instanceof ExportFileFilter) {
ExportFileFilter eff = (ExportFileFilter) ff;
String path = file.getPath();
if (!path.endsWith(eff.getExtension())) {
path = path + eff.getExtension();
}
file = new File(path);
if (file.exists()) {
// Warn that the file exists:
if (JOptionPane.showConfirmDialog(frame, Localization.lang("'%0' exists. Overwrite file?", file.getName()), Localization.lang("Export"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) {
return;
}
}
final IExportFormat format = eff.getExportFormat();
List<BibEntry> entries;
if (selectedOnly) {
// Selected entries
entries = frame.getCurrentBasePanel().getSelectedEntries();
} else {
// All entries
entries = frame.getCurrentBasePanel().getDatabase().getEntries();
}
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
// Make sure we remember which filter was used, to set
// the default for next time:
Globals.prefs.put(JabRefPreferences.LAST_USED_EXPORT, format.getConsoleName());
Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, file.getParent());
final File finFile = file;
final List<BibEntry> finEntries = entries;
AbstractWorker exportWorker = new AbstractWorker() {
String errorMessage;
@Override
public void run() {
try {
format.performExport(frame.getCurrentBasePanel().getBibDatabaseContext(), finFile.getPath(), frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()), finEntries);
} catch (Exception ex) {
LOGGER.warn("Problem exporting", ex);
if (ex.getMessage() == null) {
errorMessage = ex.toString();
} else {
errorMessage = ex.getMessage();
}
}
}
@Override
public void update() {
// No error message. Report success:
if (errorMessage == null) {
frame.output(Localization.lang("%0 export successful", format.getDisplayName()));
} else // ... or show an error dialog:
{
frame.output(Localization.lang("Could not save file.") + " - " + errorMessage);
// Need to warn the user that saving failed!
JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + "\n" + errorMessage, Localization.lang("Save library"), JOptionPane.ERROR_MESSAGE);
}
}
};
// Run the export action in a background thread:
exportWorker.getWorker().run();
// Run the update method:
exportWorker.update();
}
}
}
return new InternalExportAction(frame, selectedOnly);
}
use of javax.swing.filechooser.FileFilter in project jabref by JabRef.
the class ExportAction method createExportFileChooser.
private static JFileChooser createExportFileChooser(String currentDir) {
String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_EXPORT);
FileFilter defaultFilter = null;
JFileChooser fc = new JFileChooser(currentDir);
Set<FileFilter> filters = new TreeSet<>();
for (Map.Entry<String, IExportFormat> e : ExportFormats.getExportFormats().entrySet()) {
String formatName = e.getKey();
IExportFormat format = e.getValue();
ExportFileFilter exportFileFilter = new ExportFileFilter(format);
filters.add(exportFileFilter);
if (formatName.equals(lastUsedFormat)) {
defaultFilter = exportFileFilter;
}
}
for (FileFilter ff : filters) {
fc.addChoosableFileFilter(ff);
}
fc.setAcceptAllFileFilterUsed(false);
if (defaultFilter != null) {
fc.setFileFilter(defaultFilter);
}
return fc;
}
use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class DiceBagPluginController method chooseSaveFile.
/**
* <p>
* This static method opens a file save chooser and returns the chosen file.
* </p>
*
* @param bag
* Bag to save
* @return Returns the file to save to
*/
static File chooseSaveFile(DiceBagModel bag) {
File returnValue = null;
JFileChooser save = new JFileChooser();
String fileExt = "dbg";
FileFilter ff = new FileNameExtensionFilter("GMGen Dice Bag", fileExt);
save.addChoosableFileFilter(ff);
save.setFileFilter(ff);
if (bag.getFilePath() != null) {
save.setSelectedFile(new File(bag.getFilePath()));
} else {
String sFile = SettingsHandler.getGMGenOption(DiceBagPlugin.LOG_NAME + ".LastFile", System.getProperty("user.dir"));
save.setCurrentDirectory(new File(sFile));
}
if (save.showSaveDialog(GMGenSystem.inst) == JFileChooser.APPROVE_OPTION) {
SettingsHandler.setGMGenOption(DiceBagPlugin.LOG_NAME + ".LastFile", save.getSelectedFile().getParent());
String fileName = save.getSelectedFile().getName();
String dirName = save.getSelectedFile().getParent();
String ext = "";
if (!fileName.contains(".dbg")) {
ext = ".dbg";
}
returnValue = new File(dirName + File.separator + fileName + ext);
}
return returnValue;
}
Aggregations