Search in sources :

Example 1 with SimpleFilenameFilter

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);
            }
        }
    }
}
Also used : UserPreferences(cbit.vcell.client.server.UserPreferences) SimpleFilenameFilter(org.vcell.util.SimpleFilenameFilter) Point(java.awt.Point) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) PropertyVetoException(java.beans.PropertyVetoException) UtilCancelException(org.vcell.util.UtilCancelException) ExpressionException(cbit.vcell.parser.ExpressionException) UserCancelException(org.vcell.util.UserCancelException) ClientServerManager(cbit.vcell.client.server.ClientServerManager) JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) VCFileChooser(org.vcell.util.gui.VCFileChooser) File(java.io.File)

Aggregations

ClientServerManager (cbit.vcell.client.server.ClientServerManager)1 UserPreferences (cbit.vcell.client.server.UserPreferences)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 Point (java.awt.Point)1 BufferedImage (java.awt.image.BufferedImage)1 PropertyVetoException (java.beans.PropertyVetoException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 JFileChooser (javax.swing.JFileChooser)1 SimpleFilenameFilter (org.vcell.util.SimpleFilenameFilter)1 UserCancelException (org.vcell.util.UserCancelException)1 UtilCancelException (org.vcell.util.UtilCancelException)1 VCFileChooser (org.vcell.util.gui.VCFileChooser)1