2021-01-18 9:00 JST 〜 メンテナンスによるサービス全停止予定
  • R/O
  • SSH
  • HTTPS

nine: コミット


コミットメタ情報

リビジョン69 (tree)
日時2010-08-07 10:09:48
作者mshio

ログメッセージ

brush up the logic of calling the javascript functions

変更サマリ

差分

--- nine/trunk/src/jp/sourceforge/nine/parser/PlainStringPainter.java (revision 68)
+++ nine/trunk/src/jp/sourceforge/nine/parser/PlainStringPainter.java (revision 69)
@@ -67,10 +67,10 @@
6767 for (Token<T> t : line) {
6868 Segment s = new Segment();
6969 int begin = t.getOffset() + offset;
70- int end = begin + t.getLength();
70+ int end = begin + t.length;
7171 if (begin < p0) { begin = p0; }
7272 if (end > p1) { end = p1; }
73- T type = t.getType();
73+ T type = t.type;
7474 if (begin < end) {
7575 doc.getText(begin, end - begin, s);
7676 S status = statusManager.getStatus();
@@ -106,7 +106,7 @@
106106 for (int r = n + 1; r <= row - 1; r++) {
107107 TokenLine<T, S> line = tokenList.get(r);
108108 for (Token<T> t : line) {
109- T type = t.getType();
109+ T type = t.type;
110110 statusManager.switchStatus(type);
111111 }
112112 statusManager.sendLineEnd();
--- nine/trunk/src/jp/sourceforge/nine/parser/DefaultTokenizer.java (revision 68)
+++ nine/trunk/src/jp/sourceforge/nine/parser/DefaultTokenizer.java (revision 69)
@@ -75,7 +75,7 @@
7575 private void setEndStatus(TokenLine<T, S> line, S preStatus) {
7676 statusManager.setCurrentStatus(preStatus);
7777 for (Token<T> token: line) {
78- statusManager.switchStatus(token.getType());
78+ statusManager.switchStatus(token.type);
7979 }
8080 statusManager.sendLineEnd();
8181 line.setEndStatus(statusManager.getStatus());
@@ -86,7 +86,7 @@
8686 public void setTokenIntoLine(T type, int offset, int length) {
8787 if (iterator.hasNext()) {
8888 Token<T> t = iterator.next();
89- if (t.getType() != type || t.getOffset() != offset || t.getLength() != length) {
89+ if (t.type != type || t.getOffset() != offset || t.length != length) {
9090 Token<T> token = new Token<T>(type, offset, length);
9191 iterator.set(token);
9292 }
--- nine/trunk/src/jp/sourceforge/nine/parser/Token.java (revision 68)
+++ nine/trunk/src/jp/sourceforge/nine/parser/Token.java (revision 69)
@@ -19,9 +19,9 @@
1919 package jp.sourceforge.nine.parser;
2020
2121 public class Token<E extends Enum<?>> {
22- private final E type;
22+ public final E type;
23+ public final int length;
2324 private int startOffset;
24- private final int length;
2525
2626 public Token(E type, int offset, int length) {
2727 this.type = type;
@@ -29,14 +29,6 @@
2929 this.length = length;
3030 }
3131
32- public E getType() {
33- return type;
34- }
35-
36- public int getLength() {
37- return length;
38- }
39-
4032 public int getOffset() {
4133 return startOffset;
4234 }
--- nine/trunk/src/jp/sourceforge/nine/action/ScriptableTextAction.java (revision 68)
+++ nine/trunk/src/jp/sourceforge/nine/action/ScriptableTextAction.java (revision 69)
@@ -1,5 +1,5 @@
11 /*
2- * Copyright (C) 2009, mshio <mshio@users.sourceforge.jp>
2+ * Copyright (C) 2009-2010, mshio <mshio@users.sourceforge.jp>
33 *
44 * This program is free software: you can redistribute it and/or
55 * modify it under the terms of the GNU General Public License
@@ -25,6 +25,8 @@
2525
2626 import org.mozilla.javascript.Context;
2727 import org.mozilla.javascript.ContextFactory;
28+import org.mozilla.javascript.Function;
29+import org.mozilla.javascript.NativeObject;
2830 import org.mozilla.javascript.ScriptableObject;
2931
3032 import jp.sourceforge.nine.js.NineJsContext;
@@ -32,11 +34,8 @@
3234
3335 public abstract class ScriptableTextAction extends TextAction {
3436
35- private static final long serialVersionUID = -314379406114319977L;
36- private final static String CHECK_FORMULA =
37- "eval(function() { for (k in interactives) {"
38- + "if (k =='%s') { return true } } return false })();";
39- private final static String SOURCE_FORMAT = "interactives['%s']();";
37+ private static final long serialVersionUID = 6206716440193262671L;
38+ private static NativeObject interactives = null;
4039
4140 private boolean restrainScript = false;
4241 private Boolean scriptExists = null;
@@ -57,37 +56,41 @@
5756 restrainScript = ! on;
5857 }
5958
59+ private NativeObject hasInteractives(NineScript script) {
60+ if (interactives != null) { return interactives; }
61+
62+ NativeObject ret = null;
63+ Object o = script.get("interactives", script);
64+ if (o != null && o instanceof NativeObject) {
65+ ret = (NativeObject) o;
66+ }
67+ return ret;
68+ }
69+
70+ private void setProperties(NineScript script, Context cx, ActionEvent e) {
71+ JTextComponent t = getTextComponent(e);
72+ script.setTextComponent(t);
73+ Object textComponent = Context.javaToJS(t, script);
74+ ScriptableObject.putProperty(script, "textComponent", textComponent);
75+ Object actionEvent = Context.javaToJS(e, script);
76+ ScriptableObject.putProperty(script, "actionEvent", actionEvent);
77+ }
78+
6079 private boolean performScript(ActionEvent e) {
6180 NineScript ns = NineJsContext.getInstance().getScript();
62- Context cx = null;
81+ NativeObject defs = hasInteractives(ns);
82+ String name = getValue(TextAction.NAME).toString();
6383 if (scriptExists == null) {
64- scriptExists = false;
65- cx = new ContextFactory().enterContext();
66- try {
67- String check = String.format(CHECK_FORMULA, getValue(TextAction.NAME));
68- Object ret =
69- cx.evaluateString(ns, check, "<cmd>", 0, null);
70- if (ret != null && Context.toBoolean(ret)) {
71- scriptExists = true;
72- }
73- } finally {
74- Context.exit();
75- }
84+ scriptExists = defs == null ? false : defs.has(name, ns);
7685 }
7786 if (! scriptExists) { return false; }
7887
79- cx = new ContextFactory().enterContext();
80- JTextComponent t = getTextComponent(e);
81- ns.setTextComponent(t);
82- Object textComponent = Context.javaToJS(t, ns);
83- ScriptableObject.putProperty(ns, "textComponent", textComponent);
84- Object actionEvent = Context.javaToJS(e, ns);
85- ScriptableObject.putProperty(ns, "actionEvent", actionEvent);
86- String source = String.format(SOURCE_FORMAT, getValue(TextAction.NAME));
87- try {
88- cx.evaluateString(ns, source, source, 0, null);
89- } finally {
90- Context.exit();
88+ Context cx = new ContextFactory().enterContext();
89+ setProperties(ns, cx, e);
90+
91+ Object o = defs.get(name, ns);
92+ if (o instanceof Function) {
93+ ((Function) o).call(cx, ns, ns, null);
9194 }
9295 return true;
9396 }
--- nine/trunk/src/jp/sourceforge/nine/action/ScriptTextAction.java (revision 68)
+++ nine/trunk/src/jp/sourceforge/nine/action/ScriptTextAction.java (revision 69)
@@ -1,5 +1,5 @@
11 /*
2- * Copyright (C) 2008-2009, mshio <mshio@users.sourceforge.jp>
2+ * Copyright (C) 2008-2010, mshio <mshio@users.sourceforge.jp>
33 *
44 * This program is free software: you can redistribute it and/or
55 * modify it under the terms of the GNU General Public License
@@ -28,12 +28,14 @@
2828
2929 import org.mozilla.javascript.Context;
3030 import org.mozilla.javascript.ContextFactory;
31+import org.mozilla.javascript.Function;
32+import org.mozilla.javascript.NativeObject;
3133 import org.mozilla.javascript.ScriptableObject;
3234
3335 public class ScriptTextAction extends TextAction {
3436
35- private static final long serialVersionUID = -2144133077216046322L;
36- private static final String SOURCE_FORMAT = "interactives['%s']();";
37+ private static final long serialVersionUID = -1543245006709785164L;
38+ private static NativeObject interactives = null;
3739
3840 public ScriptTextAction(String name) {
3941 super(name);
@@ -40,18 +42,34 @@
4042 }
4143
4244 public void actionPerformed(ActionEvent e) {
43- JTextComponent t = getTextComponent(e);
44-
4545 NineScript ns = NineJsContext.getInstance().getScript();
46- ns.setTextComponent(t);
4746
4847 Context cx = new ContextFactory().enterContext();
49- Object textComponent = Context.javaToJS(t, ns);
50- ScriptableObject.putProperty(ns, "textComponent", textComponent);
51- Object actionEvent = Context.javaToJS(e, ns);
52- ScriptableObject.putProperty(ns, "actionEvent", actionEvent);
53- String source = String.format(SOURCE_FORMAT, getValue(TextAction.NAME));
54- cx.evaluateString(ns, source, source, 0, null);
55- Context.exit();
48+ setProperties(ns, cx, e);
49+
50+ if (interactives == null) { setupInteractives(ns); }
51+ if (interactives != null) {
52+ String name = getValue(TextAction.NAME).toString();
53+ Object o = interactives.get(name, ns);
54+ if (o != null && o instanceof Function) {
55+ ((Function) o).call(cx, ns, ns, null);
56+ }
57+ }
5658 }
59+
60+ private void setProperties(NineScript script, Context cx, ActionEvent e) {
61+ JTextComponent t = getTextComponent(e);
62+ script.setTextComponent(t);
63+ Object jt = Context.javaToJS(t, script);
64+ ScriptableObject.putProperty(script, "textComponent", jt);
65+ Object je = Context.javaToJS(e, script);
66+ ScriptableObject.putProperty(script, "actionEvent", je);
67+ }
68+
69+ private void setupInteractives(NineScript script) {
70+ Object o = script.get("interactives", script);
71+ if (o instanceof NativeObject) {
72+ interactives = (NativeObject) o;
73+ }
74+ }
5775 }
旧リポジトリブラウザで表示