1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.tools.jjs;
27
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Set;
31 import java.util.function.Consumer;
32 import jdk.nashorn.api.scripting.AbstractJSObject;
33 import jdk.nashorn.internal.runtime.JSType;
34 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
35
36 /*
37 * "edit" top level script function which shows an external Window
38 * for editing and evaluating scripts from it.
39 */
40 final class EditObject extends AbstractJSObject {
41 private static final Set<String> props;
42 static {
43 final HashSet<String> s = new HashSet<>();
44 s.add("editor");
45 props = Collections.unmodifiableSet(s);
46 }
47
48 private final Console console;
49 private final Consumer<String> errorHandler;
50 private final Consumer<String> evaluator;
51 private String editor;
52
99 }
100
101 @Override
102 public void accept(final String str) {
103 // ignore repeated save of the same code!
104 if (! str.equals(lastStr)) {
105 this.lastStr = str;
106 // evaluate the new code
107 evaluator.accept(str);
108 }
109 }
110 }
111
112 @Override
113 public Object call(final Object thiz, final Object... args) {
114 final String initText = args.length > 0? JSType.toString(args[0]) : "";
115 final SaveHandler saveHandler = new SaveHandler(initText);
116 if (editor != null && !editor.isEmpty()) {
117 ExternalEditor.edit(editor, errorHandler, initText, saveHandler, console);
118 } else if (! Main.HEADLESS) {
119 EditPad.edit(errorHandler, initText, saveHandler);
120 } else {
121 errorHandler.accept(Main.getMessage("no.editor"));
122 }
123 return UNDEFINED;
124 }
125
126 @Override
127 public boolean isFunction() {
128 return true;
129 }
130 }
|
1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.tools.jjs;
27
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Set;
31 import java.util.function.Consumer;
32 import java.util.ServiceLoader;
33 import jdk.nashorn.api.scripting.AbstractJSObject;
34 import jdk.internal.editor.spi.BuildInEditorProvider;
35 import jdk.nashorn.internal.runtime.JSType;
36 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
37
38 /*
39 * "edit" top level script function which shows an external Window
40 * for editing and evaluating scripts from it.
41 */
42 final class EditObject extends AbstractJSObject {
43 private static final Set<String> props;
44 static {
45 final HashSet<String> s = new HashSet<>();
46 s.add("editor");
47 props = Collections.unmodifiableSet(s);
48 }
49
50 private final Console console;
51 private final Consumer<String> errorHandler;
52 private final Consumer<String> evaluator;
53 private String editor;
54
101 }
102
103 @Override
104 public void accept(final String str) {
105 // ignore repeated save of the same code!
106 if (! str.equals(lastStr)) {
107 this.lastStr = str;
108 // evaluate the new code
109 evaluator.accept(str);
110 }
111 }
112 }
113
114 @Override
115 public Object call(final Object thiz, final Object... args) {
116 final String initText = args.length > 0? JSType.toString(args[0]) : "";
117 final SaveHandler saveHandler = new SaveHandler(initText);
118 if (editor != null && !editor.isEmpty()) {
119 ExternalEditor.edit(editor, errorHandler, initText, saveHandler, console);
120 } else if (! Main.HEADLESS) {
121 try {
122 ServiceLoader<BuildInEditorProvider> sl
123 = ServiceLoader.load(BuildInEditorProvider.class);
124 //find the highest ranking provider
125 BuildInEditorProvider provider = null;
126 for (BuildInEditorProvider p : sl){
127 if (provider == null || p.rank() > provider.rank()) {
128 provider = p;
129 }
130 }
131 if (provider != null) {
132 provider.edit(null, initText, saveHandler, errorHandler);
133 } else {
134 errorHandler.accept(Main.getMessage("jjs.err.no.builtin.editor"));
135 }
136 } catch (RuntimeException ex) {
137 errorHandler.accept(Main.getMessage("jjs.err.cant.launch.editor"));
138 }
139 } else {
140 errorHandler.accept(Main.getMessage("no.editor"));
141 }
142 return UNDEFINED;
143 }
144
145 @Override
146 public boolean isFunction() {
147 return true;
148 }
149 }
|