Repository navigation
Expand file tree
/
Copy pathBrowser.java
More file actions
249 lines (212 loc) · 9.44 KB
/
Copy pathBrowser.java
File metadata and controls
249 lines (212 loc) · 9.44 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Browser extends Application {
private static final String HOME_URL = "https://www.google.com/?hl=en";
private static final String TITLE = "SimpleJBrowser";
private TextField addressBar;
private Label tabCountLabel;
private TabPane tabs;
private Label splash;
private StackPane stackPane;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle(TITLE);
primaryStage.setWidth(1400);
primaryStage.setHeight(900);
addressBar = new TextField();
addressBar.setPromptText("Search or enter address");
addressBar.setOnAction(event -> navigate());
addressBar.setStyle("-fx-font-family: 'Adwaita Sans', 'Helvetica Neue', Arial, sans-serif; " +
"-fx-font-size: 13px; " +
"-fx-padding: 8px 14px; " +
"-fx-border-color: #4e5d7a; " +
"-fx-border-radius: 18px; " +
"-fx-background-color: #202832; " +
"-fx-text-fill: #f2f5fa; " +
"-fx-background-radius: 18px;");
tabs = new TabPane();
tabs.setTabClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB);
tabs.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> tabChanged(newTab));
tabCountLabel = new Label("1 tab");
tabCountLabel.setTextFill(Color.web("#a8b7d0"));
tabCountLabel.setStyle("-fx-font-size: 12px; -fx-padding: 0 0 0 12px;");
Button backButton = createToolButton("◀", event -> navigateBack());
Button forwardButton = createToolButton("▶", event -> navigateForward());
Button reloadButton = createToolButton("↻", event -> navigateReload());
Button homeButton = createToolButton("⌂", event -> navigateHome());
homeButton.setMinSize(44, 44);
homeButton.setStyle("-fx-background-color: #3b4b66; -fx-text-fill: #ffffff; -fx-font-weight: bold; -fx-font-size: 18px; " +
"-fx-background-radius: 22px; -fx-border-radius: 22px;");
Button newTabButton = createToolButton("+", event -> addNewTab(HOME_URL));
newTabButton.setTooltip(new javafx.scene.control.Tooltip("Open a new tab"));
ToolBar toolbar = new ToolBar(backButton, forwardButton, reloadButton, homeButton, newTabButton, tabCountLabel, addressBar);
toolbar.setStyle("-fx-background-color: #171f2b; -fx-border-color: transparent transparent #2f3e53 transparent; -fx-padding: 8px;");
addNewTab(HOME_URL);
BorderPane layout = new BorderPane();
layout.setTop(toolbar);
layout.setCenter(tabs);
splash = new Label("Loading SimpleBrowser...");
splash.setTextFill(Color.web("#e5ecff"));
splash.setStyle("-fx-background-color: #0f1620; -fx-font-family: sans-serif; -fx-font-size: 18px; " +
"-fx-alignment: center;");
splash.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
splash.setAlignment(Pos.CENTER);
stackPane = new StackPane(layout, splash);
StackPane.setAlignment(splash, Pos.CENTER);
Scene scene = new Scene(stackPane);
scene.setFill(Color.web("#0f1620"));
primaryStage.setScene(scene);
primaryStage.setTitle(TITLE);
primaryStage.show();
showSplash(true);
}
private Button createToolButton(String text, javafx.event.EventHandler<javafx.event.ActionEvent> handler) {
Button button = new Button(text);
button.setOnAction(handler);
button.setMinSize(30, 30);
button.setStyle("-fx-background-color: #2a3240; -fx-text-fill: #e5ecff; -fx-font-weight: bold; -fx-background-radius: 15px; " +
"-fx-border-radius: 15px; -fx-border-color: transparent;");
button.setOnMouseEntered(event -> button.setStyle("-fx-background-color: #3c4e6c; -fx-text-fill: #e5ecff; -fx-font-weight: bold; -fx-background-radius: 15px; " +
"-fx-border-radius: 15px; -fx-border-color: transparent;"));
button.setOnMouseExited(event -> button.setStyle("-fx-background-color: #2a3240; -fx-text-fill: #e5ecff; -fx-font-weight: bold; -fx-background-radius: 15px; " +
"-fx-border-radius: 15px; -fx-border-color: transparent;"));
return button;
}
private void addNewTab(String url) {
WebView view = new WebView();
view.setStyle("-fx-background-color: #0f1620;");
WebEngine engine = view.getEngine();
Tab tab = new Tab("New Tab", view);
tab.setOnClosed(event -> updateTabCount());
tabs.getTabs().add(tab);
tabs.getSelectionModel().select(tab);
engine.locationProperty().addListener((observable, oldValue, newValue) -> {
if (tab.equals(getCurrentTab())) {
addressBar.setText(newValue);
}
});
engine.titleProperty().addListener((observable, oldValue, newValue) -> {
tab.setText((newValue == null || newValue.isEmpty()) ? "New Tab" : newValue);
if (tab.equals(getCurrentTab())) {
Stage stage = (Stage) stackPane.getScene().getWindow();
stage.setTitle((newValue == null || newValue.isEmpty()) ? TITLE : newValue + " - " + TITLE);
}
});
engine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED && tab.equals(getCurrentTab())) {
showSplash(false);
}
});
updateTabCount();
platformLoadUrl(engine, url);
}
private void tabChanged(Tab newTab) {
if (newTab == null) {
return;
}
WebView view = (WebView) newTab.getContent();
WebEngine engine = view.getEngine();
addressBar.setText(engine.getLocation());
String title = engine.getTitle();
Stage stage = (Stage) stackPane.getScene().getWindow();
stage.setTitle((title == null || title.isEmpty()) ? TITLE : title + " - " + TITLE);
if (engine.getLoadWorker().getState() != Worker.State.SUCCEEDED) {
showSplash(true);
} else {
showSplash(false);
}
}
private Tab getCurrentTab() {
return tabs.getSelectionModel().getSelectedItem();
}
private WebEngine getCurrentEngine() {
Tab currentTab = getCurrentTab();
if (currentTab == null) {
return null;
}
WebView view = (WebView) currentTab.getContent();
return view.getEngine();
}
private void navigateBack() {
WebEngine engine = getCurrentEngine();
if (engine != null && engine.getHistory().getCurrentIndex() > 0) {
engine.getHistory().go(-1);
}
}
private void navigateForward() {
WebEngine engine = getCurrentEngine();
if (engine != null && engine.getHistory().getCurrentIndex() < engine.getHistory().getEntries().size() - 1) {
engine.getHistory().go(1);
}
}
private void navigateReload() {
WebEngine engine = getCurrentEngine();
if (engine != null) {
engine.reload();
}
}
private void navigateHome() {
WebEngine engine = getCurrentEngine();
if (engine != null) {
platformLoadUrl(engine, HOME_URL);
}
}
private void navigate() {
String url = addressBar.getText().trim();
if (url.isEmpty()) {
return;
}
if (!url.contains("://")) {
url = "https://" + url;
}
if (url.startsWith("http://") && !warnInsecure(url)) {
return;
}
WebEngine engine = getCurrentEngine();
if (engine != null) {
platformLoadUrl(engine, url);
}
}
private boolean warnInsecure(String url) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Insecure Connection");
alert.setHeaderText("You are navigating to an insecure HTTP site.");
alert.setContentText("Data sent or received on this page is not encrypted. Proceed only if you trust the site.");
alert.getButtonTypes().setAll(javafx.scene.control.ButtonType.OK, javafx.scene.control.ButtonType.CANCEL);
return alert.showAndWait().filter(response -> response == javafx.scene.control.ButtonType.OK).isPresent();
}
private void updateTabCount() {
int count = tabs.getTabs().size();
tabCountLabel.setText(count + " tab" + (count == 1 ? "" : "s"));
}
private void showSplash(boolean visible) {
splash.setVisible(visible);
splash.setManaged(visible);
}
private void platformLoadUrl(WebEngine engine, String url) {
Platform.runLater(() -> engine.load(url));
}
}