jaroslav@844
|
1 |
/**
|
jaroslav@844
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@844
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@844
|
4 |
*
|
jaroslav@844
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@844
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@844
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@844
|
8 |
*
|
jaroslav@844
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@844
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@844
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@844
|
12 |
* GNU General Public License for more details.
|
jaroslav@844
|
13 |
*
|
jaroslav@844
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@844
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@844
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@844
|
17 |
*/
|
jaroslav@844
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@844
|
19 |
|
jaroslav@844
|
20 |
import java.io.IOException;
|
jaroslav@844
|
21 |
import java.lang.reflect.Method;
|
jaroslav@844
|
22 |
import java.net.URI;
|
jaroslav@844
|
23 |
import java.net.URL;
|
jaroslav@844
|
24 |
import java.net.URLClassLoader;
|
jaroslav@844
|
25 |
|
jaroslav@844
|
26 |
import java.util.List;
|
jaroslav@844
|
27 |
import java.util.concurrent.Executors;
|
jaroslav@844
|
28 |
import javafx.application.Application;
|
jaroslav@844
|
29 |
import javafx.application.Platform;
|
jaroslav@844
|
30 |
import javafx.beans.value.ChangeListener;
|
jaroslav@844
|
31 |
import javafx.beans.value.ObservableValue;
|
jaroslav@844
|
32 |
import javafx.event.ActionEvent;
|
jaroslav@844
|
33 |
import javafx.event.EventHandler;
|
jaroslav@844
|
34 |
import javafx.geometry.HPos;
|
jaroslav@844
|
35 |
import javafx.geometry.Insets;
|
jaroslav@844
|
36 |
import javafx.geometry.VPos;
|
jaroslav@844
|
37 |
import javafx.scene.Node;
|
jaroslav@844
|
38 |
import javafx.scene.Scene;
|
jaroslav@844
|
39 |
import javafx.scene.control.Button;
|
jaroslav@844
|
40 |
import javafx.scene.control.TextField;
|
jaroslav@844
|
41 |
import javafx.scene.layout.ColumnConstraints;
|
jaroslav@844
|
42 |
import javafx.scene.layout.GridPane;
|
jaroslav@844
|
43 |
import javafx.scene.layout.Pane;
|
jaroslav@844
|
44 |
import javafx.scene.layout.Priority;
|
jaroslav@844
|
45 |
import javafx.scene.layout.VBox;
|
jaroslav@844
|
46 |
import javafx.scene.web.WebEngine;
|
jaroslav@844
|
47 |
import javafx.scene.web.WebView;
|
jaroslav@844
|
48 |
import javafx.stage.Stage;
|
jaroslav@844
|
49 |
|
jaroslav@844
|
50 |
/**
|
jaroslav@844
|
51 |
*
|
jaroslav@844
|
52 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@844
|
53 |
*/
|
jaroslav@844
|
54 |
final class WebViewLauncher extends Bck2BrwsrLauncher {
|
jaroslav@844
|
55 |
static {
|
jaroslav@844
|
56 |
try {
|
jaroslav@844
|
57 |
Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
jaroslav@844
|
58 |
m.setAccessible(true);
|
jaroslav@844
|
59 |
URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
|
jaroslav@844
|
60 |
System.err.println("url : " + l);
|
jaroslav@844
|
61 |
m.invoke(ClassLoader.getSystemClassLoader(), l);
|
jaroslav@844
|
62 |
} catch (Exception ex) {
|
jaroslav@844
|
63 |
throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
|
jaroslav@844
|
64 |
}
|
jaroslav@844
|
65 |
}
|
jaroslav@844
|
66 |
|
jaroslav@844
|
67 |
public WebViewLauncher() {
|
jaroslav@844
|
68 |
super(null);
|
jaroslav@844
|
69 |
}
|
jaroslav@844
|
70 |
|
jaroslav@844
|
71 |
@Override
|
jaroslav@844
|
72 |
protected Object[] showBrwsr(final URI url) throws IOException {
|
jaroslav@844
|
73 |
try {
|
jaroslav@844
|
74 |
Executors.newSingleThreadExecutor().submit(new Runnable() {
|
jaroslav@844
|
75 |
@Override
|
jaroslav@844
|
76 |
public void run() {
|
jaroslav@844
|
77 |
WebViewBrowser.launch(WebViewBrowser.class, url.toString());
|
jaroslav@844
|
78 |
}
|
jaroslav@844
|
79 |
});
|
jaroslav@844
|
80 |
} catch (Throwable ex) {
|
jaroslav@844
|
81 |
ex.printStackTrace();
|
jaroslav@844
|
82 |
}
|
jaroslav@844
|
83 |
return null;
|
jaroslav@844
|
84 |
}
|
jaroslav@844
|
85 |
|
jaroslav@844
|
86 |
@Override
|
jaroslav@844
|
87 |
public void close() throws IOException {
|
jaroslav@844
|
88 |
Platform.exit();
|
jaroslav@844
|
89 |
}
|
jaroslav@844
|
90 |
|
jaroslav@844
|
91 |
|
jaroslav@844
|
92 |
|
jaroslav@844
|
93 |
/**
|
jaroslav@844
|
94 |
* Demonstrates a WebView object accessing a web page.
|
jaroslav@844
|
95 |
*
|
jaroslav@844
|
96 |
* @see javafx.scene.web.WebView
|
jaroslav@844
|
97 |
* @see javafx.scene.web.WebEngine
|
jaroslav@844
|
98 |
*/
|
jaroslav@844
|
99 |
public static class WebViewBrowser extends Application {
|
jaroslav@844
|
100 |
|
jaroslav@844
|
101 |
@Override
|
jaroslav@844
|
102 |
public void start(Stage primaryStage) throws Exception {
|
jaroslav@844
|
103 |
Pane root = new WebViewPane(getParameters().getUnnamed());
|
jaroslav@844
|
104 |
primaryStage.setScene(new Scene(root, 1024, 768));
|
jaroslav@844
|
105 |
primaryStage.show();
|
jaroslav@844
|
106 |
}
|
jaroslav@844
|
107 |
|
jaroslav@844
|
108 |
/**
|
jaroslav@844
|
109 |
* Create a resizable WebView pane
|
jaroslav@844
|
110 |
*/
|
jaroslav@844
|
111 |
public class WebViewPane extends Pane {
|
jaroslav@844
|
112 |
|
jaroslav@844
|
113 |
public WebViewPane(List<String> params) {
|
jaroslav@844
|
114 |
VBox.setVgrow(this, Priority.ALWAYS);
|
jaroslav@844
|
115 |
setMaxWidth(Double.MAX_VALUE);
|
jaroslav@844
|
116 |
setMaxHeight(Double.MAX_VALUE);
|
jaroslav@844
|
117 |
|
jaroslav@844
|
118 |
WebView view = new WebView();
|
jaroslav@844
|
119 |
view.setMinSize(500, 400);
|
jaroslav@844
|
120 |
view.setPrefSize(500, 400);
|
jaroslav@844
|
121 |
final WebEngine eng = view.getEngine();
|
jaroslav@844
|
122 |
final TextField locationField = new TextField();
|
jaroslav@844
|
123 |
System.err.println("params : " + params);
|
jaroslav@844
|
124 |
if (params.size() > 0) {
|
jaroslav@844
|
125 |
eng.load(params.get(0));
|
jaroslav@844
|
126 |
locationField.setText(params.get(0));
|
jaroslav@844
|
127 |
}
|
jaroslav@844
|
128 |
locationField.setMaxHeight(Double.MAX_VALUE);
|
jaroslav@844
|
129 |
Button goButton = new Button("Go");
|
jaroslav@844
|
130 |
goButton.setDefaultButton(true);
|
jaroslav@844
|
131 |
EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
|
jaroslav@844
|
132 |
@Override
|
jaroslav@844
|
133 |
public void handle(ActionEvent event) {
|
jaroslav@844
|
134 |
eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
|
jaroslav@844
|
135 |
: "http://" + locationField.getText());
|
jaroslav@844
|
136 |
}
|
jaroslav@844
|
137 |
};
|
jaroslav@844
|
138 |
goButton.setOnAction(goAction);
|
jaroslav@844
|
139 |
locationField.setOnAction(goAction);
|
jaroslav@844
|
140 |
eng.locationProperty().addListener(new ChangeListener<String>() {
|
jaroslav@844
|
141 |
@Override
|
jaroslav@844
|
142 |
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
jaroslav@844
|
143 |
locationField.setText(newValue);
|
jaroslav@844
|
144 |
}
|
jaroslav@844
|
145 |
});
|
jaroslav@844
|
146 |
GridPane grid = new GridPane();
|
jaroslav@844
|
147 |
grid.setVgap(5);
|
jaroslav@844
|
148 |
grid.setHgap(5);
|
jaroslav@844
|
149 |
GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
|
jaroslav@844
|
150 |
GridPane.setConstraints(goButton, 1, 0);
|
jaroslav@844
|
151 |
GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
|
jaroslav@844
|
152 |
grid.getColumnConstraints().addAll(
|
jaroslav@844
|
153 |
new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
|
jaroslav@844
|
154 |
new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
|
jaroslav@844
|
155 |
grid.getChildren().addAll(locationField, goButton, view);
|
jaroslav@844
|
156 |
getChildren().add(grid);
|
jaroslav@844
|
157 |
}
|
jaroslav@844
|
158 |
|
jaroslav@844
|
159 |
@Override
|
jaroslav@844
|
160 |
protected void layoutChildren() {
|
jaroslav@844
|
161 |
List<Node> managed = getManagedChildren();
|
jaroslav@844
|
162 |
double width = getWidth();
|
jaroslav@844
|
163 |
double height = getHeight();
|
jaroslav@844
|
164 |
double top = getInsets().getTop();
|
jaroslav@844
|
165 |
double right = getInsets().getRight();
|
jaroslav@844
|
166 |
double left = getInsets().getLeft();
|
jaroslav@844
|
167 |
double bottom = getInsets().getBottom();
|
jaroslav@844
|
168 |
for (int i = 0; i < managed.size(); i++) {
|
jaroslav@844
|
169 |
Node child = managed.get(i);
|
jaroslav@844
|
170 |
layoutInArea(child, left, top,
|
jaroslav@844
|
171 |
width - left - right, height - top - bottom,
|
jaroslav@844
|
172 |
0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
|
jaroslav@844
|
173 |
}
|
jaroslav@844
|
174 |
}
|
jaroslav@844
|
175 |
}
|
jaroslav@844
|
176 |
}
|
jaroslav@844
|
177 |
|
jaroslav@844
|
178 |
}
|