OSDN Git Service

fix some mistakes (disability for function 'alert' and so on)
[sawarabi-fonts/sawarabi-fonts.git] / chartool / src / jp / sourceforge / sawarabifonts / chartool / ConfigScript.java
1 /*
2  * Copyright (C) 2010, mshio <mshio@users.sourceforge.jp>
3  *
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ---
17  * Require JDK 1.5 (or later)
18  */
19 package jp.sourceforge.sawarabifonts.chartool;
20
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.logging.Logger;
25
26 import javax.swing.JFrame;
27
28 import org.mozilla.javascript.Context;
29 import org.mozilla.javascript.EvaluatorException;
30 import org.mozilla.javascript.Function;
31 import org.mozilla.javascript.ScriptableObject;
32
33 public class ConfigScript {
34         private final static String SCRIPT_FILE = "js/charpalette.js";
35         private final static ConfigScript instance = new ConfigScript();
36         private JsScriptableObject scriptable = null;
37         private long lastModified = -1;
38
39         private ConfigScript() { }
40
41         public static ConfigScript getInstance() {
42                 return instance;
43         }
44
45         public void load() throws IOException {
46                 load(new File(SCRIPT_FILE));
47         }
48
49         public void load(File f) throws IOException {
50                 FileReader in = new FileReader(f);
51                 Context cx = Context.enter();
52                 JFrame w = (scriptable == null) ? null : scriptable.getFrame();
53                 scriptable = new JsScriptableObject();
54                 if (w != null) scriptable.setFrame(w);
55                 cx.initStandardObjects(scriptable);
56                 setupVariables();
57                 try {
58                         cx.evaluateReader(scriptable, in, "<config>", 0, null);
59                         lastModified = f.lastModified();
60                 } finally {
61                         Context.exit();
62                 }
63         }
64
65         public boolean isLoaded() {
66                 return scriptable != null && checkModified(SCRIPT_FILE);
67         }
68
69         public String[] getFontNames() {
70                 String[] ret = null;
71                 if (scriptable.has("fonts", scriptable)) {
72                         Object o = scriptable.get("fonts", scriptable);
73                         try {
74                                 ret = (String[]) Context.jsToJava(o, String[].class);
75                         } catch (EvaluatorException e) {
76                                 e.printStackTrace();
77                                 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
78                         }
79                 }
80                 return ret;
81         }
82
83         private Function[] getScriptFunction() {
84                 final String NAME = "script";
85                 Function[] ret = null;
86                 if (scriptable.has(NAME, scriptable)) {
87                         Object o = scriptable.get(NAME, scriptable);
88                         try {
89                                 ret = (Function[]) Context.jsToJava(o, Function[].class);
90                         } catch (EvaluatorException e) {
91                                 e.printStackTrace();
92                                 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
93                         }
94                 }
95                 return ret;
96         }
97
98         public void execScript(int id, String property, int modifier) throws Exception {
99                 Function[] fs = getScriptFunction();
100                 if (fs == null || fs.length <= id) { return ; }
101
102                 Context cx = Context.enter();
103 //              Class<?> clazz = org.jdesktop.jdic.desktop.Desktop.class;
104 //              cx.setApplicationClassLoader(clazz.getClassLoader());
105                 try {
106                         fs[id].call(cx, scriptable, scriptable, new Object[] {property, modifier});
107                 } finally {
108                         Context.exit();
109                 }
110         }
111
112         private void setupVariables() {
113                 final int RO = ScriptableObject.READONLY;
114                 final int DE = ScriptableObject.DONTENUM;
115                 String home = System.getProperty("user.home");
116                 ScriptableObject.defineProperty(scriptable, "HOME", home, RO);
117
118                 String[] fs = new String[] { "alert" };
119                 scriptable.defineFunctionProperties(fs, JsScriptableObject.class, DE);
120         }
121
122         public void setFrame(JFrame frame) {
123                 scriptable.setFrame(frame);
124         }
125
126         private boolean checkModified(String path) {
127                 File f = new File(path);
128                 return lastModified >= f.lastModified();
129         }
130 }