Integration With Web Applications

 

Java applets are executed in the environment of a web browser. Therefore, they can interact, in both directions, with the javascript code in the web page that contains the applet. This communication allows you to create HTML interfaces to control the map and perform simple integrations with other applications running in the environment of the web browser. This does not apply to JMap Pro applications deployed with the JavaWebStart method (not in a web browser) because no web page is involved in these cases.

Javascript to Java

The following sample web page is a normal startup page for a JMap Pro applet to which javascript functions have been added (pan and zoom). Hyperlinks are also used to call these functions. When triggered, the functions call the JMap API to control the map.

The JMap Pro application has a getApplicationContext() method that is used to access all the components of the application.

 
 <%@page contentType="text/html;charset=ISO-8859-1"%>
 
 <%
   String username = request.getParameter("username");  
   String password = request.getParameter("password");
   String parameters = null;

  if (username != null && username.length() != 0)
   {
     parameters = "?username=" + username;
    if (password != null)
       parameters += "&password=" + password;
   }
 %>
 
 <html>
   <head>
   <title>
     aa
   </title>
   <script src="library/deployJava.js"></script>
   <script src="library/jmap.js"></script>
 </head>
 
 <BODY BGCOLOR="#ffffff" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
 
 <script>
  function zoom(factor)
   {
     document.jmap.getApplicationContext().getViewManager().getActiveView().zoom(factor);
     document.jmap.getApplicationContext().getViewManager().getActiveView().refresh();
   }
  function pan(x, y)
   {
     document.jmap.getApplicationContext().getViewManager().getActiveView().pan(x, y);
     document.jmap.getApplicationContext().getViewManager().getActiveView().refresh();
   }
 </script>
 
 <a href="javascript:zoom(2.);">Zoom in</a>
 <a href="javascript:zoom(0.5);">Zoom out</a>
 <a href="javascript:pan(0, 200);">Pan north</a>
 <a href="javascript:pan(0, -200);">Pan south</a>
 <a href="javascript:pan(200, 0);">Pan west</a>
 <a href="javascript:pan(-200, 0);">Pan east</a>
 
 <script>
  var attributes =
   {
     name: "jmap",
     codebase: "http://127.0.0.1:8080/aa",
     code: "com.kheops.jmap.client.application.JMapApplicationLauncher",
     archive: "dockingClient.jar,jmap_application.jar,jmap_client.jar,jmap_client_images.jar,jmap_projections.jar,jmap_symbols.jar,custom_symbols.jar,jmap_metadata.jar,jmap_net.jar,jmap_spatial.jar,kheops_ui.jar,kheops_util.jar,gif4j_pro_2.0.jar,jide-action.jar,jide-common.jar,jide-components.jar,jide-dock.jar,jide-grids.jar",
     width: "100%",
     height: "100%",
     mayscript: true,
     separate_jvm: true,
     parameters: "-appclassname jmap.viewers.docking.AppDocking -project &quot;The World&quot; -directport 7003 -httpport 8080 <%= username != null ? username : "" %> <%= password != null ? password : "" %> -proxypath /aa/servlet/jmapproxy -serverid &quot;aa&quot; -maxmemory 33554432 -connection direct -donotlistusers false -showconnectionmoredetails false  true   "
   };

  var parameters = {fontSize:16, jnlp_href:'dockingClient.jnlp'} ;
 
   deployJava.runApplet(attributes, parameters, '1.6.0_10');
 </script>
 
 </body>
 </html>

 

Java to Javascript

From a JMap Pro applet, it is possible to call javascript functions that are in the web page containing the applet. This allows for interaction between the JMap Pro application and its HTML environment. For example, it would be possible to select an item on the map and display its information in an HTML page.

To enable this java-javascript communication, the mayscript: true parameter must be specified in the attributes used to start the applet, as shown in the following example.

 

 
<script>
var attributes =
 {
   name: "jmap",
   codebase: "http://127.0.0.1:8080/aa",
   code: "com.kheops.jmap.client.application.JMapApplicationLauncher",
   archive: "dockingClient.jar,jmap_application.jar,jmap_client.jar,jmap_client_images.jar,jmap_projections.jar,jmap_symbols.jar,custom_symbols.jar,jmap_metadata.jar,jmap_net.jar,jmap_spatial.jar,kheops_ui.jar,kheops_util.jar,gif4j_pro_2.0.jar,jide-action.jar,jide-common.jar,jide-components.jar,jide-dock.jar,jide-grids.jar",
   width: "100%",
   height: "100%",
   mayscript: true,
   separate_jvm: true,
   parameters: "-appclassname jmap.viewers.docking.AppDocking -project &quot;The World&quot; -directport 7003 -httpport 8080 <%= username != null ? username : "" %> <%= password != null ? password : "" %> -proxypath /aa/servlet/jmapproxy -serverid &quot;aa&quot; -maxmemory 33554432 -connection direct -donotlistusers false -showconnectionmoredetails false  true   "
 };

var parameters = {fontSize:16, jnlp_href:'dockingClient.jnlp'} ;
 
 deployJava.runApplet(attributes, parameters, '1.6.0_10');
</script>
 

 

To call a javascript function from your Java code (possibly your JMap extension), you must use the JSObject API, as shown in the following example.

 

JSObject w = JSObject.getWindow((Applet)appContext.getRootPaneContainer());

w.eval("show_form(" + id + ");");

 

In this example, the getWindow method receives the instance of the applet as a parameter. The getRootPaneContainer method of the JMapApplicationContext class returns the top level container of the application, the latter being the instance of the Applet when the application is running inside the web browser.

The javascript function to be called and its parameters are taken as parameters by the eval method. In this example, an ID is passed to the javascript function.