Forthz - web browser

Because we all do. :)
I've enjoyed playing with it but... I will never use JavaFX without FXML again!
It's simple, has working basic functionalities. Of course, a list of functionalities that it doesn't have...would be...quite large! But it's something to play with...



forthz.Main.java
1:  package forthz;  
2:    
3:  import java.util.Optional;  
4:  import javafx.application.Application;  
5:  import javafx.application.Platform;  
6:  import javafx.beans.value.ObservableValue;  
7:  import javafx.collections.ObservableList;  
8:  import javafx.concurrent.Worker;  
9:  import javafx.event.ActionEvent;  
10:  import javafx.event.EventHandler;  
11:  import javafx.geometry.Pos;  
12:  import javafx.geometry.Rectangle2D;  
13:  import javafx.scene.Scene;  
14:  import javafx.scene.control.Button;  
15:  import javafx.scene.control.ChoiceBox;  
16:  import javafx.scene.control.Label;  
17:  import javafx.scene.control.TextField;  
18:  import javafx.scene.control.TextInputDialog;  
19:  import javafx.scene.image.Image;  
20:  import javafx.scene.image.ImageView;  
21:  import javafx.scene.layout.AnchorPane;  
22:  import javafx.scene.layout.Background;  
23:  import javafx.scene.layout.HBox;  
24:  import javafx.scene.layout.VBox;  
25:  import javafx.scene.web.WebEngine;  
26:  import javafx.scene.web.WebView;  
27:  import javafx.stage.Screen;  
28:  import javafx.stage.Stage;  
29:  import javafx.stage.StageStyle;  
30:    
31:  /**  
32:   *  
33:   * @author Djordje Gavrilovic  
34:   */  
35:  public class Main extends Application {  
36:    
37:    public static void main(String[] args) { launch(args); }  
38:    
39:    @Override  
40:    public void start(Stage primaryStage) throws Exception {  
41:    
42:      TextField tf = new TextField(); // url field  
43:      ChoiceBox historyBox = new ChoiceBox(); // history list   
44:    
45:      /*   
46:       * SETTING THE ENGINE  
47:       */  
48:      WebView browser = new WebView();  
49:      browser.prefHeightProperty().bind(primaryStage.heightProperty()); // binding to  
50:      browser.prefWidthProperty().bind(primaryStage.widthProperty()); // fit the stage size  
51:    
52:      WebEngine engine = browser.getEngine();  
53:      engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {  
54:        if (Worker.State.SUCCEEDED.equals(newValue)) {  
55:          tf.setText(engine.getLocation());  
56:          historyBox.getItems().add(engine.getLocation());  
57:        }  
58:      });  
59:    
60:      /*  
61:       * HOME PAGE // if ever to be needed  
62:       */  
63:      String homeUrl = "http://google.com/";  
64:      engine.load(homeUrl);  
65:    
66:      /*  
67:       * URL BAR  
68:       */  
69:      tf.setPrefColumnCount(100);  
70:      tf.setOnAction(new EventHandler<ActionEvent>() {  
71:        @Override  
72:        public void handle(ActionEvent event) {  
73:          String url = "https://" + tf.getText();  
74:          engine.load(url);  
75:        }  
76:      });  
77:      // refresh button  
78:      Image reImg = new Image("icons/replay.png");  
79:      ImageView reIV = new ImageView(reImg);  
80:      reIV.setFitWidth(20); // resizing  
81:      reIV.setPreserveRatio(true);  
82:      reIV.setSmooth(true);  
83:      reIV.setCache(true); // cache to improve performance(?)  
84:      Button reBtn = new Button("", reIV);  
85:      reBtn.setOnAction(new EventHandler<ActionEvent>() {  
86:        @Override  
87:        public void handle(ActionEvent event) {  
88:          engine.load(browser.getEngine().getLocation());  
89:        }  
90:      });  
91:    
92:      /*  
93:       * SEARCH BUTTON / DIALOG  
94:       */  
95:      Image srImg = new Image("icons/search.png");  
96:      ImageView srIV = new ImageView(srImg);  
97:      srIV.setFitWidth(20);  
98:      srIV.setPreserveRatio(true);  
99:      srIV.setSmooth(true);  
100:      srIV.setCache(true);  
101:      Button srBtn = new Button("", srIV);  
102:      srBtn.setOnAction(new EventHandler<ActionEvent>() {  
103:        @Override  
104:        public void handle(ActionEvent event) {  
105:          TextInputDialog dialog = new TextInputDialog();  
106:          dialog.getDialogPane().getScene().getStylesheets().add("css/MainStyle.css");  
107:          dialog.initStyle(StageStyle.TRANSPARENT);  
108:          dialog.setTitle("Forthz doing Search");  
109:          dialog.setHeaderText("Forthz doing search");  
110:    
111:          Optional<String> result = dialog.showAndWait();  
112:    
113:          if (result.isPresent()) {  
114:            String[] res = result.get().split(" ");  
115:            StringBuilder query = new StringBuilder();  
116:            for (String q : res) {  
117:              query.append(q);  
118:              query.append("+");  
119:            }  
120:            String zx = query.substring(0, query.length() - 1);  
121:            engine.load("http://www.google.com/search?q=" + zx);  
122:          }  
123:        }  
124:      });  
125:    
126:      /*  
127:       * HISTORY  
128:       */  
129:      historyBox.setId("historyBox");  
130:      historyBox.getSelectionModel().selectedItemProperty().addListener(  
131:          (ObservableValue observable, Object oldValue, Object newValue)  
132:          -> {  
133:        engine.load(newValue.toString());  
134:      });  
135:    
136:      /*  
137:       * HOME BUTTON  
138:       */  
139:      Image homeImg = new Image("icons/homeicon2.png");  
140:      ImageView homeIV = new ImageView(homeImg);  
141:      homeIV.setFitWidth(20);  
142:      homeIV.setPreserveRatio(true);  
143:      homeIV.setSmooth(true);  
144:      homeIV.setCache(true);  
145:      Button homeBtn = new Button("", homeIV);  
146:      homeBtn.setId("homeBtn");  
147:      homeBtn.setOnAction(new EventHandler<ActionEvent>() {  
148:        @Override  
149:        public void handle(ActionEvent event) {  
150:          engine.load(homeUrl);  
151:        }  
152:      });  
153:    
154:      /*  
155:       * SYSTEM BUTTONS  
156:       */  
157:      // close button  
158:      Image closeImg = new Image("icons/close.png");  
159:      ImageView closeIV = new ImageView(closeImg);  
160:      closeIV.setFitWidth(17);  
161:      closeIV.setPreserveRatio(true);  
162:      closeIV.setSmooth(true);  
163:      closeIV.setCache(true);  
164:      Button closeBtn = new Button("", closeIV);  
165:      closeBtn.setOnAction(new EventHandler<ActionEvent>() {  
166:        @Override  
167:        public void handle(ActionEvent event) {  
168:          Platform.exit();  
169:        }  
170:      });  
171:      // max button  
172:      Image maxImg = new Image("icons/max.png");  
173:      ImageView maxIV = new ImageView(maxImg);  
174:      maxIV.setFitWidth(17);  
175:      maxIV.setPreserveRatio(true);  
176:      maxIV.setSmooth(true);  
177:      maxIV.setCache(true);  
178:      Button maxBtn = new Button("", maxIV);  
179:      maxBtn.setOnAction(new EventHandler<ActionEvent>() {  
180:        @Override  
181:        public void handle(ActionEvent event) {  
182:          // primaryStage.setMaximized(true); // not working on Ubuntu/Mint  
183:          ObservableList<Screen> screens = Screen.getScreensForRectangle(new Rectangle2D(  
184:              primaryStage.getX(), primaryStage.getY(), primaryStage.getWidth(), primaryStage.getHeight()));  
185:          Rectangle2D bounds = screens.get(0).getVisualBounds();  
186:          primaryStage.setX(bounds.getMinX());  
187:          primaryStage.setY(bounds.getMinY());  
188:          primaryStage.setWidth(bounds.getWidth());  
189:          primaryStage.setHeight(bounds.getHeight());  
190:        }  
191:      });  
192:      // min button  
193:      Image minImg = new Image("icons/min.png");  
194:      ImageView minIV = new ImageView(minImg);  
195:      minIV.setFitWidth(17);  
196:      minIV.setPreserveRatio(true);  
197:      minIV.setSmooth(true);  
198:      minIV.setCache(true);  
199:      Button minBtn = new Button("", minIV);  
200:      minBtn.setOnAction(new EventHandler<ActionEvent>() {  
201:        @Override  
202:        public void handle(ActionEvent event) {  
203:          primaryStage.setIconified(true);  
204:        }  
205:      });  
206:      // Fortz doing site labels  
207:      Label forthzL = new Label("Forthz");  
208:      forthzL.setId("titId");  
209:    
210:      Label dots = new Label("doing");  
211:      dots.setId("dots");  
212:    
213:      Label siteL = new Label();  
214:      siteL.setId("siteLId");  
215:      siteL.textProperty().bind(browser.getEngine().titleProperty());  
216:    
217:      AnchorPane st = new AnchorPane(forthzL, dots, siteL, minBtn, maxBtn, closeBtn);  
218:      st.setLeftAnchor(forthzL, 10.0);  
219:      st.setTopAnchor(forthzL, 5.0);  
220:      st.setLeftAnchor(dots, 60.0);  
221:      st.setTopAnchor(dots, 6.0);  
222:      st.setLeftAnchor(siteL, 95.0);  
223:      st.setTopAnchor(siteL, 6.0);  
224:      st.setRightAnchor(closeBtn, 5.0);  
225:      st.setRightAnchor(maxBtn, closeBtn.getWidth() + 35.0);  
226:      st.setRightAnchor(minBtn, 65.0);      
227:    
228:      /*  
229:       * LAYOUT  
230:       */      
231:      Label l1 = new Label(" "); // silly quick hacks  
232:      Label l2 = new Label(); // wish I used FXML  
233:        
234:      HBox tab = new HBox(5, homeBtn, tf, reBtn, srBtn, historyBox, l1);  
235:      tab.setAlignment(Pos.BASELINE_LEFT);  
236:      tab.setBackground(Background.EMPTY);  
237:    
238:      VBox root = new VBox(st, tab, l2, browser);  
239:      root.setBackground(Background.EMPTY);  
240:    
241:      Scene scene = new Scene(root, 1000, 700);  
242:      scene.getStylesheets().add("css/MainStyle.css");  
243:    
244:      Image img2 = new Image("icons/appicon.png");  
245:    
246:      primaryStage.getIcons().add(img2);  
247:      primaryStage.setScene(scene);  
248:      primaryStage.titleProperty().bind(browser.getEngine().titleProperty());  
249:      primaryStage.initStyle(StageStyle.UNDECORATED);  
250:      primaryStage.show();  
251:    
252:    }  
253:    
254:  }  
255:    

css.MainStyle.css
1:  .root {  
2:    -fx-background-color: #6B7A8F;  
3:    -fx-overflow-x: hidden;  
4:    -fx-overflow-y: hidden;    
5:  }  
6:  .button {  
7:    -fx-background-color: #6B7A8F;  
8:    -fx-cursor:hand;  
9:    -fx-text-fill:#F7C331;  
10:  }  
11:  .scroll-bar:vertical {  
12:    -fx-background-color: white;  
13:  }  
14:  .dialog-pane {  
15:    -fx-background-color: #6B7A8F;  
16:  }  
17:  .dialog-pane .label {  
18:    -fx-text-fill: white;  
19:  }  
20:  .dialog-pane:header .header-panel {  
21:    -fx-background-color: orange  
22:  }  
23:  .dialog-pane:header .header-panel .label {  
24:    -fx-font-style: italic;  
25:    -fx-font-size: 1em;  
26:  }  
27:  #historyBox {  
28:    -fx-background-color: white;  
29:    -fx-mark-color: orange;   
30:    -fx-max-width: 25px;  
31:    -fx-cursor:hand;  
32:  }  
33:  #historyBox .context-menu { -fx-background-color: #6B7A8F; }  
34:  #historyBox .menu-item:focused { -fx-background-color: #DCC7AA; }  
35:  #historyBox .menu-item > .label { -fx-text-fill: white; }  
36:  #titId {  
37:    -fx-text-fill: #DCC7AA;  
38:    -fx-font-size: 12px;  
39:    -fx-font-weight: bold;    
40:  }  
41:  #dots {  
42:    -fx-text-fill:orange;  
43:    -fx-font-size: 10px;  
44:    -fx-font-style:italic;  
45:  }  
46:  #siteLId {  
47:    -fx-text-fill:azure;  
48:    -fx-font-size: 10px;  
49:    -fx-font-weight: bold;  
50:  }  
51:  #cre {  
52:    -fx-text-fill:azure;  
53:    -fx-font-size: 8px;  
54:    -fx-font-weight: bold;  
55:  }  
56:  #helloL {  
57:    -fx-text-fill: azure;  
58:    -fx-font-size: 10px;  
59:    -fx-font-weight: bold;  
60:  }  
61:  #userL {  
62:    -fx-text-fill:#F7C331;  
63:    -fx-font-size: 10px;  
64:    -fx-font-weight: bold;  
65:    -fx-font-style:italic;  
66:  }  
67:    


Comments