-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
147 lines (126 loc) · 3.6 KB
/
Controller.java
File metadata and controls
147 lines (126 loc) · 3.6 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Controller implements Initializable {
@FXML
private Button btnBack;
@FXML
private Button btnForward;
@FXML
private Button btnRefresh;
@FXML
private Button btnSource;
@FXML
private Button btnZoomIn;
@FXML
private Button btnZoomOut;
@FXML
private TextField txtFieldUrl;
@FXML
private WebView webView;
private WebEngine engine;
private double size;
@FXML
void back() {
if (engine.getHistory().getCurrentIndex() > 0) {
engine.getHistory().go(-1);
}
}
@FXML
void forward() {
if (engine.getHistory().getCurrentIndex() + 1 < engine.getHistory().getEntries().size()) {
engine.getHistory().go(1);
}
}
@FXML
void refresh() {
engine.reload();
}
private void loadUrl() {
String URL = txtFieldUrl.getText();
if (!URL.contains(".")) {
webView.getEngine().load("https://www.google.com/search?q=" + URL);
return;
}
if (!URL.startsWith("http://") && !URL.startsWith("https://")) {
URL = "https://" + URL;
}
webView.getEngine().load(URL);
}
@FXML
void source() {
showSource("test", (String) engine.executeScript("document.documentElement.outerHTML"));
}
private void showSource(String title, String source) {
TextArea newRoot = new TextArea(source);
Scene newScene = new Scene(newRoot, 600, 400);
Stage newWindow = new Stage();
newWindow.setTitle(title);
newWindow.setScene(newScene);
newWindow.show();
}
@FXML
void goWeb(ActionEvent event) {
loadUrl();
}
@FXML
void zoomIn() {
size += 0.1;
if (size > 3)
size = 3;
webView.setZoom(size);
}
@FXML
void zoomOut() {
size -= 0.1;
if (size < 0.25)
size = 0.25;
webView.setZoom(size);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
size = 1.00;
engine = webView.getEngine();
String homePage = "https://www.google.com";
txtFieldUrl.setText(homePage);
engine.locationProperty().addListener((ov, oldstr, newstr) -> {
txtFieldUrl.setText(newstr);
});
webView.setOnKeyPressed((KeyEvent event) -> {
if (event.getCode() == KeyCode.F12) {
source();
}
if (event.isControlDown()) {
if (event.getCode() == KeyCode.MINUS) {
zoomOut();
} else if (event.getCode() == KeyCode.EQUALS) {
zoomIn();
}
}
if (event.getCode() == KeyCode.F5) {
refresh();
}
if (event.isAltDown()) {
if (event.getCode() == KeyCode.HOME) {
engine.load(homePage);
} else if (event.getCode() == KeyCode.LEFT) {
back();
} else if (event.getCode() == KeyCode.RIGHT) {
forward();
}
}
});
loadUrl();
}
}