Author: echatellier Date: 2013-04-25 22:22:15 +0200 (Thu, 25 Apr 2013) New Revision: 600 Url: http://nuiton.org/projects/sandbox/repository/revisions/600 Log: Add sample about intercepting url to link ajax with java code Modified: jfxwvpoc/src/html/index.html jfxwvpoc/src/org/codelutin/test/Main.java Modified: jfxwvpoc/src/html/index.html =================================================================== --- jfxwvpoc/src/html/index.html 2013-04-24 15:55:37 UTC (rev 599) +++ jfxwvpoc/src/html/index.html 2013-04-25 20:22:15 UTC (rev 600) @@ -95,6 +95,24 @@ -webkit-transform:translateY(-90px) rotateX(-90deg); } </style> + + <script type="text/javascript"> + + function testAlert() { + + var xhr = new XMLHttpRequest(); + alert(xhr); + xhr.open("GET", "http://whatever/jtimer/getTasks", true); + + xhr.onreadystatechange = function() { + if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { + alert(xhr.responseText); + } + }; + xhr.send(null); + } + + </script> </head> <body> @@ -105,6 +123,8 @@ Jtimer : <input type="button" value="Run 3D" onClick="this.parentNode.setAttribute('class', 'main mainanim');" /> <input type="button" value="Stop 3D" onClick="this.parentNode.setAttribute('class', 'main');" /><br /> + Ajaxs : <input type="button" value="Test Ajax" onClick="testAlert();" /> + <form> Date picker : <input type="date" name="toto" autofocus="autofocus" /><br /> Search : <input type="search" placeholder="Recherche" name="recherche" /><br /> Modified: jfxwvpoc/src/org/codelutin/test/Main.java =================================================================== --- jfxwvpoc/src/org/codelutin/test/Main.java 2013-04-24 15:55:37 UTC (rev 599) +++ jfxwvpoc/src/org/codelutin/test/Main.java 2013-04-25 20:22:15 UTC (rev 600) @@ -17,15 +17,25 @@ package org.codelutin.test; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; +import javax.swing.JOptionPane; + import javafx.application.Application; +import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.VPos; import javafx.scene.Scene; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; +import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; import javafx.stage.Stage; @@ -60,6 +70,51 @@ URL url = getClass().getResource("/html/index.html"); webEngine.load(url.toExternalForm()); getChildren().add(browser); + + webEngine.setOnAlert(new EventHandler<WebEvent<String>>(){ + @Override + public void handle(WebEvent<String> we) { + JOptionPane.showMessageDialog(null, we.getData()); + } + }); + + // override + URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + + if ("http".equals(protocol)) { + // TODO try to fix restricted api + return new sun.net.www.protocol.http.Handler() { + + public URLConnection openConnection(URL url) throws IOException { + + String query = url.getPath(); + System.out.println(url); + if (query != null && query.startsWith("/jtimer/")) { + return new URLConnection(url) { + @Override + public void connect() throws IOException { + // heu + } + + public InputStream getInputStream() throws IOException { + + InputStream is = null; + String content = "Non mais allo quoi !"; + is = new ByteArrayInputStream(content.getBytes()); + return is; + } + }; + } else { + return super.openConnection(url); + } + } + }; + } + return null; // back to default handler + } + }); } @Override
participants (1)
-
echatellier@users.nuiton.org