aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/console/ConsolePane.java
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2015-03-09 17:03:48 -0300
committerEduardo Pedroni <e.pedroni91@gmail.com>2015-03-09 17:03:48 -0300
commitd69fa8746728367646494fd8c2c18944f306c6a2 (patch)
treef063f9efc6c93a5520991f509f3a481543b73a91 /src/jcgp/gui/console/ConsolePane.java
parent9062115b7d15cb05552632dc5486a5cd15a45289 (diff)
Added existing source code
Diffstat (limited to 'src/jcgp/gui/console/ConsolePane.java')
-rw-r--r--src/jcgp/gui/console/ConsolePane.java122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/jcgp/gui/console/ConsolePane.java b/src/jcgp/gui/console/ConsolePane.java
new file mode 100644
index 0000000..de193a5
--- /dev/null
+++ b/src/jcgp/gui/console/ConsolePane.java
@@ -0,0 +1,122 @@
+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.constants.Constants;
+
+/**
+ * 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<ActionEvent>() {
+ @Override
+ public void handle(ActionEvent event) {
+ textArea.copy();
+ }
+ });
+ MenuItem selectAll = new MenuItem("Select all");
+ selectAll.setOnAction(new EventHandler<ActionEvent>() {
+ @Override
+ public void handle(ActionEvent event) {
+ textArea.selectAll();
+ }
+ });
+ MenuItem clearConsole = new MenuItem("Clear");
+ clearConsole.setOnAction(new EventHandler<ActionEvent>() {
+ @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, Constants.RESIZE_MARGIN);
+ AnchorPane.setBottomAnchor(textArea, 0.0);
+ AnchorPane.setRightAnchor(textArea, 0.0);
+ AnchorPane.setLeftAnchor(textArea, 0.0);
+
+ setMinHeight(Constants.CONSOLE_MIN_HEIGHT);
+ setPrefHeight(Constants.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();
+ }
+
+}