Changeset 748 for trunk/src/org/expeditee/items/widgets
- Timestamp:
- 01/24/14 11:32:31 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/JfxBrowser.java
r733 r748 17 17 import java.awt.event.MouseListener; 18 18 import java.awt.event.MouseMotionListener; 19 import java.lang.reflect.InvocationTargetException; 19 20 /* 21 * JavaFX is not on the default java classpath until Java 8 (but is still included with Java 7), so your IDE will probably complain that the imports below can't be resolved. 22 * In Eclipse hitting'Proceed' when told 'Errors exist in project' should allow you to run Expeditee without any issues (although the JFX Browser widget will not display), 23 * or you can just exclude JfxBrowser, WebParser and JfxbrowserActions from the build path. 24 * 25 * If you are using Ant to build/run, 'ant build' will try to build with JavaFX jar added to the classpath. 26 * If this fails, 'ant build-nojfx' will build with the JfxBrowser, WebParser and JfxbrowserActions excluded from the build path. 27 */ 28 29 import javafx.application.Platform; 30 import javafx.beans.value.ChangeListener; 31 import javafx.beans.value.ObservableValue; 32 import javafx.concurrent.Worker.State; 33 import javafx.embed.swing.JFXPanel; 34 import javafx.event.EventHandler; 35 import javafx.scene.Scene; 36 import javafx.scene.web.WebEngine; 37 import javafx.scene.web.WebView; 20 38 21 39 import javax.swing.JButton; … … 35 53 import org.expeditee.items.Item; 36 54 import org.expeditee.items.Text; 37 import org.expeditee.reflection.JavaFX;38 55 import org.expeditee.settings.network.NetworkSettings; 39 56 … … 56 73 57 74 private WebBrowserPanel _browser; 58 59 /** 60 * ID of the mouse button that is currently pressed. Uses {@link MouseEvent} constants 61 */ 62 private int buttonDownId; 75 76 static { 77 Platform.setImplicitExit(false); 78 } 63 79 64 80 private static class WebBrowserPanel extends JPanel implements ComponentListener { … … 66 82 private static final long serialVersionUID = 1L; 67 83 68 Object webview; 69 Object jfxPanel; 84 WebView webview; 85 WebEngine webEngine; 86 JFXPanel jfxPanel; 70 87 JfxBrowser owner; 71 88 … … 87 104 this.addComponentListener(this); 88 105 try { 89 jfxPanel = JavaFX.JFXPanel.newInstance();106 jfxPanel = new JFXPanel(); 90 107 91 108 // Toolbar (that can't be dragged) to hold the nav buttons, url bar, etc … … 258 275 this.add((Component) jfxPanel); 259 276 260 JavaFX.PlatformRunLater.invoke(null,new Runnable() {277 Platform.runLater(new Runnable() { 261 278 @Override 262 279 public void run() { … … 287 304 private void initFx(String url) { 288 305 try { 289 this.webview = JavaFX.WebView.newInstance(); 290 291 Object scene = JavaFX.SceneConstructor.newInstance(this.webview); 292 293 JavaFX.JFXPanelSetScene.invoke(jfxPanel, scene); 294 295 JavaFX.ReadOnlyObjectPropertyAddListener.invoke(JavaFX.WorkerStateProperty.invoke(JavaFX.WebEngineGetLoadWorker 296 .invoke(JavaFX.WebViewGetEngine.invoke(this.webview))), java.lang.reflect.Proxy.newProxyInstance( 297 JavaFX.ChangeListener.getClassLoader(), new java.lang.Class[] { JavaFX.ChangeListener }, 298 new java.lang.reflect.InvocationHandler() { 299 @Override 300 public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) 301 throws java.lang.Throwable { 302 String method_name = method.getName(); 303 // Class<?>[] classes = method.getParameterTypes(); 304 // public void changed(ObservableValue ov, State oldState, State newState) 305 if (method_name.equals("changed")) { 306 // changed takes 3 args 307 if (args == null || args.length != 3) { 308 return null; 309 } 310 // args[0] is the ObservableValue 311 System.out.println(args[0]); 312 System.out.println(args[0].getClass()); 313 // args[2] is the new State 314 if (args[2].getClass() == JavaFX.State) { 315 int id = JavaFX.StateConstants.indexOf(args[2]); 316 switch (id) { 317 case 0: // READY 318 MessageBay.displayMessage("WebEngine ready"); 319 break; 320 case 1: // SCHEDULED 321 MessageBay.displayMessage("Scheduled page load"); 322 break; 323 case 2: // RUNNING 324 MessageBay.displayMessage("WebEngine running"); 325 326 // Updating the URL bar to display the URL of the page being loaded 327 WebBrowserPanel.this.urlField.setText((String) JavaFX.WebEngineGetLocation.invoke(JavaFX.WebViewGetEngine.invoke(WebBrowserPanel.this.webview))); 328 break; 329 case 3: // SUCCEEDED 330 MessageBay.displayMessage("Finished loading page"); 331 break; 332 case 4: // CANCELLED 333 MessageBay.displayMessage("Cancelled loading page"); 334 break; 335 case 5: // FAILED 336 MessageBay.displayMessage("Failed to load page"); 337 break; 338 } 339 } 340 System.out.println("\n"); 341 } 342 return null; 343 } 344 })); 306 this.webview = new WebView(); 307 308 this.webEngine = this.webview.getEngine(); 309 310 Scene scene = new Scene(this.webview); 311 312 this.jfxPanel.setScene(scene); 313 314 this.webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { 315 316 @Override 317 public void changed(ObservableValue<? extends State> ov, State oldState, State newState) { 318 319 switch (newState) { 320 case READY: // READY 321 // MessageBay.displayMessage("WebEngine ready"); 322 break; 323 case SCHEDULED: // SCHEDULED 324 // MessageBay.displayMessage("Scheduled page load"); 325 break; 326 case RUNNING: // RUNNING 327 System.out.println("Loading page!"); 328 329 // Updating the URL bar to display the URL of the page being loaded 330 WebBrowserPanel.this.urlField.setText(WebBrowserPanel.this.webEngine.getLocation()); 331 332 break; 333 case SUCCEEDED: // SUCCEEDED 334 MessageBay.displayMessage("Finished loading page"); 335 break; 336 case CANCELLED: // CANCELLED 337 MessageBay.displayMessage("Cancelled loading page"); 338 break; 339 case FAILED: // FAILED 340 MessageBay.displayMessage("Failed to load page"); 341 break; 342 } 343 } 344 }); 345 345 346 346 // Captures mouse click events on webview to enable expeditee like behavior for JavaFX browser. 347 JavaFX.NodeSetOnMouseClicked.invoke(this.webview, java.lang.reflect.Proxy.newProxyInstance( 348 JavaFX.EventHandler.getClassLoader(), new java.lang.Class[] { JavaFX.EventHandler }, 349 new java.lang.reflect.InvocationHandler() { 350 @Override 351 public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) 352 throws java.lang.Throwable { 353 354 if(method.getName().equals("handle")) { 355 // Gets button clicked enum values 356 Object[] buttonEnum = JavaFX.MouseButton.getEnumConstants(); 357 358 if(JavaFX.MouseEventGetButton.invoke(args[0]).equals(buttonEnum[1])) /* Left Mouse Button */ { 359 360 } else if(JavaFX.MouseEventGetButton.invoke(args[0]).equals(buttonEnum[2])) /* Middle Mouse Button */ { 361 362 } else if(JavaFX.MouseEventGetButton.invoke(args[0]).equals(buttonEnum[3])) /* Right Mouse Button */ { 363 // Gets text currently selected in webview 364 String selection = (String) JavaFX.WebEngineExecuteScript.invoke(JavaFX.WebViewGetEngine.invoke(webview), "window.getSelection().toString()"); 365 366 // Do nothing if no text is selected 367 if(!selection.equals("")) { 368 // Copy text and attach to cursor 369 Text t = new Text(selection); 370 t.setParent(DisplayIO.getCurrentFrame()); 371 t.setXY(FrameMouseActions.getX(), FrameMouseActions.getY()); 372 FrameMouseActions.pickup(t); 373 } 374 } 375 } 376 377 return null; 347 this.webview.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() { 348 @Override 349 public void handle(javafx.scene.input.MouseEvent e) { 350 switch (e.getButton()) { 351 case SECONDARY: 352 // Gets text currently selected in webview 353 String selection = (String) webEngine.executeScript("window.getSelection().toString()"); 354 355 // Do nothing if no text is selected 356 if (!selection.equals("")) { 357 // Copy text and attach to cursor 358 Text t = new Text(selection); 359 t.setParent(DisplayIO.getCurrentFrame()); 360 t.setXY(FrameMouseActions.getX(), FrameMouseActions.getY()); 361 FrameMouseActions.pickup(t); 378 362 } 379 })); 363 364 break; 365 } 366 } 367 }); 380 368 381 369 // Disables JavaFX webview's right click menu 382 JavaFX.WebViewSetContextMenuEnabled.invoke(this.webview, false); 383 384 // webEngine.getLoadWorker().stateProperty().addListener( 385 // new ChangeListener<State>() { 386 // public void changed(ObservableValue ov, State oldState, State newState) { 387 // if (newState == State.SUCCEEDED) { 388 // stage.setTitle(webEngine.getLocation()); 389 // } 390 // } 391 // }); 392 393 JavaFX.WebEngineLoad.invoke(JavaFX.WebViewGetEngine.invoke(this.webview), url); 370 this.webview.setContextMenuEnabled(false); 371 372 this.webEngine.load(url); 394 373 } catch (Exception e) { 395 374 e.printStackTrace(); … … 455 434 System.out.println(actualURL); 456 435 try { 457 JavaFX.PlatformRunLater.invoke(null,new Runnable() {436 Platform.runLater(new Runnable() { 458 437 @Override 459 438 public void run() { 460 439 try { 461 J avaFX.WebEngineLoad.invoke(JavaFX.WebViewGetEngine.invoke(JfxBrowser.this._browser.webview),actualURL);440 JfxBrowser.this._browser.webEngine.load(actualURL); 462 441 } catch (Exception e) { 463 442 e.printStackTrace(); … … 476 455 public void navigateBack() { 477 456 try { 478 final Object webEngine = JavaFX.WebViewGetEngine.invoke(this._browser.webview); 479 try { 480 JavaFX.PlatformRunLater.invoke(null, new Runnable() { 481 @Override 482 public void run() { 483 try { 484 JavaFX.WebHistoryGo.invoke(JavaFX.WebEngineGetHistory.invoke(webEngine), -1); 485 FreeItems.getInstance().clear(); 486 } catch (InvocationTargetException e) { 487 MessageBay.displayMessage("Start of History"); 488 } catch (Exception e) { 489 e.printStackTrace(); 490 } 491 } 492 }); 493 } catch (Exception e) { 494 e.printStackTrace(); 495 } 457 Platform.runLater(new Runnable() { 458 @Override 459 public void run() { 460 try { 461 JfxBrowser.this._browser.webEngine.getHistory().go(-1); 462 FreeItems.getInstance().clear(); 463 } catch (IndexOutOfBoundsException e) { 464 MessageBay.displayMessage("Start of History"); 465 } 466 } 467 }); 496 468 } catch (Exception e) { 497 469 e.printStackTrace(); … … 505 477 public void navigateForward() { 506 478 try { 507 final Object webEngine = JavaFX.WebViewGetEngine.invoke(this._browser.webview); 508 try { 509 JavaFX.PlatformRunLater.invoke(null, new Runnable() { 510 @Override 511 public void run() { 512 try { 513 JavaFX.WebHistoryGo.invoke(JavaFX.WebEngineGetHistory.invoke(webEngine), 1); 514 FreeItems.getInstance().clear(); 515 } catch (InvocationTargetException e) { 516 MessageBay.displayMessage("End of History"); 517 } catch (Exception e) { 518 e.printStackTrace(); 519 } 520 } 521 }); 522 } catch (Exception e) { 523 e.printStackTrace(); 524 } 479 Platform.runLater(new Runnable() { 480 @Override 481 public void run() { 482 try { 483 JfxBrowser.this._browser.webEngine.getHistory().go(1); 484 FreeItems.getInstance().clear(); 485 } catch (IndexOutOfBoundsException e) { 486 MessageBay.displayMessage("End of History"); 487 } 488 } 489 }); 525 490 } catch (Exception e) { 526 491 e.printStackTrace(); … … 533 498 public void refresh() { 534 499 try { 535 final Object webEngine = JavaFX.WebViewGetEngine.invoke(this._browser.webview); 536 try { 537 JavaFX.PlatformRunLater.invoke(null, new Runnable() { 538 @Override 539 public void run() { 540 try { 541 JavaFX.WebEngineReload.invoke(webEngine); 542 FreeItems.getInstance().clear(); 543 MessageBay.displayMessage("Page Refreshing"); 544 } catch (Exception e) { 545 e.printStackTrace(); 546 } 547 } 548 }); 549 } catch (Exception e) { 550 e.printStackTrace(); 551 } 500 Platform.runLater(new Runnable() { 501 @Override 502 public void run() { 503 try { 504 JfxBrowser.this._browser.webEngine.reload(); 505 FreeItems.getInstance().clear(); 506 MessageBay.displayMessage("Page Reloading"); 507 } catch (Exception e) { 508 e.printStackTrace(); 509 } 510 } 511 }); 552 512 } catch (Exception e) { 553 513 e.printStackTrace(); 554 514 } 515 555 516 } 556 517 … … 560 521 public void getFrame() { 561 522 try { 562 WebParser.parsePage( JavaFX.WebViewGetEngine.invoke(this._browser.webview), DisplayIO.getCurrentFrame());523 WebParser.parsePage(this._browser.webEngine, DisplayIO.getCurrentFrame()); 563 524 } catch (Exception e) { 564 525 e.printStackTrace(); … … 572 533 // this.layout(this._browser); 573 534 try { 574 WebParser.parsePageSimple( JavaFX.WebViewGetEngine.invoke(this._browser.webview), this._browser.webview, (JComponent) this._browser.jfxPanel, DisplayIO.getCurrentFrame());535 WebParser.parsePageSimple(this._browser.webEngine, this._browser.webview, (JComponent) this._browser.jfxPanel, DisplayIO.getCurrentFrame()); 575 536 } catch (Exception e) { 576 537 e.printStackTrace(); … … 672 633 if (this._browser.webview != null) { 673 634 try { 674 r = new String[] { (String) JavaFX.WebEngineGetLocation.invoke(JavaFX.WebViewGetEngine.invoke(this._browser.webview)) };635 r = new String[] { this._browser.webEngine.getLocation() }; 675 636 } catch (Exception e) { 676 637 e.printStackTrace();
Note:
See TracChangeset
for help on using the changeset viewer.