GWT - COMPLEX WIDGETS“Complex widgets” allow users to perform advanced interaction capability with the application. Every complex widget inherits properties from Widget class which in turn inherits properties from UIObject.
Introduction The class UIObject is the superclass for all user-interface objects. It simply wraps a DOM element, and cannot receive events. It provides direct child classes like Widget, MenuItem, MenuItemSeparator, TreeItem.
Following is the declaration for com.google.gwt.user.client.ui.UIObject class:
public abstract class UIObject
extends java.lang.Object Field Following are the fields for com.google.gwt.user.client.ui.UIObject class:
This class inherits methods from the following classes:
Introduction The class Widget is the base class for the majority of user-interface objects. Widget adds support for receiving events from the browser and being added directly to panels. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.Widget class:
public class Widget
extends UIObject implements EventListener Field Following are the fields for com.google.gwt.user.client.ui.Widget class:
This class inherits methods from the following classes:
Complex Widgets Following are few important Complex Widgets:
Introduction The Tree widget represents a standard hierarchical tree widget. The tree contains a hierarchy of TreeItems that the user can open, close, and select. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.Tree class:
public class Tree
extends Widget implements HasWidgets, SourcesTreeEvents, HasFocus, HasAnimation, HasAllKeyHandlers, HasAllFocusHandlers, HasSelectionHandlers, HasOpenHandlers, HasCloseHandlers, SourcesMouseEvents, HasAllMouseHandlers CSS Style Rules Following default CSS Style rules will be applied to all the Tree widget. You can override it as per your requirements.
.gwt-Tree {}
.gwt-TreeItem {} .gwt-TreeItem-selected {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a Tree Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } .gwt-Label { font-weight: bold; color: maroon; } .gwt-Tree .gwt-TreeItem { padding: 1px 0px; margin: 0px; white-space: nowrap; cursor: hand; cursor: pointer; } .gwt-Tree .gwt-TreeItem-selected { background: #ebeff9; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>Tree Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of Tree widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Tree; import com.google.gwt.user.client.ui.TreeItem; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { //create a label final Label labelMessage = new Label(); labelMessage.setWidth("300"); // Create a root tree item as department TreeItem department = new TreeItem("Department"); //create other tree items as department names TreeItem salesDepartment = new TreeItem("Sales"); TreeItem marketingDepartment = new TreeItem("Marketing"); TreeItem manufacturingDepartment = new TreeItem("Manufacturing"); //create other tree items as employees TreeItem employee1 = new TreeItem("Robert"); TreeItem employee2 = new TreeItem("Joe"); TreeItem employee3 = new TreeItem("Chris"); //add employees to sales department salesDepartment.addItem(employee1); salesDepartment.addItem(employee2); salesDepartment.addItem(employee3); //create other tree items as employees TreeItem employee4 = new TreeItem("Mona"); TreeItem employee5 = new TreeItem("Tena"); //add employees to marketing department marketingDepartment.addItem(employee4); marketingDepartment.addItem(employee5); //create other tree items as employees TreeItem employee6 = new TreeItem("Rener"); TreeItem employee7 = new TreeItem("Linda"); //add employees to sales department manufacturingDepartment.addItem(employee6); manufacturingDepartment.addItem(employee7); //add departments to department item department.addItem(salesDepartment); department.addItem(marketingDepartment); department.addItem(manufacturingDepartment); //create the tree Tree tree = new Tree(); //add root item to the tree tree.addItem(department); tree.addSelectionHandler(new SelectionHandlerL<TreeItem>() { @Override public void onSelection(SelectionEvent<TreeItem> event) { labelMessage.setText("Selected Value: " + event.getSelectedItem().getText()); } }); // Add text boxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(tree); panel.add(labelMessage); //add the tree to the root panel RootPanel.get("gwtContainer").add(panel); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, this will produce the following result: Selecting any value in tree, will update a message below the tree displaying the selected value. GWT - MenuBar Widget Introduction The MenuBar widget represents a standard menu bar widget. A menu bar can contain any number of menu items, each of which can either fire a Command or open a cascaded menu bar. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.MenuBar class:
public class MenuBar
extends Widget implements PopupListener, HasAnimation, HasCloseHandlers<PopupPanel> CSS Style Rules Following default CSS Style rules will be applied to all the MenuBar widget. You can override it as per your requirements.
.gwt-MenuBar {}
.gwt-MenuBar-horizontal {} .gwt-MenuBar-vertical{} .gwt-MenuBar .gwt-MenuItem {} .gwt-MenuBar .gwt-MenuItem-selected {} .gwt-MenuBar .gwt-MenuItemSeparator {} .gwt-MenuBar .gwt-MenuItemSeparator .menuSeparatorInner {} .gwt-MenuBarPopup .menuPopupTopLeft {} .gwt-MenuBarPopup .menuPopupTopLeftInner {} .gwt-MenuBarPopup .menuPopupTopCenter {} .gwt-MenuBarPopup .menuPopupTopCenterInner {} .gwt-MenuBarPopup .menuPopupTopRight {} .gwt-MenuBarPopup .menuPopupTopRightInner {} .gwt-MenuBarPopup .menuPopupMiddleLeft {} .gwt-MenuBarPopup .menuPopupMiddleLeftInner {} .gwt-MenuBarPopup .menuPopupMiddleCenter {} .gwt-MenuBarPopup .menuPopupMiddleCenterInner {} .gwt-MenuBarPopup .menuPopupMiddleRight {} .gwt-MenuBarPopup .menuPopupMiddleRightInner {} .gwt-MenuBarPopup .menuPopupBottomLeft {} .gwt-MenuBarPopup .menuPopupBottomLeftInner {} .gwt-MenuBarPopup .menuPopupBottomCenter {} .gwt-MenuBarPopup .menuPopupBottomCenterInner {} .gwt-MenuBarPopup .menuPopupBottomRight {} .gwt-MenuBarPopup .menuPopupBottomRightInner {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a MenuBar Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } .gwt-MenuBar { cursor: default; } .gwt-MenuBar .gwt-MenuItem { cursor: default; font-family: Arial Unicode MS, Arial, sans-serif; font-size: 12px; } .gwt-MenuBar .gwt-MenuItem-selected { background: #E3E8F3; } .gwt-MenuBar-horizontal { background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px; border: 1px solid #e0e0e0; } .gwt-MenuBar-horizontal .gwt-MenuItem { padding: 5px 10px; vertical-align: bottom; color: #000; font-weight: bold; } .gwt-MenuBar-horizontal .gwt-MenuItemSeparator { width: 1px; padding: 0px; margin: 0px; border: 0px; border-left: 1px solid #ccc; background: white; } .gwt-MenuBar-horizontal .gwt-MenuItemSeparator .menuSeparatorInner { width: 1px; height: 1px; background:white; } .gwt-MenuBar-vertical { margin-top: 0px; margin-left: 0px; background: white; } .gwt-MenuBar-vertical table { border-collapse: collapse; } .gwt-MenuBar-vertical .gwt-MenuItem { padding: 2px 40px 2px 1px; } .gwt-MenuBar-vertical .gwt-MenuItemSeparator { padding: 2px 0px; } .gwt-MenuBar-vertical .gwt-MenuItemSeparator .menuSeparatorInner { height: 1px; padding: 0px; border: 0px; border-top: 1px solid #ccc; overflow: hidden; } .gwt-MenuBar-vertical .subMenuIcon { padding-right: 4px; } .gwt-MenuBar-vertical .subMenuIcon-selected { background: #E3E8F3; } .gwt-MenuBarPopup { margin: 0px 0px 0px 3px; } .gwt-MenuBarPopup .menuPopupTopLeftInner { width: 5px; height: 5px; zoom: 1; } .gwt-MenuBarPopup .menuPopupTopRightInner { width: 8px; height: 5px; zoom: 1; } .gwt-MenuBarPopup .menuPopupBottomLeftInner { width: 5px; height: 8px; zoom: 1; } .gwt-MenuBarPopup .menuPopupBottomRightInner { width: 8px; height: 8px; zoom: 1; } .gwt-MenuBarPopup .menuPopupTopLeft { background: url(images/corner.png) no-repeat 0px -36px; -background: url(images/corner_ie6.png) no-repeat 0px -36px; } .gwt-MenuBarPopup .menuPopupTopRight { background: url(images/corner.png) no-repeat -5px -36px; -background: url(images/corner_ie6.png) no-repeat -5px -36px; } .gwt-MenuBarPopup .menuPopupBottomLeft { background: url(images/corner.png) no-repeat 0px -41px; -background: url(images/corner_ie6.png) no-repeat 0px -41px; } .gwt-MenuBarPopup .menuPopupBottomRight { background: url(images/corner.png) no-repeat -5px -41px; -background: url(images/corner_ie6.png) no-repeat -5px -41px; } html>body .gwt-MenuBarPopup { } * html .gwt-MenuBarPopup .menuPopupTopLeftInner { width: 5px; height: 5px; overflow: hidden; } * html .gwt-MenuBarPopup .menuPopupTopRightInner { width: 8px; height: 5px; overflow: hidden; } * html .gwt-MenuBarPopup .menuPopupBottomLeftInner { width: 5px; height: 8px; overflow: hidden; } * html .gwt-MenuBarPopup .menuPopupBottomRightInner { width: 8px; height: 8px; overflow: hidden; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>MenuBar Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of MenuBar widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.MenuBar; import com.google.gwt.user.client.ui.MenuItem; import com.google.gwt.user.client.ui.RootPanel; public class HelloWorld implements EntryPoint { private void showSelectedMenuItem(String menuItemName){ Window.alert("Menu item: "+menuItemName+" selected"); } public void onModuleLoad() { // Create a menu bar MenuBar menu = new MenuBar(); menu.setAutoOpen(true); menu.setWidth("100px"); menu.setAnimationEnabled(true); // Create the file menu MenuBar fileMenu = new MenuBar(true); fileMenu.setAnimationEnabled(true); fileMenu.addItem("New", new Command() { @Override public void execute() { showSelectedMenuItem("New"); } }); fileMenu.addItem("Open", new Command() { @Override public void execute() { showSelectedMenuItem("Open"); } }); fileMenu.addSeparator(); fileMenu.addItem("Exit", new Command() { @Override public void execute() { showSelectedMenuItem("Exit"); } }); // Create the edit menu MenuBar editMenu = new MenuBar(true); editMenu.setAnimationEnabled(true); editMenu.addItem("Undo", new Command() { @Override public void execute() { showSelectedMenuItem("Undo"); } }); editMenu.addItem("Redo", new Command() { @Override public void execute() { showSelectedMenuItem("Redo"); } }); editMenu.addItem("Cut", new Command() { @Override public void execute() { showSelectedMenuItem("Cut"); } }); editMenu.addItem("Copy", new Command() { @Override public void execute() { showSelectedMenuItem("Copy"); } }); editMenu.addItem("Paste", new Command() { @Override public void execute() { showSelectedMenuItem("Paste"); } }); menu.addItem(new MenuItem("File", fileMenu)); menu.addSeparator(); menu.addItem(new MenuItem("Edit", editMenu)); //add the menu to the root panel RootPanel.get("gwtContainer").add(menu); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then this will produce the following result: Selecting any value in menubar, will popup an alert message showing the selected value. GWT - Date Picker Widget Introduction The DatePicker widget represents a standard GWT date picker. Class Declaration Following is the declaration for com.google.gwt.user.datepicker.client.DatePicker class:
public class DatePicker
extends Composite implements HasHighlightHandlers<java.util.Date>, HasShowRangeHandlers<java.util.Date>, HasValue<java.util.Date> CSS Style Rules Following default CSS Style rules will be applied to all the DatePicker widget. You can override it as per your requirements.
.gwt-DatePicker { }
.datePickerMonthSelector { } .datePickerMonth { } .datePickerPreviousButton { } .datePickerNextButton { } .datePickerDays { } .datePickerWeekdayLabel { } .datePickerWeekendLabel { } .datePickerDay { } .datePickerDayIsToday { } .datePickerDayIsWeekend { } .datePickerDayIsFiller { } .datePickerDayIsValue { } .datePickerDayIsDisabled { } .datePickerDayIsHighlighted { } .datePickerDayIsValueAndHighlighted { } Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a DatePicker Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } .gwt-DatePicker { border: 1px solid #ccc; border-top:1px solid #999; cursor: default; } .gwt-DatePicker td, .datePickerMonthSelector td:focus { outline: none; } .datePickerMonthSelector td:focus { outline: none; } .datePickerDays { width: 100%; background: white; } .datePickerDay, .datePickerWeekdayLabel, .datePickerWeekendLabel { font-size: 85%; text-align: center; padding: 4px; outline: none; font-weight:bold; color:#333; border-right: 1px solid #EDEDED; border-bottom: 1px solid #EDEDED; } .datePickerWeekdayLabel, .datePickerWeekendLabel { background: #fff; padding: 0px 4px 2px; cursor: default; color:#666; font-size:70%; font-weight:normal; } .datePickerDay { padding: 4px 7px; cursor: hand; cursor: pointer; } .datePickerDayIsWeekend { background: #f7f7f7; } .datePickerDayIsFiller { color: #999; font-weight:normal; } .datePickerDayIsValue { background: #d7dfe8; } .datePickerDayIsDisabled { color: #AAAAAA; font-style: italic; } .datePickerDayIsHighlighted { background: #F0E68C; } .datePickerDayIsValueAndHighlighted { background: #d7dfe8; } .datePickerDayIsToday { padding: 3px; color: #00f; background: url(images/hborder.png) repeat-x 0px -2607px; } .datePickerMonthSelector { width: 100%; padding: 1px 0 5px 0; background: #fff; } .datePickerPreviousButton, .datePickerNextButton { font-size: 120%; line-height: 1em; color: #3a6aad; cursor: hand; cursor: pointer; font-weight: bold; padding: 0px 4px; outline: none; } td.datePickerMonth { text-align: center; vertical-align: middle; white-space: nowrap; font-size: 100%; font-weight: bold; color: #333; } .gwt-DateBox { padding: 5px 4px; border: 1px solid #ccc; border-top: 1px solid #999; font-size: 100%; } .gwt-DateBox input { width: 8em; } .dateBoxFormatError { background: #ffcccc; } .dateBoxPopup { } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>DatePicker Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of Tree widget.
package com.jtc.client;
import java.util.Date; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.datepicker.client.DateBox; import com.google.gwt.user.datepicker.client.DatePicker; public class HelloWorld implements EntryPoint { public void onModuleLoad() { // Create a basic date picker DatePicker datePicker = new DatePicker(); final Label text = new Label(); // Set the value in the text box when the user selects a date datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() { @Override public void onValueChange(ValueChangeEvent<Date> event) { Date date = event.getValue(); String dateString = DateTimeFormat.getFormat("MM/dd/yyyy").format(date); text.setText(dateString); } }); // Set the default value datePicker.setValue(new Date(), true); // Create a DateBox DateTimeFormat dateFormat = DateTimeFormat.getFormat("MM/dd/yyyy"); DateBox dateBox = new DateBox(); dateBox.setFormat(new DateBox.DefaultFormat(dateFormat)); Label selectLabel = new Label("Permanent DatePicker:"); Label selectLabel2 = new Label("DateBox with popup DatePicker:"); // Add widgets to the root panel. VerticalPanel vPanel = new VerticalPanel(); vPanel.setSpacing(10); vPanel.add(selectLabel); vPanel.add(text); vPanel.add(datePicker); vPanel.add(selectLabel2); vPanel.add(dateBox); RootPanel.get("gwtContainer").add(vPanel); } } Once you are are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then will produce the following result: GWT - Cell Tree Widget Introduction The CellTree widget represents a view of a tree.
public class CellTree
extends AbstractCellTree implements HasAnimation, Focusable Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a CellTree Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CellTree Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of CellTree widget.
package com.jtc.client;
import java.util.ArrayList; import java.util.List; import com.google.gwt.cell.client.AbstractCell; import com.google.gwt.cell.client.Cell; import com.google.gwt.cell.client.TextCell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.user.cellview.client.CellTree; import com.google.gwt.user.cellview.client. HasKeyboardSelectionPolicy.KeyboardSelectionPolicy; import com.google.gwt.user.cellview.client.TreeNode; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.SingleSelectionModel; import com.google.gwt.view.client.TreeViewModel; public class HelloWorld implements EntryPoint { /** * A list of songs. */ private static class Playlist { private final String name; private final List<String> songs = new ArrayList7lt;String>(); public Playlist(String name) { this.name = name; } /** * Add a song to the playlist. * * @param name the name of the song */ public void addSong(String name) { songs.add(name); } public String getName() { return name; } /** * Return the list of songs in the playlist. */ public List<String> getSongs() { return songs; } } /** * A composer of classical music. */ private static class Composer { private final String name; private final List<Playlist> playlists = new ArrayList<Playlist>(); public Composer(String name) { this.name = name; } /** * Add a playlist to the composer. * * @param playlist the playlist to add */ public Playlist addPlaylist(Playlist playlist) { playlists.add(playlist); return playlist; } public String getName() { return name; } /** * Return the rockin' playlist for this composer. */ public List<Playlist> getPlaylists() { return playlists; } } /** * The model that defines the nodes in the tree. */ private static class CustomTreeModel implements TreeViewModel { private final List<Composer> composers; /** * This selection model is shared across all leaf nodes. * A selection model can also be shared across all nodes * in the tree, or each set of child nodes can have * its own instance. This gives you flexibility to * determine how nodes are selected. */ private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>(); public CustomTreeModel() { // Create a database of information. composers = new ArrayList<Composer>(); // Add compositions by Beethoven. { Composer beethoven = new Composer("Beethoven"); composers.add(beethoven); Playlist concertos = beethoven.addPlaylist( new Playlist("Concertos")); concertos.addSong("No. 1 - C"); concertos.addSong("No. 2 - B-Flat Major"); concertos.addSong("No. 3 - C Minor"); concertos.addSong("No. 4 - G Major"); concertos.addSong("No. 5 - E-Flat Major"); Playlist quartets = beethoven.addPlaylist( new Playlist("Quartets")); quartets.addSong("Six String Quartets"); quartets.addSong("Three String Quartets"); quartets.addSong("Grosse Fugue for String Quartets"); Playlist sonatas = beethoven.addPlaylist( new Playlist("Sonatas")); sonatas.addSong("Sonata in A Minor"); sonatas.addSong("Sonata in F Major"); Playlist symphonies = beethoven.addPlaylist( new Playlist("Symphonies")); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 3 - E-Flat Major"); symphonies.addSong("No. 4 - B-Flat Major"); symphonies.addSong("No. 5 - C Minor"); symphonies.addSong("No. 6 - F Major"); symphonies.addSong("No. 7 - A Major"); symphonies.addSong("No. 8 - F Major"); symphonies.addSong("No. 9 - D Minor"); } // Add compositions by Brahms. { Composer brahms = new Composer("Brahms"); composers.add(brahms); Playlist concertos = brahms.addPlaylist( new Playlist("Concertos")); concertos.addSong("Violin Concerto"); concertos.addSong("Double Concerto - A Minor"); concertos.addSong("Piano Concerto No. 1 - D Minor"); concertos.addSong("Piano Concerto No. 2 - B-Flat Major"); Playlist quartets = brahms.addPlaylist( new Playlist("Quartets")); quartets.addSong("Piano Quartet No. 1 - G Minor"); quartets.addSong("Piano Quartet No. 2 - A Major"); quartets.addSong("Piano Quartet No. 3 - C Minor"); quartets.addSong("String Quartet No. 3 - B-Flat Minor"); Playlist sonatas = brahms.addPlaylist( new Playlist("Sonatas")); sonatas.addSong("Two Sonatas for Clarinet - F Minor"); sonatas.addSong("Two Sonatas for Clarinet - E-Flat Major"); Playlist symphonies = brahms.addPlaylist( new Playlist("Symphonies")); symphonies.addSong("No. 1 - C Minor"); symphonies.addSong("No. 2 - D Minor"); symphonies.addSong("No. 3 - F Major"); symphonies.addSong("No. 4 - E Minor"); } // Add compositions by Mozart. { Composer mozart = new Composer("Mozart"); composers.add(mozart); Playlist concertos = mozart.addPlaylist( new Playlist("Concertos")); concertos.addSong("Piano Concerto No. 12"); concertos.addSong("Piano Concerto No. 17"); concertos.addSong("Clarinet Concerto"); concertos.addSong("Violin Concerto No. 5"); concertos.addSong("Violin Concerto No. 4"); } } /** * Get the {@link NodeInfo} that provides the children of the * specified value. */ public <T> NodeInfo<?> getNodeInfo(T value) { if (value == null) { // LEVEL 0. // We passed null as the root value. Return the composers. // Create a data provider that contains the list of composers. ListDataProvider<Composer> dataProvider = new ListDataProvider<HelloWorld.Composer>( composers); // Create a cell to display a composer. Cell<HelloWorld.Composer> cell = new AbstractCell<HelloWorld.Composer>() { @Override public void render(Composer value, Object key, SafeHtmlBuilder sb) { if (value != null) { sb.appendHtmlConstant(" "); sb.appendEscaped(value.getName()); } } }; // Return a node info that pairs the data provider and the cell. return new DefaultNodeInfo<Composer>(dataProvider, cell); } else if (value instanceof Composer) { // LEVEL 1. // We want the children of the composer. Return the playlists. ListDataProvider<HelloWorld.Playlist> dataProvider = new ListDataProvider<HelloWorld.Playlist>( ((Composer) value).getPlaylists()); Cell<HelloWorld.Playlist> cell = new AbstractCell<HelloWorld.Playlist>() { @Override public void render(Playlist value, Object key, SafeHtmlBuilder sb) { if (value != null) { sb.appendHtmlConstant(" "); sb.appendEscaped(value.getName()); } } }; return new DefaultNodeInfo<Playlist>(dataProvider, cell); } else if (value instanceof Playlist) { // LEVEL 2 - LEAF. // We want the children of the playlist. Return the songs. ListDataProvider<String> dataProvider = new ListDataProvider<String>( ((Playlist) value).getSongs()); // Use the shared selection model. return new DefaultNodeInfo<String>(dataProvider, new TextCell(), selectionModel, null); } return null; } /** * Check if the specified value represents a leaf node. * Leaf nodes cannot be opened. */ public boolean isLeaf(Object value) { // The leaf nodes are the songs, which are Strings. if (value instanceof String) { return true; } return false; } } public void onModuleLoad() { // Create a model for the tree. TreeViewModel model = new CustomTreeModel(); //Get CellTree style using its BasicResources //CellTree.Resources res = GWT.create(CellTree.BasicResources.class); /* * Create the tree using the model. We use <code>null</code> * as the default value of the root node. The default value will * be passed to CustomTreeModel#getNodeInfo(); */ CellTree tree = new CellTree(model, null); tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); // Open the first playlist by default. TreeNode rootNode = tree.getRootTreeNode(); TreeNode firstPlaylist = rootNode.setChildOpen(0, true); firstPlaylist.setChildOpen(0, true); VerticalPanel panel = new VerticalPanel(); panel.setBorderWidth(1); panel.setWidth("300"); panel.add(tree); // Add the widgets to the root panel. RootPanel.get().add(panel); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then this will produce the following result: GWT - Cell List Widget Introduction The CellList widget represents a single column list of cells. Class Declaration Following is the declaration for com.google.gwt.user.cellview.client.CellList<T> class:
public class CellList<T>
extends AbstractHasData<T> Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a CellList Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CellList Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of CellList widget.
package com.jtc.client;
import java.util.Arrays; import java.util.List; import com.google.gwt.cell.client.AbstractCell; import com.google.gwt.cell.client.Cell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.user.cellview.client.CellList; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.ProvidesKey; import com.google.gwt.view.client.SelectionModel; import com.google.gwt.view.client.SingleSelectionModel; public class HelloWorld implements EntryPoint { /** * A simple data type that represents a contact. */ private static class Contact { private static int nextId = 0; private final int id; private String name; public Contact(String name) { nextId++; this.id = nextId; this.name = name; } } /** * A custom {@link Cell} used to render a {@link Contact}. */ private static class ContactCell extends AbstractCell<Contact> { @Override public void render(Contact value, Object key, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.name); } } } /** * The list of data to display. */ private static final List<Contact> CONTACTS = Arrays.asList(new Contact( "John"), new Contact("Joe"), new Contact("Michael"), new Contact("Sarah"), new Contact("George")); public void onModuleLoad() { /* * Define a key provider for a Contact. We use the unique ID * as the key, which allows to maintain selection even if the * name changes. */ ProvidesKey<Contact> keyProvider = new ProvidesKey<Contact>() { public Object getKey(Contact item) { // Always do a null check. return (item == null) ? null : item.id; } }; // Create a CellList using the keyProvider. CellList<Contact> cellList = new CellList<Contact>(new ContactCell(), keyProvider); // Push data into the CellList. cellList.setRowCount(CONTACTS.size(), true); cellList.setRowData(0, CONTACTS); // Add a selection model using the same keyProvider. SelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>( keyProvider); cellList.setSelectionModel(selectionModel); /* * Select a contact. The selectionModel will select based on the * ID because we used a keyProvider. */ Contact sarah = CONTACTS.get(3); selectionModel.setSelected(sarah, true); // Modify the name of the contact. sarah.name = "Sara"; /* * Redraw the CellList. Sarah/Sara will still be selected because we * identify her by ID. If we did not use a keyProvider, * Sara would not be selected. */ cellList.redraw(); VerticalPanel panel = new VerticalPanel(); panel.setBorderWidth(1); panel.setWidth("200"); panel.add(cellList); // Add the widgets to the root panel. RootPanel.get().add(panel); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then this will produce the following result: GWT - Cell Table Widget Introduction The CellTable widget represents a A tabular view that supports paging and columns. Class Declaration Following is the declaration for com.google.gwt.user.cellview.client.CellTable<T> class:
public class CellTable<T>
extends AbstractHasData<T> Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a CellTable Widget in GWT. The following steps will help you update the GWT application which we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CellTable Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of CellTable widget.
package com.jtc.client;
import java.util.Arrays; import java.util.Date; import java.util.List; import com.google.gwt.cell.client.DateCell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.cellview.client.CellTable; import com.google.gwt.user.cellview.client.Column; import com.google.gwt.user.cellview.client .HasKeyboardSelectionPolicy.KeyboardSelectionPolicy; import com.google.gwt.user.cellview.client.TextColumn; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.SelectionChangeEvent; import com.google.gwt.view.client.SingleSelectionModel; public class HelloWorld implements EntryPoint { /** * A simple data type that represents a contact. */ private static class Contact { private final String address; private final Date birthday; private final String name; public Contact(String name, Date birthday, String address) { this.birthday = birthday; } } /** * The list of data to display. */ private static final List<Contact> CONTACTS = Arrays.asList( new Contact("John", new Date(80, 4, 12), "123 Fourth Avenue"), new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"), new Contact("George",new Date(46, 6, 6),"1600 Pennsylvania Avenue")); public void onModuleLoad() { // Create a CellTable. CellTable<Contact> table = new CellTable<Contact>(); table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); // Add a text column to show the name. TextColumn<Contact> nameColumn = new TextColumn<Contact>() { @Override public String getValue(Contact object) { return object.name; } }; table.addColumn(nameColumn, "Name"); // Add a date column to show the birthday. DateCell dateCell = new DateCell(); Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) { @Override public Date getValue(Contact object) { return object.birthday; } }; table.addColumn(dateColumn, "Birthday"); // Add a text column to show the address. TextColumn<Contact> addressColumn = new TextColumn<Contact>() { @Override public String getValue(Contact object) { return object.address; } }; table.addColumn(addressColumn, "Address"); // Add a selection model to handle user selection. final SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>(); table.setSelectionModel(selectionModel); selectionModel.addSelectionChangeHandler( new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { Contact selected = selectionModel.getSelectedObject(); if (selected != null) { Window.alert("You selected: " + selected.name); } } }); // Set the total row count. This isn't strictly necessary, // but it affects paging calculations, so its good habit to // keep the row count up to date. table.setRowCount(CONTACTS.size(), true); // Push the data into the widget. table.setRowData(0, CONTACTS); VerticalPanel panel = new VerticalPanel(); panel.setBorderWidth(1); panel.setWidth("400"); panel.add(table); // Add the widgets to the root panel. RootPanel.get().add(panel); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then this will produce the following result: GWT - CellBrowser Widget Introduction The CellBrowser widget represents a browsable view of a tree in which only a single node per level may be open at one time. Class Declaration Following is the declaration for com.google.gwt.user.cellview.client.CellBrowser<T> class:
public class CellBrowser
extends AbstractCellTree implements ProvidesResize, RequiresResize, HasAnimation Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a CellBrowser Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name='com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class='com.jtc.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path='client'/> <source path='shared'/> </module> Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center; font-family: verdana, sans-serif; } h1{ font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CellBrowser Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of CellBrowser widget.
package com.jtc.client;
import java.util.ArrayList; import java.util.List; import com.google.gwt.cell.client.AbstractCell; import com.google.gwt.cell.client.Cell; import com.google.gwt.cell.client.TextCell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.user.cellview.client.CellBrowser; import com.google.gwt.user.cellview.client. HasKeyboardSelectionPolicy.KeyboardSelectionPolicy; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.SingleSelectionModel; import com.google.gwt.view.client.TreeViewModel; public class HelloWorld implements EntryPoint { /** * A list of songs. */ private static class Playlist { private final String name; private final List<String> songs = new ArrayList<String>(); public Playlist(String name) { this.name = name; } /** * Add a song to the playlist. * * @param name the name of the song */ public void addSong(String name) { songs.add(name); } public String getName() { return name; } /** * Return the list of songs in the playlist. */ public List<String> getSongs() { return songs; } } /** * A composer of classical music. */ private static class Composer { private final String name; private final List<Playlist> playlists = new ArrayList<Playlist>(); public Composer(String name) { this.name = name; } /** * Add a playlist to the composer. * * @param playlist the playlist to add */ public Playlist addPlaylist(Playlist playlist) { playlists.add(playlist); return playlist; } public String getName() { return name; } /** * Return the rockin' playlist for this composer. */ public List<Playlist> getPlaylists() { return playlists; } } /** * The model that defines the nodes in the tree. */ private static class CustomTreeModel implements TreeViewModel { private final List<Composer> composers; /** * This selection model is shared across all leaf nodes. * A selection model can also be shared across all nodes * in the tree, or each set of child nodes can have its * own instance. This gives you flexibility to determine * how nodes are selected. */ private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>(); public CustomTreeModel() { // Create a database of information. composers = new ArrayList<Composer>(); // Add compositions by Beethoven. { Composer beethoven = new Composer("Beethoven"); composers.add(beethoven); Playlist concertos = beethoven.addPlaylist( new Playlist("Concertos")); concertos.addSong("No. 1 - C"); concertos.addSong("No. 2 - B-Flat Major"); concertos.addSong("No. 3 - C Minor"); concertos.addSong("No. 4 - G Major"); concertos.addSong("No. 5 - E-Flat Major"); Playlist quartets = beethoven.addPlaylist( new Playlist("Quartets")); quartets.addSong("Six String Quartets"); quartets.addSong("Three String Quartets"); quartets.addSong("Grosse Fugue for String Quartets"); Playlist sonatas = beethoven.addPlaylist( new Playlist("Sonatas")); sonatas.addSong("Sonata in A Minor"); sonatas.addSong("Sonata in F Major"); Playlist symphonies = beethoven.addPlaylist( new Playlist("Symphonies")); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 3 - E-Flat Major"); symphonies.addSong("No. 4 - B-Flat Major"); symphonies.addSong("No. 5 - C Minor"); symphonies.addSong("No. 6 - F Major"); symphonies.addSong("No. 7 - A Major"); symphonies.addSong("No. 8 - F Major"); symphonies.addSong("No. 9 - D Minor"); } // Add compositions by Brahms. { Composer brahms = new Composer("Brahms"); composers.add(brahms); Playlist concertos = brahms.addPlaylist( new Playlist("Concertos")); concertos.addSong("Violin Concerto"); concertos.addSong("Double Concerto - A Minor"); concertos.addSong("Piano Concerto No. 1 - D Minor"); concertos.addSong("Piano Concerto No. 2 - B-Flat Major"); Playlist quartets = brahms.addPlaylist( new Playlist("Quartets")); quartets.addSong("Piano Quartet No. 1 - G Minor"); quartets.addSong("Piano Quartet No. 2 - A Major"); quartets.addSong("Piano Quartet No. 3 - C Minor"); quartets.addSong("String Quartet No. 3 - B-Flat Minor"); Playlist sonatas = brahms.addPlaylist( new Playlist("Sonatas")); sonatas.addSong("Two Sonatas for Clarinet - F Minor"); sonatas.addSong("Two Sonatas for Clarinet - E-Flat Major"); Playlist symphonies = brahms.addPlaylist( new Playlist("Symphonies")); symphonies.addSong("No. 1 - C Minor"); symphonies.addSong("No. 2 - D Minor"); symphonies.addSong("No. 3 - F Major"); symphonies.addSong("No. 4 - E Minor"); } // Add compositions by Mozart. { Composer mozart = new Composer("Mozart"); composers.add(mozart); Playlist concertos = mozart.addPlaylist( new Playlist("Concertos")); concertos.addSong("Piano Concerto No. 12"); concertos.addSong("Piano Concerto No. 17"); concertos.addSong("Clarinet Concerto"); concertos.addSong("Violin Concerto No. 5"); concertos.addSong("Violin Concerto No. 4"); } } /** * Get the {@link NodeInfo} that provides the children of the specified * value. */ public <T> NodeInfo<?> getNodeInfo(T value) { if (value == null) { // LEVEL 0. // We passed null as the root value. Return the composers. // Create a data provider that contains the list of composers. ListDataProvider<Composer> dataProvider = new ListDataProvider<Composer>(composers); //Create a cell to display a composer. Cell<Composer> cell = new AbstractCell<Composer>(){ @Override public void render(Composer value, Object key, SafeHtmlBuilder sb) { sb.appendEscaped(value.getName()); } }; // Return a node info that pairs the data provider and the cell. return new DefaultNodeInfo<Composer>(dataProvider, cell); } else if (value instanceof Composer) { // LEVEL 1. // We want the children of the composer. Return the playlists. ListDataProvider<Playlist> dataProvider = new ListDataProvider<Playlist>( ((Composer) value).getPlaylists()); Cell<Playlist> cell = new AbstractCell<Playlist>() { @Override public void render(Playlist value, Object key, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.getName()); } } }; return new DefaultNodeInfo<Playlist>(dataProvider, cell); } else if (value instanceof Playlist) { // LEVEL 2 - LEAF. // We want the children of the playlist. Return the songs. ListDataProvider<String> dataProvider = new ListDataProvider<String>( ((Playlist) value).getSongs()); // Use the shared selection model. return new DefaultNodeInfo<String>(dataProvider, new TextCell(), selectionModel, null); } return null; } /** * Check if the specified value represents a leaf node. * Leaf nodes cannot be opened. */ public boolean isLeaf(Object value) { // The leaf nodes are the songs, which are Strings. if (value instanceof String) { return true; } return false; } } public void onModuleLoad() { // Create a model for the browser. TreeViewModel model = new CustomTreeModel(); /* * Create the browser using the model. We use <code>null</code> as the * default value of the root node. The default value will be passed to * CustomTreeModel#getNodeInfo(); */ CellBrowser browser = new CellBrowser(model, null); browser.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); browser.setHeight("200"); browser.setWidth("630"); VerticalPanel panel = new VerticalPanel(); panel.setBorderWidth(1); panel.add(browser); // Add the widgets to the root panel. RootPanel.get().add(panel); } } Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, then this will produce the following result: |