Ignore:
Timestamp:
01/24/14 11:32:31 (10 years ago)
Author:
ngw8
Message:

Changed JFX files to no longer use reflection (as some new features were impossible via reflection) and just import javafx instead. This will probably cause Eclipse to complain if you're using a Java version <= 7, but building/running Expeditee will still work (using Eclipse or Ant).
Have also added an Ant target that builds without ant JFX stuff. See the comments near the top of JfxBrowser.java or WebParser.java for details

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r733 r748  
    1717import java.awt.event.MouseListener;
    1818import 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
     29import javafx.application.Platform;
     30import javafx.beans.value.ChangeListener;
     31import javafx.beans.value.ObservableValue;
     32import javafx.concurrent.Worker.State;
     33import javafx.embed.swing.JFXPanel;
     34import javafx.event.EventHandler;
     35import javafx.scene.Scene;
     36import javafx.scene.web.WebEngine;
     37import javafx.scene.web.WebView;
    2038
    2139import javax.swing.JButton;
     
    3553import org.expeditee.items.Item;
    3654import org.expeditee.items.Text;
    37 import org.expeditee.reflection.JavaFX;
    3855import org.expeditee.settings.network.NetworkSettings;
    3956
     
    5673       
    5774        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        }
    6379
    6480        private static class WebBrowserPanel extends JPanel implements ComponentListener {
     
    6682                private static final long serialVersionUID = 1L;
    6783
    68                 Object webview;
    69                 Object jfxPanel;
     84                WebView webview;
     85                WebEngine webEngine;
     86                JFXPanel jfxPanel;
    7087                JfxBrowser owner;
    7188
     
    87104                        this.addComponentListener(this);
    88105                        try {
    89                                 jfxPanel = JavaFX.JFXPanel.newInstance();
     106                                jfxPanel = new JFXPanel();
    90107
    91108                                // Toolbar (that can't be dragged) to hold the nav buttons, url bar, etc
     
    258275                                this.add((Component) jfxPanel);
    259276
    260                                 JavaFX.PlatformRunLater.invoke(null, new Runnable() {
     277                                Platform.runLater(new Runnable() {
    261278                                        @Override
    262279                                        public void run() {
     
    287304                private void initFx(String url) {
    288305                        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                                });
    345345                               
    346346                                // 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);
    378362                                                        }
    379                                         }));
     363
     364                                                        break;
     365                                                }
     366                                        }
     367                                });
    380368
    381369                                // 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);
    394373                        } catch (Exception e) {
    395374                                e.printStackTrace();
     
    455434                System.out.println(actualURL);
    456435                try {
    457                         JavaFX.PlatformRunLater.invoke(null, new Runnable() {
     436                        Platform.runLater(new Runnable() {
    458437                                @Override
    459438                                public void run() {
    460439                                        try {
    461                                                 JavaFX.WebEngineLoad.invoke(JavaFX.WebViewGetEngine.invoke(JfxBrowser.this._browser.webview), actualURL);
     440                                                JfxBrowser.this._browser.webEngine.load(actualURL);
    462441                                        } catch (Exception e) {
    463442                                                e.printStackTrace();
     
    476455        public void navigateBack() {
    477456                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                });
    496468                } catch (Exception e) {
    497469                        e.printStackTrace();
     
    505477        public void navigateForward() {
    506478                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                });
    525490                } catch (Exception e) {
    526491                        e.printStackTrace();
     
    533498        public void refresh() {
    534499                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                });
    552512                } catch (Exception e) {
    553513                        e.printStackTrace();
    554514                }
     515
    555516        }
    556517       
     
    560521        public void getFrame() {
    561522                try {
    562                         WebParser.parsePage(JavaFX.WebViewGetEngine.invoke(this._browser.webview), DisplayIO.getCurrentFrame());
     523                        WebParser.parsePage(this._browser.webEngine, DisplayIO.getCurrentFrame());
    563524                } catch (Exception e) {
    564525                        e.printStackTrace();
     
    572533                // this.layout(this._browser);
    573534                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());
    575536                } catch (Exception e) {
    576537                        e.printStackTrace();
     
    672633                if (this._browser.webview != null) {
    673634                        try {
    674                                 r = new String[] { (String) JavaFX.WebEngineGetLocation.invoke(JavaFX.WebViewGetEngine.invoke(this._browser.webview)) };
     635                                r = new String[] { this._browser.webEngine.getLocation() };
    675636                        } catch (Exception e) {
    676637                                e.printStackTrace();
Note: See TracChangeset for help on using the changeset viewer.