Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.scene.Parent;
  3. import javafx.scene.Scene;
  4. import javafx.stage.Stage;
  5.  
  6. public class Main extends Application {
  7.  
  8. public static void main(String[] args) {
  9. launch(args);
  10. }
  11.  
  12. public Parent createContent() {
  13. final WebBrowser browser = new WebBrowser();
  14. return browser;
  15. }
  16.  
  17. @Override
  18. public void start(Stage primaryStage) throws Exception {
  19. primaryStage.setResizable(true);
  20. Scene scene = new Scene(createContent());
  21. primaryStage.setTitle("Eric's Web Demo");
  22. scene.getStylesheets().add("style/template.css");
  23. primaryStage.setScene(scene);
  24. primaryStage.show();
  25. }
  26. }
  27.  
  28. import org.w3c.dom.Attr;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;
  32.  
  33. import javafx.beans.value.ObservableValue;
  34. import javafx.collections.FXCollections;
  35. import javafx.collections.ListChangeListener.Change;
  36. import javafx.event.ActionEvent;
  37. import javafx.geometry.Dimension2D;
  38. import javafx.geometry.HPos;
  39. import javafx.geometry.Insets;
  40. import javafx.geometry.VPos;
  41. import javafx.scene.control.Button;
  42. import javafx.scene.control.ComboBox;
  43. import javafx.scene.control.Label;
  44. import javafx.scene.control.ProgressBar;
  45. import javafx.scene.control.TextField;
  46. import javafx.scene.layout.BorderPane;
  47. import javafx.scene.layout.GridPane;
  48. import javafx.scene.layout.HBox;
  49. import javafx.scene.layout.Priority;
  50. import javafx.scene.web.WebEngine;
  51. import javafx.scene.web.WebHistory;
  52. import javafx.scene.web.WebHistory.Entry;
  53. import javafx.scene.web.WebView;
  54.  
  55. public class WebBrowser extends BorderPane {
  56.  
  57. private static final Dimension2D DIM = new Dimension2D(1220, 680);
  58. private final WebView webView;
  59. private final WebEngine webEngine;
  60. private final ComboBox<String> addressBox;
  61. private final TextField searchField;
  62. private final Button backButton;
  63. private final Button forwardButton;
  64. private final Button proceedButton;
  65. private final Button searchButton;
  66. private final ProgressBar progressBar;
  67. private final Label statusLabel;
  68.  
  69. public WebBrowser() {
  70.  
  71. this.setMinSize(DIM.getWidth(), DIM.getHeight());
  72. this.setPrefSize(DIM.getWidth(), DIM.getHeight());
  73.  
  74. backButton = new Button("uD83EuDC78");
  75. backButton.setOnAction(this::backButtonListener);
  76.  
  77. forwardButton = new Button("u2794");
  78. forwardButton.setDefaultButton(true);
  79. forwardButton.setOnAction(this::forwardButtonListener);
  80.  
  81. proceedButton = new Button("Go");
  82. proceedButton.setOnAction(this::proceedButtonListener);
  83.  
  84. final HBox buttonGroup = new HBox();
  85. buttonGroup.setSpacing(5);
  86. buttonGroup.setPadding(new Insets(10, 5, 10, 5));
  87. buttonGroup.getChildren().addAll(backButton, forwardButton, proceedButton);
  88.  
  89. addressBox = new ComboBox<String>();
  90. addressBox.setItems(FXCollections.observableArrayList());
  91. addressBox.setValue("http://stackoverflow.com/questions/32783532/applying-css-file-to-javafx-webview");
  92. addressBox.setOnAction(this::proceedButtonListener);
  93. addressBox.setEditable(true);
  94. addressBox.setMaxWidth(Double.MAX_VALUE);
  95.  
  96. searchField = new TextField();
  97. searchField.setPromptText("uD83DuDD0D Search");
  98.  
  99. searchButton = new Button("uD83DuDD0D");
  100. searchButton.setDefaultButton(true);
  101. searchButton.setOnAction(this::searchButtonListener);
  102.  
  103. statusLabel = new Label("Status: ");
  104. progressBar = new ProgressBar(0);
  105.  
  106. webView = new WebView();
  107. webEngine = webView.getEngine();
  108. webEngine.load(addressBox.getValue());
  109. webEngine.getLoadWorker().stateProperty().addListener(this::stateChangeListener);
  110. webEngine.locationProperty().addListener(this::urlChangeListener);
  111. progressBar.progressProperty().bind(webEngine.getLoadWorker().progressProperty());
  112.  
  113. final WebHistory history = webEngine.getHistory();
  114. history.getEntries().addListener(this::historyListener);
  115.  
  116. final GridPane root = new GridPane();
  117. GridPane.setConstraints(buttonGroup, 0, 0, 1, 1, HPos.LEFT, VPos.CENTER, Priority.NEVER, Priority.NEVER);
  118. GridPane.setConstraints(addressBox, 1, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.NEVER);
  119. GridPane.setConstraints(searchField, 2, 0, 1, 1, HPos.RIGHT, VPos.CENTER, Priority.NEVER, Priority.NEVER);
  120. GridPane.setConstraints(searchButton, 3, 0, 1, 1, HPos.RIGHT, VPos.CENTER, Priority.NEVER, Priority.NEVER);
  121. GridPane.setConstraints(webView, 0, 1, 4, 1, HPos.LEFT, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
  122. GridPane.setConstraints(statusLabel, 0, 2, 1, 1, HPos.LEFT, VPos.CENTER, Priority.NEVER, Priority.NEVER);
  123. GridPane.setConstraints(progressBar, 3, 2, 3, 1, HPos.RIGHT, VPos.CENTER, Priority.NEVER, Priority.NEVER);
  124. GridPane.setFillWidth(webView, true);
  125. GridPane.setMargin(addressBox, new Insets(5, 0, 5, 0));
  126. GridPane.setMargin(searchField, new Insets(5, 5, 5, 5));
  127. GridPane.setMargin(searchButton, new Insets(5, 8, 5, 0));
  128. GridPane.setMargin(statusLabel, new Insets(5, 0, 5, 5));
  129. GridPane.setMargin(progressBar, new Insets(5, 5, 5, 5));
  130. root.addRow(0, buttonGroup, addressBox, searchField, searchButton);
  131. root.addRow(1, webView);
  132. root.addRow(2,statusLabel, progressBar);
  133.  
  134. this.setCenter(root);
  135. }
  136.  
  137. public void historyListener(Change<? extends Entry> changeValue) {
  138. changeValue.next();
  139. for (Entry entry : changeValue.getRemoved()) {
  140. addressBox.getItems().remove(entry.getUrl());
  141. System.out.print("Removed url: ");
  142. System.out.println(entry.getUrl());
  143. }
  144. for (Entry entry : changeValue.getAddedSubList()) {
  145. System.out.print("Added url: ");
  146. addressBox.getItems().add(entry.getUrl());
  147. System.out.println(entry.getUrl());
  148. }
  149. }
  150.  
  151. public void progressBarListener(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
  152. progressBar.setProgress(new_val.doubleValue());
  153. }
  154.  
  155. private void stateChangeListener(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
  156. setWebpageTheme(true);
  157. String output = newValue.toString().toLowerCase();
  158. statusLabel.setText("Status: " + output);
  159. }
  160.  
  161. private void urlChangeListener(ObservableValue<? extends String> observable, String oldValue, String newValue) {
  162. addressBox.setValue(newValue);
  163. }
  164.  
  165. public void forwardButtonListener(ActionEvent event) {
  166. webEngine.executeScript("history.forward()");
  167. }
  168.  
  169. private void backButtonListener(ActionEvent event) {
  170. webEngine.executeScript("history.back()");
  171. }
  172.  
  173. private void searchButtonListener(ActionEvent event) {
  174. String google = "http://www.google.com/search?q=" + searchField.getText();
  175. webEngine.load(google.startsWith("http://") || google.startsWith("https://")
  176. ? google : "http://" + google);
  177. }
  178.  
  179. private void proceedButtonListener(ActionEvent event) {
  180. String url = addressBox.valueProperty().getValue();
  181. webEngine.load(url.startsWith("http://") || url.startsWith("https://")
  182. ? url : "http://" + url);
  183. }
  184.  
  185. private void setWebpageTheme(Boolean succeeded) {
  186. // Can safely access DOM and set styles.
  187. if (succeeded == true) {
  188. // This gives the DOM Document for the web page.
  189. Document doc = webEngine.getDocument();
  190. Attr newAttr = null;
  191. NodeList htmlTags = doc.getElementsByTagName("*");
  192. for (int i = 0; i < htmlTags.getLength(); i++) {
  193. if (htmlTags != null) {
  194. Node htmlNode = htmlTags.item(i);
  195. newAttr = doc.createAttribute("style");
  196. newAttr.setValue("background-color: #222; border-color: #333; background: #222; color: #bbb; ");
  197. htmlNode.getAttributes().setNamedItem(newAttr);
  198. }
  199. }
  200. }
  201. }
  202. }
  203.  
  204. .root{
  205. -fx-background: rgb(44,44,44);
  206. }
  207.  
  208. .button {
  209. -fx-border-radius: 5;
  210. -fx-background-radius: 5;
  211. -fx-min-height: 32;
  212. -fx-min-width: 40;
  213. -fx-background-color: radial-gradient(radius 100%, rgb(22, 33, 188), rgb(3,22,122));
  214. -fx-text-fill: rgb(196,188,222);
  215. }
  216.  
  217. TextField {
  218. -fx-border-radius: 5;
  219. -fx-background-radius: 5;
  220. -fx-max-height: 32;
  221. -fx-min-width: 280;
  222. -fx-border-color: #555;
  223. -fx-border-width: 1 1 1 1;
  224. -fx-background-color: #333;
  225. -fx-text-fill: rgb(196,188,222);
  226. }
  227.  
  228. .combo-box-base {
  229. -fx-border-radius: 5;
  230. -fx-background-radius: 5;
  231. -fx-min-height: 35;
  232. -fx-background-color: #333;
  233. -fx-border-color: transparent;
  234. -fx-border-width: 2 2 2 2;
  235. }
  236.  
  237. .combo-box-base .arrow-button {
  238. -fx-background-color: radial-gradient(radius 100%, rgb(22, 33, 188), rgb(3,22,122));
  239. }
  240.  
  241. .combo-box .combo-box-popup, .list-view, .list-cell {
  242. -fx-background-color: #333;
  243. -fx-text-fill: rgb(155,188,166);
  244. }
  245.  
  246. .combo-box .text-input {
  247. -fx-border-radius: 5;
  248. -fx-background-radius: 5;
  249. -fx-background-color: #333;
  250. -fx-border-color: #555;
  251. -fx-border-width: 1 1 1 1;
  252. -fx-text-fill: rgb(155,188,166);
  253. }
  254.  
  255. .scroll-bar {
  256. -fx-background-color: #222;
  257. }
  258.  
  259. .scroll-bar .thumb {
  260. -fx-background-color: radial-gradient(radius 100%, rgb(22, 33, 188), rgb(3,22,122));
  261. }
  262.  
  263. .label {
  264. -fx-font: 14px "Arial";
  265. -fx-border-color: rgb(57, 58, 59);
  266. -fx-border-width: 2 2 2 2;
  267. -fx-text-fill: rgb(155,188,166);
  268. }
  269.  
  270. .progress-bar > .track {
  271. -fx-text-box-border: rgb(44, 44, 44);
  272. -fx-control-inner-background: rgb(22, 22, 44);
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement