Wednesday, September 9, 2015

List of applications using GWT

In my initial days of GWT programming, with so many features why Google is not using GWT in it's own web applications. 

Later I found the below list of applications from Google which using GWT as their framework.


AdWords http://google.com/adwords

AdSense http://google.com/adsense


Flights http://flights.google.com


Hotel Finder http://www.google.com/hotelfinder


Offers https://www.google.com/offers


Wallet http://wallet.google.com


The New Blogger http://www.blogger.com/


Chrome Webstore https://chrome.google.com/webstore  


Product Search http://www.google.com/prdhp?hl=en&tab=mf


Public Data http://www.google.com/publicdata/home


New Google Groups http://groups.google.com


Orkut http://www.orkut.com


It seems there are many companies and famous websites also built with GWT. Will be back with complete list soon. 

Share it GWT lovers.

Contacting server with GWT. And complete example of RPC call.

There are couple of  possibilities to hit the database with GWT like RequestFactory and RPC.

Before getting started with server calls please go through,

 - GWT RPC (Which makes server calls Asynchronously)

 - RequestFactory (Alternative to GWT-RPC  which use proxies in between).

In this perticular post, let see the complete example of GWT RPC call to server.

Simple RPC structure can show  as below :

 GWT Code <===> InterfaceAsync <===> Interface (Synchronous)<===> Server Code 


Ok, Let's write code for small RPC, which is a PingPong. Client just says Ping to server and server responds with Pong.

ASynchronous Interface PingPongRPCInterfaceAsync , which we can able to access on client side.

     import com.google.gwt.user.client.rpc.AsyncCallback;  
     public interface PingPongRPCInterfaceAsync {  
     public void PingPongRPC (String message, AsyncCallback callback);  
     } 

The Synchronous Interface PingPongRPCInterface

   import com.google.gwt.user.client.rpc.RemoteService;  
   public interface PingPongRPCInterface extends RemoteService {  
     public String PingPongRPC(String message);  
   } 

Here is the  Service layer class which is equals to Servlet in J2EE and which implements our server side interface PingPongRPCInterface.

    public class PingPongRPCImpl extends  RemoteServiceServlet implements PingPongRPCInterface {  
     private static final long serialVersionUID = 1L;  
     public String pingPongRPC(Sting message)  
     {  
       // Hey I received a message here from client   
             // And sending response to client back   
       System.out.println("Message from client" + message);       
       String serverResponse= "Pong";  
       return serverResponse;              
     }  
   } 


Map the above server impl class in your web.xml;

     <servlet>  
     <servlet-name>beanrpc</servlet-name>  
     <servlet-class>com.server.pingPongRPCImpl</servlet-class>  
    </servlet>  
    <servlet-mapping>  
     <servlet-name>pingpongrpc</servlet-name>  
     <url-pattern>/pingpongrpc</url-pattern>  
    </servlet-mapping>


We all done with implementation part and let see how we can use this service call in our client side.

       private final pingPongRPCInterfaceAsync beanService =  
       GWT.create(pingPongRPCInterface.class);  
       ServiceDefTarget endpoint = (ServiceDefTarget) service;  
       endpoint.setServiceEntryPoint('pingpongrpc');  


Once you register your service you can just invoke the method from your client interface as
   

String message = "Ping to server";  
   beanService.pingPongRPC(message, callback);  
    AsyncCallback callback = new AsyncCallback()  
     {  
       public void onFailure(Throwable caught)  
       {  
         //Do on fail  
       }  
       public void onSuccess(String result)  
       {  
         //Process successfully done with result  
       }  
     };


And please note down the below package structures:

PingPongRPCInterfaceAsync ,pingPongRPCInterface should be in client* package
PingPongRPCImpl   should be in  server package.

That's a complete example of RPC. Please post in comments if you have any doubt or exception in middle of RPC.




  


Thursday, December 11, 2014

No source code is available for type *Class*: did you forget to inherit a required module?


If you found the above titled error in your IDE, probably you are facing the most common error for every GWT beginner. The fact is that,  in GWT you are limited to use only some of the Java classes from whole Java API. You cannot simply use all the available classes in Java with GWT. 

Keeping in mind the fact that GWT compiles Java code to JavaScript  there are multiple reasons and limitations from JavaScript side to limit most of the Java features. So GWT is very choosy with Java API classes and you have to use those classes only.

With GWT it is not possible to convert all the Java classes to JavaScript because there are some functional differences with Java and JavaScript. It is not quite possible to achieve all the functional benefits of Java to JavaScript.

For instance, let see two functional differences, why it is not possible to use those Java classes in Javascript.

Instance 1 : java.io.File

You cannot use/do IO operations in Javascript since HTML and Javascript cannot have access to file system. That's the reason GWT restrict io classes of Java to use.

Instance 2 : java.lang.Thread

You cannot use/do multi threading in Javascript since it is single thread model. That's the reason GWT restrict Thread related classes to use.

Like above instances, there are other instances to restrict the usage of many classes from Java. Again coming to the solution part, we can't do anything about the restriction and we have to stick to the available classes. You need not to remember all those classes list. Below is the complete list of subset that GWT can emulate.

Below are the packages support in GWT from JRE.

java.lang
java.lang.annotation
java.math
java.io
java.sql
java.util
java.util.logging.

Even from the above packages you cannot use all the classes. Just go through the below link for complete list of classes GWT support from Java library.

http://www.gwtproject.org/doc/latest/RefJreEmulation.html


When you stumble with the titled error just go through this list and check that you are using the white listed Java classes in GWT or not. If you are not using some other class/classes which are not white listed, you'll end up with the exception/error



Recent post : Why GWT loads slow and tips to load GWT app faster

Wednesday, November 5, 2014

Why GWT loads slow and tips to load GWT app faster.

Why GWT loads very slow for first time:


GWT uses perfect caching technique. The initial loading time is really depends on many factors.

When user/browser requests for the very first time, all the resources related to the page loads. That takes few seconds to load and really depends on your internet speed. Once the complete page loaded and if you are requesting new page/ reloading the current page, all the resources won't load this time.

Tuesday, September 16, 2014

Print preview images and css not loading for first time.

While I'm trying to print one of my web pages in browser, faced a strange issue today. When I click the print button for the first time, the print preview (in chrome browser) looks like weird and no images and styles are appeared in the print preview. Where as if I click again, strangely everything is fine.

Monday, September 15, 2014

Why to use UIBinder in GWT

One of my favorite feature released in GWT 2.4 is Declarative Layout aka UiBinder, a quick way to design the UI. The biggest advantage of using it is to separate the UI declaration from sensitive Java code. When the release team introducing UiBinder, they stated as below

Saturday, August 23, 2014

The style name 'z-index' should be in camelCase format

Here is one more exception we usually face while coding in GWT. When we try to give a style property to the element in the below way as we just give a property in CSS


getElement().getStyle().setProperty("z-index", "0");  

Immediately we run into the below exception,

Thursday, July 24, 2014

Context Menu or Right Click Handler in GWT

Context menu is one of the strange thing to deal with it and hard to implement it also. In this post let see how we deal with right click menu for browser (means whole application) and for a single widget in browser. The below steps shows how to implement a basic context menu aka right click menu.
  • Create a menu of what you are trying to show.
  • Take a Popup Panel. And add your menu to that Popup Panel.
  • Just place the handler to the Root-panel which is going to fire when user right click's on the browser.
Lets implement the same points in code and see how it goes.
 rootPanel.sinkEvents(Event.ONCONTEXTMENU);
 rootPanel.addHandler(
     new ContextMenuHandler() {@
         Override
         public void onContextMenu(ContextMenuEvent event) {
             event.preventDefault();
             event.stopPropagation();
             popupMenu.setPopupPosition(event.getNativeEvent().getClientX(),
                 event.getNativeEvent().getClientY());
             popupMenu.show();
         }
     }, ContextMenuEvent.getType());
The above code tweaks the right click handler and shows the custom pop up menu on right click of right click. And if you want to show custom right click menu for a single widget, the below code shows it how.
 lable.sinkEvents(Event.ONCONTEXTMENU);
lable.addHandler(
    new ContextMenuHandler() {@
        Override
        public void onContextMenu(ContextMenuEvent event) {
            event.preventDefault();
            event.stopPropagation();
            popupMenu.setPopupPosition(event.getNativeEvent().getClientX(),
                event.getNativeEvent().getClientY());
            popupMenu.show();
        }
    }, ContextMenuEvent.getType());
That is all to do show context menu for specific label or any other widget referred.

Next >> Multiple inheritance in interface supports in Java 

Monday, July 21, 2014

Getting all child widgets added to GWT Panel's (Children Widgets of a Panel)

When we are dealing with OOP, we hate code duplication and love to use loops. As the same applies to gwt widgets as well, let see how we iterate over widgets. Consider a panel which have no. of widgets on it and at some point of time when we want to apply some style or action on it, as a programmer we prefer a loop rather than copy pasting. Let see now how we can iterate over all children attached to a parent widget.

 Iterator < Widget > arrayOfWidgets = anyGWTPanel.iterator();
 while (arrayOfWidgets.hasNext()) {
    Widget ch = arrayOfWidgets.next();
    //Play with ch   
}   
That is all to do to iterate all over the parent widget and in case of to find out specific type of widgets, for example to iterate over all buttons of a widget we can simply use instanceof key word and find out all the buttons.
 Iterator < Widget > arrayOfWidgets = anyGWTPanel.iterator();
 while (arrayOfWidgets.hasNext()) {
    Widget ch = arrayOfWidgets.next();
    if (ch instanceof Button) {
        //Do something (in your case make an arraylist of your objects)  
    }
}
Iterator() method is very handy when you are creating widgets in Java rather than UiBinder in GWT. If you are using UiBinder, then the task is very easy since we have very expertise in dealing with HTML.

If you are familiar with Java for-each loop, then it is quite easy to write as 


 for (Widget ch : anyGWTPanel) {  
 if (ch instanceof Button) {  
     //Do something (in your case make an arraylist of your objects)   
   }  
 }

Note: The iterator() method just gives the widgets added to the panel but not those added to its children panels. So you need to iterate again on the specific panel to get it children as well.


Next >>  Why to use GWT UIBinder