From ffd90a7a8352a3899e3d85de69fc6dd803c90e35 Mon Sep 17 00:00:00 2001 From: ishmal Date: Tue, 1 Apr 2008 17:25:33 +0000 Subject: [PATCH] Improve 'dirty' editor handling --- src/bind/java/org/inkscape/script/Editor.java | 180 +++++++++++++++++- .../org/inkscape/script/ScriptConsole.java | 89 +-------- 2 files changed, 179 insertions(+), 90 deletions(-) diff --git a/src/bind/java/org/inkscape/script/Editor.java b/src/bind/java/org/inkscape/script/Editor.java index ac1c49aaf..81d65f4cf 100644 --- a/src/bind/java/org/inkscape/script/Editor.java +++ b/src/bind/java/org/inkscape/script/Editor.java @@ -25,12 +25,20 @@ package org.inkscape.script; - -import javax.swing.JPanel; -import javax.swing.JTextPane; import java.awt.BorderLayout; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextPane; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + /** @@ -59,6 +67,28 @@ void trace(String fmt, Object... arguments) parent.trace("Editor:" + fmt, arguments); } + +//######################################################################## +//# U T I L I T Y +//######################################################################## + +private JFileChooser _chooser; + +JFileChooser getChooser() +{ + if (_chooser == null) + { + _chooser = new JFileChooser(); + _chooser.setAcceptAllFileFilterUsed(false); + _chooser.setCurrentDirectory(new File(".")); + FileNameExtensionFilter filter = new FileNameExtensionFilter( + "Script Files", "js", "py", "r"); + _chooser.setFileFilter(filter); + } + return _chooser; +} + + /** * Returns the current text contained in this editor */ @@ -113,18 +143,158 @@ String getHash(String text) return toHex(hash); } + +//######################################################################## +//# L O A D / S A V E +//######################################################################## +String fileName = ""; + +/** + * Gets the name of the current file in the editor + */ +public String getFileName() +{ + return fileName; +} + +/** + * Sets the name of the current file in the editor + */ +public void setFileName(String val) +{ + fileName = val; +} + +/** + * Selects and opens a file, loading into the editor + */ +public boolean openFile() +{ + JFileChooser chooser = getChooser(); + int ret = chooser.showOpenDialog(this); + if (ret != JFileChooser.APPROVE_OPTION) + return false; + File f = chooser.getSelectedFile(); + String fname = f.getName(); + try + { + FileReader in = new FileReader(fname); + StringBuffer buf = new StringBuffer(); + while (true) + { + int ch = in.read(); + if (ch < 0) + break; + buf.append((char)ch); + } + in.close(); + setText(buf.toString()); + } + catch (IOException e) + { + err("save file:" + e); + return false; + } + return true; +} + + +/** + * Saves the file currently in the editor. Uses the Save + * selector if there is not current file name. + */ +public boolean saveFile() +{ + if (!isDirty()) + return true; + + String fname = getFileName(); + if (fname == null || fname.length()==0) + { + JFileChooser chooser = getChooser(); + int ret = chooser.showSaveDialog(this); + if (ret != JFileChooser.APPROVE_OPTION) + return false; + File f = chooser.getSelectedFile(); + fname = f.getName(); + } + try + { + FileWriter out = new FileWriter(fname); + out.write(getText()); + out.close(); + setFileName(fname); + resetDirty(); + } + catch (IOException e) + { + err("save file:" + e); + return false; + } + return true; +} + + +/** + * Saves the file currently in the editor under a new name. + * Get the new name from the chooser, and see if it already exists. + */ +public boolean saveAsFile() +{ + JFileChooser chooser = getChooser(); + int ret = chooser.showSaveDialog(this); + if (ret != JFileChooser.APPROVE_OPTION) + return false; + File f = chooser.getSelectedFile(); + String fname = f.getName(); + if (f.exists()) + { + ret = JOptionPane.showConfirmDialog(this, + "File '" + fname + "' already exists. Overwrite?"); + if (ret != JOptionPane.YES_OPTION) + return false; + } + try + { + FileWriter out = new FileWriter(fname); + out.write(getText()); + out.close(); + setFileName(fname); + resetDirty(); + } + catch (IOException e) + { + err("saveAs file:" + e); + return false; + } + return true; +} + + +/** + * State that the editor is now 'unedited' + */ +public void resetDirty() +{ + lastHash = getHash(getText()); +} + +/** + * Determines if the editor has been edited since the last open/save + */ public boolean isDirty() { String txt = getText(); String hash = getHash(txt); - if (lastHash != null && !lastHash.equals(hash)) + if ( (lastHash == null && txt.length()>0) || + (lastHash != null && !lastHash.equals(hash)) ) return true; return false; } /** - * + * Creates the editor for the ScriptConsole */ public Editor(ScriptConsole par) { diff --git a/src/bind/java/org/inkscape/script/ScriptConsole.java b/src/bind/java/org/inkscape/script/ScriptConsole.java index 6f95ddf50..241a95fcb 100644 --- a/src/bind/java/org/inkscape/script/ScriptConsole.java +++ b/src/bind/java/org/inkscape/script/ScriptConsole.java @@ -37,10 +37,8 @@ import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; -import javax.swing.JFileChooser; import javax.swing.JTabbedPane; import javax.swing.JToolBar; -import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.Action; import javax.swing.AbstractAction; @@ -54,6 +52,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; + import java.util.List; import java.util.HashMap; @@ -101,26 +100,6 @@ void alert(String msg) } -//######################################################################## -//# U T I L I T Y -//######################################################################## - -private JFileChooser _chooser; - -JFileChooser getChooser() -{ - if (_chooser == null) - { - _chooser = new JFileChooser(); - _chooser.setAcceptAllFileFilterUsed(false); - _chooser.setCurrentDirectory(new File(".")); - FileNameExtensionFilter filter = new FileNameExtensionFilter( - "Script Files", "js", "py", "r"); - _chooser.setFileFilter(filter); - } - return _chooser; -} - //######################################################################## @@ -407,30 +386,7 @@ class OpenAction extends AbstractAction public void actionPerformed(ActionEvent evt) { - JFileChooser chooser = getChooser(); - int ret = chooser.showOpenDialog(ScriptConsole.this); - if (ret != JFileChooser.APPROVE_OPTION) - return; - File f = chooser.getSelectedFile(); - String fname = f.getName(); - try - { - FileReader in = new FileReader(fname); - StringBuffer buf = new StringBuffer(); - while (true) - { - int ch = in.read(); - if (ch < 0) - break; - buf.append((char)ch); - } - in.close(); - editor.setText(buf.toString()); - } - catch (IOException e) - { - err("save file:" + e); - } + editor.openFile(); } public OpenAction() @@ -485,22 +441,7 @@ class SaveAction extends AbstractAction public void actionPerformed(ActionEvent evt) { - JFileChooser chooser = getChooser(); - int ret = chooser.showSaveDialog(ScriptConsole.this); - if (ret != JFileChooser.APPROVE_OPTION) - return; - File f = chooser.getSelectedFile(); - String fname = f.getName(); - try - { - FileWriter out = new FileWriter(fname); - out.write(editor.getText()); - out.close(); - } - catch (IOException e) - { - err("save file:" + e); - } + editor.saveFile(); } public SaveAction() @@ -518,29 +459,7 @@ class SaveAsAction extends AbstractAction public void actionPerformed(ActionEvent evt) { - JFileChooser chooser = getChooser(); - int ret = chooser.showSaveDialog(ScriptConsole.this); - if (ret != JFileChooser.APPROVE_OPTION) - return; - File f = chooser.getSelectedFile(); - String fname = f.getName(); - if (f.exists()) - { - ret = JOptionPane.showConfirmDialog(ScriptConsole.this, - "File '" + fname + "' already exists. Overwrite?"); - if (ret != JOptionPane.YES_OPTION) - return; - } - try - { - FileWriter out = new FileWriter(fname); - out.write(editor.getText()); - out.close(); - } - catch (IOException e) - { - err("saveAs file:" + e); - } + editor.saveAsFile(); } public SaveAsAction() -- 2.30.2