Changeset 541


Ignore:
Timestamp:
11/27/13 12:24:38 (10 years ago)
Author:
jts21
Message:

Add JfxBrowser status callback

File:
1 edited

Legend:

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

    r540 r541  
    77import java.awt.event.ComponentListener;
    88import java.io.File;
    9 import java.io.IOException;
    109import java.lang.reflect.Constructor;
    1110import java.lang.reflect.InvocationTargetException;
    1211import java.lang.reflect.Method;
    13 import java.net.MalformedURLException;
    1412import java.net.URL;
    1513import java.net.URLClassLoader;
     14import java.util.Arrays;
     15import java.util.List;
    1616
    1717import javax.swing.JPanel;
    1818
    1919import org.expeditee.gui.FreeItems;
     20import org.expeditee.gui.MessageBay;
    2021import org.expeditee.items.Text;
    2122
     
    4445        static Method WebViewSetPrefSize;
    4546        static Method WebViewGetEngine;
    46        
     47
    4748        static Class WebEngine;
    4849        static Method WebEngineLoad;
    4950        static Method WebEngineGetLocation;
     51        static Method WebEngineGetLoadWorker;
     52
     53        static Class Worker;
     54        static Method WorkerStateProperty;
     55
     56        static Class ReadOnlyObjectProperty;
     57        static Method ReadOnlyObjectPropertyAddListener;
     58
     59        static Class ChangeListener;
     60
     61        static Class ObservableValue;
     62
     63        static Class State;
     64        static List<Object> StateConstants;
    5065
    5166        static {
     
    5469                        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    5570
    56 
    5771                        File jar = new File(javaLibDir + "jfxrt.jar");
    5872
     
    8094                        WebViewSetPrefSize = WebView.getMethod("setPrefSize", double.class, double.class);
    8195                        WebViewGetEngine = WebView.getMethod("getEngine");
    82                        
     96
    8397                        WebEngine = classLoader.loadClass("javafx.scene.web.WebEngine");
    8498                        WebEngineLoad = WebEngine.getMethod("load", String.class);
    8599                        WebEngineGetLocation = WebEngine.getMethod("getLocation");
    86 
    87 
    88                 } catch (MalformedURLException e) {
     100                        WebEngineGetLoadWorker = WebEngine.getMethod("getLoadWorker");
     101
     102                        Worker = classLoader.loadClass("javafx.concurrent.Worker");
     103                        WorkerStateProperty = Worker.getMethod("stateProperty");
     104
     105                        ChangeListener = classLoader.loadClass("javafx.beans.value.ChangeListener");
     106
     107                        ReadOnlyObjectProperty = classLoader.loadClass("javafx.beans.property.ReadOnlyObjectProperty");
     108                        ReadOnlyObjectPropertyAddListener = ReadOnlyObjectProperty.getMethod("addListener", ChangeListener);
     109
     110                        ObservableValue = classLoader.loadClass("javafx.beans.value.ObservableValue");
     111
     112                        State = classLoader.loadClass("javafx.concurrent.Worker$State");
     113                        StateConstants = Arrays.asList(State.getEnumConstants());
     114                        System.out.println(StateConstants);
     115
     116                } catch (Exception e) {
    89117                        e.printStackTrace();
    90                 } catch (ClassNotFoundException e) {
    91                         e.printStackTrace();
    92                 } catch (NoSuchMethodException e) {
    93                         e.printStackTrace();
    94                 } catch (SecurityException e) {
    95                         e.printStackTrace();
    96                 } catch (IllegalAccessException e) {
    97                         e.printStackTrace();
    98                 } catch (IllegalArgumentException e) {
    99                         e.printStackTrace();
    100                 } catch (InvocationTargetException e) {
    101                         e.printStackTrace();
    102                 } catch (IOException e) {
    103                         e.printStackTrace();
    104118                }
    105119        }
    106120
    107121        private static class WebBrowserPanel extends JPanel implements ComponentListener {
    108                
     122
    109123                private static final long serialVersionUID = 1L;
    110124
    111125                Object webview;
    112126                Object jfxPanel;
    113                
     127
    114128                /**
    115                  * @return A JPanel that either contains a JavaFX Webview or an empty
    116                  *         panel if the JFX panel can't be initialized
     129                 * @return A JPanel that either contains a JavaFX Webview or an empty panel if the JFX panel can't be
     130                 *         initialized
    117131                 */
    118132                public WebBrowserPanel(final String url) {
     
    160174
    161175                                JFXPanelSetScene.invoke(jfxPanel, scene);
     176
     177                                ReadOnlyObjectPropertyAddListener.invoke(WorkerStateProperty.invoke(WebEngineGetLoadWorker
     178                                        .invoke(WebViewGetEngine.invoke(this.webview))), java.lang.reflect.Proxy.newProxyInstance(
     179                                        ChangeListener.getClassLoader(), new java.lang.Class[] { ChangeListener },
     180                                        new java.lang.reflect.InvocationHandler() {
     181                                                @Override
     182                                                public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
     183                                                        throws java.lang.Throwable {
     184                                                        String method_name = method.getName();
     185                                                        // Class<?>[] classes = method.getParameterTypes();
     186                                                        // public void changed(ObservableValue ov, State oldState, State newState)
     187                                                        if (method_name.equals("changed")) {
     188                                                                // changed takes 3 args
     189                                                                if (args == null || args.length != 3) {
     190                                                                        return null;
     191                                                                }
     192                                                                // args[0] is the ObservableValue
     193                                                                System.out.println(args[0]);
     194                                                                System.out.println(args[0].getClass());
     195                                                                // args[2] is the new State
     196                                                                if (args[2].getClass() == State) {
     197                                                                        int id = StateConstants.indexOf(args[2]);
     198                                                                        switch (id) {
     199                                                                                case 0: // READY
     200                                                                                        MessageBay.displayMessage("WebEngine ready");
     201                                                                                        break;
     202                                                                                case 1: // SCHEDULED
     203                                                                                        MessageBay.displayMessage("Scheduled page load");
     204                                                                                        break;
     205                                                                                case 2: // RUNNING
     206                                                                                        MessageBay.displayMessage("WebEngine running");
     207                                                                                        break;
     208                                                                                case 3: // SUCCEEDED
     209                                                                                        MessageBay.displayMessage("Finished loading page");
     210                                                                                        break;
     211                                                                                case 4: // CANCELLED
     212                                                                                        MessageBay.displayMessage("Cancelled loading page");
     213                                                                                        break;
     214                                                                                case 5: // FAILED
     215                                                                                        MessageBay.displayMessage("Failed to load page");
     216                                                                                        break;
     217                                                                                }
     218                                                                        }
     219                                                                        System.out.println("\n");
     220                                                                }
     221                                                                return null;
     222                                                        }
     223                                        }));
     224
     225                                // webEngine.getLoadWorker().stateProperty().addListener(
     226                                // new ChangeListener<State>() {
     227                                // public void changed(ObservableValue ov, State oldState, State newState) {
     228                                // if (newState == State.SUCCEEDED) {
     229                                // stage.setTitle(webEngine.getLocation());
     230                                // }
     231                                // }
     232                                // });
    162233
    163234                                WebEngineLoad.invoke(WebViewGetEngine.invoke(this.webview), url);
     
    209280        public JfxBrowser(Text source, String[] args) {
    210281                // Initial page is either the page stored in the arguments (if there is one stored) or Google
    211                 //super(source, new WebBrowserPanel((args != null && args.length > 0) ? args[0] : "http://google.com"), -1, 500, -1, -1, 300, -1);
     282
     283                // super(source, new WebBrowserPanel((args != null && args.length > 0) ? args[0] : "http://google.com"), -1, 500, -1, -1, 300, -1);
    212284                super(source, new WebBrowserPanel(args[0]), -1, 500, -1, -1, 300, -1);
    213285                _browser = (WebBrowserPanel) _swingComponent;
     
    231303                }
    232304        }
    233        
     305
    234306        @Override
    235307        public boolean ItemsLeftClickDropped() {
    236308                Text carried = null;
    237                 if((carried = FreeItems.getTextAttachedToCursor()) != null) {
     309                if ((carried = FreeItems.getTextAttachedToCursor()) != null) {
    238310                        this.navigate(carried.getText());
    239311                        return true;
Note: See TracChangeset for help on using the changeset viewer.