2015-09-10 16:22:07 +00:00
|
|
|
/*
|
2020-07-03 05:21:55 +00:00
|
|
|
* Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
|
2015-09-10 16:22:07 +00:00
|
|
|
* (the "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
|
2020-07-03 05:21:55 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
|
|
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
|
|
* ANY KIND, either express or implied. See the License for the specific language
|
2015-09-10 16:22:07 +00:00
|
|
|
* governing permissions and limitations under the License.
|
|
|
|
*/
|
2014-05-25 05:22:26 +00:00
|
|
|
package org.badvision.outlaweditor.ui;
|
2014-03-25 05:01:01 +00:00
|
|
|
|
|
|
|
import java.net.URL;
|
2014-04-13 03:36:25 +00:00
|
|
|
import java.util.ListResourceBundle;
|
|
|
|
import java.util.Map;
|
2014-03-25 05:01:01 +00:00
|
|
|
import java.util.ResourceBundle;
|
2014-04-13 03:36:25 +00:00
|
|
|
import java.util.Set;
|
2020-07-03 05:21:55 +00:00
|
|
|
import javafx.application.Platform;
|
|
|
|
import javafx.beans.value.ObservableValue;
|
2014-04-04 04:19:59 +00:00
|
|
|
import javafx.concurrent.Worker.State;
|
2014-03-25 05:01:01 +00:00
|
|
|
import javafx.event.ActionEvent;
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.fxml.Initializable;
|
|
|
|
import javafx.scene.control.MenuItem;
|
2015-06-02 01:42:52 +00:00
|
|
|
import javafx.scene.web.PromptData;
|
2014-03-25 05:01:01 +00:00
|
|
|
import javafx.scene.web.WebView;
|
2020-07-03 05:21:55 +00:00
|
|
|
import javafx.stage.Stage;
|
2014-04-13 03:36:25 +00:00
|
|
|
import netscape.javascript.JSObject;
|
2014-05-25 05:22:26 +00:00
|
|
|
import org.badvision.outlaweditor.MythosEditor;
|
2014-03-25 05:01:01 +00:00
|
|
|
|
|
|
|
public class MythosScriptEditorController
|
2014-04-13 03:36:25 +00:00
|
|
|
implements Initializable {
|
|
|
|
|
2014-03-25 05:01:01 +00:00
|
|
|
public static final String MYTHOS_EDITOR = "/mythos/mythos-editor/html/editor.html";
|
2014-04-13 03:36:25 +00:00
|
|
|
public static final String ONLOAD_SCRIPT = "onloadScript";
|
|
|
|
// This is tied to the Mythos object defined in mythos_uncompressed
|
|
|
|
JSObject mythos;
|
2014-03-25 05:01:01 +00:00
|
|
|
|
|
|
|
@FXML // fx:id="editorView"
|
|
|
|
WebView editorView; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemAbortChanges"
|
|
|
|
private MenuItem menuItemAbortChanges; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemAboutBlockly"
|
|
|
|
private MenuItem menuItemAboutBlockly; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemApplyChanges"
|
|
|
|
private MenuItem menuItemApplyChanges; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemMythosHelp"
|
|
|
|
private MenuItem menuItemMythosHelp; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemRedo"
|
|
|
|
private MenuItem menuItemRedo; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
@FXML // fx:id="menuItemUndo"
|
|
|
|
private MenuItem menuItemUndo; // Value injected by FXMLLoader
|
|
|
|
|
|
|
|
MythosEditor editor;
|
2014-04-13 03:36:25 +00:00
|
|
|
|
2014-03-25 05:01:01 +00:00
|
|
|
public void setEditor(MythosEditor editor) {
|
|
|
|
this.editor = editor;
|
2014-04-13 03:36:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 05:01:01 +00:00
|
|
|
// Handler for MenuItem[fx:id="menuItemAbortChanges"] onAction
|
|
|
|
public void onAbortChangesSelected(ActionEvent event) {
|
|
|
|
editor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for MenuItem[fx:id="menuItemAboutBlockly"] onAction
|
|
|
|
public void onAboutBlocklySelected(ActionEvent event) {
|
|
|
|
// handle the event here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for MenuItem[fx:id="menuItemApplyChanges"] onAction
|
|
|
|
public void onApplyChangesSelected(ActionEvent event) {
|
|
|
|
editor.applyChanges();
|
|
|
|
editor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for MenuItem[fx:id="menuItemMythosHelp"] onAction
|
|
|
|
public void onMythosHelpSelected(ActionEvent event) {
|
|
|
|
// handle the event here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for MenuItem[fx:id="menuItemRedo"] onAction
|
|
|
|
public void onRedoSelected(ActionEvent event) {
|
|
|
|
// handle the event here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for MenuItem[fx:id="menuItemUndo"] onAction
|
|
|
|
public void onUndoSelected(ActionEvent event) {
|
|
|
|
// handle the event here
|
2014-04-13 03:36:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 05:01:01 +00:00
|
|
|
@Override // This method is called by the FXMLLoader when initialization is complete
|
|
|
|
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
|
|
|
|
assert editorView != null : "fx:id=\"editorView\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemAbortChanges != null : "fx:id=\"menuItemAbortChanges\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemAboutBlockly != null : "fx:id=\"menuItemAboutBlockly\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemApplyChanges != null : "fx:id=\"menuItemApplyChanges\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemMythosHelp != null : "fx:id=\"menuItemMythosHelp\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemRedo != null : "fx:id=\"menuItemRedo\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
|
|
|
assert menuItemUndo != null : "fx:id=\"menuItemUndo\" was not injected: check your FXML file 'MythosScriptEditor.fxml'.";
|
2014-04-13 03:36:25 +00:00
|
|
|
|
|
|
|
final String loadScript = resources.getString(ONLOAD_SCRIPT);
|
|
|
|
if (loadScript != null) {
|
|
|
|
editorView.getEngine().getLoadWorker().stateProperty().addListener(
|
2015-06-02 01:42:52 +00:00
|
|
|
(value, old, newState) -> {
|
|
|
|
if (newState == State.SUCCEEDED) {
|
|
|
|
mythos = (JSObject) editorView.getEngine().executeScript("Mythos");
|
|
|
|
mythos.setMember("editor", editor);
|
|
|
|
editorView.getEngine().executeScript(loadScript);
|
2022-11-30 15:33:59 +00:00
|
|
|
editorView.getEngine().executeScript("window.dispatchEvent(new Event('resize'));");
|
2014-04-13 03:36:25 +00:00
|
|
|
}
|
|
|
|
});
|
2020-07-03 05:21:55 +00:00
|
|
|
|
2015-06-02 01:42:52 +00:00
|
|
|
editorView.getEngine().setPromptHandler((PromptData prompt) -> {
|
2016-07-05 07:53:22 +00:00
|
|
|
return UIAction.getText(prompt.getMessage(), prompt.getDefaultValue());
|
2015-06-02 01:42:52 +00:00
|
|
|
});
|
2014-04-13 03:36:25 +00:00
|
|
|
}
|
2015-06-02 01:42:52 +00:00
|
|
|
|
2020-07-03 05:21:55 +00:00
|
|
|
// JavaFX8 has a bug where stage maximize events do not trigger resize events to webview components
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
Stage stage = (Stage) editorView.getScene().getWindow();
|
|
|
|
if (stage != null) {
|
|
|
|
stage.maximizedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
|
|
|
|
Platform.runLater(()->editorView.getEngine().executeScript("window.dispatchEvent(new Event('resize'));"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-01 23:43:51 +00:00
|
|
|
//TODO: Verify the path conversion works in Win7 with a jar file
|
2016-07-04 17:39:14 +00:00
|
|
|
// Affected by https://bugs.openjdk.java.net/browse/JDK-8136466
|
2014-04-13 03:36:25 +00:00
|
|
|
editorView.getEngine().load(getClass().getResource(MYTHOS_EDITOR).toExternalForm());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ResourceBundle createResourceBundle(final Map<String, String> input) {
|
|
|
|
return new ListResourceBundle() {
|
|
|
|
@Override
|
|
|
|
protected Object[][] getContents() {
|
|
|
|
Object[][] output = new Object[input.size()][2];
|
|
|
|
Set<String> keys = input.keySet();
|
|
|
|
int i = 0;
|
|
|
|
for (String key : keys) {
|
|
|
|
output[i] = new Object[]{key, input.get(key)};
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getScriptXml() {
|
|
|
|
return String.valueOf(editorView.getEngine().executeScript("Mythos.getScriptXml();"));
|
2014-03-25 05:01:01 +00:00
|
|
|
}
|
2020-07-03 05:21:55 +00:00
|
|
|
}
|