package jcgp.gui.console; import javafx.event.EventHandler; import javafx.scene.Cursor; import javafx.scene.control.TextArea; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import jcgp.gui.GUI; public class GUIConsole extends AnchorPane implements Console { private boolean dragging; private TextArea textArea = new TextArea("Welcome to JCGP!"); private StringBuffer printBuffer = new StringBuffer(); public GUIConsole() { super(); textArea.setEditable(false); setResizeListeners(); AnchorPane.setTopAnchor(textArea, GUI.RESIZE_MARGIN); AnchorPane.setBottomAnchor(textArea, 0.0); AnchorPane.setRightAnchor(textArea, 0.0); AnchorPane.setLeftAnchor(textArea, 0.0); setMinHeight(GUI.CONSOLE_HEIGHT); setPrefHeight(GUI.CONSOLE_HEIGHT); getChildren().add(textArea); } /** * */ private void setResizeListeners() { setOnMousePressed(new EventHandler() { @Override public void handle(MouseEvent event) { // ignore clicks outside of the draggable margin if(isInDraggableZone(event)) { dragging = true; } } }); setOnMouseDragged(new EventHandler() { @Override public void handle(MouseEvent event) { if(dragging) { double newHeight = getHeight() - event.getY(); if (newHeight >= getMinHeight()) { setPrefHeight(newHeight); } else { setPrefHeight(getMinHeight()); } } } }); setOnMouseMoved(new EventHandler() { @Override public void handle(MouseEvent event) { if(isInDraggableZone(event) || dragging) { setCursor(Cursor.V_RESIZE); } else { setCursor(Cursor.DEFAULT); } } }); setOnMouseReleased(new EventHandler() { @Override public void handle(MouseEvent event) { dragging = false; setCursor(Cursor.DEFAULT); } }); textArea.setOnMouseEntered(new EventHandler() { @Override public void handle(MouseEvent event) { setCursor(Cursor.DEFAULT); } }); } private boolean isInDraggableZone(MouseEvent event) { return event.getY() < (GUI.RESIZE_MARGIN); } @Override public void println(String s) { printBuffer.append("\n" + s); } @Override public void print(String s) { printBuffer.append(s); } @Override public void flush() { textArea.appendText(printBuffer.toString()); printBuffer = new StringBuffer(); } }