├── .gitignore ├── src └── main │ ├── resources │ └── info │ │ └── gianlucacosta │ │ └── omnieditor │ │ ├── actionIcons │ │ ├── about.png │ │ ├── copy.png │ │ ├── cut.png │ │ ├── new.png │ │ ├── open.png │ │ ├── paste.png │ │ ├── redo.png │ │ ├── save.png │ │ ├── start.png │ │ ├── stop.png │ │ ├── undo.png │ │ ├── saveAs.png │ │ └── onlineReference.png │ │ └── MainWindow.fxml │ └── scala │ └── info │ └── gianlucacosta │ └── omnieditor │ ├── Style.scala │ ├── OmniEditor.scala │ ├── OutputThread.scala │ ├── OmniWorkspace.scala │ ├── AppStrategy.scala │ ├── StyledCodeEditor.scala │ └── MainWindowController.scala ├── settings.gradle ├── mainIcon.svg ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /.gradle/ 2 | /.idea/ 3 | /build/ 4 | /*.iml 5 | 6 | /src/generated/ 7 | -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/about.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/about.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/copy.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/cut.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/new.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/open.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/paste.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/redo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/redo.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/save.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/start.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/stop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/stop.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/undo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/undo.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/saveAs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/saveAs.png -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/actionIcons/onlineReference.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/giancosta86/OmniEditor/HEAD/src/main/resources/info/gianlucacosta/omnieditor/actionIcons/onlineReference.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | rootProject.name = 'OmniEditor' 22 | 23 | -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/Style.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | private case class Style(cssClass: String, pattern: String) -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/OmniEditor.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | import javafx.fxml.FXMLLoader 24 | import javafx.scene.Scene 25 | import javafx.scene.layout.BorderPane 26 | import javafx.stage.Stage 27 | 28 | import scalafx.application.Platform 29 | 30 | case class OmniEditor(appStrategy: AppStrategy) { 31 | def start(primaryStage: Stage) { 32 | val loader = new FXMLLoader(getClass.getResource("MainWindow.fxml")) 33 | 34 | val root = loader.load[BorderPane]() 35 | val mainWindowController = loader.getController[MainWindowController]() 36 | 37 | val scene = new Scene(root) 38 | 39 | val syntaxCssUrl = appStrategy.syntaxCss 40 | scene.getStylesheets.add(syntaxCssUrl.toExternalForm) 41 | 42 | mainWindowController.init(primaryStage, appStrategy) 43 | 44 | Platform.runLater { 45 | primaryStage.setScene(scene) 46 | primaryStage.show() 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/OutputThread.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | import java.time.Duration 24 | 25 | import info.gianlucacosta.helios.concurrency.AtomicStringBuilder 26 | 27 | import scalafx.application.Platform 28 | 29 | private case class OutputThread( 30 | refreshRate: Duration, 31 | outputBuffer: AtomicStringBuilder, 32 | outputAction: String => Unit 33 | ) extends Thread { 34 | 35 | override def run(): Unit = { 36 | while (!Thread.interrupted) { 37 | { 38 | tryToOutput() 39 | try { 40 | Thread.sleep(refreshRate.toMillis) 41 | } 42 | catch { 43 | case e: InterruptedException => 44 | tryToOutput() 45 | return 46 | } 47 | } 48 | } 49 | } 50 | 51 | private def tryToOutput(): Unit = { 52 | val textToOutput = outputBuffer.extract() 53 | if (!textToOutput.isEmpty) { 54 | Platform.runLater { 55 | outputAction(textToOutput) 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/OmniWorkspace.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | import java.io.File 24 | import java.nio.file.Files 25 | import javafx.stage.Stage 26 | 27 | import info.gianlucacosta.helios.fx.workspace.Workspace 28 | 29 | import scalafx.stage.FileChooser 30 | 31 | private class OmniWorkspace(stage: Stage, documentFileChooser: FileChooser, codeEditor: StyledCodeEditor) extends Workspace(stage, documentFileChooser) { 32 | override def doNew(): Boolean = { 33 | codeEditor.clear() 34 | 35 | codeEditor.getUndoManager.forgetHistory() 36 | codeEditor.requestFocus() 37 | 38 | true 39 | } 40 | 41 | 42 | override def doOpen(sourceFile: File): Boolean = { 43 | val fileContent = new String( 44 | Files.readAllBytes(sourceFile.toPath), 45 | "utf-8" 46 | ) 47 | 48 | codeEditor.setText(fileContent) 49 | 50 | codeEditor.getUndoManager.forgetHistory() 51 | codeEditor.requestFocus() 52 | 53 | true 54 | } 55 | 56 | 57 | override def doSave(targetFile: File): Boolean = { 58 | Files.write( 59 | targetFile.toPath, 60 | codeEditor.getText.getBytes("utf-8") 61 | ) 62 | 63 | true 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /mainIcon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 22 | 25 | 29 | 33 | 37 | 41 | 42 | 53 | 54 | 73 | 75 | 76 | 78 | image/svg+xml 79 | 81 | 82 | 83 | 84 | 85 | 90 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/AppStrategy.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | import java.io.File 24 | import java.net.URL 25 | 26 | import info.gianlucacosta.helios.concurrency.AtomicStringBuilder 27 | 28 | import scalafx.stage.FileChooser 29 | 30 | /** 31 | * Strategy employed to define customized IDE behaviour 32 | */ 33 | trait AppStrategy { 34 | /** 35 | * Application title, shown in windows and dialogs. 36 | * 37 | * @return The application title 38 | */ 39 | def title: String 40 | 41 | /** 42 | * Creates the file chooser employed when opening and saving source files. 43 | * 44 | * @return The file chooser 45 | */ 46 | def createSourceFileChooser(): FileChooser 47 | 48 | /** 49 | * Creates the code editor for source files. 50 | * 51 | * @return The code editor 52 | */ 53 | def createCodeEditor(): StyledCodeEditor 54 | 55 | /** 56 | * Computes the actual file after the user has confirmed a file when saving the source code. 57 | * For example, the function can add a default file extension if the user did not. 58 | * 59 | * @param sourceFileChooser The file chooser used to save the file 60 | * @param selectedFile The file originally saved by the user 61 | * @return The actual target file 62 | */ 63 | def getSavedSourceFile(sourceFileChooser: FileChooser, selectedFile: File): File 64 | 65 | /** 66 | * Tells the IDE whether to show the "Settings" menu item 67 | * 68 | * @return true if the "Settings" menu item should be shown 69 | */ 70 | def settingsSupported: Boolean 71 | 72 | /** 73 | * Shows a settings dialog. Can do nothing if settingsSupported() returns false 74 | */ 75 | def showSettings(): Unit 76 | 77 | /** 78 | * Runs the current program, probably by employing a dedicated compiler/interpreter. 79 | *

80 | * Please, design such executor in order to support thread interruption 81 | * (that is, by checking for Thread.interrupted()), as the user might want to 82 | * stop the program, which consists in calling Thread.interrupt() on the thread 83 | * executing this run() method. 84 | * 85 | * Throws InterruptedException: such exception can be thrown by the custom language's 86 | * virtual machine upon receiving a Thread.interrupt() 87 | * 88 | * @param programCode The source code in the editor 89 | * @param outputBuffer The buffer to write output to 90 | */ 91 | @throws[InterruptedException] 92 | def run(programCode: String, outputBuffer: AtomicStringBuilder) 93 | 94 | /** 95 | * Retrieves the URL of the CSS file employed to style the code editor 96 | * 97 | * @return The CSS url 98 | */ 99 | def syntaxCss: URL 100 | 101 | /** 102 | * Shows online reference (for example, opens a web page) 103 | */ 104 | def showOnlineReference(): Unit 105 | 106 | /** 107 | * Shows the "About..." window 108 | */ 109 | def showAboutWindow(): Unit 110 | } 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OmniEditor 2 | 3 | *Ready-made ScalaFX IDE for custom languages* 4 | 5 | ## Introduction 6 | 7 | OmniEditor is a ScalaFX library providing a ready-made, user-friendly IDE for custom programming languages as well as an independent code editor with syntax highlighting. 8 | 9 | First of all, it supports the basic interactive activities provided by a document workspace: 10 | 11 | * New 12 | * Open 13 | * Save 14 | * Save as 15 | * Undo 16 | * Redo 17 | * Cut 18 | * Copy 19 | * Paste 20 | * Settings 21 | * Help 22 | * About 23 | 24 | In addition to this, OmniEditor enables *the execution of interpreters/compilers* on the code written in the text editor, allowing the user to cancel the process via the *Stop* button in the user interface (for example, in case of infinite loops). 25 | 26 | Finally, for Java/JavaFX development (without Scala), the 2.x series is still available on Hephaestus, its Gradle/Maven repository. 27 | 28 | 29 | ## Core classes 30 | 31 | ### OmniEditor 32 | 33 | The *OmniEditor* class provides a customizable IDE application packaged as a Scala class. 34 | 35 | To run it, just add a few lines to the *start* method of your JavaFX/ScalaFX application subclass: 36 | 37 | ```scala 38 | val omniEditor = new OmniEditor(appStrategy) 39 | omniEditor.start(primaryStage) 40 | ``` 41 | 42 | where *appStrategy* is an instance of a class implementing the *AppStrategy* interface defined by the library, and *primaryStage* is the parameter of the *start* method in your JavaFX/ScalaFX application. 43 | 44 | For further information, please refer to the Scaladoc documentation. 45 | 46 | 47 | ### StyledCodeEditor 48 | 49 | Stemming from the "JavaKeywords" example provided by [RichTextFX](https://github.com/TomasMikula/RichTextFX), this class is the core of OmniEditor and creates a code editor easily customizable via CSS. 50 | 51 | In particular: 52 | 53 | * you can define a chain of *regex patterns*, each describing which CSS class should be applied to its matching text 54 | 55 | * a shortcut method is defined to apply a CSS class to a set of tokens (especially keywords) 56 | 57 | * it supports *indented new lines*, to keep each new line aligned with the previous one whenever the user presses ENTER 58 | 59 | * it supports *dynamic tabs* - the insertion of a given number of space characters in lieu of \\t whenever the user presses TAB. 60 | 61 | 62 | Please, refer to its Scaladoc for more details. 63 | 64 | 65 | ## Requirements 66 | 67 | OmniEditor requires: 68 | 69 | * Java 8 Update 91 or later compatible 70 | 71 | * Scala 2.11.8 or later compatible 72 | 73 | 74 | ## Download 75 | 76 | For further information about downloading or referencing OmniEditor via Gradle or Maven, please visit [its page](https://bintray.com/giancosta86/Hephaestus/OmniEditor) on Hephaestus, its Gradle/Maven repository. 77 | 78 | 79 | ## OmniEditor in action 80 | 81 | The very first open source application employing OmniEditor is [Chronos IDE](https://github.com/giancosta86/Chronos-IDE), for the [Chronos programming language](https://github.com/giancosta86/Chronos). 82 | 83 | [![Chronos IDE - Screenshot](https://github.com/giancosta86/Chronos-IDE/blob/master/Screenshot.png)](https://github.com/giancosta86/Chronos-IDE) 84 | 85 | ## Special thanks 86 | 87 | * OmniEditor is based on [RichTextFX](https://github.com/TomasMikula/RichTextFX), and was created starting from its brilliant "JavaKeywords" demo 88 | 89 | 90 | * OmniEditor also employs [Helios-fx](https://github.com/giancosta86/Helios-fx), an open source ScalaFX library of shared utilities 91 | 92 | * The UI icons are taken from the elegant [Kids Icons](http://www.iconarchive.com/show/kids-icons-icons-by-everaldo.1.html) set, by [Everaldo Coelho](http://www.everaldo.com/). 93 | 94 | 95 | 96 | ### RichTextFX - License information 97 | 98 | >Copyright (c) 2013-2014, Tomas Mikula 99 | >All rights reserved. 100 | > 101 | >Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 102 | > 103 | >1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 104 | > 105 | >2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 106 | > 107 | >THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 108 | -------------------------------------------------------------------------------- /src/main/scala/info/gianlucacosta/omnieditor/StyledCodeEditor.scala: -------------------------------------------------------------------------------- 1 | /*§ 2 | =========================================================================== 3 | OmniEditor 4 | =========================================================================== 5 | Copyright (C) 2015-2016 Gianluca Costa 6 | =========================================================================== 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | =========================================================================== 19 | */ 20 | 21 | package info.gianlucacosta.omnieditor 22 | 23 | import java.time.Duration 24 | import java.util 25 | import java.util.concurrent.Semaphore 26 | import java.util.regex.Pattern 27 | import javafx.event.EventHandler 28 | import javafx.scene.input.{KeyCode, KeyEvent} 29 | 30 | import org.fxmisc.richtext.{CodeArea, LineNumberFactory, StyleSpans, StyleSpansBuilder} 31 | 32 | import scala.collection.JavaConversions._ 33 | import scalafx.application.Platform 34 | 35 | 36 | object StyledCodeEditor { 37 | private val LeadingSpacePattern = Pattern.compile(raw"^\s*") 38 | } 39 | 40 | /** 41 | * Code editor enabling easy pattern-based syntax highlighting. 42 | *

43 | * This class derives from and extends the JavaKeywords demo of the CodeArea control. 44 | */ 45 | class StyledCodeEditor(stylingSleepDuration: Duration = Duration.ofMillis(334)) extends CodeArea { 46 | private var syntaxPattern = Pattern.compile("") 47 | private var styles = Map[String, Style]() 48 | private var descriptorKeyCounter = 0 49 | 50 | private var tabReplacementString: Option[String] = None 51 | 52 | 53 | setParagraphGraphicFactory(LineNumberFactory.get(this)) 54 | 55 | 56 | new StyleDaemon(this, stylingSleepDuration) { 57 | start() 58 | } 59 | 60 | 61 | /** 62 | * Binds a pattern to a CSS class. 63 | *

64 | * Consequently, such class will style code portions matching the pattern. 65 | * Patterns are applied in registration (FIFO) order, stopping when a pattern matches. 66 | * 67 | * @param cssClass the CSS class used to style text matching the pattern 68 | * @param regexPattern a regular expression pattern used to match text in the editor 69 | */ 70 | def addPattern(cssClass: String, regexPattern: String) { 71 | val key = "S" + descriptorKeyCounter 72 | descriptorKeyCounter += 1 73 | 74 | val style = Style(cssClass, regexPattern) 75 | styles += (key -> style) 76 | 77 | updateSyntaxPattern() 78 | } 79 | 80 | 81 | /** 82 | * Simplified version of addPattern(), focusing on tokens (for example, keywords) 83 | * 84 | * @param cssClass the CSS class used to style the tokens 85 | * @param tokens the tokens to style 86 | */ 87 | def addTokens(cssClass: String, tokens: String*) { 88 | val tokensPattern = 89 | tokens 90 | .map(token => s"\\b${Pattern.quote(token)}\\b") 91 | .mkString("|") 92 | 93 | addPattern(cssClass, tokensPattern) 94 | } 95 | 96 | 97 | private def updateSyntaxPattern(): Unit = { 98 | val patternComponents = styles.map { case (key, style) => 99 | s"(?<${key}>${style.pattern})" 100 | } 101 | 102 | val patternString = 103 | patternComponents.mkString("|") 104 | 105 | syntaxPattern = Pattern.compile(patternString) 106 | } 107 | 108 | 109 | def setText(text: String) { 110 | clear() 111 | replaceText(0, 0, text) 112 | } 113 | 114 | /** 115 | * When the user presses ENTER, initial space is added to the new line 116 | * to keep it aligned with the previous one. 117 | */ 118 | def enableIndentedNewline() { 119 | addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler[KeyEvent] { 120 | override def handle(keyEvent: KeyEvent): Unit = { 121 | keyEvent.getCode match { 122 | case KeyCode.ENTER => 123 | val currentPosition = getCaretPosition 124 | 125 | val previousText = getText.substring(0, currentPosition) 126 | val previousNewlineCharPosition = previousText.lastIndexOf('\n') 127 | 128 | val currentLine = previousText.substring(previousNewlineCharPosition + 1) 129 | 130 | val leadingSpaceMatcher = StyledCodeEditor.LeadingSpacePattern.matcher(currentLine) 131 | 132 | if (leadingSpaceMatcher.find()) { 133 | val leadingSpace = leadingSpaceMatcher.group 134 | 135 | keyEvent.consume() 136 | 137 | val totalSpaceToInsert = "\n" + leadingSpace 138 | insertText(currentPosition, totalSpaceToInsert) 139 | 140 | moveTo( 141 | currentPosition + totalSpaceToInsert.length 142 | ) 143 | } 144 | 145 | case _ => 146 | } 147 | } 148 | }) 149 | } 150 | 151 | /** 152 | * Pressing the "Tab" key will only add the "space" character, for the given number of times 153 | * 154 | * @param spaceCharacterCount The number of "space" characters to add in lieu of "\t" 155 | */ 156 | def enableDynamicTabs(spaceCharacterCount: Int) { 157 | require(spaceCharacterCount >= 0) 158 | 159 | require(tabReplacementString.isEmpty) 160 | 161 | tabReplacementString = Some(" " * spaceCharacterCount) 162 | 163 | addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler[KeyEvent] { 164 | override def handle(keyEvent: KeyEvent): Unit = { 165 | keyEvent.getCode match { 166 | case KeyCode.TAB => 167 | keyEvent.consume() 168 | 169 | val currentPosition = getCaretPosition 170 | 171 | insertText(currentPosition, tabReplacementString.get) 172 | 173 | moveTo( 174 | currentPosition + spaceCharacterCount 175 | ) 176 | 177 | case _ => 178 | } 179 | } 180 | }) 181 | } 182 | 183 | 184 | private var stylingEnabled: Boolean = true 185 | 186 | 187 | /** 188 | * Stops the background thread employed for styling the editor 189 | */ 190 | def stopStyling(): Unit = { 191 | stylingEnabled = false 192 | } 193 | 194 | 195 | private class StyleDaemon(codeEditor: StyledCodeEditor, sleepDuration: Duration) extends Thread { 196 | setDaemon(true) 197 | 198 | 199 | override def run(): Unit = { 200 | var text = "" 201 | var latestStyledText = "" 202 | 203 | val guiSemaphore = new Semaphore(0) 204 | 205 | 206 | while (codeEditor.stylingEnabled) { 207 | Platform.runLater { 208 | text = 209 | codeEditor.getText() 210 | 211 | guiSemaphore.release() 212 | } 213 | 214 | 215 | guiSemaphore.acquire() 216 | 217 | 218 | if (text != latestStyledText) { 219 | val styleSpans = 220 | computeHighlighting(text) 221 | 222 | Platform.runLater { 223 | try { 224 | codeEditor.setStyleSpans(0, styleSpans) 225 | latestStyledText = text 226 | } catch { 227 | case _: Exception => 228 | //Just do nothing 229 | } 230 | 231 | guiSemaphore.release() 232 | } 233 | 234 | guiSemaphore.acquire() 235 | } 236 | 237 | 238 | Thread.sleep(sleepDuration.toMillis) 239 | } 240 | } 241 | 242 | 243 | private def computeHighlighting(text: String): StyleSpans[util.Collection[String]] = { 244 | val spansBuilder = new StyleSpansBuilder[util.Collection[String]] 245 | 246 | val matcher = syntaxPattern.matcher(text) 247 | var latestMatchEndPosition = 0 248 | 249 | while (matcher.find()) { 250 | val styleKey = 251 | styles 252 | .keySet 253 | .filter(key => matcher.group(key) != null) 254 | .head 255 | 256 | val styleClass = styles(styleKey).cssClass 257 | 258 | spansBuilder.add(Seq(), matcher.start() - latestMatchEndPosition) 259 | spansBuilder.add(Seq(styleClass), matcher.end() - matcher.start()) 260 | latestMatchEndPosition = matcher.end() 261 | } 262 | spansBuilder.add(Seq(), text.length() - latestMatchEndPosition) 263 | spansBuilder.create() 264 | } 265 | } 266 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/main/resources/info/gianlucacosta/omnieditor/MainWindow.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 |

10 | 12 | 13 | 14 | 15 | 18 | 19 | 20 | 21 | 22 |