From: ishmal Date: Tue, 1 Apr 2008 16:55:51 +0000 (+0000) Subject: Improve file handling X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=aadffb0c53cb958cd70e6520b7c24acf5fc7d06f;p=inkscape.git Improve file handling --- diff --git a/src/bind/java/org/inkscape/cmn/Resource.java b/src/bind/java/org/inkscape/cmn/Resource.java index a5a58a777..3ad139d8a 100644 --- a/src/bind/java/org/inkscape/cmn/Resource.java +++ b/src/bind/java/org/inkscape/cmn/Resource.java @@ -1,4 +1,26 @@ - +/** + * This is a simple mechanism to bind Inkscape to Java, and thence + * to all of the nice things that can be layered upon that. + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2007-2008 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ package org.inkscape.cmn; diff --git a/src/bind/java/org/inkscape/script/Editor.java b/src/bind/java/org/inkscape/script/Editor.java index ba57465ca..ac1c49aaf 100644 --- a/src/bind/java/org/inkscape/script/Editor.java +++ b/src/bind/java/org/inkscape/script/Editor.java @@ -1,3 +1,27 @@ +/** + * This is a simple mechanism to bind Inkscape to Java, and thence + * to all of the nice things that can be layered upon that. + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2007-2008 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + package org.inkscape.script; @@ -5,17 +29,38 @@ package org.inkscape.script; import javax.swing.JPanel; import javax.swing.JTextPane; import java.awt.BorderLayout; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + /** * A simple script editor for quick fixes. */ public class Editor extends JPanel { +ScriptConsole parent; JTextPane textPane; +//######################################################################## +//# MESSSAGES +//######################################################################## +void err(String fmt, Object... arguments) +{ + parent.err("Editor err:" + fmt, arguments); +} + +void msg(String fmt, Object... arguments) +{ + parent.msg("Editor:" + fmt, arguments); +} + +void trace(String fmt, Object... arguments) +{ + parent.trace("Editor:" + fmt, arguments); +} /** - * + * Returns the current text contained in this editor */ public String getText() { @@ -23,21 +68,68 @@ public String getText() } +String lastHash = null; + /** - * + * Sets the text of this editor */ public void setText(String txt) { textPane.setText(txt); + lastHash = getHash(txt); + trace("hash:" + lastHash); +} + +MessageDigest md = null; + +final String hex = "0123456789abcdef"; + +String toHex(byte arr[]) +{ + StringBuffer buf = new StringBuffer(); + for (byte b : arr) + { + buf.append(hex.charAt((b>>4) & 15)); + buf.append(hex.charAt((b ) & 15)); + } + return buf.toString(); +} + +String getHash(String text) +{ + if (md == null) + { + try + { + md = MessageDigest.getInstance("MD5"); + } + catch (NoSuchAlgorithmException e) + { + err("getHash: " + e); + return ""; + } + } + byte hash[] = md.digest(text.getBytes()); + return toHex(hash); +} + +public boolean isDirty() +{ + String txt = getText(); + String hash = getHash(txt); + if (lastHash != null && !lastHash.equals(hash)) + return true; + return false; } /** * */ -public Editor() +public Editor(ScriptConsole par) { super(); + parent = par; setLayout(new BorderLayout()); textPane = new JTextPane(); add(textPane, BorderLayout.CENTER); diff --git a/src/bind/java/org/inkscape/script/ScriptConsole.java b/src/bind/java/org/inkscape/script/ScriptConsole.java index dc4b8dc34..6f95ddf50 100644 --- a/src/bind/java/org/inkscape/script/ScriptConsole.java +++ b/src/bind/java/org/inkscape/script/ScriptConsole.java @@ -1,4 +1,26 @@ - +/** + * This is a simple mechanism to bind Inkscape to Java, and thence + * to all of the nice things that can be layered upon that. + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2007-2008 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ package org.inkscape.script; @@ -29,6 +51,8 @@ import java.awt.BorderLayout; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.List; import java.util.HashMap; @@ -53,7 +77,7 @@ JMenuBar menubar; //######################################################################## void err(String fmt, Object... arguments) { - terminal.errorf("ScriptConsole err:" + fmt, arguments); + terminal.errorf("ScriptConsole err:" + fmt + "\n", arguments); } void msg(String fmt, Object... arguments) @@ -63,7 +87,7 @@ void msg(String fmt, Object... arguments) void trace(String fmt, Object... arguments) { - terminal.outputf("ScriptConsole:" + fmt, arguments); + terminal.outputf("ScriptConsole:" + fmt + "\n", arguments); } @@ -89,6 +113,7 @@ JFileChooser getChooser() { _chooser = new JFileChooser(); _chooser.setAcceptAllFileFilterUsed(false); + _chooser.setCurrentDirectory(new File(".")); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Script Files", "js", "py", "r"); _chooser.setFileFilter(filter); @@ -309,13 +334,13 @@ private void initScripts() String engVersion = factory.getEngineVersion(); String langName = factory.getLanguageName(); String langVersion = factory.getLanguageVersion(); - trace("\tScript Engine: %s (%s)\n", engName, engVersion); + trace("\tScript Engine: %s (%s)", engName, engVersion); List engNames = factory.getNames(); for(String name: engNames) { - trace("\tEngine Alias: %s\n", name); + trace("\tEngine Alias: %s", name); } - trace("\tLanguage: %s (%s)\n", langName, langVersion); + trace("\tLanguage: %s (%s)", langName, langVersion); ScriptEngineAction action = new ScriptEngineAction(factory); JRadioButtonMenuItem item = new JRadioButtonMenuItem(action); group.add(item); @@ -388,7 +413,24 @@ public void actionPerformed(ActionEvent evt) return; File f = chooser.getSelectedFile(); String fname = f.getName(); - alert("You selected : " + fname); + 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); + } } public OpenAction() @@ -449,7 +491,16 @@ public void actionPerformed(ActionEvent evt) return; File f = chooser.getSelectedFile(); String fname = f.getName(); - alert("You selected : " + fname); + try + { + FileWriter out = new FileWriter(fname); + out.write(editor.getText()); + out.close(); + } + catch (IOException e) + { + err("save file:" + e); + } } public SaveAction() @@ -473,7 +524,23 @@ public void actionPerformed(ActionEvent evt) return; File f = chooser.getSelectedFile(); String fname = f.getName(); - alert("You selected : " + fname); + 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); + } } public SaveAsAction() @@ -601,7 +668,7 @@ private boolean setup() terminal); terminal.output("\nscript> "); - editor = new Editor(); + editor = new Editor(this); tabPane.addTab("Script", Resource.getIcon("accessories-text-editor.png"), editor); diff --git a/src/bind/java/org/inkscape/script/Terminal.java b/src/bind/java/org/inkscape/script/Terminal.java index 054382482..22d2cb251 100644 --- a/src/bind/java/org/inkscape/script/Terminal.java +++ b/src/bind/java/org/inkscape/script/Terminal.java @@ -1,3 +1,27 @@ +/** + * This is a simple mechanism to bind Inkscape to Java, and thence + * to all of the nice things that can be layered upon that. + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2007-2008 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + package org.inkscape.script; import java.awt.BorderLayout;