GWT – Complex Widgets ”; Previous Next “Complex widgets” allows users to advanced interaction capability with the application. Every Complex widget inherits properties from Widget class which in turn inherits properties from UIObject. Sr.No. Widget & Description 1 GWT UIObject Class This widget contains text, not interpreted as HTML using a <div>element, causing it to be displayed with block layout. 2 GWT Widget Class This widget can contain HTML text and displays the html content using a <div> element, causing it to be displayed with block layout. Complex Widgets Following are few important Complex Widgets − Sr.No. Widget & Description 1 Tree This widget represents a standard hierarchical tree widget. The tree contains a hierarchy of TreeItems that the user can open, close, and select. 2 MenuBar This 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. 3 DatePicker This widget represents a standard GWT date picker. 4 CellTree This widget represents a view of a tree. This widget will only work in standards mode, which requires that the HTML page in which it is run have an explicit <!DOCTYPE> declaration. 5 CellList This widget represents a single column list of cells. 6 CellTable This widget represents a tabular view that supports paging and columns. 7 CellBrowser This widget represents a browsable view of a tree in which only a single node per level may be open at one time. This widget will only work in standards mode, which requires that the HTML page in which it is run have an explicit <!DOCTYPE> declaration. Print Page Previous Next Advertisements ”;
Category: gwt
GWT – UIBinder
GWT – UiBinder ”; Previous Next Introduction The UiBinder is a framework designed to separate Functionality and View of User Interface. The UiBinder framework allows developers to build gwt applications as HTML pages with GWT widgets configured throughout them. The UiBinder framework makes easier collaboration with UI designers who are more comfortable with XML, HTML and CSS than Java source code The UIBinder provides a declarative way of defining User Interface. The UIBinder seperates the programmic logic from UI. The UIBinder is similar to what JSP is to Servlets. UiBinder Workflow Step 1 – Create UI Declaration XML File Create a XML/HTML based User Interface declaration file. We”ve created a Login.ui.xml file in our example. <ui:UiBinder xmlns:ui = ”urn:ui:com.google.gwt.uibinder” xmlns:gwt = ”urn:import:com.google.gwt.user.client.ui” xmlns:res = ”urn:with:com.tutorialspoint.client.LoginResources”> <ui:with type = “com.tutorialspoint.client.LoginResources” field = “res”> </ui:with> <gwt:HTMLPanel> … </gwt:HTMLPanel> </ui:UiBinder> Step 2 – Use ui:field for Later Binding Use ui:field attribute in XML/HTML element to relate UI field in XML with UI field in JAVA file for later binding. <gwt:Label ui:field = “completionLabel1” /> <gwt:Label ui:field = “completionLabel2″ /> Step 3 – Create Java counterpart of UI XML Create Java based counterpart of XML based layout by extending Composite widget. We”ve created a Login.java file in our example. package com.tutorialspoint.client; … public class Login extends Composite { … } Step 4 – Bind Java UI fields with UiField annotation use @UiField annotation in Login.java to designate counterpart class members to bind to XML-based fields in Login.ui.xml public class Login extends Composite { … @UiField Label completionLabel1; @UiField Label completionLabel2; … } Step 5 – Bind Java UI with UI XML with UiTemplate annotation Instruct GWT to bind java based component Login.java and XML based layout Login.ui.xml using @UiTemplate annotation public class Login extends Composite { private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class); /* * @UiTemplate is not mandatory but allows multiple XML templates * to be used for the same widget. * Default file loaded will be <class-name>.ui.xml */ @UiTemplate(“Login.ui.xml”) interface LoginUiBinder extends UiBinder<Widget, Login> { } … } Step 6 – Create CSS File Create an external CSS fileLogin.css and Java based Resource LoginResources.java file equivalent to css styles .blackText { font-family: Arial, Sans-serif; color: #000000; font-size: 11px; text-align: left; } … Step 7 – Create Java based Resource File for CSS File package com.tutorialspoint.client; … public interface LoginResources extends ClientBundle { public interface MyCss extends CssResource { String blackText(); … } @Source(“Login.css”) MyCss style(); } Step 8 – Attach CSS resource in Java UI Code file. Attach an external CSS fileLogin.css using Contructor of Java based widget class Login.java public Login() { this.res = GWT.create(LoginResources.class); res.style().ensureInjected(); initWidget(uiBinder.createAndBindUi(this)); } UIBinder Complete Example This example will take you through simple steps to show usage of a UIBinder in GWT. Follow the following steps to update the GWT application we created in GWT – Create Application chapter − Step Description 1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT – Create Application chapter. 2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. 3 Compile and run the application to verify the result of the implemented logic. Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml. <?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”/> <!– Inherit the UiBinder module. –> <inherits name = “com.google.gwt.uibinder.UiBinder”/> <!– Specify the app entry point class. –> <entry-point class = ”com.tutorialspoint.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>UiBinder Demonstration</h1> <div id = “gwtContainer”></div> </body> </html> Now create a new UiBinder template and owner class (File → New → UiBinder). Choose the client package for the project and then name it Login. Leave all of the other defaults. Click Finish button and the plugin will create a new UiBinder template and owner class. Now create Login.css file in the src/com.tutorialspoint/client package and place the following contents in it .blackText { font-family: Arial, Sans-serif; color: #000000; font-size: 11px; text-align: left; } .redText { font-family: Arial, Sans-serif; color: #ff0000; font-size: 11px; text-align: left; } .loginButton { border: 1px solid #3399DD; color: #FFFFFF; background: #555555; font-size: 11px; font-weight: bold; margin: 0 5px 0 0; padding: 4px 10px 5px; text-shadow: 0 -1px 0 #3399DD; } .box { border: 1px solid #AACCEE; display: block; font-size: 12px; margin: 0 0 5px; padding: 3px; width: 203px; } .background { background-color: #999999; border: 1px none transparent; color: #000000; font-size: 11px; margin-left: -8px; margin-top: 5px; padding: 6px; } Now create LoginResources.java file in the src/com.tutorialspoint/client package and place the following contents in it package com.tutorialspoint.client; import com.google.gwt.resources.client.ClientBundle; import com.google.gwt.resources.client.CssResource; public interface LoginResources extends ClientBundle { /** * Sample CssResource. */ public interface MyCss extends CssResource { String blackText(); String redText(); String loginButton(); String box(); String background(); } @Source(“Login.css”) MyCss style(); } Replace the contents of Login.ui.xml in src/com.tutorialspoint/client package with the following <ui:UiBinder xmlns:ui = ”urn:ui:com.google.gwt.uibinder” xmlns:gwt = ”urn:import:com.google.gwt.user.client.ui” xmlns:res = ”urn:with:com.tutorialspoint.client.LoginResources”> <ui:with type = “com.tutorialspoint.client.LoginResources” field = “res”> </ui:with> <gwt:HTMLPanel> <div align = “center”> <gwt:VerticalPanel res:styleName = “style.background”> <gwt:Label text = “Login” res:styleName = “style.blackText” /> <gwt:TextBox ui:field=”loginBox” res:styleName = “style.box” /> <gwt:Label text = “Password” res:styleName = “style.blackText” /> <gwt:PasswordTextBox ui:field = “passwordBox” res:styleName = “style.box” /> <gwt:HorizontalPanel verticalAlignment = “middle”> <gwt:Button ui:field = “buttonSubmit” text=”Submit” res:styleName = “style.loginButton” /> <gwt:CheckBox ui:field = “myCheckBox” /> <gwt:Label ui:field = “myLabel” text = “Remember me” res:styleName = “style.blackText” /> </gwt:HorizontalPanel> <gwt:Label ui:field = “completionLabel1” res:styleName = “style.blackText” />
GWT – Discussion
Discuss GWT ”; Previous Next Google Web Toolkit (GWT) is a development toolkit for building and optimizing complex browser-based applications. GWT is used by many products at Google, including Google AdWords and Orkut. GWT is an open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache License version 2.0. This tutorial will give you a great understanding of GWT concepts needed to get a web application up and running. Print Page Previous Next Advertisements ”;
GWT – Internationalization
GWT – Internationalization ”; Previous Next GWT provides three ways to internationalize a GWT application, We”ll demonstrate use of Static String Internationalization being most commonly used among projects. Sr.No. Technique & Description 1 Static String Internationalization This technique is most prevalent and requires very little overhead at runtime; is a very efficient technique for translating both constant and parameterized strings;simplest to implement. Static string internationalization uses standard Java properties files to store translated strings and parameterized messages, and strongly-typed Java interfaces are created to retrieve their values. 2 Dynamic String Internationalization This technique is very flexible but slower than static string internationalization. Host page contains the localized strings therefore, applications are not required to be recompiled when we add a new locale. If GWT application is to be integrated with an existing server-side localization system, then this technique is to be used. 3 Localizable Interface This technique is the most powerful among the three techniques. Implementing Localizable allows us to create localized versions of custom types. It”s an advanced internationalization technique. Workflow of Internationalizing a GWT Application Step 1 – Create properties files Create properties file containing the messages to be used in the application. We”ve created a HelloWorldMessages.properties file in our example. enterName = Enter your name clickMe = Click Me applicationTitle = Application Internationalization Demonstration greeting = Hello {0} Create properties files containing translated values specific to locale. We”ve created a HelloWorldMessages_de.properties file in our example. This file contains translations in german language. _de specifies the german locale and we”re going to support german language in our application. If you are creating properties file using Eclipse then change the encoding of the file to UTF-8.Select the file and then right-click in it to open its properties window.Select Text file encoding as Other UTF-8. Apply and Save the change. enterName = Geben Sie Ihren Namen clickMe = Klick mich applicationTitle = Anwendung Internationalisierung Demonstration greeting = Hallo {0} Step 2 – Add i18n module to Module Descriptor XML File Update module file HelloWorld.gwt.xml to include support for german locale <?xml version = “1.0” encoding = “UTF-8″?> <module rename-to = ”helloworld”> … <extend-property name = “locale” values=”de” /> … </module> Step 3 – Create Interface equivalent to properties file Create HelloWorldMessages.java interface by extending Messages interface of GWT to include support for internalization. It should contain same method names as keys in properties file. Place holder would be replaced with String argument. public interface HelloWorldMessages extends Messages { @DefaultMessage(“Enter your name”) String enterName(); @DefaultMessage(“Click Me”) String clickMe(); @DefaultMessage(“Application Internalization Demonstration”) String applicationTitle(); @DefaultMessage(“Hello {0}”) String greeting(String name); } Step 4 – Use Message Interface in UI component. Use the object of HelloWorldMessages in HelloWorld to get the messages. public class HelloWorld implements EntryPoint { /* create an object of HelloWorldMessages interface using GWT.create() method */ private HelloWorldMessages messages = GWT.create(HelloWorldMessages.class); public void onModuleLoad() { … Label titleLabel = new Label(messages.applicationTitle()); //Add title to the application RootPanel.get(“gwtAppTitle”).add(titleLabel); … } } Internationalization – Complete Example This example will take you through simple steps to demonstrate Internationalization capability of a GWT application. Follow the following steps to update the GWT application we created in GWT – Create Application chapter − Step Description 1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT – Create Application chapter. 2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. 3 Compile and run the application to verify the result of the implemented logic. Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml. <?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.tutorialspoint.client.HelloWorld”/> <extend-property name = “locale” values=”de” /> <!– 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 id = “gwtAppTitle”></h1> <div id = “gwtContainer”></div> </body> </html> Now create HelloWorldMessages.properties file in the src/com.tutorialspoint/client package and place the following contents in it enterName = Enter your name clickMe = Click Me applicationTitle = Application Internationalization Demonstration greeting = Hello {0} Now create HelloWorldMessages_de.properties file in the src/com.tutorialspoint/client package and place the following contents in it enterName = Geben Sie Ihren Namen clickMe = Klick mich applicationTitle = Anwendung Internationalisierung Demonstration greeting = Hallo {0} Now create HelloWorldMessages.java class in the src/com.tutorialspoint/client package and place the following contents in it package com.tutorialspoint.client; import com.google.gwt.i18n.client.Messages; public interface HelloWorldMessages extends Messages { @DefaultMessage(“Enter your name”) String enterName(); @DefaultMessage(“Click Me”) String clickMe(); @DefaultMessage(“Application Internationalization Demonstration”) String applicationTitle(); @DefaultMessage(“Hello {0}”) String greeting(String name); } Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java using which we will demonstrate Internationalization capability of GWT Code. package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DecoratorPanel; import com.google.gwt.user.client.ui.HasHorizontalAlignment; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { /* create an object of HelloWorldMessages interface using GWT.create() method */ private HelloWorldMessages messages = GWT.create(HelloWorldMessages.class); public void onModuleLoad() { /*create UI */ final TextBox txtName = new TextBox(); txtName.setWidth(“200”); txtName.addKeyUpHandler(new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){ Window.alert(getGreeting(txtName.getValue())); } } }); Label lblName = new Label(messages.enterName() + “: “); Button buttonMessage = new Button(messages.clickMe() + “!”); buttonMessage.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert(getGreeting(txtName.getValue())); } }); HorizontalPanel hPanel = new HorizontalPanel(); hPanel.add(lblName); hPanel.add(txtName); VerticalPanel vPanel = new VerticalPanel(); vPanel.setSpacing(10); vPanel.add(hPanel); vPanel.add(buttonMessage); vPanel.setCellHorizontalAlignment(buttonMessage,
GWT – Layout Panels
GWT – Layout Panels ”; Previous Next Layout Panels can contain other widgets. These panels controls the way widgets to be shown on User Interface. Every Panel widget inherits properties from Panel class which in turn inherits properties from Widget class and which in turn inherits properties from UIObject class. Sr.No. Widget & Description 1 GWT UIObject Class This widget contains text, not interpreted as HTML using a <div>element, causing it to be displayed with block layout. 2 GWT Widget Class This widget can contain HTML text and displays the html content using a <div> element, causing it to be displayed with block layout. 3 GWT Panel Class This is an is the abstract base class for all panels, which are widgets that can contain other widgets. Layout Panels Following are few important Layout Panels − Sr.No. Widget & Description 1 FlowPanel This widget represents a panel that formats its child widgets using the default HTML layout behavior. 2 HorizontalPanel This widget represents a panel that lays all of its widgets out in a single horizontal column. 3 VerticalPanel This widget represents a panel that lays all of its widgets out in a single vertical column. 4 HorizontalSplitPanel This widget represents a panel that arranges two widgets in a single horizontal row and allows the user to interactively change the proportion of the width dedicated to each of the two widgets. Widgets contained within a HorizontalSplitPanel will be automatically decorated with scrollbars when necessary. 5 VerticalSplitPanel This widget represents a A panel that arranges two widgets in a single vertical column and allows the user to interactively change the proportion of the height dedicated to each of the two widgets. Widgets contained within a VertialSplitPanel will be automatically decorated with scrollbars when necessary. 6 FlexTable This widget represents a flexible table that creates cells on demand. It can be jagged (that is, each row can contain a different number of cells) and individual cells can be set to span multiple rows or columns. 7 Grid This widget represents a A rectangular grid that can contain text, html, or a child Widget within its cells. It must be resized explicitly to the desired number of rows and columns. 8 DeckPanel panel that displays all of its child widgets in a ”deck”, where only one can be visible at a time. It is used by TabPanel. 9 DockPanel This widget represents a panel that lays its child widgets out “docked” at its outer edges, and allows its last widget to take up the remaining space in its center. 10 HTMLPanel This widget represents a panel that contains HTML, and which can attach child widgets to identified elements within that HTML. 11 TabPanel This widget represents a panel that represents a tabbed set of pages, each of which contains another widget. Its child widgets are shown as the user selects the various tabs associated with them. The tabs can contain arbitrary HTML. 12 Composite This widget represents a type of widget that can wrap another widget, hiding the wrapped widget”s methods. When added to a panel, a composite behaves exactly as if the widget it wraps had been added. 13 SimplePanel This widget represents a Base class for panels that contain only one widget. 14 ScrollPanel This widget represents a simple panel that wraps its contents in a scrollable area 15 FocusPanel This widget represents a simple panel that makes its contents focusable, and adds the ability to catch mouse and keyboard events. 16 FormPanel This widget represents a panel that wraps its contents in an HTML <FORM> element. 17 PopupPanel This widget represents a panel that can pop up over other widgets. It overlays the browser”s client area (and any previously-created popups). 18 DialogBox This widget represents a form of popup that has a caption area at the top and can be dragged by the user. Unlike a PopupPanel, calls to PopupPanel.setWidth(String) and PopupPanel.setHeight(String) will set the width and height of the dialog box itself, even if a widget has not been added as yet. Print Page Previous Next Advertisements ”;
GWT – Overview
GWT – Overview ”; Previous Next What is GWT? Google Web Toolkit (GWT) is a development toolkit to create RICH Internet Applications (RIA). Here are some of its notable features − GWT provides developers option to write client side application in JAVA. GWT compiles the code written in JAVA to JavaScript code. Application written in GWT is cross-browser compliant. GWT automatically generates javascript code suitable for each browser. GWT is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache License version 2.0. Overall, GWT is a framework to build large scale and high performance web appliation while keeping them as easy-to-maintain. Why to use GWT? Being Java based, you can use JAVA IDEs like Eclipse to develop a GWT application. Developers can use code auto-complete/refactoring/navigation/project management and all features of IDEs.GWT which provides full debugging capability. Developers can debug the client side application just as a Java Application. GWT provides easy integration with Junit and Maven. Again being Java based, GWT has a low learning curve for Java Developers. GWT generates optimized javascript code, produces browser”s specific javascript code by self. GWT provides Widgets library provides most of tasks required in an application. GWT is extensible and custom widget can be created to cater to application needs. On top of everything, GWT applications can run on all major browsers and smart phones including Android and iOS based phones/tablets. Disadvantages of GWT Although GWT offers plenty of advantages, it suffers from the following disadvantages − Not Indexable − Web pages generated by GWT would not be indexed by search engines because these applications are generated dynamically. Not Degradable − If your application user disables Javascript then user will just see the basic page and nothing more. Not Designer”s Friendly − GWT is not suitable for web designers who prefer using plain HTML with placeholders for inserting dynamic content at later point in time. The GWT Components The GWT framework can be divided into following three major parts − GWT Java to JavaScript compiler − This is the most important part of GWT which makes it a powerful tool for building RIAs. The GWT compiler is used to translate all the application code written in Java into JavaScript. JRE Emulation library − Google Web Toolkit includes a library that emulates a subset of the Java runtime library. The list includes java.lang, java.lang.annotation, java.math, java.io, java.sql, java.util and java.util.logging GWT UI building library − This part of GWT consists of many subparts which includes the actual UI components, RPC support, History management, and much more. GWT also provides a GWT Hosted Web Browser which lets you run and execute your GWT applications in hosted mode, where your code runs as Java in the Java Virtual Machine without compiling to JavaScript. Print Page Previous Next Advertisements ”;
GWT – Applications
GWT – Applications ”; Previous Next Before we start with creating actual “HelloWorld” application using GWT, let us see what are the actual parts of a GWT application are − A GWT application consists of following four important parts out of which last part is optional but first three parts are mandatory. Module descriptors Public resources Client-side code Server-side code Sample locations of different parts of a typical gwt application HelloWord will be as shown below − Name Location Project root HelloWorld/ Module descriptor src/com/tutorialspoint/HelloWorld.gwt.xml Public resources src/com/tutorialspoint/war/ Client-side code src/com/tutorialspoint/client/ Server-side code src/com/tutorialspoint/server/ Module Descriptors A module descriptor is the configuration file in the form of XML which is used to configure a GWT application. A module descriptor file extension is *.gwt.xml, where * is the name of the application and this file should reside in the project”s root. Following will be a default module descriptor HelloWorld.gwt.xml for a HelloWorld application − <?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.tutorialspoint.client.HelloWorld”/> <!– specify the paths for translatable code –> <source path = ”…”/> <source path = ”…”/> <!– specify the paths for static files like html, css etc. –> <public path = ”…”/> <public path = ”…”/> <!– specify the paths for external javascript files –> <script src = “js-url” /> <script src = “js-url” /> <!– specify the paths for external style sheet files –> <stylesheet src = “css-url” /> <stylesheet src = “css-url” /> </module> Following is the brief detail about different parts used in module descriptor. Sr.No. Nodes & Description 1 <module rename-to = “helloworld”> This provides name of the application. 2 <inherits name = “logical-module-name” /> This adds other gwt module in application just like import does in java applications. Any number of modules can be inherited in this manner. 3 <entry-point class = “classname” /> This specifies the name of class which will start loading the GWT Application. Any number of entry-point classes can be added and they are called sequentially in the order in which they appear in the module file. So when the onModuleLoad() of your first entry point finishes, the next entry point is called immediately. 4 <source path = “path” /> This specifies the names of source folders which GWT compiler will search for source compilation. 5 <public path = “path” /> The public path is the place in your project where static resources referenced by your GWT module, such as CSS or images, are stored. The default public path is the public subdirectory underneath where the Module XML File is stored. 6 <script src=”js-url” /> Automatically injects the external JavaScript file located at the location specified by src. 7 <stylesheet src=”css-url” /> Automatically injects the external CSS file located at the location specified by src. Public Resources These are all files referenced by your GWT module, such as Host HTML page, CSS or images. The location of these resources can be configured using <public path = “path” /> element in module configuration file. By default, it is the public subdirectory underneath where the Module XML File is stored. When you compile your application into JavaScript, all the files that can be found on your public path are copied to the module”s output directory. The most important public resource is host page which is used to invoke actual GWT application. A typical HTML host page for an application might not include any visible HTML body content at all but it is always expected to include GWT application via a <script…/> tag as follows <html> <head> <title>Hello World</title> <link rel = “stylesheet” href = “HelloWorld.css”/> <script language = “javascript” src = “helloworld/helloworld.nocache.js”> </script> </head> <body> <h1>Hello World</h1> <p>Welcome to first GWT application</p> </body> </html> Following is the sample style sheet which we have included in our host page − 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; } Client-side Code This is the actual Java code written implementing the business logic of the application and that the GWT compiler translates into JavaScript, which will eventually run inside the browser. The location of these resources can be configured using <source path = “path” /> element in module configuration file. For example Entry Point code will be used as client side code and its location will be specified using <source path = “path” />. A module entry-point is any class that is assignable to EntryPoint and that can be constructed without parameters. When a module is loaded, every entry point class is instantiated and its EntryPoint.onModuleLoad() method gets called. A sample HelloWorld Entry Point class will be as follows − public class HelloWorld implements EntryPoint { public void onModuleLoad() { Window.alert(“Hello, World!”); } } Server-side Code This is the server side part of your application and its very much optional. If you are not doing any backend processing with-in your application then you do not need this part, but if there is some processing required at backend and your client-side application interact with the server then you will have to develop these components. Next chapter will make use of all the above mentioned concepts to create HelloWorld application using Eclipse IDE. Print Page Previous Next Advertisements ”;
GWT – Style with CSS
GWT – Style with CSS ”; Previous Next GWT widgets rely on cascading style sheets (CSS) for visual styling. By default, the class name for each component is gwt-<classname>. For example, the Button widget has a default style of gwt-Button and similar way TextBox widgest has a default style of gwt-TextBox. In order to give all buttons and text boxes a larger font, you could put the following rule in your application”s CSS file .gwt-Button { font-size: 150%; } .gwt-TextBox { font-size: 150%; } By default, neither the browser nor GWT creates default id attributes for widgets. You must explicitly create a unique id for the elements which you can use in CSS. In order to give a particular button with id my-button-id a larger font, you could put the following rule in your application”s CSS file − #my-button-id { font-size: 150%; } To set the id for a GWT widget, retrieve its DOM Element and then set the id attribute as follows − Button b = new Button(); DOM.setElementAttribute(b.getElement(), “id”, “my-button-id”) CSS Styling APIs There are many APIs available to hangle CSS setting for any GWT widget. Following are few important APIs which will help you in your day to day web programming using GWT − Sr.No. API & Description 1 public void setStyleName(java.lang.String style) This method will clear any existing styles and set the widget style to the new CSS class provided using style. 2 public void addStyleName(java.lang.String style) This method will add a secondary or dependent style name to the widget. A secondary style name is an additional style name that is,so if there were any previous style names applied they are kept. 3 public void removeStyleName(java.lang.String style) This method will remove given style from the widget and leaves any others associated with the widget. 4 public java.lang.String getStyleName() This method gets all of the object”s style names, as a space-separated list. 5 public void setStylePrimaryName(java.lang.String style) This method sets the object”s primary style name and updates all dependent style names. For example, let”s define two new styles which we will apply to a text − .gwt-Big-Text { font-size:150%; } .gwt-Small-Text { font-size:75%; } .gwt-Red-Text { color:red; } Now you can use setStyleName(Style) to change the default setting to new setting. After applying the below rule, a text”s font will become large txtWidget.setStyleName(“gwt-Big-Text”); We can apply a secondary CSS rule on the same widget to change its color as follows − txtWidget.addStyleName(“gwt-Red-Text”); Using above method you can add as many styles as you like to apply on a widget. If you remove first style from the button widget then second style will still remain with the text. txtWidget.removeStyleName(“gwt-Big-Text”); Primary & Secondary Styles By default, the primary style name of a widget will be the default style name for its widget class for example gwt-Button for Button widgets. When we add and remove style names using AddStyleName() method, those styles are called secondary styles. The final appearance of a widget is determined by the sum of all the secondary styles added to it, plus its primary style. You set the primary style of a widget with the setStylePrimaryName(String) method. To illustrate, let”s say we have a Label widget. In our CSS file, we have the following rules defined − .MyText { color: blue; } .BigText { font-size: large; } .LoudText { font-weight: bold; } Let”s suppose we want a particular label widget to always display blue text, and in some cases, use a larger, bold font for added emphasis. We could do something like this − // set up our primary style Label someText = new Label(); someText.setStylePrimaryName(“MyText”); … // later on, to really grab the user”s attention someText.addStyleName(“BigText”); someText.addStyleName(“LoudText”); … // after the crisis is over someText.removeStyleName(“BigText”); someText.removeStyleName(“LoudText”); Associating CSS Files There are multiple approaches for associating CSS files with your module. Modern GWT applications typically use a combination of CssResource and UiBinder. We are using only first approach in our examples. Using a <link> tag in the host HTML page. Using the <stylesheet> element in the module XML file. Using a CssResource contained within a ClientBundle. Using an inline <ui:style> element in a UiBinder template. GWT CSS Example This example will take you through simple steps to apply different CSS rules on your GWT widgest. Let us have working Eclipse IDE along with GWT plug in place and follow the following steps to create a GWT application − Step Description 1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT – Create Application chapter. 2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. 3 Compile and run the application to verify the result of the implemented logic. Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml. <?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.tutorialspoint.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-Button { font-size: 150%; font-weight: bold; width:100px; height:100px; } .gwt-Big-Text { font-size:150%; } .gwt-Small-Text { font-size:75%; } Following is the content of the modified HTML host file war/HelloWorld.html to accomodate two buttons. <html> <head> <title>Hello World</title> <link rel = “stylesheet” href = “HelloWorld.css”/> <script language = “javascript” src = “helloworld/helloworld.nocache.js”> </script> </head> <body> <div id = “mytext”><h1>Hello, World!</h1></div> <div id = “gwtGreenButton”></div> <div id = “gwtRedButton”></div> </body> </html> Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java which will take care of adding two buttons in HTML and will apply custom CSS style. package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.ui.Button;
GWT – Basic Widgets
GWT – Basic Widgets ”; Previous Next Every user interface considers the following three main aspects − UI elements − Thes are the core visual elements the user eventually sees and interacts with. GWT provides a huge list of widely used and common elements varying from basic to complex which we will cover in this tutorial. Layouts − They define how UI elements should be organized on the screen and provide a final look and feel to the GUI (Graphical User Interface). This part will be covered in Layout chapter. Behavior − These are events which occur when the user interacts with UI elements. This part will be covered in Event Handling chapter. GWT UI Elements The GWT library provides classes in a well-defined class hierarchy to create complex web-based user interfaces. All classes in this component hierarchy has been derived from the UIObject base class as shown below − Every Basic UI widget inherits properties from Widget class which in turn inherits properties from UIObject. Tree and Menu will be covered in complex widgets tutorial. Sr.No. Widget & Description 1 GWT UIObject Class This widget contains text, not interpreted as HTML using a <div>element, causing it to be displayed with block layout. 2 GWT Widget Class This widget can contain HTML text and displays the html content using a <div> element, causing it to be displayed with block layout. Basic Widgets Following are few important Basic Widgets − Sr.No. Widget & Description 1 Label This widget contains text, not interpreted as HTML using a <div>element, causing it to be displayed with block layout. 2 HTML This widget can contain HTML text and displays the html content using a <div> element, causing it to be displayed with block layout. 3 Image This widget displays an image at a given URL. 4 Anchor This widget represents a simple <a> element. Print Page Previous Next Advertisements ”;
GWT – Deploy Application
GWT – Deploy Application ”; Previous Next This tutorial will explain you how to create an application “war” file and how to deploy that in Apache Tomcat Websever root. If you understood this simple example then you will also be able to deploy a complex GWT application following the same steps. Let us have working Eclipse IDE along with GWT plug in place and follow the following steps to create a GWT application − Step Description 1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT – Create Application chapter. 2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. 3 Compile and run the application to make sure business logic is working as per the requirements. 4 Finally, zip the content of the war folder of the application in the form of war file and deploy it in Apache Tomcat Webserver. 5 Launch your web application using appropriate URL as explained below in the last step. Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml. <?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.tutorialspoint.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>Hello World</h1> <div id = “gwtContainer”></div> </body> </html> I modified HTML a little bit from previous example. Here I created a placeholder <div>…</div> where we will insert some content using our entry point java class. So let us have following content of Java file src/com.tutorialspoint/HelloWorld.java. package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { HTML html = new HTML(“<p>Welcome to GWT application</p>”); RootPanel.get(“gwtContainer”).add(html); } } Here we created on basic widgest HTML and added it inside the div tag having id=”gwtContainer”. We will study different GWT widgets in coming chapters. 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 following result − Create WAR File Now our applictaion is working fine and we are ready to export it as a war file. Follow the following steps − Go into your project”s war directory C:workspaceHelloWorldwar Select all the files & folders available inside war directory. Zip all the selected files & folders in a file called HelloWorld.zip. Rename HelloWorld.zip to HelloWorld.war. Deploy WAR file Stop the tomcat server. Copy the HelloWorld.war file to tomcat installation directory > webapps folder. Start the tomcat server. Look inside webapps directory, there should be a folder helloworld got created. Now HelloWorld.war is successfully deployed in Tomcat Webserver root. Run Application Enter a url in web browser: http://localhost:8080/HelloWorld to launch the application Server name (localhost) and port (8080) may vary as per your tomcat configuration. Print Page Previous Next Advertisements ”;