From c35a6806df01481c1b169cd0fc47660ea1cc10fb Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Wed, 14 May 2014 01:32:51 +0100 Subject: Refactored Mutable, commented a little bit of the GUI package --- src/jcgp/gui/console/ConsolePane.java | 123 ++++++++++++++++++++++++++++++++++ src/jcgp/gui/console/GUIConsole.java | 123 ---------------------------------- 2 files changed, 123 insertions(+), 123 deletions(-) create mode 100644 src/jcgp/gui/console/ConsolePane.java delete mode 100644 src/jcgp/gui/console/GUIConsole.java (limited to 'src/jcgp/gui/console') diff --git a/src/jcgp/gui/console/ConsolePane.java b/src/jcgp/gui/console/ConsolePane.java new file mode 100644 index 0000000..c4cfc21 --- /dev/null +++ b/src/jcgp/gui/console/ConsolePane.java @@ -0,0 +1,123 @@ +package jcgp.gui.console; + +import javafx.event.ActionEvent; +import javafx.event.Event; +import javafx.event.EventDispatchChain; +import javafx.event.EventDispatcher; +import javafx.event.EventHandler; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.MenuItem; +import javafx.scene.control.SeparatorMenuItem; +import javafx.scene.control.TextArea; +import javafx.scene.input.MouseButton; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.AnchorPane; +import jcgp.backend.resources.Console; +import jcgp.gui.GUI; + +/** + * Console pane used by the GUI to display CGP output messages. + * This class realises {@code Console}. It consists of a JavaFX + * {@code TextArea} and a {@code StringBuffer}. The buffer is filled + * as print messages are queued. Calling {@code flush()} writes the + * contents of the buffer to the {@code TextArea} and empties the buffer. + * + * @see Console + * @author Eduardo Pedroni + * + */ +public class ConsolePane extends AnchorPane implements Console { + + private TextArea textArea = new TextArea("Welcome to JCGP!\n"); + private StringBuffer printBuffer = new StringBuffer(); + + /** + * Creates a new instance of this class. + */ + public ConsolePane() { + super(); + textArea.setEditable(false); + + /* + * This nasty hack is needed because the default TextArea ContextMenu is not + * in the public API, making it impossible to override it with a custom one. + * This has not been fixed as of 8/4/2014. + * + * The following code modifies the EventDispatcher to consume the right mouse + * button click, preventing the default menu from appearing. It propagates the mouse + * click further so other elements will respond appropriately. + * + * TODO this should be refactored once the API is updated. + */ + final EventDispatcher initial = textArea.getEventDispatcher(); + textArea.setEventDispatcher(new EventDispatcher() { + @Override + public Event dispatchEvent(Event event, EventDispatchChain tail) { + if (event instanceof MouseEvent) { + MouseEvent mouseEvent = (MouseEvent)event; + if (mouseEvent.getButton() == MouseButton.SECONDARY || + (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.isControlDown())) { + event.consume(); + } + } + return initial.dispatchEvent(event, tail); + } + }); + + // make the new context menu including the clear option + MenuItem copySelected = new MenuItem("Copy"); + copySelected.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + textArea.copy(); + } + }); + MenuItem selectAll = new MenuItem("Select all"); + selectAll.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + textArea.selectAll(); + } + }); + MenuItem clearConsole = new MenuItem("Clear"); + clearConsole.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + textArea.setText(""); + } + }); + + textArea.setContextMenu(new ContextMenu(copySelected, + selectAll, + new SeparatorMenuItem(), + clearConsole)); + + // anchor the text area so it resizes automatically + AnchorPane.setTopAnchor(textArea, GUI.RESIZE_MARGIN); + AnchorPane.setBottomAnchor(textArea, 0.0); + AnchorPane.setRightAnchor(textArea, 0.0); + AnchorPane.setLeftAnchor(textArea, 0.0); + + setMinHeight(GUI.CONSOLE_MIN_HEIGHT); + setPrefHeight(GUI.CONSOLE_MIN_HEIGHT); + + getChildren().add(textArea); + } + + @Override + public void println(String s) { + printBuffer.append(s + "\n"); + } + + @Override + public void print(String s) { + printBuffer.append(s); + } + + @Override + public void flush() { + textArea.appendText(printBuffer.toString()); + printBuffer = new StringBuffer(); + } + +} diff --git a/src/jcgp/gui/console/GUIConsole.java b/src/jcgp/gui/console/GUIConsole.java deleted file mode 100644 index b3d037c..0000000 --- a/src/jcgp/gui/console/GUIConsole.java +++ /dev/null @@ -1,123 +0,0 @@ -package jcgp.gui.console; - -import javafx.event.ActionEvent; -import javafx.event.Event; -import javafx.event.EventDispatchChain; -import javafx.event.EventDispatcher; -import javafx.event.EventHandler; -import javafx.scene.control.ContextMenu; -import javafx.scene.control.MenuItem; -import javafx.scene.control.SeparatorMenuItem; -import javafx.scene.control.TextArea; -import javafx.scene.input.MouseButton; -import javafx.scene.input.MouseEvent; -import javafx.scene.layout.AnchorPane; -import jcgp.backend.resources.Console; -import jcgp.gui.GUI; - -/** - * Console pane used by the GUI to display CGP output messages. - * This class realises {@code Console}. It consists of a JavaFX - * {@code TextArea} and a {@code StringBuffer}. The buffer is filled - * as print messages are queued. Calling {@code flush()} writes the - * contents of the buffer to the {@code TextArea} and empties the buffer. - * - * @see Console - * @author Eduardo Pedroni - * - */ -public class GUIConsole extends AnchorPane implements Console { - - private TextArea textArea = new TextArea("Welcome to JCGP!\n"); - private StringBuffer printBuffer = new StringBuffer(); - - /** - * Creates a new instance of this class. - */ - public GUIConsole() { - super(); - textArea.setEditable(false); - - /* - * This nasty hack is needed because the default TextArea ContextMenu is not - * in the public API, making it impossible to override it with a custom one. - * This has not been fixed as of 8/4/2014. - * - * The following code modifies the EventDispatcher to consume the right mouse - * button click, preventing the default menu from appearing. It propagates the mouse - * click further so other elements will respond appropriately. - * - * TODO this should be refactored once the API is updated. - */ - final EventDispatcher initial = textArea.getEventDispatcher(); - textArea.setEventDispatcher(new EventDispatcher() { - @Override - public Event dispatchEvent(Event event, EventDispatchChain tail) { - if (event instanceof MouseEvent) { - MouseEvent mouseEvent = (MouseEvent)event; - if (mouseEvent.getButton() == MouseButton.SECONDARY || - (mouseEvent.getButton() == MouseButton.PRIMARY && mouseEvent.isControlDown())) { - event.consume(); - } - } - return initial.dispatchEvent(event, tail); - } - }); - - // make the new context menu including the clear option - MenuItem copySelected = new MenuItem("Copy"); - copySelected.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - textArea.copy(); - } - }); - MenuItem selectAll = new MenuItem("Select all"); - selectAll.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - textArea.selectAll(); - } - }); - MenuItem clearConsole = new MenuItem("Clear"); - clearConsole.setOnAction(new EventHandler() { - @Override - public void handle(ActionEvent event) { - textArea.setText(""); - } - }); - - textArea.setContextMenu(new ContextMenu(copySelected, - selectAll, - new SeparatorMenuItem(), - clearConsole)); - - // anchor the text area so it resizes automatically - AnchorPane.setTopAnchor(textArea, GUI.RESIZE_MARGIN); - AnchorPane.setBottomAnchor(textArea, 0.0); - AnchorPane.setRightAnchor(textArea, 0.0); - AnchorPane.setLeftAnchor(textArea, 0.0); - - setMinHeight(GUI.CONSOLE_MIN_HEIGHT); - setPrefHeight(GUI.CONSOLE_MIN_HEIGHT); - - getChildren().add(textArea); - } - - @Override - public void println(String s) { - printBuffer.append(s + "\n"); - } - - @Override - public void print(String s) { - printBuffer.append(s); - } - - @Override - public void flush() { - textArea.appendText(printBuffer.toString()); - printBuffer = new StringBuffer(); - } - -} -- cgit v1.2.3