2 * Copyright (C) 2010, mshio <mshio@users.sourceforge.jp>
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.
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.
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/>.
17 * Require JDK 1.5 (or later)
19 package jp.sourceforge.sawarabifonts.chartool;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.logging.Logger;
26 import javax.swing.JFrame;
28 import org.mozilla.javascript.Context;
29 import org.mozilla.javascript.EvaluatorException;
30 import org.mozilla.javascript.Function;
31 import org.mozilla.javascript.ScriptableObject;
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;
39 private ConfigScript() { }
41 public static ConfigScript getInstance() {
45 public void load() throws IOException {
46 load(new File(SCRIPT_FILE));
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);
58 cx.evaluateReader(scriptable, in, "<config>", 0, null);
59 lastModified = f.lastModified();
65 public boolean isLoaded() {
66 return scriptable != null && checkModified(SCRIPT_FILE);
69 public String[] getFontNames() {
71 if (scriptable.has("fonts", scriptable)) {
72 Object o = scriptable.get("fonts", scriptable);
74 ret = (String[]) Context.jsToJava(o, String[].class);
75 } catch (EvaluatorException e) {
77 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
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);
89 ret = (Function[]) Context.jsToJava(o, Function[].class);
90 } catch (EvaluatorException e) {
92 Logger.getLogger(Main.LOGNAME).warning(e.getMessage());
98 public void execScript(int id, String property, int modifier) throws Exception {
99 Function[] fs = getScriptFunction();
100 if (fs == null || fs.length <= id) { return ; }
102 Context cx = Context.enter();
103 // Class<?> clazz = org.jdesktop.jdic.desktop.Desktop.class;
104 // cx.setApplicationClassLoader(clazz.getClassLoader());
106 fs[id].call(cx, scriptable, scriptable, new Object[] {property, modifier});
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);
118 String[] fs = new String[] { "alert" };
119 scriptable.defineFunctionProperties(fs, JsScriptableObject.class, DE);
122 public void setFrame(JFrame frame) {
123 scriptable.setFrame(frame);
126 private boolean checkModified(String path) {
127 File f = new File(path);
128 return lastModified >= f.lastModified();