Code

Improve file handling
[inkscape.git] / src / bind / java / org / inkscape / script / Editor.java
1 /**
2  * This is a simple mechanism to bind Inkscape to Java, and thence
3  * to all of the nice things that can be layered upon that.
4  *
5  * Authors:
6  *   Bob Jamison
7  *
8  * Copyright (C) 2007-2008 Bob Jamison
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 3 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  */
25 package org.inkscape.script;
29 import javax.swing.JPanel;
30 import javax.swing.JTextPane;
31 import java.awt.BorderLayout;
32 import java.security.MessageDigest;
33 import java.security.NoSuchAlgorithmException;
36 /**
37  * A simple script editor for quick fixes.
38  */
39 public class Editor extends JPanel
40 {
41 ScriptConsole parent;
42 JTextPane textPane;
44 //########################################################################
45 //# MESSSAGES
46 //########################################################################
47 void err(String fmt, Object... arguments)
48 {
49     parent.err("Editor err:" + fmt, arguments);
50 }
52 void msg(String fmt, Object... arguments)
53 {
54     parent.msg("Editor:" + fmt, arguments);
55 }
57 void trace(String fmt, Object... arguments)
58 {
59     parent.trace("Editor:" + fmt, arguments);
60 }
62 /**
63  * Returns the current text contained in this editor
64  */
65 public String getText()
66 {
67     return textPane.getText();
68 }
71 String lastHash = null;
73 /**
74  *  Sets the text of this editor
75  */
76 public void setText(String txt)
77 {
78     textPane.setText(txt);
79     lastHash = getHash(txt);
80     trace("hash:" + lastHash);
81 }
83 MessageDigest md = null;
85 final String hex = "0123456789abcdef";
87 String toHex(byte arr[])
88 {
89     StringBuffer buf = new StringBuffer();
90     for (byte b : arr)
91         {
92         buf.append(hex.charAt((b>>4) & 15));
93         buf.append(hex.charAt((b   ) & 15));
94                 }
95         return buf.toString();
96 }
98 String getHash(String text)
99 {
100     if (md == null)
101         {
102         try
103             {
104             md = MessageDigest.getInstance("MD5");
105                         }
106                 catch (NoSuchAlgorithmException e)
107                     {
108                     err("getHash: " + e);
109                         return "";
110                         }
111                 }
112     byte hash[] = md.digest(text.getBytes());
113     return toHex(hash);
116 public boolean isDirty()
118     String txt = getText();
119     String hash = getHash(txt);
120     if (lastHash != null && !lastHash.equals(hash))
121         return true;
122     return false;
126 /**
127  *
128  */
129 public Editor(ScriptConsole par)
131     super();
132     parent = par;
133     setLayout(new BorderLayout());
134     textPane = new JTextPane();
135     add(textPane, BorderLayout.CENTER);