use of org.vcell.util.SimpleFilenameFilter in project vcell by virtualcell.
the class ReactionCartoonTool method showSaveReactionImageDialog.
// TO DO: allow user preferences for directory selection.
public void showSaveReactionImageDialog() throws Exception {
// set file filter
String result = DialogUtils.showInputDialog0(getDialogOwner(getGraphPane()), "Enter pixel width of saved rxdiagram image (height will be proportional)", "800");
int width = Integer.parseInt(result);
SimpleFilenameFilter jpgFilter = new SimpleFilenameFilter("jpg");
final java.io.File defaultFile = new java.io.File(getModel().getName() + ".jpg");
ClientServerManager csm = (ClientServerManager) getDocumentManager().getSessionManager();
UserPreferences userPref = csm.getUserPreferences();
File defaultPath = userPref.getCurrentDialogPath();
VCFileChooser fileChooser = new VCFileChooser(defaultPath);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.addChoosableFileFilter(jpgFilter);
fileChooser.setSelectedFile(defaultFile);
fileChooser.setDialogTitle("Save Image As...");
// name once the user changes the directory.
class FileChooserFix implements java.beans.PropertyChangeListener {
public void propertyChange(java.beans.PropertyChangeEvent ev) {
JFileChooser chooser = (JFileChooser) ev.getSource();
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(ev.getPropertyName())) {
chooser.setSelectedFile(defaultFile);
}
}
}
fileChooser.addPropertyChangeListener(new FileChooserFix());
// process user input
if (fileChooser.showSaveDialog(getDialogOwner(getGraphPane())) == JFileChooser.APPROVE_OPTION) {
java.io.File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
if (selectedFile.exists()) {
int question = javax.swing.JOptionPane.showConfirmDialog(getDialogOwner(getGraphPane()), "Overwrite file: " + selectedFile.getPath() + "?");
if (question == javax.swing.JOptionPane.NO_OPTION || question == javax.swing.JOptionPane.CANCEL_OPTION) {
return;
}
}
BufferedImage bufferedImage = ITextWriter.generateDocReactionsImage(this.getModel(), width);
FileOutputStream reactionImageOutputStream = null;
try {
reactionImageOutputStream = new FileOutputStream(selectedFile);
reactionImageOutputStream.write(ITextWriter.encodeJPEG(bufferedImage).toByteArray());
} finally {
try {
if (reactionImageOutputStream != null) {
reactionImageOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// reset the user preference for the default path, if needed.
File newPath = selectedFile.getParentFile();
if (!newPath.equals(defaultPath)) {
userPref.setCurrentDialogPath(newPath);
}
}
}
}
Aggregations