Monday, July 28, 2014

Difference between final and effectively final in Java.

While reading about Local classes, observed a brand new word called effectively final. The statement I read goes like "However, starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final." So started to find the exact difference between them.

Thursday, July 24, 2014

Multiple Inheritance in Interfaces supports in Java.

We all know that Java won't support multiple inheritance to avoid diamond problem. So there is no way of extending multiple classes in java. That ends the discussion about multiple inheritance in classes. But still Java allows multiple inheritance in Interfaces. That raises the question about the same diamond problem in Interface as well right?

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