svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial.app / org.gvsig.h2spatial.app.mainplugin / src / main / java / org / gvsig / webbrowser / jfx / JFXWebBrowserPanel.java @ 43377
History | View | Annotate | Download (8.5 KB)
1 |
package org.gvsig.webbrowser.jfx; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.KeyAdapter; |
8 |
import java.awt.event.KeyEvent; |
9 |
import java.net.MalformedURLException; |
10 |
import java.net.URL; |
11 |
import java.text.MessageFormat; |
12 |
import org.gvsig.webbrowser.WebBrowserPanel; |
13 |
import javafx.event.EventHandler; |
14 |
import javafx.beans.value.ChangeListener; |
15 |
import javafx.beans.value.ObservableValue; |
16 |
import javafx.application.Platform; |
17 |
import javafx.concurrent.Worker; |
18 |
import javafx.embed.swing.JFXPanel; |
19 |
import javafx.scene.web.WebView; |
20 |
import javafx.scene.Scene; |
21 |
import javafx.scene.web.WebEngine; |
22 |
import javafx.scene.web.WebEvent; |
23 |
import javax.swing.ImageIcon; |
24 |
import javax.swing.JComponent; |
25 |
import javax.swing.JTextField; |
26 |
|
27 |
public class JFXWebBrowserPanel extends JFXWebBrowserPanelView implements WebBrowserPanel { |
28 |
|
29 |
private static final long serialVersionUID = -5048314340699267249L; |
30 |
|
31 |
private JFXWebBrowser browser;
|
32 |
|
33 |
private class JFXWebBrowser extends JFXPanel { |
34 |
|
35 |
private static final long serialVersionUID = 3021277085276506035L; |
36 |
|
37 |
private WebView view;
|
38 |
private WebEngine engine;
|
39 |
private Scene scene;
|
40 |
|
41 |
private class MyEventHandler implements EventHandler<WebEvent<String>> { |
42 |
|
43 |
@Override
|
44 |
public void handle(WebEvent<String> event) { |
45 |
if( event.getEventType() == WebEvent.STATUS_CHANGED ) {
|
46 |
String s = event.getData();
|
47 |
if( s == null ) { |
48 |
lblStatus.setText("");
|
49 |
} else {
|
50 |
lblStatus.setText(s); |
51 |
} |
52 |
|
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
private class MyChangeListener implements ChangeListener<Worker.State> { |
58 |
|
59 |
@Override
|
60 |
public void changed(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) { |
61 |
if( newState == Worker.State.SUCCEEDED ) {
|
62 |
txtURL.setText(engine.getLocation()); |
63 |
} |
64 |
} |
65 |
} |
66 |
|
67 |
public JFXWebBrowser() {
|
68 |
this.view = null; |
69 |
this.engine = null; |
70 |
this.scene = null; |
71 |
|
72 |
Platform.runLater(new Runnable() { |
73 |
@Override
|
74 |
public void run() { |
75 |
createScene(); |
76 |
} |
77 |
}); |
78 |
Platform.setImplicitExit(false);
|
79 |
} |
80 |
|
81 |
private void createScene() { |
82 |
this.view = new WebView(); |
83 |
this.engine = this.view.getEngine(); |
84 |
this.engine.setOnStatusChanged(new MyEventHandler()); |
85 |
this.engine.getLoadWorker().stateProperty().addListener(new MyChangeListener()); |
86 |
this.engine.setJavaScriptEnabled(true); |
87 |
this.scene = new Scene(this.view); |
88 |
this.setScene(this.scene); |
89 |
} |
90 |
|
91 |
private void load(URL url) { |
92 |
this.engine.load(url.toString());
|
93 |
this.revalidate();
|
94 |
} |
95 |
|
96 |
private void loadContent(String content, String contentType) { |
97 |
this.engine.loadContent(content, contentType);
|
98 |
this.revalidate();
|
99 |
} |
100 |
|
101 |
private void goHistory(int index) { |
102 |
this.engine.getHistory().go(index);
|
103 |
} |
104 |
|
105 |
public void setPage(final URL url) { |
106 |
Platform.runLater(new Runnable() { |
107 |
@Override
|
108 |
public void run() { |
109 |
load(url); |
110 |
} |
111 |
}); |
112 |
} |
113 |
|
114 |
public void setContent(final String content, final String contentType) { |
115 |
Platform.runLater(new Runnable() { |
116 |
@Override
|
117 |
public void run() { |
118 |
loadContent(content, contentType); |
119 |
} |
120 |
}); |
121 |
} |
122 |
|
123 |
public void goPrevious() { |
124 |
Platform.runLater(new Runnable() { |
125 |
@Override
|
126 |
public void run() { |
127 |
goHistory(-1);
|
128 |
} |
129 |
}); |
130 |
} |
131 |
|
132 |
public void goNext() { |
133 |
Platform.runLater(new Runnable() { |
134 |
@Override
|
135 |
public void run() { |
136 |
goHistory(1);
|
137 |
} |
138 |
}); |
139 |
} |
140 |
|
141 |
public String getPage() { |
142 |
return this.engine.getLocation(); |
143 |
} |
144 |
|
145 |
public String getTitle() { |
146 |
return this.engine.getTitle(); |
147 |
} |
148 |
|
149 |
public Object executeScript(String js) { |
150 |
Object r = this.engine.executeScript(js); |
151 |
return r;
|
152 |
} |
153 |
|
154 |
public void find(final String text, final boolean backwards) { |
155 |
try {
|
156 |
Platform.runLater(new Runnable() { |
157 |
@Override
|
158 |
public void run() { |
159 |
String script = MessageFormat.format( |
160 |
"window.find(\"{0}\",false,{1},true)",
|
161 |
text, |
162 |
backwards ? "true" : "false" |
163 |
); |
164 |
|
165 |
} |
166 |
}); |
167 |
} catch (Throwable th) { |
168 |
|
169 |
} |
170 |
} |
171 |
|
172 |
} |
173 |
|
174 |
public JFXWebBrowserPanel() {
|
175 |
this.initComponents();
|
176 |
} |
177 |
|
178 |
private void initComponents() { |
179 |
this.browser = new JFXWebBrowser(); |
180 |
this.containerBrowser.setLayout(new BorderLayout()); |
181 |
this.containerBrowser.add(this.browser, BorderLayout.CENTER); |
182 |
|
183 |
this.btnPrevious.setIcon(imageIcon("/images/webbrowser/arrow_left.png")); |
184 |
this.btnNext.setIcon(imageIcon("/images/webbrowser/arrow_right.png")); |
185 |
this.btnRefresh.setIcon(imageIcon("/images/webbrowser/arrow_refresh.png")); |
186 |
this.btnConfig.setIcon(imageIcon("/images/webbrowser/add.png")); |
187 |
this.btnSearchPrevious.setIcon(imageIcon("/images/webbrowser/arrow_up.png")); |
188 |
this.btnSearchNext.setIcon(imageIcon("/images/webbrowser/arrow_down.png")); |
189 |
this.imgSearch.setIcon(imageIcon("/images/webbrowser/search.png")); |
190 |
|
191 |
this.txtURL.addKeyListener(new KeyAdapter() { |
192 |
@Override
|
193 |
public void keyPressed(KeyEvent keyEvent) { |
194 |
if( keyEvent.getKeyChar() == '\n' ) { |
195 |
setPage(txtURL.getText()); |
196 |
} |
197 |
} |
198 |
}); |
199 |
this.txtSearch.addKeyListener(new KeyAdapter() { |
200 |
@Override
|
201 |
public void keyPressed(KeyEvent event) { |
202 |
JTextField jtext = txtSearch;
|
203 |
if( event.getKeyChar() == 0x1b ) { // ESC |
204 |
jtext.setText("");
|
205 |
browser.find("", true); |
206 |
} else if( event.getKeyChar() == '\n' ) { |
207 |
browser.find(jtext.getText(), true);
|
208 |
} |
209 |
} |
210 |
}); |
211 |
this.btnSearchPrevious.addActionListener(new ActionListener() { |
212 |
@Override
|
213 |
public void actionPerformed(ActionEvent e) { |
214 |
browser.find(txtSearch.getText(),false);
|
215 |
} |
216 |
}); |
217 |
this.btnSearchNext.addActionListener(new ActionListener() { |
218 |
@Override
|
219 |
public void actionPerformed(ActionEvent e) { |
220 |
browser.find(txtSearch.getText(),true);
|
221 |
} |
222 |
}); |
223 |
this.btnPrevious.addActionListener(new ActionListener() { |
224 |
@Override
|
225 |
public void actionPerformed(ActionEvent e) { |
226 |
browser.goPrevious(); |
227 |
} |
228 |
}); |
229 |
this.btnNext.addActionListener(new ActionListener() { |
230 |
@Override
|
231 |
public void actionPerformed(ActionEvent e) { |
232 |
browser.goNext(); |
233 |
} |
234 |
}); |
235 |
this.setPreferredSize(new Dimension(600, 500)); |
236 |
} |
237 |
|
238 |
private ImageIcon imageIcon(String res) { |
239 |
URL url = this.getClass().getClassLoader().getResource(res); |
240 |
if( url == null ) { |
241 |
return new ImageIcon(); |
242 |
} |
243 |
return new ImageIcon(url); |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public void setPage(String url) { |
248 |
try {
|
249 |
this.setPage(new URL(url)); |
250 |
} catch (MalformedURLException ex) { |
251 |
throw new RuntimeException(ex); |
252 |
} |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public void setPage(URL url) { |
257 |
this.txtURL.setText(url.toString());
|
258 |
this.browser.setPage(url);
|
259 |
} |
260 |
|
261 |
@Override
|
262 |
public String getPage() { |
263 |
return this.browser.getPage(); |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void setContent(final String content, final String contentType) { |
268 |
this.browser.setContent(content, contentType);
|
269 |
} |
270 |
|
271 |
@Override
|
272 |
public String getTitle() { |
273 |
return this.browser.getTitle(); |
274 |
} |
275 |
|
276 |
@Override
|
277 |
public JComponent asJComponent() { |
278 |
return this; |
279 |
} |
280 |
} |