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.function.Consumer; 29 import jdk.nashorn.api.scripting.AbstractJSObject; 30 import jdk.nashorn.internal.runtime.JSType; 31 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 32 33 /* 34 * "edit" top level script function which shows an external Window 35 * for editing and evaluating scripts from it. 36 */ 37 final class EditObject extends AbstractJSObject { 38 private final Consumer<String> errorHandler; 39 private final Consumer<String> evaluator; 40 private final Console console; 41 private String editor; 42 43 EditObject(final Consumer<String> errorHandler, final Consumer<String> evaluator, 44 final Console console) { 45 this.errorHandler = errorHandler; 46 this.evaluator = evaluator; 47 this.console = console; 48 } 49 50 @Override 51 public Object getDefaultValue(final Class<?> hint) { 52 if (hint == String.class) { 53 return toString(); 54 } 55 return UNDEFINED; 56 } 57 58 @Override 59 public String toString() { 60 return "function edit() { [native code] }"; 61 } 62 63 @Override 64 public Object getMember(final String name) { 65 if (name.equals("editor")) { 66 return editor; 67 } 68 return UNDEFINED; 69 } 70 71 @Override 72 public void setMember(final String name, final Object value) { 73 if (name.equals("editor")) { 74 this.editor = JSType.toString(value); 75 } 76 } 77 78 // called whenever user 'saves' script in editor 79 class SaveHandler implements Consumer<String> { 80 private String lastStr; // last seen code 81 82 SaveHandler(final String str) { 83 this.lastStr = str; 84 } 85 86 @Override 87 public void accept(final String str) { 88 // ignore repeated save of the same code! 89 if (! str.equals(lastStr)) { 90 this.lastStr = str; 91 // evaluate the new code 92 evaluator.accept(str); 93 } 94 } 95 } 96 97 @Override 98 public Object call(final Object thiz, final Object... args) { 99 final String initText = args.length > 0? JSType.toString(args[0]) : ""; 100 final SaveHandler saveHandler = new SaveHandler(initText); 101 if (editor != null && !editor.isEmpty()) { 102 ExternalEditor.edit(editor, errorHandler, initText, saveHandler, console); 103 } else { 104 EditPad.edit(errorHandler, initText, saveHandler); 105 } 106 return UNDEFINED; 107 } 108 109 @Override 110 public boolean isFunction() { 111 return true; 112 } 113 }