views:

541

answers:

4

With the introduction of the HTML5 <canvas> element, could Swing theoretically be implemented in Google Web Toolkit (GWT) by using the <canvas> tag for drawing?

I'm aware of efforts to port source code from using Swing calls to GWT calls, but what I'm after is a pure behind the scenes port where a Swing application would compile under GWT without any source code modifications.

Is that theoretically possible? Why? Why not?

A: 

Short answer: Not possible at the moment. Zero accessibility!

itpastorn
Care to elaborate? :-)
knorv
A Swing widget recreated in Canvas will only have the looks of a true widget. Its functionality will not be exposed to assistive technologies.
itpastorn
+2  A: 

It is theoretically possible, though it will be non-trivial amount of work and questionable utility. What you need to do is implement custom Look and Feel that defines UI delegates generating GWT widgets and on the GWT side, capture the mouse and keyboard events on the client and feed them to the Swing event queue.

The problem is that the typical Swing application there are too many input events and widget state updates that will have to go back and forth between your browser and backend. This would kill your application responsiveness.

ddimitrov
Why would the input events and widget state updates have to go back and forth between the browser and backend? GWT can run 100 % on the client-side.
knorv
Any non-trivial GWT can't run entirely on the client side. It has to communicate to the server to read/write data if it has to be meaningful. Think gmail - the user interface works on the client side, but your emails are on the server.
sri
Sripathi: That does NOT imply that widget state updates have to go back and forth between the browser and backend.
knorv
@ddimitrov and Sripathi: Eclipse did something similar with SWT (which is somewhat comparable to Swing). IMO, it's amazing, because it seems to be near impossible at first, but the demos prove, that it actually is possible, and works quite well: http://www.eclipse.org/rap/demos.php
Chris Lercher
The design of Swing revolves around the Event Dispatch Thread. It will be fairly easy to use GWT for UI delegates and process the events in the JVM. Moving the EDT to the client side is much bigger undertaking as it does not fit directly in Swing's design (in other words, you need a hack). See also http://www.wingsframework.org/
ddimitrov
@chris_l - I just took a look at RAP and I'm not impressed. In general, taking a desktop API and putting it on the web is something people have been trying for quite some time now (see my link to WingS) and it has never been a good idea. In particular, RAP feels sluggish even for the simple forms of the provided demos. In a real applications, you often have much more complex layouts, deal with more data and business users won't consider 1 sec wait "pretty good responsiveness". You can make rich and responsive app with something like Flex/Curl/Silverlight, but there is not so easy to do it well
ddimitrov
+3  A: 

There are some challenges.

Local data storage Swing applications can use the disk to store data. Assuming local data store available in HTML 5 works, it'd still be difficult to auto-magically map file/disk access to local data store calls.

Multithreading & Synchronization Swing applications typically create threads, and that's not possible with GWT. Tricky to get it right.

Network Access Swing apps can connect to arbitrary network locations, which doesn't work with GWT.

Using java language features not available in browser Anything outside core-java is inaccessible, so the automatic port will likely fail.

Memory & Performance optimizations Garbage Collection patterns are entirely different. How do you optimize for optimal download sizes? How do you map multiple swing screens to use something like code-splitting for performance gains?

Look and Feel All said and done, you have to get your hands dirty to write some CSS code to get the right look and feel. An automatic port cannot do that.

Given all this, I think it is not-possible for anything but trivial applications. And for trivial applications you might as well re-write the code.

sri
Great answer! Thanks!
knorv
+1  A: 

I would say it's possible, given this technology: http://www.creamtec.com/products/ajaxswing/demos.html

What AjaxSwing may not have yet is Java2D/HTML5 Canvas (frankly, I haven't checked). But how far away is that, with IE9 jumping on board?

yottzumm