aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/console/GUIConsole.java
blob: de4b378cab34b4df9329b56f9d04b25c53ad11a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;

public class GUIConsole extends AnchorPane implements Console {
	
	private TextArea textArea = new TextArea("Welcome to JCGP!\n");
	
	private StringBuffer printBuffer = new StringBuffer();
		
	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.
		 */
		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));
		
		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();
	}
	
}