Révision 3108
tmp/org.txm.libs.javafx/swt/JFXBrowserText.java (revision 3108) | ||
---|---|---|
1 |
package snippet; |
|
2 |
|
|
3 |
import java.io.PrintWriter; |
|
4 |
import java.io.StringWriter; |
|
5 |
|
|
6 |
import org.eclipse.jface.resource.JFaceResources; |
|
7 |
import org.eclipse.swt.SWT; |
|
8 |
import org.eclipse.swt.custom.BusyIndicator; |
|
9 |
import org.eclipse.swt.events.SelectionListener; |
|
10 |
import org.eclipse.swt.graphics.Color; |
|
11 |
import org.eclipse.swt.graphics.Rectangle; |
|
12 |
import org.eclipse.swt.layout.GridData; |
|
13 |
import org.eclipse.swt.layout.GridLayout; |
|
14 |
import org.eclipse.swt.widgets.Button; |
|
15 |
import org.eclipse.swt.widgets.Composite; |
|
16 |
import org.eclipse.swt.widgets.Control; |
|
17 |
import org.eclipse.swt.widgets.Label; |
|
18 |
import org.eclipse.swt.widgets.Link; |
|
19 |
import org.eclipse.swt.widgets.Text; |
|
20 |
import org.eclipse.ui.internal.browser.FallbackScrolledComposite; |
|
21 |
import org.eclipse.ui.internal.browser.IBrowserViewerContainer; |
|
22 |
|
|
23 |
public class JFXBrowserText { |
|
24 |
|
|
25 |
private String url; |
|
26 |
|
|
27 |
private FallbackScrolledComposite scomp; |
|
28 |
|
|
29 |
private Label title; |
|
30 |
|
|
31 |
private Label exTitle; |
|
32 |
|
|
33 |
private Label text; |
|
34 |
|
|
35 |
private Label sep; |
|
36 |
|
|
37 |
protected Link link; |
|
38 |
|
|
39 |
private JFXBrowserViewer viewer; |
|
40 |
|
|
41 |
private Button button; |
|
42 |
|
|
43 |
private Text exception; |
|
44 |
|
|
45 |
private boolean expanded; |
|
46 |
|
|
47 |
private Throwable ex; |
|
48 |
|
|
49 |
class ReflowScrolledComposite extends FallbackScrolledComposite { |
|
50 |
|
|
51 |
public ReflowScrolledComposite(Composite parent, int style) { |
|
52 |
super(parent, style); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void reflow(boolean flushCache) { |
|
57 |
updateWidth(this); |
|
58 |
super.reflow(flushCache); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
public JFXBrowserText(Composite parent, JFXBrowserViewer viewer, Throwable ex) { |
|
63 |
this.viewer = viewer; |
|
64 |
this.ex = ex; |
|
65 |
Color bg = parent.getDisplay() |
|
66 |
.getSystemColor(SWT.COLOR_LIST_BACKGROUND); |
|
67 |
scomp = new ReflowScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL); |
|
68 |
Composite client = new Composite(scomp, SWT.NULL); |
|
69 |
fillContent(client, bg); |
|
70 |
scomp.setContent(client); |
|
71 |
scomp.setBackground(bg); |
|
72 |
} |
|
73 |
|
|
74 |
private void fillContent(Composite parent, Color bg) { |
|
75 |
GridLayout layout = new GridLayout(); |
|
76 |
layout.verticalSpacing = 10; |
|
77 |
parent.setLayout(layout); |
|
78 |
title = new Label(parent, SWT.WRAP); |
|
79 |
title.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_title); |
|
80 |
title.setFont(JFaceResources.getHeaderFont()); |
|
81 |
title.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|
82 |
title.setBackground(bg); |
|
83 |
|
|
84 |
link = new Link(parent, SWT.WRAP); |
|
85 |
link.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_link); |
|
86 |
link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|
87 |
link.setToolTipText(org.eclipse.ui.internal.browser.Messages.BrowserText_tooltip); |
|
88 |
link.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { |
|
89 |
BusyIndicator.showWhile(link.getDisplay(), () -> doOpenExternal()); |
|
90 |
})); |
|
91 |
link.setBackground(bg); |
|
92 |
sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); |
|
93 |
sep.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|
94 |
exTitle = new Label(parent, SWT.NULL); |
|
95 |
exTitle.setBackground(bg); |
|
96 |
exTitle.setFont(JFaceResources.getBannerFont()); |
|
97 |
exTitle.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_dtitle); |
|
98 |
exTitle.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|
99 |
text = new Label(parent, SWT.WRAP); |
|
100 |
text.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_text); |
|
101 |
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
|
102 |
text.setBackground(bg); |
|
103 |
button = new Button(parent, SWT.PUSH); |
|
104 |
updateButtonText(); |
|
105 |
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { |
|
106 |
toggleException(); |
|
107 |
})); |
|
108 |
exception = new Text(parent, SWT.MULTI); |
|
109 |
loadExceptionText(); |
|
110 |
GridData gd = new GridData(GridData.FILL_BOTH); |
|
111 |
gd.exclude = true; |
|
112 |
exception.setLayoutData(gd); |
|
113 |
} |
|
114 |
|
|
115 |
private void loadExceptionText() { |
|
116 |
StringWriter swriter = new StringWriter(); |
|
117 |
try (PrintWriter writer = new PrintWriter(swriter)) { |
|
118 |
writer.println(ex.getMessage()); |
|
119 |
ex.printStackTrace(writer); |
|
120 |
} |
|
121 |
exception.setText(swriter.toString()); |
|
122 |
} |
|
123 |
|
|
124 |
protected void toggleException() { |
|
125 |
expanded = !expanded; |
|
126 |
updateButtonText(); |
|
127 |
GridData gd = (GridData) exception.getLayoutData(); |
|
128 |
gd.exclude = !expanded; |
|
129 |
exception.setVisible(expanded); |
|
130 |
refresh(); |
|
131 |
} |
|
132 |
|
|
133 |
private void updateButtonText() { |
|
134 |
if (expanded) |
|
135 |
button.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_button_collapse); |
|
136 |
else |
|
137 |
button.setText(org.eclipse.ui.internal.browser.Messages.BrowserText_button_expand); |
|
138 |
} |
|
139 |
|
|
140 |
protected void updateWidth(Composite parent) { |
|
141 |
Rectangle area = parent.getClientArea(); |
|
142 |
updateWidth(title, area.width); |
|
143 |
updateWidth(text, area.width); |
|
144 |
updateWidth(sep, area.width); |
|
145 |
updateWidth(link, area.width); |
|
146 |
updateWidth(exTitle, area.width); |
|
147 |
updateWidth(exception, area.width); |
|
148 |
} |
|
149 |
|
|
150 |
private void updateWidth(Control c, int width) { |
|
151 |
GridData gd = (GridData) c.getLayoutData(); |
|
152 |
if (gd != null) |
|
153 |
gd.widthHint = width - 10; |
|
154 |
} |
|
155 |
|
|
156 |
protected void doOpenExternal() { |
|
157 |
IBrowserViewerContainer container = viewer.getContainer(); |
|
158 |
if (container != null) |
|
159 |
container.openInExternalBrowser(url); |
|
160 |
} |
|
161 |
|
|
162 |
public Control getControl() { |
|
163 |
return scomp; |
|
164 |
} |
|
165 |
|
|
166 |
public boolean setUrl(String url) { |
|
167 |
this.url = url; |
|
168 |
return true; |
|
169 |
} |
|
170 |
|
|
171 |
public void setFocus() { |
|
172 |
link.setFocus(); |
|
173 |
} |
|
174 |
|
|
175 |
public String getUrl() { |
|
176 |
return url; |
|
177 |
} |
|
178 |
|
|
179 |
public void refresh() { |
|
180 |
scomp.reflow(true); |
|
181 |
} |
|
182 |
} |
|
0 | 183 |
tmp/org.txm.libs.javafx/swt/JFXBrowser.java (revision 3108) | ||
---|---|---|
1 |
package snippet; |
|
2 |
|
|
3 |
import java.util.concurrent.Semaphore; |
|
4 |
|
|
5 |
import org.eclipse.swt.SWT; |
|
6 |
import org.eclipse.swt.SWTError; |
|
7 |
import org.eclipse.swt.browser.CloseWindowListener; |
|
8 |
import org.eclipse.swt.browser.LocationEvent; |
|
9 |
import org.eclipse.swt.browser.LocationListener; |
|
10 |
import org.eclipse.swt.browser.OpenWindowListener; |
|
11 |
import org.eclipse.swt.browser.ProgressEvent; |
|
12 |
import org.eclipse.swt.browser.ProgressListener; |
|
13 |
import org.eclipse.swt.browser.StatusTextListener; |
|
14 |
import org.eclipse.swt.browser.TitleListener; |
|
15 |
import org.eclipse.swt.browser.VisibilityWindowAdapter; |
|
16 |
import org.eclipse.swt.events.KeyEvent; |
|
17 |
import org.eclipse.swt.events.KeyListener; |
|
18 |
import org.eclipse.swt.events.MouseEvent; |
|
19 |
import org.eclipse.swt.events.MouseListener; |
|
20 |
import org.eclipse.swt.graphics.Point; |
|
21 |
import org.eclipse.swt.layout.GridData; |
|
22 |
import org.eclipse.swt.layout.GridLayout; |
|
23 |
import org.eclipse.swt.widgets.Composite; |
|
24 |
import org.eclipse.swt.widgets.Display; |
|
25 |
import org.eclipse.swt.widgets.Event; |
|
26 |
import org.eclipse.swt.widgets.Label; |
|
27 |
import org.eclipse.swt.widgets.Listener; |
|
28 |
import org.eclipse.swt.widgets.ProgressBar; |
|
29 |
import org.eclipse.swt.widgets.Shell; |
|
30 |
import org.eclipse.swt.widgets.Text; |
|
31 |
import org.eclipse.swt.widgets.ToolBar; |
|
32 |
import org.eclipse.swt.widgets.ToolItem; |
|
33 |
|
|
34 |
import javafx.application.Platform; |
|
35 |
import javafx.embed.swt.FXCanvas; |
|
36 |
import javafx.scene.Group; |
|
37 |
import javafx.scene.Scene; |
|
38 |
import javafx.scene.paint.Color; |
|
39 |
import javafx.scene.web.WebView; |
|
40 |
|
|
41 |
/** |
|
42 |
* WebView (webkit) wrapper. |
|
43 |
* not finished, see work to be done in org.eclipse.swt.browser.Webkit.class to make this class fully operational |
|
44 |
* |
|
45 |
* @author mdecorde |
|
46 |
* |
|
47 |
*/ |
|
48 |
public class JFXBrowser extends FXCanvas { |
|
49 |
|
|
50 |
protected static final String NOMEDIA = ""; //$NON-NLS-1$ |
|
51 |
|
|
52 |
private WebView jfxBrowser; |
|
53 |
|
|
54 |
// private GLComposite videoComposite; |
|
55 |
|
|
56 |
public WebView getEmbeddedMediaPlayer() { |
|
57 |
return jfxBrowser; |
|
58 |
} |
|
59 |
|
|
60 |
Semaphore s = new Semaphore(1); |
|
61 |
|
|
62 |
// private FXCanvas fxCanvas; |
|
63 |
|
|
64 |
Point previousP; |
|
65 |
|
|
66 |
public JFXBrowser(Composite parent, int style) { |
|
67 |
super(parent, style); |
|
68 |
GridLayout gl = new GridLayout(1, true); |
|
69 |
gl.horizontalSpacing = 0; |
|
70 |
this.setLayout(gl); |
|
71 |
|
|
72 |
// THE PLAYER |
|
73 |
// if (RuntimeUtil.isMac()) { |
|
74 |
// try { |
|
75 |
// LibC.INSTANCE.setenv("VLC_PLUGIN_PATH", "/Applications/VLC.app/Contents/MacOS/plugins", 1); |
|
76 |
// } |
|
77 |
// catch (Exception ex) { |
|
78 |
// ex.printStackTrace(); |
|
79 |
// } |
|
80 |
// } |
|
81 |
// videoComposite = new GLComposite(this, SWT.NONE, "Video"); |
|
82 |
GridData gdata = new GridData(SWT.FILL, SWT.FILL, true, true); |
|
83 |
this.setLayoutData(gdata); |
|
84 |
jfxBrowser = null; |
|
85 |
|
|
86 |
this.addListener(SWT.Resize, new Listener() { |
|
87 |
|
|
88 |
@Override |
|
89 |
public void handleEvent(Event e) { |
|
90 |
resizeView(); |
|
91 |
} |
|
92 |
}); |
|
93 |
|
|
94 |
// fxCanvas = new FXCanvas(videoComposite, SWT.BORDER); |
|
95 |
// GridData gdata2 = new GridData(SWT.FILL, SWT.FILL, true, true); |
|
96 |
// fxCanvas.setLayoutData(gdata2); |
|
97 |
|
|
98 |
initializeBrowser(); |
|
99 |
} |
|
100 |
|
|
101 |
// @Override |
|
102 |
// public void addMouseListener(MouseListener listener) { |
|
103 |
// // jfxBrowser.setOnMousePressed(new EventHandler<MouseEvent>() { |
|
104 |
// // |
|
105 |
// // @Override |
|
106 |
// // public void handle(MouseEvent event) { |
|
107 |
// // // TODO Auto-generated method stub |
|
108 |
// // |
|
109 |
// // } |
|
110 |
// // }); |
|
111 |
// fxCanvas.addMouseListener(listener); |
|
112 |
// } |
|
113 |
|
|
114 |
protected void resizeView() { |
|
115 |
if (jfxBrowser != null) { |
|
116 |
Point p = this.getSize(); |
|
117 |
// if (previousP == null || (p.x != previousP.x && p.y != previousP.y)) { // ensure size changed |
|
118 |
jfxBrowser.setPrefHeight(p.y); |
|
119 |
// jfxBrowser.setMaxWidth(p.x); |
|
120 |
jfxBrowser.setPrefWidth(p.x); |
|
121 |
previousP = p; |
|
122 |
// } |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
private Group group; |
|
127 |
|
|
128 |
private Scene scene; |
|
129 |
|
|
130 |
protected boolean initializeBrowser() { |
|
131 |
|
|
132 |
Platform.setImplicitExit(false); |
|
133 |
jfxBrowser = new WebView(); |
|
134 |
|
|
135 |
group = new Group(jfxBrowser); |
|
136 |
|
|
137 |
// scene = new Scene(group, Color.rgb(fxCanvas.getBackground().getRed(), fxCanvas.getBackground().getGreen(), fxCanvas.getBackground().getBlue())); |
|
138 |
scene = new Scene(group, Color.rgb(0, 0, 0)); |
|
139 |
|
|
140 |
this.setScene(jfxBrowser.getScene()); |
|
141 |
|
|
142 |
return true; |
|
143 |
} |
|
144 |
|
|
145 |
public boolean back() { |
|
146 |
try { |
|
147 |
jfxBrowser.getEngine().getHistory().go(-1); |
|
148 |
return true; |
|
149 |
} |
|
150 |
catch (Exception e) { |
|
151 |
return false; |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
public boolean forward() { |
|
156 |
try { |
|
157 |
jfxBrowser.getEngine().getHistory().go(1); |
|
158 |
return true; |
|
159 |
} |
|
160 |
catch (Exception e) { |
|
161 |
return false; |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
public void stop() { |
|
166 |
// jfxBrowser.getEngine().canc(); |
|
167 |
System.out.println("TODO JFXBrowser.stop..."); |
|
168 |
} |
|
169 |
|
|
170 |
public void refresh() { |
|
171 |
jfxBrowser.getEngine().reload(); |
|
172 |
} |
|
173 |
|
|
174 |
public void setUrl(String url) { |
|
175 |
jfxBrowser.getEngine().load(url); |
|
176 |
} |
|
177 |
|
|
178 |
public void addProgressListener(ProgressListener progressListener) { |
|
179 |
// TODO Auto-generated method stub |
|
180 |
System.out.println("TODO JFXBrowser.addProgressListener(" + progressListener + ")"); |
|
181 |
} |
|
182 |
|
|
183 |
public void addStatusTextListener(StatusTextListener listener) { |
|
184 |
// TODO Auto-generated method stub |
|
185 |
System.out.println("TODO JFXBrowser.addStatusTextListener(" + listener + ")"); |
|
186 |
} |
|
187 |
|
|
188 |
public void addLocationListener(LocationListener locationListener) { |
|
189 |
// TODO Auto-generated method stub |
|
190 |
System.out.println("TODO JFXBrowser.addLocationListener(" + locationListener + ")"); |
|
191 |
} |
|
192 |
|
|
193 |
public void addOpenWindowListener(OpenWindowListener listener) { |
|
194 |
// TODO Auto-generated method stub |
|
195 |
System.out.println("TODO JFXBrowser.addOpenWindowListener(" + listener + ")"); |
|
196 |
} |
|
197 |
|
|
198 |
public void addVisibilityWindowListener(VisibilityWindowAdapter visibilityWindowAdapter) { |
|
199 |
// TODO Auto-generated method stub |
|
200 |
System.out.println("TODO JFXBrowser.addVisibilityWindowListener(" + visibilityWindowAdapter + ")"); |
|
201 |
} |
|
202 |
|
|
203 |
public void addCloseWindowListener(CloseWindowListener listener) { |
|
204 |
// TODO Auto-generated method stub |
|
205 |
System.out.println("TODO JFXBrowser.addCloseWindowListener(" + listener + ")"); |
|
206 |
} |
|
207 |
|
|
208 |
public void addTitleListener(TitleListener listener) { |
|
209 |
// TODO Auto-generated method stub |
|
210 |
System.out.println("TODO JFXBrowser.addTitleListener(" + listener + ")"); |
|
211 |
} |
|
212 |
|
|
213 |
public void removeLocationListener(LocationListener locationListener) { |
|
214 |
// TODO Auto-generated method stub |
|
215 |
System.out.println("TODO JFXBrowser.removeLocationListener(" + locationListener + ")"); |
|
216 |
} |
|
217 |
|
|
218 |
public static void main(String[] args) { |
|
219 |
Display display = new Display(); |
|
220 |
final Shell shell = new Shell(display); |
|
221 |
shell.setText("Snippet 128"); |
|
222 |
GridLayout gridLayout = new GridLayout(); |
|
223 |
gridLayout.numColumns = 3; |
|
224 |
shell.setLayout(gridLayout); |
|
225 |
ToolBar toolbar = new ToolBar(shell, SWT.NONE); |
|
226 |
ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH); |
|
227 |
itemBack.setText("Back"); |
|
228 |
ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH); |
|
229 |
itemForward.setText("Forward"); |
|
230 |
ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); |
|
231 |
itemStop.setText("Stop"); |
|
232 |
ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); |
|
233 |
itemRefresh.setText("Refresh"); |
|
234 |
ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); |
|
235 |
itemGo.setText("Go"); |
|
236 |
|
|
237 |
GridData data = new GridData(); |
|
238 |
data.horizontalSpan = 3; |
|
239 |
toolbar.setLayoutData(data); |
|
240 |
|
|
241 |
Label labelAddress = new Label(shell, SWT.NONE); |
|
242 |
labelAddress.setText("Address"); |
|
243 |
|
|
244 |
final Text location = new Text(shell, SWT.BORDER); |
|
245 |
data = new GridData(); |
|
246 |
data.horizontalAlignment = GridData.FILL; |
|
247 |
data.horizontalSpan = 2; |
|
248 |
data.grabExcessHorizontalSpace = true; |
|
249 |
location.setLayoutData(data); |
|
250 |
|
|
251 |
final JFXBrowser browser; |
|
252 |
try { |
|
253 |
browser = new JFXBrowser(shell, SWT.NONE); |
|
254 |
} |
|
255 |
catch (SWTError e) { |
|
256 |
System.out.println("Could not instantiate Browser: " + e.getMessage()); |
|
257 |
display.dispose(); |
|
258 |
return; |
|
259 |
} |
|
260 |
data = new GridData(); |
|
261 |
data.horizontalAlignment = GridData.FILL; |
|
262 |
data.verticalAlignment = GridData.FILL; |
|
263 |
data.horizontalSpan = 3; |
|
264 |
data.grabExcessHorizontalSpace = true; |
|
265 |
data.grabExcessVerticalSpace = true; |
|
266 |
browser.setLayoutData(data); |
|
267 |
|
|
268 |
final Label status = new Label(shell, SWT.NONE); |
|
269 |
data = new GridData(GridData.FILL_HORIZONTAL); |
|
270 |
data.horizontalSpan = 2; |
|
271 |
status.setLayoutData(data); |
|
272 |
|
|
273 |
final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE); |
|
274 |
data = new GridData(); |
|
275 |
data.horizontalAlignment = GridData.END; |
|
276 |
progressBar.setLayoutData(data); |
|
277 |
|
|
278 |
/* event handling */ |
|
279 |
Listener listener = event -> { |
|
280 |
ToolItem item = (ToolItem) event.widget; |
|
281 |
String string = item.getText(); |
|
282 |
if (string.equals("Back")) |
|
283 |
browser.back(); |
|
284 |
else if (string.equals("Forward")) |
|
285 |
browser.forward(); |
|
286 |
else if (string.equals("Stop")) |
|
287 |
browser.stop(); |
|
288 |
else if (string.equals("Refresh")) |
|
289 |
browser.refresh(); |
|
290 |
else if (string.equals("Go")) |
|
291 |
browser.setUrl(location.getText()); |
|
292 |
}; |
|
293 |
browser.addProgressListener(new ProgressListener() { |
|
294 |
|
|
295 |
@Override |
|
296 |
public void changed(ProgressEvent event) { |
|
297 |
if (event.total == 0) return; |
|
298 |
int ratio = event.current * 100 / event.total; |
|
299 |
progressBar.setSelection(ratio); |
|
300 |
} |
|
301 |
|
|
302 |
@Override |
|
303 |
public void completed(ProgressEvent event) { |
|
304 |
progressBar.setSelection(0); |
|
305 |
} |
|
306 |
}); |
|
307 |
|
|
308 |
browser.addLocationListener(new LocationListener() { |
|
309 |
|
|
310 |
@Override |
|
311 |
public void changing(LocationEvent event) {} |
|
312 |
|
|
313 |
@Override |
|
314 |
public void changed(LocationEvent event) { |
|
315 |
if (event.top) location.setText(event.location); |
|
316 |
} |
|
317 |
}); |
|
318 |
// browser.addLocationListener(LocationListener.changed(event -> { |
|
319 |
// if (event.top) location.setText(event.location); |
|
320 |
// })); |
|
321 |
itemBack.addListener(SWT.Selection, listener); |
|
322 |
itemForward.addListener(SWT.Selection, listener); |
|
323 |
itemStop.addListener(SWT.Selection, listener); |
|
324 |
itemRefresh.addListener(SWT.Selection, listener); |
|
325 |
itemGo.addListener(SWT.Selection, listener); |
|
326 |
location.addListener(SWT.DefaultSelection, e -> browser.setUrl(location.getText())); |
|
327 |
|
|
328 |
browser.addMouseListener(new MouseListener() { |
|
329 |
|
|
330 |
@Override |
|
331 |
public void mouseUp(MouseEvent e) { |
|
332 |
System.out.println("M UP"); |
|
333 |
} |
|
334 |
|
|
335 |
@Override |
|
336 |
public void mouseDown(MouseEvent e) { |
|
337 |
System.out.println("M DOWN"); |
|
338 |
} |
|
339 |
|
|
340 |
@Override |
|
341 |
public void mouseDoubleClick(MouseEvent e) { |
|
342 |
System.out.println("M DCLICK"); |
|
343 |
} |
|
344 |
}); |
|
345 |
|
|
346 |
browser.addKeyListener(new KeyListener() { |
|
347 |
|
|
348 |
@Override |
|
349 |
public void keyReleased(KeyEvent e) {} |
|
350 |
|
|
351 |
@Override |
|
352 |
public void keyPressed(KeyEvent e) { |
|
353 |
System.out.println("K " + e); |
|
354 |
} |
|
355 |
}); |
|
356 |
|
|
357 |
shell.open(); |
|
358 |
// browser.setUrl("http://eclipse.org"); |
|
359 |
|
|
360 |
while (!shell.isDisposed()) { |
|
361 |
if (!display.readAndDispatch()) |
|
362 |
display.sleep(); |
|
363 |
} |
|
364 |
display.dispose(); |
|
365 |
} |
|
366 |
|
|
367 |
public WebView getWebView() { |
|
368 |
return this.jfxBrowser; |
|
369 |
} |
|
370 |
|
|
371 |
public String getUrl() { |
|
372 |
return jfxBrowser.getEngine().getLocation(); |
|
373 |
} |
|
374 |
|
|
375 |
public boolean isBackEnabled() { |
|
376 |
return jfxBrowser.getEngine().getHistory().getCurrentIndex() > 0; |
|
377 |
} |
|
378 |
|
|
379 |
public boolean isForwardEnabled() { |
|
380 |
return jfxBrowser.getEngine().getHistory().getCurrentIndex() < jfxBrowser.getEngine().getHistory().getEntries().size(); |
|
381 |
} |
|
382 |
|
|
383 |
public boolean setUrl(String url, String postData, String[] headers) { |
|
384 |
// TODO Auto-generated method stub |
|
385 |
jfxBrowser.getEngine().load(url); |
|
386 |
return true; |
|
387 |
} |
|
388 |
} |
|
0 | 389 |
tmp/org.txm.libs.javafx/swt/TXMJFXBrowserEditor.java (revision 3108) | ||
---|---|---|
1 |
/******************************************************************************* |
|
2 |
* Copyright (c) 2003, 2016 IBM Corporation and others. |
|
3 |
* All rights reserved. This program and the accompanying materials |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
5 |
* which accompanies this distribution, and is available at |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
7 |
* |
|
8 |
* Contributors: |
|
9 |
* IBM Corporation - Initial API and implementation |
|
10 |
* |
|
11 |
* |
|
12 |
*******************************************************************************/ |
|
13 |
package snippet; |
|
14 |
|
|
15 |
import java.beans.PropertyChangeListener; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.net.URL; |
|
18 |
|
|
19 |
import org.eclipse.core.runtime.Adapters; |
|
20 |
import org.eclipse.core.runtime.IPath; |
|
21 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
22 |
import org.eclipse.jface.action.IAction; |
|
23 |
import org.eclipse.jface.resource.ImageDescriptor; |
|
24 |
import org.eclipse.osgi.util.NLS; |
|
25 |
import org.eclipse.swt.graphics.Image; |
|
26 |
import org.eclipse.swt.widgets.Composite; |
|
27 |
import org.eclipse.swt.widgets.Display; |
|
28 |
import org.eclipse.ui.IActionBars; |
|
29 |
import org.eclipse.ui.IEditorDescriptor; |
|
30 |
import org.eclipse.ui.IEditorInput; |
|
31 |
import org.eclipse.ui.IEditorPart; |
|
32 |
import org.eclipse.ui.IEditorReference; |
|
33 |
import org.eclipse.ui.IEditorRegistry; |
|
34 |
import org.eclipse.ui.IEditorSite; |
|
35 |
import org.eclipse.ui.IPathEditorInput; |
|
36 |
import org.eclipse.ui.IWorkbenchPage; |
|
37 |
import org.eclipse.ui.IWorkbenchWindow; |
|
38 |
import org.eclipse.ui.PartInitException; |
|
39 |
import org.eclipse.ui.PlatformUI; |
|
40 |
import org.eclipse.ui.browser.IWorkbenchBrowserSupport; |
|
41 |
import org.eclipse.ui.internal.browser.BrowserViewer; |
|
42 |
import org.eclipse.ui.internal.browser.IBrowserViewerContainer; |
|
43 |
import org.eclipse.ui.internal.browser.ImageResource; |
|
44 |
import org.eclipse.ui.internal.browser.TextAction; |
|
45 |
import org.eclipse.ui.internal.browser.Trace; |
|
46 |
import org.eclipse.ui.internal.browser.WebBrowserEditorInput; |
|
47 |
import org.eclipse.ui.internal.browser.WebBrowserUIPlugin; |
|
48 |
import org.eclipse.ui.part.EditorPart; |
|
49 |
|
|
50 |
/** |
|
51 |
* An integrated JFX Web browser (webkit), defined as an editor to make better use of the desktop. |
|
52 |
* |
|
53 |
* TODO the JFXBrowser has to implements lots of event trigger to be fully functionnal |
|
54 |
*/ |
|
55 |
public class TXMJFXBrowserEditor extends EditorPart implements IBrowserViewerContainer { |
|
56 |
|
|
57 |
protected static final String PROPERTY_TITLE = "title"; //$NON-NLS-1$ |
|
58 |
|
|
59 |
public static final String WEB_BROWSER_EDITOR_ID = "org.txm.rcp.editors.TXMJFXBrowserEditor"; //$NON-NLS-1$ |
|
60 |
|
|
61 |
protected JFXBrowserViewer webBrowser; |
|
62 |
|
|
63 |
protected String initialURL; |
|
64 |
|
|
65 |
protected Image image; |
|
66 |
|
|
67 |
protected TextAction cutAction; |
|
68 |
|
|
69 |
protected TextAction copyAction; |
|
70 |
|
|
71 |
protected TextAction pasteAction; |
|
72 |
|
|
73 |
private boolean disposed; |
|
74 |
|
|
75 |
private boolean lockName; |
|
76 |
|
|
77 |
/** |
|
78 |
* WebBrowserEditor constructor comment. |
|
79 |
*/ |
|
80 |
public TXMJFXBrowserEditor() { |
|
81 |
super(); |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public void createPartControl(Composite parent) { |
|
86 |
WebBrowserEditorInput input = getWebBrowserEditorInput(); |
|
87 |
|
|
88 |
int style = 0; |
|
89 |
if (input == null || input.isLocationBarLocal()) { |
|
90 |
style += BrowserViewer.LOCATION_BAR; |
|
91 |
} |
|
92 |
if (input == null || input.isToolbarLocal()) { |
|
93 |
style += BrowserViewer.BUTTON_BAR; |
|
94 |
} |
|
95 |
webBrowser = new JFXBrowserViewer(parent, style); |
|
96 |
|
|
97 |
webBrowser.setURL(initialURL); |
|
98 |
webBrowser.setContainer(this); |
|
99 |
|
|
100 |
if (input == null || input.isLocationBarLocal()) { |
|
101 |
// cutAction = new TextAction(webBrowser, TextAction.CUT); |
|
102 |
// copyAction = new TextAction(webBrowser, TextAction.COPY); |
|
103 |
// pasteAction = new TextAction(webBrowser, TextAction.PASTE); |
|
104 |
} |
|
105 |
|
|
106 |
if (!lockName) { |
|
107 |
PropertyChangeListener propertyChangeListener = event -> { |
|
108 |
if (PROPERTY_TITLE.equals(event.getPropertyName())) { |
|
109 |
setPartName((String) event.getNewValue()); |
|
110 |
} |
|
111 |
}; |
|
112 |
webBrowser.addPropertyChangeListener(propertyChangeListener); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public void setPartName(String partName) { |
|
118 |
super.setPartName(partName); |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
public void setTitleImage(Image titleImage) { |
|
123 |
super.setTitleImage(titleImage); |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public void dispose() { |
|
128 |
if (image != null && !image.isDisposed()) |
|
129 |
image.dispose(); |
|
130 |
image = null; |
|
131 |
|
|
132 |
super.dispose(); |
|
133 |
// mark this instance as disposed to avoid stale references |
|
134 |
disposed = true; |
|
135 |
} |
|
136 |
|
|
137 |
public boolean isDisposed() { |
|
138 |
return disposed; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public void doSave(IProgressMonitor monitor) { |
|
143 |
// do nothing |
|
144 |
} |
|
145 |
|
|
146 |
@Override |
|
147 |
public void doSaveAs() { |
|
148 |
// do nothing |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* Returns the copy action. |
|
153 |
* |
|
154 |
* @return org.eclipse.jface.action.IAction |
|
155 |
*/ |
|
156 |
public IAction getCopyAction() { |
|
157 |
return copyAction; |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* Returns the cut action. |
|
162 |
* |
|
163 |
* @return org.eclipse.jface.action.IAction |
|
164 |
*/ |
|
165 |
public IAction getCutAction() { |
|
166 |
return cutAction; |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Returns the web editor input, if available. If the input was of |
|
171 |
* another type, <code>null</code> is returned. |
|
172 |
* |
|
173 |
* @return org.eclipse.ui.internal.browser.IWebBrowserEditorInput |
|
174 |
*/ |
|
175 |
protected WebBrowserEditorInput getWebBrowserEditorInput() { |
|
176 |
IEditorInput input = getEditorInput(); |
|
177 |
if (input instanceof WebBrowserEditorInput) |
|
178 |
return (WebBrowserEditorInput) input; |
|
179 |
return null; |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* Returns the paste action. |
|
184 |
* |
|
185 |
* @return org.eclipse.jface.action.IAction |
|
186 |
*/ |
|
187 |
public IAction getPasteAction() { |
|
188 |
return pasteAction; |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
public void init(IEditorSite site, IEditorInput input) throws PartInitException { |
|
193 |
Trace.trace(Trace.FINEST, "Opening browser: " + input); //$NON-NLS-1$ |
|
194 |
if (input instanceof IPathEditorInput) { |
|
195 |
IPathEditorInput pei = (IPathEditorInput) input; |
|
196 |
final IPath path = pei.getPath(); |
|
197 |
URL url = null; |
|
198 |
try { |
|
199 |
if (path != null) { |
|
200 |
setPartName(path.lastSegment()); |
|
201 |
url = path.toFile().toURI().toURL(); |
|
202 |
} |
|
203 |
if (url != null) |
|
204 |
initialURL = url.toExternalForm(); |
|
205 |
} |
|
206 |
catch (Exception e) { |
|
207 |
Trace.trace(Trace.SEVERE, "Error getting URL to file"); //$NON-NLS-1$ |
|
208 |
} |
|
209 |
if (webBrowser != null) { |
|
210 |
if (initialURL != null) |
|
211 |
webBrowser.setURL(initialURL); |
|
212 |
site.getWorkbenchWindow().getActivePage().activate(this); |
|
213 |
} |
|
214 |
|
|
215 |
if (url != null) |
|
216 |
setTitleToolTip(url.getFile()); |
|
217 |
|
|
218 |
Image oldImage = image; |
|
219 |
ImageDescriptor id = ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER); |
|
220 |
image = id.createImage(); |
|
221 |
|
|
222 |
setTitleImage(image); |
|
223 |
if (oldImage != null && !oldImage.isDisposed()) |
|
224 |
oldImage.dispose(); |
|
225 |
// addResourceListener(file); |
|
226 |
} |
|
227 |
else if (input instanceof WebBrowserEditorInput) { |
|
228 |
WebBrowserEditorInput wbei = (WebBrowserEditorInput) input; |
|
229 |
initialURL = null; |
|
230 |
if (wbei.getURL() != null) |
|
231 |
initialURL = wbei.getURL().toExternalForm(); |
|
232 |
if (webBrowser != null) { |
|
233 |
webBrowser.setURL(initialURL); |
|
234 |
site.getWorkbenchWindow().getActivePage().activate(this); |
|
235 |
} |
|
236 |
|
|
237 |
setPartName(wbei.getName()); |
|
238 |
setTitleToolTip(wbei.getToolTipText()); |
|
239 |
lockName = false; |
|
240 |
|
|
241 |
Image oldImage = image; |
|
242 |
ImageDescriptor id = wbei.getImageDescriptor(); |
|
243 |
image = id.createImage(); |
|
244 |
|
|
245 |
setTitleImage(image); |
|
246 |
if (oldImage != null && !oldImage.isDisposed()) |
|
247 |
oldImage.dispose(); |
|
248 |
} |
|
249 |
else { |
|
250 |
IPathEditorInput pinput = Adapters.adapt(input, IPathEditorInput.class); |
|
251 |
if (pinput != null) { |
|
252 |
init(site, pinput); |
|
253 |
} |
|
254 |
else { |
|
255 |
throw new PartInitException(NLS.bind(org.eclipse.ui.internal.browser.Messages.errorInvalidEditorInput, input.getName())); |
|
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
setSite(site); |
|
260 |
setInput(input); |
|
261 |
} |
|
262 |
|
|
263 |
@Override |
|
264 |
public boolean isDirty() { |
|
265 |
return false; |
|
266 |
} |
|
267 |
|
|
268 |
@Override |
|
269 |
public boolean isSaveAsAllowed() { |
|
270 |
return false; |
|
271 |
} |
|
272 |
|
|
273 |
/** |
|
274 |
* Open the input in the internal Web browser. |
|
275 |
*/ |
|
276 |
public static void open(WebBrowserEditorInput input) { |
|
277 |
IWorkbenchWindow workbenchWindow = WebBrowserUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow(); |
|
278 |
IWorkbenchPage page = workbenchWindow.getActivePage(); |
|
279 |
|
|
280 |
try { |
|
281 |
IEditorReference[] editors = page.getEditorReferences(); |
|
282 |
int size = editors.length; |
|
283 |
for (int i = 0; i < size; i++) { |
|
284 |
if (WEB_BROWSER_EDITOR_ID.equals(editors[i].getId())) { |
|
285 |
IEditorPart editor = editors[i].getEditor(true); |
|
286 |
if (editor != null && editor instanceof TXMJFXBrowserEditor) { |
|
287 |
TXMJFXBrowserEditor webEditor = (TXMJFXBrowserEditor) editor; |
|
288 |
WebBrowserEditorInput input2 = webEditor.getWebBrowserEditorInput(); |
|
289 |
if (input2 == null || input.canReplaceInput(input2)) { |
|
290 |
editor.init(editor.getEditorSite(), input); |
|
291 |
return; |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
} |
|
296 |
|
|
297 |
page.openEditor(input, TXMJFXBrowserEditor.WEB_BROWSER_EDITOR_ID); |
|
298 |
} |
|
299 |
catch (Exception e) { |
|
300 |
Trace.trace(Trace.SEVERE, "Error opening Web browser", e); //$NON-NLS-1$ |
|
301 |
} |
|
302 |
} |
|
303 |
|
|
304 |
/* |
|
305 |
* Asks this part to take focus within the workbench. |
|
306 |
*/ |
|
307 |
@Override |
|
308 |
public void setFocus() { |
|
309 |
if (webBrowser != null) |
|
310 |
webBrowser.setFocus(); |
|
311 |
} |
|
312 |
|
|
313 |
/** |
|
314 |
* Close the editor correctly. |
|
315 |
*/ |
|
316 |
@Override |
|
317 |
public boolean close() { |
|
318 |
final boolean[] result = new boolean[1]; |
|
319 |
Display.getDefault() |
|
320 |
.asyncExec(() -> result[0] = getEditorSite().getPage().closeEditor(TXMJFXBrowserEditor.this, false)); |
|
321 |
return result[0]; |
|
322 |
} |
|
323 |
|
|
324 |
@Override |
|
325 |
public IActionBars getActionBars() { |
|
326 |
return getEditorSite().getActionBars(); |
|
327 |
} |
|
328 |
|
|
329 |
@Override |
|
330 |
public void openInExternalBrowser(String url) { |
|
331 |
final IEditorInput input = getEditorInput(); |
|
332 |
final String id = getEditorSite().getId(); |
|
333 |
Runnable runnable = () -> doOpenExternalEditor(id, input); |
|
334 |
Display display = getSite().getShell().getDisplay(); |
|
335 |
close(); |
|
336 |
display.asyncExec(runnable); |
|
337 |
} |
|
338 |
|
|
339 |
protected void doOpenExternalEditor(String id, IEditorInput input) { |
|
340 |
IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); |
|
341 |
String name = input.getName(); |
|
342 |
IEditorDescriptor[] editors = registry.getEditors(name); |
|
343 |
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
|
344 |
|
|
345 |
String editorId = null; |
|
346 |
for (IEditorDescriptor editor : editors) { |
|
347 |
if (editor.getId().equals(id)) |
|
348 |
continue; |
|
349 |
editorId = editor.getId(); |
|
350 |
break; |
|
351 |
} |
|
352 |
|
|
353 |
IEditorDescriptor ddesc = registry.getDefaultEditor(name); |
|
354 |
if (ddesc != null && ddesc.getId().equals(id)) { |
|
355 |
int dot = name.lastIndexOf('.'); |
|
356 |
String ext = name; |
|
357 |
if (dot != -1) |
|
358 |
ext = "*." + name.substring(dot + 1); //$NON-NLS-1$ |
|
359 |
registry.setDefaultEditor(ext, null); |
|
360 |
} |
|
361 |
|
|
362 |
if (editorId == null) { |
|
363 |
// no editor |
|
364 |
// next check with the OS for an external editor |
|
365 |
if (registry.isSystemExternalEditorAvailable(name)) |
|
366 |
editorId = IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID; |
|
367 |
} |
|
368 |
|
|
369 |
if (editorId != null) { |
|
370 |
try { |
|
371 |
page.openEditor(input, editorId); |
|
372 |
return; |
|
373 |
} |
|
374 |
catch (PartInitException e) { |
|
375 |
// ignore |
|
376 |
} |
|
377 |
} |
|
378 |
|
|
379 |
// no registered editor - open using browser support |
|
380 |
try { |
|
381 |
URL theURL = new URL(webBrowser.getURL()); |
|
382 |
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); |
|
383 |
support.getExternalBrowser().openURL(theURL); |
|
384 |
} |
|
385 |
catch (MalformedURLException e) { |
|
386 |
// TODO handle this |
|
387 |
} |
|
388 |
catch (PartInitException e) { |
|
389 |
// TODO handle this |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
0 | 393 |
tmp/org.txm.libs.javafx/swt/JFXBrowserViewer.java (revision 3108) | ||
---|---|---|
1 |
package snippet; |
|
2 |
|
|
3 |
import java.beans.PropertyChangeEvent; |
|
4 |
import java.beans.PropertyChangeListener; |
|
5 |
import java.io.File; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
10 |
import org.eclipse.jface.action.IStatusLineManager; |
|
11 |
import org.eclipse.swt.SWT; |
|
12 |
import org.eclipse.swt.SWTError; |
|
13 |
import org.eclipse.swt.SWTException; |
|
14 |
import org.eclipse.swt.browser.Browser; |
|
15 |
import org.eclipse.swt.browser.LocationAdapter; |
|
16 |
import org.eclipse.swt.browser.LocationEvent; |
|
17 |
import org.eclipse.swt.browser.LocationListener; |
|
18 |
import org.eclipse.swt.browser.ProgressEvent; |
|
19 |
import org.eclipse.swt.browser.ProgressListener; |
|
20 |
import org.eclipse.swt.browser.VisibilityWindowAdapter; |
|
21 |
import org.eclipse.swt.browser.WindowEvent; |
|
22 |
import org.eclipse.swt.dnd.Clipboard; |
|
23 |
import org.eclipse.swt.events.MouseAdapter; |
|
24 |
import org.eclipse.swt.events.MouseEvent; |
|
25 |
import org.eclipse.swt.events.SelectionListener; |
|
26 |
import org.eclipse.swt.layout.FillLayout; |
|
27 |
import org.eclipse.swt.layout.GridData; |
|
28 |
import org.eclipse.swt.layout.GridLayout; |
|
29 |
import org.eclipse.swt.widgets.Combo; |
|
30 |
import org.eclipse.swt.widgets.Composite; |
|
31 |
import org.eclipse.swt.widgets.Display; |
|
32 |
import org.eclipse.swt.widgets.Shell; |
|
33 |
import org.eclipse.swt.widgets.ToolBar; |
|
34 |
import org.eclipse.swt.widgets.ToolItem; |
|
35 |
import org.eclipse.ui.PlatformUI; |
|
36 |
import org.eclipse.ui.internal.browser.BusyIndicator; |
|
37 |
import org.eclipse.ui.internal.browser.ContextIds; |
|
38 |
import org.eclipse.ui.internal.browser.IBrowserViewerContainer; |
|
39 |
import org.eclipse.ui.internal.browser.ImageResource; |
|
40 |
import org.eclipse.ui.internal.browser.ToolbarLayout; |
|
41 |
import org.eclipse.ui.internal.browser.Trace; |
|
42 |
import org.eclipse.ui.internal.browser.WebBrowserPreference; |
|
43 |
import org.eclipse.ui.internal.browser.WebBrowserUtil; |
|
44 |
import org.txm.rcp.commands.OpenLocalizedWebPage; |
|
45 |
import org.txm.rcp.swt.JFXBrowser; |
|
46 |
|
|
47 |
/** |
|
48 |
* A Web browser widget. It extends the Eclipse SWT Browser widget by adding an |
|
49 |
* optional toolbar complete with a URL combo box, history, back & forward, and |
|
50 |
* refresh buttons. |
|
51 |
* <p> |
|
52 |
* Use the style bits to choose which toolbars are available within the browser |
|
53 |
* composite. You can access the embedded SWT Browser directly using the |
|
54 |
* getBrowser() method. |
|
55 |
* </p> |
|
56 |
* <p> |
|
57 |
* Additional capabilities are available when used as the internal Web browser, |
|
58 |
* including status text and progress on the Eclipse window's status line, or |
|
59 |
* moving the toolbar capabilities up into the main toolbar. |
|
60 |
* </p> |
|
61 |
* <dl> |
|
62 |
* <dt><b>Styles:</b></dt> |
|
63 |
* <dd>LOCATION_BAR, BUTTON_BAR</dd> |
|
64 |
* <dt><b>Events:</b></dt> |
|
65 |
* <dd>None</dd> |
|
66 |
* </dl> |
|
67 |
* |
|
68 |
* @since 1.0 |
|
69 |
*/ |
|
70 |
public class JFXBrowserViewer extends Composite { |
|
71 |
|
|
72 |
/** |
|
73 |
* Style parameter (value 1) indicating that the URL and Go button will be |
|
74 |
* on the local toolbar. |
|
75 |
*/ |
|
76 |
public static final int LOCATION_BAR = 1 << 1; |
|
77 |
|
|
78 |
/** |
|
79 |
* Style parameter (value 2) indicating that the toolbar will be available |
|
80 |
* on the web browser. This style parameter cannot be used without the |
|
81 |
* LOCATION_BAR style. |
|
82 |
*/ |
|
83 |
public static final int BUTTON_BAR = 1 << 2; |
|
84 |
|
|
85 |
protected static final String PROPERTY_TITLE = "title"; //$NON-NLS-1$ |
|
86 |
|
|
87 |
private static final int MAX_HISTORY = 50; |
|
88 |
|
|
89 |
public Clipboard clipboard; |
|
90 |
|
|
91 |
public Combo combo; |
|
92 |
|
|
93 |
protected boolean showToolbar; |
|
94 |
|
|
95 |
protected boolean showURLbar; |
|
96 |
|
|
97 |
protected ToolItem back; |
|
98 |
|
|
99 |
protected ToolItem forward; |
|
100 |
|
|
101 |
protected BusyIndicator busy; |
|
102 |
|
|
103 |
protected boolean loading; |
|
104 |
|
|
105 |
protected static java.util.List<String> history; |
|
106 |
|
|
107 |
protected JFXBrowser browser; |
|
108 |
|
|
109 |
protected JFXBrowserText text; |
|
110 |
|
|
111 |
protected boolean newWindow; |
|
112 |
|
|
113 |
protected IBrowserViewerContainer container; |
|
114 |
|
|
115 |
protected String title; |
|
116 |
|
|
117 |
protected int progressWorked = 0; |
|
118 |
|
|
119 |
protected List<PropertyChangeListener> propertyListeners; |
|
120 |
|
|
121 |
/** |
|
122 |
* Under development - do not use |
|
123 |
*/ |
|
124 |
public static interface ILocationListener { |
|
125 |
|
|
126 |
public void locationChanged(String url); |
|
127 |
|
|
128 |
public void historyChanged(String[] history2); |
|
129 |
} |
|
130 |
|
|
131 |
public ILocationListener locationListener; |
|
132 |
|
|
133 |
/** |
|
134 |
* Under development - do not use |
|
135 |
*/ |
|
136 |
public static interface IBackNextListener { |
|
137 |
|
|
138 |
public void updateBackNextBusy(); |
|
139 |
} |
|
140 |
|
|
141 |
public IBackNextListener backNextListener; |
|
142 |
|
|
143 |
/** |
|
144 |
* Creates a new Web browser given its parent and a style value describing |
|
145 |
* its behavior and appearance. |
|
146 |
* <p> |
|
147 |
* The style value is either one of the style constants defined in the class |
|
148 |
* header or class <code>SWT</code> which is applicable to instances of |
|
149 |
* this class, or must be built by <em>bitwise OR</em>'ing together (that |
|
150 |
* is, using the <code>int</code> "|" operator) two or more of those |
|
151 |
* <code>SWT</code> style constants. The class description lists the style |
|
152 |
* constants that are applicable to the class. Style bits are also inherited |
|
153 |
* from superclasses. |
|
154 |
* </p> |
|
155 |
* |
|
156 |
* @param parent |
|
157 |
* a composite control which will be the parent of the new |
|
158 |
* instance (cannot be null) |
|
159 |
* @param style |
|
160 |
* the style of control to construct |
|
161 |
*/ |
|
162 |
public JFXBrowserViewer(Composite parent, int style) { |
|
163 |
super(parent, SWT.NONE); |
|
164 |
|
|
165 |
if ((style & LOCATION_BAR) != 0) |
|
166 |
showURLbar = true; |
|
167 |
|
|
168 |
if ((style & BUTTON_BAR) != 0) |
|
169 |
showToolbar = true; |
|
170 |
|
|
171 |
GridLayout layout = new GridLayout(); |
|
172 |
layout.marginHeight = 0; |
|
173 |
layout.marginWidth = 0; |
|
174 |
layout.horizontalSpacing = 0; |
|
175 |
layout.verticalSpacing = 0; |
|
176 |
layout.numColumns = 1; |
|
177 |
setLayout(layout); |
|
178 |
setLayoutData(new GridData(GridData.FILL_BOTH)); |
|
179 |
clipboard = new Clipboard(parent.getDisplay()); |
|
180 |
|
|
181 |
if (showToolbar || showURLbar) { |
|
182 |
Composite toolbarComp = new Composite(this, SWT.NONE); |
|
183 |
toolbarComp.setLayout(new ToolbarLayout()); |
|
184 |
toolbarComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL)); |
|
185 |
|
|
186 |
if (showToolbar) |
|
187 |
createToolbar(toolbarComp); |
|
188 |
|
|
189 |
if (showURLbar) |
|
190 |
createLocationBar(toolbarComp); |
|
191 |
|
|
192 |
if (showToolbar | showURLbar) { |
|
193 |
busy = new BusyIndicator(toolbarComp, SWT.NONE); |
|
194 |
busy.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); |
|
195 |
busy.addMouseListener(new MouseAdapter() { |
|
196 |
|
|
197 |
@Override |
|
198 |
public void mouseDown(MouseEvent e) { |
|
199 |
setURL(OpenLocalizedWebPage.URL_TEXTOMETRIE); |
|
200 |
} |
|
201 |
}); |
|
202 |
} |
|
203 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ContextIds.WEB_BROWSER); |
|
204 |
} |
|
205 |
|
|
206 |
// create a new SWT Web browser widget, checking once again to make sure |
|
207 |
// we can use it in this environment |
|
208 |
// if (WebBrowserUtil.canUseInternalWebBrowser()) |
|
209 |
try { |
|
210 |
this.browser = new JFXBrowser(this, SWT.NONE); |
|
211 |
} |
|
212 |
catch (SWTError e) { |
|
213 |
if (e.code != SWT.ERROR_NO_HANDLES) { |
|
214 |
WebBrowserUtil.openError(org.eclipse.ui.internal.browser.Messages.errorCouldNotLaunchInternalWebBrowser); |
|
215 |
return; |
|
216 |
} |
|
217 |
text = new JFXBrowserText(this, this, e); |
|
218 |
} |
|
219 |
|
|
220 |
if (showURLbar) |
|
221 |
updateHistory(); |
|
222 |
if (showToolbar) |
|
223 |
updateBackNextBusy(); |
|
224 |
|
|
225 |
if (browser != null) { |
|
226 |
browser.setLayoutData(new GridData(GridData.FILL_BOTH)); |
|
227 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(browser, |
|
228 |
ContextIds.WEB_BROWSER); |
|
229 |
} |
|
230 |
else |
|
231 |
text.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); |
|
232 |
|
|
233 |
addBrowserListeners(); |
|
234 |
// listen(); |
|
235 |
} |
|
236 |
|
|
237 |
/** |
|
238 |
* Returns the underlying SWT browser widget. |
|
239 |
* |
|
240 |
* @return the underlying browser |
|
241 |
*/ |
|
242 |
public JFXBrowser getBrowser() { |
|
243 |
return browser; |
|
244 |
} |
|
245 |
|
|
246 |
/** |
|
247 |
* Navigate to the home URL. |
|
248 |
*/ |
|
249 |
public void home() { |
|
250 |
System.out.println("JFXBrowserViewer.home()"); |
|
251 |
// browser.getWebView().setH.set.setText(""); //$NON-NLS-1$ |
|
252 |
} |
|
253 |
|
|
254 |
/** |
|
255 |
* Loads a URL. |
|
256 |
* |
|
257 |
* @param url |
|
258 |
* the URL to be loaded |
|
259 |
* @return true if the operation was successful and false otherwise. |
|
260 |
* @exception IllegalArgumentException |
|
261 |
* <ul> |
|
262 |
* <li>ERROR_NULL_ARGUMENT - if the url is null</li> |
|
263 |
* </ul> |
|
264 |
* @exception SWTException |
|
265 |
* <ul> |
|
266 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the |
|
267 |
* wrong thread</li> |
|
268 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been |
|
269 |
* disposed</li> |
|
270 |
* </ul> |
|
271 |
* @see #getURL() |
|
272 |
*/ |
|
273 |
public void setURL(String url) { |
|
274 |
setURL(url, true); |
|
275 |
} |
|
276 |
|
|
277 |
protected void updateBackNextBusy() { |
|
278 |
if (!back.isDisposed()) { |
|
279 |
back.setEnabled(isBackEnabled()); |
|
280 |
} |
|
281 |
if (!forward.isDisposed()) { |
|
282 |
forward.setEnabled(isForwardEnabled()); |
|
283 |
} |
|
284 |
if (!busy.isDisposed()) { |
|
285 |
busy.setBusy(loading); |
|
286 |
} |
|
287 |
|
|
288 |
if (backNextListener != null) |
|
289 |
backNextListener.updateBackNextBusy(); |
|
290 |
} |
|
291 |
|
|
292 |
protected void updateLocation() { |
|
293 |
if (locationListener != null) |
|
294 |
locationListener.historyChanged(null); |
|
295 |
|
|
296 |
if (locationListener != null) |
|
297 |
locationListener.locationChanged(null); |
|
298 |
} |
|
299 |
|
|
300 |
/** |
|
301 |
* |
|
302 |
*/ |
|
303 |
private void addBrowserListeners() { |
|
304 |
if (browser == null) return; |
|
305 |
// respond to ExternalBrowserInstance StatusTextEvents events by |
|
306 |
// updating the status line |
|
307 |
browser.addStatusTextListener(event -> { |
|
308 |
// System.out.println("status: " + event.text); //$NON-NLS-1$ |
|
309 |
if (container != null) { |
|
310 |
IStatusLineManager status = container.getActionBars().getStatusLineManager(); |
|
311 |
status.setMessage(event.text); |
|
312 |
} |
|
313 |
}); |
|
314 |
|
|
315 |
// Add listener for new window creation so that we can instead of |
|
316 |
// opening a separate |
|
317 |
// new window in which the session is lost, we can instead open a new |
|
318 |
// window in a new |
|
319 |
// shell within the browser area thereby maintaining the session. |
|
320 |
browser.addOpenWindowListener(event -> { |
|
321 |
Shell shell2 = new Shell(getShell(), SWT.SHELL_TRIM); |
|
322 |
shell2.setLayout(new FillLayout()); |
|
323 |
shell2.setText(org.eclipse.ui.internal.browser.Messages.viewWebBrowserTitle); |
|
324 |
shell2.setImage(getShell().getImage()); |
|
325 |
if (event.location != null) |
|
326 |
shell2.setLocation(event.location); |
|
327 |
if (event.size != null) |
|
328 |
shell2.setSize(event.size); |
|
329 |
int style = 0; |
|
330 |
if (showURLbar) |
|
331 |
style += LOCATION_BAR; |
|
332 |
if (showToolbar) |
|
333 |
style += BUTTON_BAR; |
|
334 |
JFXBrowserViewer browser2 = new JFXBrowserViewer(shell2, style); |
|
335 |
browser2.newWindow = true; |
|
336 |
event.browser = null; // TODO should be browser2 |
|
337 |
}); |
|
338 |
|
|
339 |
browser.addVisibilityWindowListener(new VisibilityWindowAdapter() { |
|
340 |
|
|
341 |
@Override |
|
342 |
public void show(WindowEvent e) { |
|
343 |
Browser browser2 = (Browser) e.widget; |
|
344 |
if (browser2.getParent().getParent() instanceof Shell) { |
|
345 |
Shell shell = (Shell) browser2.getParent().getParent(); |
|
346 |
if (e.location != null) |
|
347 |
shell.setLocation(e.location); |
|
348 |
if (e.size != null) |
|
349 |
shell.setSize(shell.computeSize(e.size.x, e.size.y)); |
|
350 |
shell.open(); |
|
351 |
} |
|
352 |
} |
|
353 |
}); |
|
354 |
|
|
355 |
browser.addCloseWindowListener(event -> { |
|
356 |
// if shell is not null, it must be a secondary popup window, |
|
357 |
// else its an editor window |
|
358 |
if (newWindow) |
|
359 |
getShell().dispose(); |
|
360 |
else |
|
361 |
container.close(); |
|
362 |
}); |
|
363 |
|
|
364 |
browser.addProgressListener(new ProgressListener() { |
|
365 |
|
|
366 |
@Override |
|
367 |
public void changed(ProgressEvent event) { |
|
368 |
// System.out.println("progress: " + event.current + ", " + event.total); //$NON-NLS-1$ //$NON-NLS-2$ |
|
369 |
if (event.total == 0) |
|
370 |
return; |
|
371 |
|
|
372 |
boolean done = (event.current == event.total); |
|
373 |
|
|
374 |
int percentProgress = event.current * 100 / event.total; |
|
375 |
if (container != null) { |
|
376 |
IProgressMonitor monitor = container.getActionBars() |
|
377 |
.getStatusLineManager().getProgressMonitor(); |
|
378 |
if (done) { |
|
379 |
monitor.done(); |
|
380 |
progressWorked = 0; |
|
381 |
} |
|
382 |
else if (progressWorked == 0) { |
|
383 |
monitor.beginTask("", event.total); //$NON-NLS-1$ |
|
384 |
progressWorked = percentProgress; |
|
385 |
} |
|
386 |
else { |
|
387 |
monitor.worked(event.current - progressWorked); |
|
388 |
progressWorked = event.current; |
|
389 |
} |
|
390 |
} |
|
391 |
|
|
392 |
if (showToolbar) { |
|
393 |
if (!busy.isBusy() && !done) |
|
394 |
loading = true; |
|
395 |
else if (busy.isBusy() && done) // once the progress hits |
|
396 |
// 100 percent, done, set |
|
397 |
// busy to false |
|
398 |
loading = false; |
|
399 |
|
|
400 |
// System.out.println("loading: " + loading); //$NON-NLS-1$ |
|
401 |
updateBackNextBusy(); |
|
402 |
updateHistory(); |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
406 |
@Override |
|
407 |
public void completed(ProgressEvent event) { |
|
408 |
if (container != null) { |
|
409 |
IProgressMonitor monitor = container.getActionBars() |
|
410 |
.getStatusLineManager().getProgressMonitor(); |
|
411 |
monitor.done(); |
|
412 |
} |
|
413 |
if (showToolbar) { |
|
414 |
loading = false; |
|
415 |
updateBackNextBusy(); |
|
416 |
updateHistory(); |
|
417 |
} |
|
418 |
} |
|
419 |
}); |
|
420 |
|
|
421 |
if (showURLbar) { |
|
422 |
browser.addLocationListener(new LocationAdapter() { |
|
423 |
|
|
424 |
@Override |
|
425 |
public void changed(LocationEvent event) { |
|
426 |
if (!event.top) |
|
427 |
return; |
|
428 |
if (combo != null) { |
|
429 |
if (!"about:blank".equals(event.location)) { //$NON-NLS-1$ |
|
430 |
combo.setText(event.location); |
|
431 |
addToHistory(event.location); |
|
432 |
updateHistory(); |
|
433 |
}// else |
|
434 |
// combo.setText(""); //$NON-NLS-1$ |
|
435 |
} |
|
436 |
} |
|
437 |
}); |
|
438 |
} |
|
439 |
|
|
440 |
browser.addTitleListener(event -> { |
|
441 |
String oldTitle = title; |
|
442 |
title = event.title; |
|
443 |
firePropertyChangeEvent(PROPERTY_TITLE, oldTitle, title); |
|
444 |
}); |
|
445 |
} |
|
446 |
|
|
447 |
/** |
|
448 |
* Add a property change listener to this instance. |
|
449 |
* |
|
450 |
* @param listener java.beans.PropertyChangeListener |
|
451 |
*/ |
|
452 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
453 |
if (propertyListeners == null) |
|
454 |
propertyListeners = new ArrayList<>(); |
|
455 |
propertyListeners.add(listener); |
|
456 |
} |
|
457 |
|
|
458 |
/** |
|
459 |
* Remove a property change listener from this instance. |
|
460 |
* |
|
461 |
* @param listener java.beans.PropertyChangeListener |
|
462 |
*/ |
|
463 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
464 |
if (propertyListeners != null) |
|
465 |
propertyListeners.remove(listener); |
|
466 |
} |
|
467 |
|
|
468 |
/** |
|
469 |
* Fire a property change event. |
|
470 |
*/ |
|
471 |
protected void firePropertyChangeEvent(String propertyName, Object oldValue, Object newValue) { |
|
472 |
if (propertyListeners == null) |
|
473 |
return; |
|
474 |
|
|
475 |
PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, oldValue, newValue); |
|
476 |
// Trace.trace("Firing: " + event + " " + oldValue); |
|
477 |
try { |
|
478 |
int size = propertyListeners.size(); |
|
479 |
PropertyChangeListener[] pcl = new PropertyChangeListener[size]; |
|
480 |
propertyListeners.toArray(pcl); |
|
481 |
|
|
482 |
for (int i = 0; i < size; i++) |
|
483 |
try { |
|
484 |
pcl[i].propertyChange(event); |
|
485 |
} |
|
486 |
catch (Exception e) { |
|
487 |
// ignore |
|
488 |
} |
|
489 |
} |
|
490 |
catch (Exception e) { |
|
491 |
// ignore |
|
492 |
} |
|
493 |
} |
|
494 |
|
|
495 |
/** |
|
496 |
* Navigate to the next session history item. Convenience method that calls |
|
497 |
* the underlying SWT browser. |
|
498 |
* |
|
499 |
* @return <code>true</code> if the operation was successful and |
|
500 |
* <code>false</code> otherwise |
|
501 |
* @exception SWTException |
Formats disponibles : Unified diff