What goes on underneath Overlay Types

Posted by Andrew Bowers - Tuesday, November 04, 2008 at 1:35:00 PM

Recently I was giving an introductory talk on JavaScript Overlay Types in GWT. A question came up about what I meant by the following:

final native void foo(int x, int y) /*-{ ... }-*/;

can magically become

final static native void foo(MyJSO this_, int x, int y) /*-{ ... }-*/;

It was a great question, and merited explaining why Overlay Types have certain constraints on them and what goes on during the compile process. If you are brand new to Overlay Types, you'll want to look over the earlier blog post or docs first.

As the earlier post explained, in Overlay Types you can augment the Java type without disturbing the underlying JavaScript object. For example, you can have getters and setters in the Java class that don't exist on the underlying JavaScript object. At the same time, your Java class is providing this richer functionality without GWT having to modify the underlying JavaScript object instance or its prototype.

This is possible because by design we disallow polymorphic calls on instance methods. All methods must be 'final' and/or 'private'. Consequently, every method on an overlay type is statically resolvable by the compiler, so there is never a need for dynamic dispatch at runtime. We can either inline the method body or create a global function external to the object itself.

This all makes more sense if we look at a concrete example, so let's do that. Here we have a JavaScript snippet embedded in an HTML page. This snippet defines a native JavaScript object of type 'nativeCustomer' and creates a instance called 'fred'.

//JavaScript in an HTML page
<script>
  function nativeCustomer () {
    this.FirstName = "";
    this.LastName = "";
  }
 
  var fred = new nativeCustomer();
  fred.FirstName = "Fred";
  fred.LastName = "Wilson";
</script>

Now we'll overlay that native JavaScript object with an Overlay Type in GWT.

// An overlay type in GWT, which must extend JavaScriptObject
class Customer extends JavaScriptObject {

  // Overlay types always have protected, zero-arg constructors
  protected Customer() { }
   
  // Typically, methods on overlay types are JSNI
  public final native String getFirstName() /*-{ return this.FirstName; }-*/;
  public final native String getLastName()  /*-{ return this.LastName;  }-*/;
  
    public final String thankCustomer(String thanksType){
       if (thanksType.equals("buying")){
         return "Dear " + getFirstName() + ", thanks for buying our product";
       } else {
         return "Dear " + getFirstName() + ", thanks for referring your friend";
       }
     }
}

For good measure, I'll add a JSNI method to get the native JavaScript object from the wild, returning the Customer overlay type above.

private native Customer getNativeCustomer() /*-{
  return $wnd.fred;
}-*/;

Now that we have our native JavaScript object, a way to access it, and an Overlay Type, we'll want to use it. Below I have some GWT code that simply gets the JavaScript object from the wild as an Overlay type, then calls some methods on it.

Customer customer = getNativeCustomer();
Window.alert(customer.getFirstName() + " " + customer.getLastName());
Window.alert(customer.getFullName());

In this blog post we're interested in what's happening underneath - what the compiled output looks like. You'll notice that the methods in our Overlay Type don't exist on the actual JavaScript object. Where do they go? As we said above, where possible we try to just get rid of them. Yep, gone. The compiler will actually inline the augmented method bodies, so that the above code looks like this:

var customer;
customer = $wnd.fred;
$wnd.alert(customer.FirstName + ' ' + customer.LastName);
$wnd.alert(customer.FirstName + ' ' + customer.LastName);

You'll notice that we're accessing the underlying object's attributes (FirstName, LastName) directly. Pretty cool.

Sometimes it isn't possible to inline the augmented Java method, in which case a global function is created. Take the method thankCustomer() in the Customer class above. If we can't inline the method body, then we need to create a function somewhere. However, we don't want to add it to the underlying object or its prototype, so we create a global function that takes the underlying object as an argument. We get the same effect, but without the overhead of indirection.

So the 'thankCustomer' method above becomes

function $thankCustomer(this$static, thanksType){
  if ($equals_0(thanksType, 'buying')) {
    return 'Dear ' + this$static.FirstName + ', thanks for buying our product';
  }
   else {
    return 'Dear ' + this$static.FirstName + ', thanks for referring your friend';
  }
}

which means a Java source code line like

Window.alert(customer.thankCustomer("buying"));

in the end compiles to a call to global function $thankCustomer with the customer object passed as an argument.

$wnd.alert($thankCustomer(customer, 'buying'));

And that takes us full circle back to the code snippet at the beginning of this post. When we say that

final native void foo(int x, int y) /*-{ ... }-*/;
        can magically become
final static native void foo(MyJSO this_, int x, int y) /*-{ ... }-*/;

We're showing that if foo can't be inlined, then it will become a global function with the object passed as an argument rather than a method on the object itself.

You may also now see why we don't allow attributes on an Overlay Type -- we'd have nowhere to store them in this model. However, as you've seen we can allow augmented Java methods through either inlining the method away, or creating a global function where the first argument is the object itself.

Gadgets, Gears, and AJAX Search Google API Libraries for GWT Now Available

Posted by Sumit Chandel, Developer Programs Engineer - Friday, October 31, 2008 at 10:14:00 AM

By Eric Ayers, Google Web Toolkit Team

We're pleased to announce the final release of the Gears 1.1, Gadgets 1.0, and Search 1.1 APIs, all part of the Google API Libraries for Google Web Toolkit project.  We are also making available a second release candidate for the Maps API.

Download Now

Online Documentation

Each library comes with:

  • Apache 2.0 Open Source license
  • Online Developer Guide
  • Online Tutorial
  • Online and Offline Javadoc documentation
  • Sample Code
  • Community discussion and Support through the Google-Web-Toolkit Google Group
  • Issue Tracking through Google Code at http://code.google.com/p/gwt-google-apis

Changes since last Release Candidate:

Gears 1.1 (1.1.1)

  •   Bugfixes
  •   Sample Code updated
  •   Messages can now be of types other than String
  •   WorkerPool demo added

Gagets 1.0 (1.0.1)

  •   Bugfixes
  •   RPC sample added
  •   HelloGadgets sample overhauled
  •   @InjectContent annotation added to add additional HTML to the module spec

Search 1.0 (1.0.1)

  •   Bugfixes
  •   LocalSearch integrates better with the Maps API

Maps 1.0 RC2 (1.0.1)

  •   Bugfixes
  •   Updates to allow extention and subclassing of various Maps components
  •   Maps API samples set the Maps version to v=2 for better stability

ARIA For GWT: My health Is feeling accessible

Posted by A Googler - Tuesday, October 14, 2008 at 3:28:00 PM

Google Health is a rich Web-2.0 application built using GWT. It enables users to manage their online health records and relies on GWT to provide a highly reactive user interface.

As described in our earlier article outlining ARIA support in GWT1.5, widgets now include basic support for W3C ARIA, an emerging set of Web standards that enable AJAX applications to work smoothly with screenreaders. But there is more to using an application than interacting with individual user interface widgets — overall usability is determined by the complete user experience.

We have improved the usability of Google Health for screenreader users by enhancing the built-in support available in GWT 1.5 via JavaScript. The resulting user experience enables users of screenreaders and self-voicing browsers to easily navigate the interface to obtain relevant auditory feedback — see the following sections for additional details.

First Time User Tips:

Note that the accessibility support in Google Health requires support from both the browser as well as the adaptive technology in use. At present, we recommend Firefox 3.0 with screenreaders that support ARIA, alternatively, you can also use Fire Vox, the self-voicing extension to Firefox 3.0.

When signing in, first-time users should use this link ARIA-Enabled Google Health to turn on the accessibility enhancements. If you would like to use these enhancements whenever you sign in to Google Health, please perform these additional steps:

  1. Activate settings by pressing hot key e, or alternatively, press . to bring up the available actions and pick settings from the list of choices.
  2. Listen to the spoken messages that guide you through the settings interface.
  3. Check the option labeled enable screenreader support.
  4. Save your preferences.
  5. This will automatically activate ARIA support the next time you sign in as long as you use a supported browser.

Google Health: A High-Level Overview

You can have one or more health profiles on Google Health, and once logged in, you can create and access health profiles from the main screen. Use the navigation keys (arrow keys) to navigate the interface. The left and right arrow keys move you through the major categories, whereas the up and down arrow keys move through the items in a given category. adaptive technology in use produces appropriate feedback as you move. In addition, the item under focus is visually highlighted and can be magnified by pressing the = key. Auditory feedback includes spoken output from the adaptive technology, augmented by short auditory icons that help orient the user within the application.

The bulk of the Google Health application consists of navigating among various categories, finding the relevant item within a category, and if needed, updating the contents of that item. As an example, when working with your health profile, you can:

  1. Navigate to the category that holds biometric information, e.g., your height and weight.
  2. Navigate through the various items of data in this category with the up and down arrow keys.
  3. As you navigate, the current value of each item is spoken.
  4. Pressing enter allows you to modify the value of the current item.
  5. You can pull up a list of available actions at any time by pressing .. Use the up and down arrows to move through this list, and press enter to activate the current action. Notice that this list can be filtered by typing a partial command name in the edit field — this provides a power key feature.

This paradigm of navigating with the arrow keys, and pressing enter to edit the current item and using power keys for picking the desired action is applied consistently across all aspects of Google Health. Many categories in this application have a large number of items, e.g., the list of medications. In addition to the up and down arrow keys, these lists can be navigated by pressing capital letters to directly jump to the relevant section in the list of items. In addition, the application provides wizards for performing complex tasks, e.g., finding a doctor.

To conclude, here is a short summary of the generic key-bindings that are available throughout the application:

KeyFunction
ESCReturn to the main Health Profile screen
.Display available actions with completion.
?Speak keyboard help
=Magnify item under focus
-Shrink item under focus
LEFTMove to previous category
RIGHTMove to next category
UPMove to previous item in category
DOWNMove to next item in category

Release Candidate Available: Google Maps API Library for GWT

Posted by Andrew Bowers - Tuesday, September 23, 2008 at 11:37:00 AM

Following up on our earlier post, a release candidate of the Google Maps API Library for GWT is now available for download. This library provides a way to access the Google Maps API from a GWT project without having to write additional JavaScript code. The library gives you full control using the standard Maps components such as InfoWindows, Markers, MapTypes, and Geocoding. You can even use advanced features such as adding GWT widgets to the Map, creating custom overlays, custom map types, and other components.

In addition to maps, the Google API Libraries for GWT also provide libraries for Gears, Gadgets and the Google AJAX Search API. Please try out these release candidates and give us feedback, either through the issue tracker on code.google.com, or through the Google-Web-Toolkit or Google-Web-Toolkit-Contributors group.

New GWT Incubator Drop available

Posted by Sumit Chandel, Developer Programs Engineer - Tuesday, September 09, 2008 at 7:29:00 AM

With GWT 1.5 now officially out, we're pleased to announce a new drop of the GWT Incubator with 1.5 specific features.

A special thanks to the great group of engineers outside of Google who have contributed to the project including Ray Cromwell, Jason Essington, Daniel Florey, Fred Sauer.

Widgets

There are a variety of small widgets percolating in incubator right now
  • DatePicker, a fully internationalized and extensible date picking widget
  • GlassPanel, produces a translucent effect around UI components, useful to indicate currently non-interactive components
  • FastTree, a fully customizable tree with a rich event mode
  • SliderBar/ProgressBar/Spinner, a set of really useful widgets to add pizazz to your project

Tables

The table suite provides performance and functionality to tables
  • Bulk rendering of tables(up to 10x faster than normal rendering)
  • Scrolling tables, where the footer and header are fixed
  • Paging tables, where the table can page through arbitrary data sets
  • Editable tables, complete with common cell editors

Logging

The GWT logging suite adds additional logging capabilities
  • Logging levels based on the Java logging system
  • Categories and filters to narrow logging output
  • A native Java mode for shared code, where the output can be integrated with your server logging system
  • Compilation options so logging can be completely removed or minimally included for production mode
  • A variety of logging handlers that output to everything from Firebug to a GWT tree widget

CSS support

CssResource enables your css source files to be fully commented and modularized without a performance hit. For those of you that use css heavily, you know the trade-offs between readable/modular css and css that is efficient to download, and will want to give this a spin.
  • Strips CSS of comments and white space
  • Aggregates CSS into a single html file
  • (And in an advanced configuration)
    • Obfucates css style names
    • Eliminates unused css rules
    • Selectively applies rules per browser

Graphics

Vector graphics have also made their way to the GWT Incubator. The GWTCanvas Widget exposes an API for drawing and transforming shapes and images, as well as for defining paths to create custom shapes.
  • Rotations, scales, and translation
  • Custom paths including arcs, lines and curve
  • Image drawing and transformations
  • Compositing operations and transparency

Built-in Accessibility in GWT 1.5 Applications

Posted by Andrew Bowers - Friday, September 05, 2008 at 10:42:00 AM

As websites and applications become more interactive, users relying on assistive technology can find navigation difficult. Dynamic web applications are often written in ways that screen readers -- which interpret visual interfaces by speaking out loud or producing braille -- have difficulty interpreting. A developer building a tree widget, for example, might use a list element that has been altered to behave like a tree control. By default, a screen reader would present the control as a list, making the tree unusable for blind or visually impaired users. Screen readers also generally treat HTML span or div elements as regular static text elements, regardless of the presence of JavaScript event handlers for user interaction. You can easily imagine how this causes problems.

However, there's hope on the horizon in the form of WAI-ARIA, a specification for making web applications accessible via a standard set of DOM attributes. Currently a work-in-progress at the W3C, WAI-ARIA describes how to encode the purpose and function of a widget so browsers and screen readers can work together to properly identify widgets made up of DOM elements.

To make applications built with GWT more easily accessible, we've added accessibility support to the GWT library by baking ARIA roles and states into our widgets. With the accompanying ARIA support in Firefox and Opera (and soon, WebKit and Internet Explorer), applications built with the latest release of GWT will provide the information needed by screen readers such as JAWS, Window-Eyes, and FireVox. The ARIA support in the end-user tools is a bit of a moving target, but we've documented what works and what doesn't at present, so your application can evolve with the web.

If you've built your own widgets, you can make them accessible with the new Accessibility class, and to test out your newly-accessible widgets, you can watch for the ARIA attributes with a DOM inspector like Firebug. To check for the appropriate accessibility events, you can use a tool like Microsoft's Accessible Event Watcher -- or just install FireVox and listen to your application read aloud.

For more on ARIA and how to make sure your GWT application is easily accessible, you can read up on Accessibility in the GWT Developer's Guide.

GWT 1.5 Now Available

Posted by Bruce Johnson, Engineering Manager - Thursday, August 28, 2008 at 4:14:00 PM

We're happy — no, ecstatic — to announce that GWT 1.5 is now officially released and available for download.

Download GWT 1.5

GWT 1.5 delivers what we think are an impressive number of improvements, about four hundred issues if you're counting. We're also happy that one of those is issue 168, our most-requested feature, Support for Java 5.

We've blogged about several of the new features already. Now that it's official, let's recap and expand the list a bit...

Java 5 language support and enhanced JRE emulation

  • You can now use Java generics to add clarity and type safety to your client-side Java source. Generics also greatly simplify the definition of GWT RPC services — no more @gwt.typeArgs.
  • Simplify your code with for-each loops, autoboxing, static imports, and enum types.
  • Subsystems such as RPC, image bundles, internationalization, and benchmarking now use proper annotations. To maximize code re-use, the GWT compiler also ignores unrecognized annotations, such as those required by JPA, that typically appear on types shared between the client and server.
  • New JRE emulation classes have been added including StringBuilder, TreeMap, LinkedHashMap, and other popular utility classes.
  • Assertions are now optionally supported in web mode. If you invoke the GWT compiler using the -ea flag, assert statements in your code will be retained in the compiled JavaScript. This can be helpful during QA cycles.

Performance optimizations and easier JavaScript interop

  • The GWT compiler has gotten smarter. It now does comprehensive method inlining, which can eliminate signficant overhead due to function calls. Developers using GWT 1.5 release candidate builds have reported speedups of up to 2x, especially in performance-sensitive areas of their code. We've even heard from end users that apps compiled with GWT 1.5 just feel faster. Admittedly, feels faster isn't a quantifiable benchmark, but we sure like the sound of it.
  • The venerable Tree widget has gotten much faster in GWT 1.5. In tests, we've measured huge improvements across the board, including 5x and 10x speedups on IE7 and IE6, respectively.
  • With new JavaScript overlay types, you can seamlessly and efficiently integrate with objects originating in handwritten JavaScript. Overlay types also provide a particularly elegant new way to interact with JSON data from GWT.
  • The new Linker subsystem gives you total control over your code's packaging and bootstrap behavior so that you can deploy GWT output into any context where JavaScript is relevant (think Gadgets, Firefox extensions, Greasemonkey scripts and Gears worker threads). If it sounds complicated, don't worry: most GWT users can simply re-use the built-in linkers. But it's nice to know you can create your own if you need to. See Bob Vawter's Google I/O talk on Linkers for the theory, and Ray Cromwell's GWT Extreme! talk for some enlightening real-world examples.

Prettier widgets, better DOM, accessibility, and bi-di

  • The original KitchenSink sample has been retired in favor of a new sample called Showcase. In addition to demonstrating a wide variety of GWT features — widgets, themes, animation, history, accessibility, internationalization and bi-di to name a few — Showcase helps developers understand how code and CSS fit together by providing the source for each showcased feature.
  • Experienced GWT developers know and like the fact that widgets rely almost exclusively on CSS for styling. However, prior to GWT 1.5, GWT projects did not include a stylesheet by default, giving developers new to GWT the unfortunate impression that GWT widgets were inherently ugly. With this release, we're introducing three nice-looking default CSS themes you can use as a starting point for your own designs.
  • Not only are the standard GWT widgets prettier in this release, they're more functional. The major widgets such as Tree, MenuBar, and TabPanel now support ARIA for enhanced accessibility. In addition, widgets now support bi-directionality so that UI works properly for right-to-left languages. (You can see all this in action in the Showcase sample.)
  • A new cross-browser DOM package maps the entire set of W3C bindings onto GWT-compatible Java classes. After all, not every project needs full-on widgets. If you want to do low-level DOM programming or you're writing new widgets, you'll really like these new classes. It feels as if you're writing JavaScript DOM manipulation code but better: you get reliable code completion, easy debugging, cross-browser portability and compiler optimizations.

Those are some highlights. The (new) GWT Developer's Guide has a more detailed explanation of GWT 1.5 changes, including notes about a few breaking changes you'll want to be aware of.

For further information or for help getting started with GWT, you may find the follow links helpful:

  • If you're new to GWT, the Getting Started Guide takes you through a very quick example of installing and using GWT.
  • There's a new in-depth GWT tutorial that guides you through the process of building and enhancing an application from scratch.
  • If you're particularly curious, you can also browse the entire 1.5 issue list.

We really hope that you'll find that GWT 1.5 helps you build the most sophisticated web apps you can envision. And when you build the next big thing, please share your success stories in the GWT developer forum.

Download GWT 1.5