10.07.2015 Views

Tutorial section in PDF - Custom Training Courses - Coreservlets.com

Tutorial section in PDF - Custom Training Courses - Coreservlets.com

Tutorial section in PDF - Custom Training Courses - Coreservlets.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>The Google Web Toolkit (GWT):Declarative Layout with UiB<strong>in</strong>der –Advanced Topics(GWT 2.5 Version)Orig<strong>in</strong>als of Slides and Source Code for Examples:http://courses.coreservlets.<strong>com</strong>/Course-Materials/gwt.html<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>For live Ajax & GWT tra<strong>in</strong><strong>in</strong>g, see tra<strong>in</strong><strong>in</strong>gcourses at http://courses.coreservlets.<strong>com</strong>/.Taught by the author of Core Servlets and JSP,More Servlets and JSP, and this tutorial. Available atpublic venues, or customized versions can be heldon-site at your organization.• <strong>Courses</strong> developed and taught by Marty Hall– JSF 2, PrimeFaces, servlets/JSP, Ajax, jQuery, Android development, Java 7 or 8 programm<strong>in</strong>g, custom mix of topics– <strong>Courses</strong> <strong>Custom</strong>ized available <strong>in</strong> any state Java or country. EE Tra<strong>in</strong><strong>in</strong>g: Maryland/DC http://courses.coreservlets.<strong>com</strong>/area <strong>com</strong>panies can also choose afternoon/even<strong>in</strong>g courses.GWT, • <strong>Courses</strong> Java 7 developed and 8, JSF and 2, PrimeFaces, taught by coreservlets.<strong>com</strong> HTML5, Servlets, JSP, experts Ajax, (edited jQuery, by Spr<strong>in</strong>g, Marty) Hibernate, REST, Hadoop, Android.– Spr<strong>in</strong>g, Hibernate/JPA, GWT, Hadoop, HTML5, RESTful Web ServicesDeveloped and taught by well-known Contact author hall@coreservlets.<strong>com</strong> and developer. At public for details venues or onsite at your location.


Topics <strong>in</strong> This Section5• Quick review of UiB<strong>in</strong>der steps– HTML-based and Widget-based• Us<strong>in</strong>g widget attributes <strong>in</strong> UiB<strong>in</strong>der XML• Three ways to deal with lack of widget zeroargconstructor <strong>in</strong> UiB<strong>in</strong>der XML– Us<strong>in</strong>g @UiConstructor– Us<strong>in</strong>g @UiFactory– Us<strong>in</strong>g @UiField(provided=true)• Layout panels with special UiB<strong>in</strong>der XMLsyntax• Work<strong>in</strong>g with CSS <strong>in</strong> UiB<strong>in</strong>der• Work<strong>in</strong>g with images <strong>in</strong> UiB<strong>in</strong>der© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Quick Review ofUiB<strong>in</strong>der Steps<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.


HTML-Based(Eclipse Wizard Builds #1 & #2)1. Create XML file– Top-level element is an HTML tag like – Put raw HTML <strong>in</strong>side– Use ui:field to mark places for later <strong>in</strong>sertion2. Create Java class that represents the XML– Extend UiObject– Use UiB<strong>in</strong>der <strong>in</strong>terface and GWT.create to b<strong>in</strong>d the Java class to theXML representation– Mark fields with @UiField (one for each ui:field <strong>in</strong> XML)– Call setElement– Insert content <strong>in</strong>to those fields3. Use new Java class <strong>in</strong> EntryPo<strong>in</strong>t class– Use DOM API with getElement and appendChild7Widget-Based(Eclipse Wizard Builds #1 & #2)81. Create XML file– Top-level element represents a Widget: – Put mixture of raw HTML and <strong>in</strong>side– Use ui:field to mark places for later <strong>in</strong>sertion2. Create Java class that represents the XML– Extend Composite– Use UiB<strong>in</strong>der <strong>in</strong>terface and GWT.create to b<strong>in</strong>d the Java class to theXML representation– Mark fields with @UiField (one for each ui:field <strong>in</strong> XML)– Call <strong>in</strong>itWidget– Set properties of those fields3. Use new Java class <strong>in</strong> EntryPo<strong>in</strong>t class– Use normal Widget methods• E.g., RootPanel.get(…).add(yourWidget)


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Us<strong>in</strong>g Widget Attributes<strong>in</strong> UiB<strong>in</strong>der<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.Summary• Idea– Most widgets have setters that alter their look or behavior• Previously: called <strong>in</strong> Java– After fields were bound, we called widget setters <strong>in</strong> Javacode• Reduces readability of UI XML template• New alternative: declared <strong>in</strong> XML– Use XML attributes that correspond to setter methods• Follows JavaBean notation• Example– 10


Example: Improved States/Cities ListBoxImprovedUi.ui.xmlUser ControlResultState:City:11Example: Improved States/Cities ListBoxImprovedUi.java...public class ImprovedUi extends Composite {private static ImprovedUiUiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(ImprovedUiUiB<strong>in</strong>der.class);<strong>in</strong>terface ImprovedUiUiB<strong>in</strong>derextends UiB<strong>in</strong>der {}@UiField ListBox stateList;@UiField ListBox cityList;S<strong>in</strong>ce all the visual setup is done <strong>in</strong> UI XMLtemplate, no longer a need for setupListBoxmethod. All we need is to do is populate bothListBoxes.public ImprovedUi() {<strong>in</strong>itWidget(uiB<strong>in</strong>der.createAndB<strong>in</strong>dUi(this));populateStateList();cityList.addItem("Select City");}...12


Example: Improved States/Cities ListBoxGwtUiB<strong>in</strong>der1.html & EntryPo<strong>in</strong>t Class...Improved HelloWidgetWorld and ListBox with attributes...public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...13}}ImprovedUi improvedUi = new ImprovedUi();RootPanel.get("improved-ui").add(improvedUi);...Example: Improved States/Cities ListBox:Result14


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Giv<strong>in</strong>g WidgetConstructor Arguments<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.Motivation• Widgets declared <strong>in</strong> UI XML template getcreated by a call to GWT.create()– In most cases, this means the widget must have a default,(zero-arg) constructor• Some widgets might only have non-defaultconstructors available– Widget designer may <strong>in</strong>sist that client-provided state bespecified via constructor arguments– You don’t have control over third-party, so you can’tprovide a default constructor16


Three Ways To Deal with Lack ofWidget’s Zero-Arg Constructor17• Use @UiContructor <strong>in</strong>side the widget– Requires least amount of cod<strong>in</strong>g– UI XML code be<strong>com</strong>es very clear– Downside: if 3 rd party, must unwrap/code/rewrap JAR• Use @UiFactory <strong>in</strong>side the UiB<strong>in</strong>der owner widget– Good choice if us<strong>in</strong>g a 3 rd party widget library– No need to change the orig<strong>in</strong>al source code– UI XML is still very clear– Allows for <strong>in</strong>itialization with <strong>com</strong>plex objects (not as attributes)• Use @UiField(provided=true) <strong>in</strong>side the UiB<strong>in</strong>derowner widget– Same as @UiFactory, but UI XML is not as clear as all<strong>in</strong>itialization happens <strong>in</strong> Java – no constructor attributes– Mostly used for shar<strong>in</strong>g widget <strong>in</strong>stances between views@UiConstructor• Place @UiConstructor <strong>in</strong> front of widgetconstructor– E.g., @UiConstructor public MyWidget(Str<strong>in</strong>g name) {…}• Import widget library package as usual• Inside .ui.xml, use constructor arg names asattributes– Names must match exactly (unlike with setters)– E.g., …(Str<strong>in</strong>g name){…} would be • Example preparation:– Copied OptionalTextBox.java from Gwt<strong>Custom</strong>Widgets1project <strong>in</strong>to GwtUiB<strong>in</strong>der1 project18


Example: OptionalTextBox.java19package coreservlets.client.widget;...public class OptionalTextBox extends Composite {private TextBox textBox = new TextBox();private CheckBox checkBox = new CheckBox();private boolean checkedEnablesText;...@UiConstructorpublic OptionalTextBox(Str<strong>in</strong>g caption,}UI XML must use caption, checkedEnablesText,and isChecked attributes.boolean checkedEnablesText,boolean isChecked) {this.checkedEnablesText = checkedEnablesText;FlowPanel panel = new FlowPanel();<strong>in</strong>itWidget(panel);setStyleName("core-opt-text-widget");textBox.setStyleName("core-opt-textbox");checkBox.setStyleName("core-opt-checkbox");...Example: ContructorArg1.ui.xmlUs<strong>in</strong>g @UiConstructor:...20


Example: GwtUiB<strong>in</strong>der1.html andEntry Class...Optional Textboxes with Non-defaultConstructors...21public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...ConstructorArg1 constArg1 = new ConstructorArg1();RootPanel.get("optional-textbox-constructor").add(constArg1);...}}Example: Result22


@UiFactory• Create non-private method with widget type asthe return type• Place @UiFactory <strong>in</strong> front of that method– E.g., @UiFactory MyWidget make(Str<strong>in</strong>g name){return new MyWidget(…);}– The method’s argument names be<strong>com</strong>e attributes to the widget<strong>in</strong>side the UI XML as if they were part of a constructor• Import widget library package as usual• Inside .ui.xml, use widget as usual– Can still use attributes with setters besides the constructor args• Usually, this approach works well when us<strong>in</strong>g a3 rd party widget library23Example Preparation• Use FancyOptionalTextBox fromGwt<strong>Custom</strong>Widgets2 project– Exported Gwt<strong>Custom</strong>Widgets2 Java JAR <strong>in</strong>to theGwtUiB<strong>in</strong>der1/war/WEB-INF/lib directory• S<strong>in</strong>ce this JAR has no server-side <strong>com</strong>ponents, it can really beplaced anywhere outside the war directory so it doesn’t getdeployed with the web app but only <strong>com</strong>piled <strong>in</strong>to JavaScript– Adjusted GwtUiB<strong>in</strong>der1 project properties (Libraries) to<strong>in</strong>clude the gwtcustomwidgets2.jar• Inserted this l<strong>in</strong>e <strong>in</strong>to GwtUiB<strong>in</strong>der1.gwt.xml:24


Example: ConstructorArg1.javapackage coreservlets.client.ui;...public class ConstructorArg1 extends Composite {private static ConstructorArg1UiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(ConstructorArg1UiB<strong>in</strong>der.class);<strong>in</strong>terface ConstructorArg1UiB<strong>in</strong>derextends UiB<strong>in</strong>der {}}...@UiFactoryFancyOptionalTextBox makeFancyTextBox(Str<strong>in</strong>g caption,}These names be<strong>com</strong>e attributes <strong>in</strong> XML UI file.boolean checkedEnablesText,boolean isChecked) {return new FancyOptionalTextBox(caption,checkedEnablesText,isChecked);25Example: ContructorArg1.ui.xml26...Us<strong>in</strong>g @UiFactory:...Attributes correspond<strong>in</strong>g to setters can still beused as usual. This one corresponds tosetClearTextOnDisable(…).


Example: GwtUiB<strong>in</strong>der1.html andEntry Class...Optional Textboxes with Non-defaultConstructors...Same as before s<strong>in</strong>ce we areus<strong>in</strong>g the same UI XML file.27public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...ConstructorArg1 constArg1 = new ConstructorArg1();RootPanel.get("optional-textbox-constructor").add(constArg1);...}}Example: Result28


@UiField(provided=true)29• Inside .ui.xml, import widget library package• Inside .ui.xml, use widget as usual without anyconstructor correspond<strong>in</strong>g attributes– Make sure to set the widget’s ui:field=“…" attribute• In correspond<strong>in</strong>g Java class, declare field with@UiField(provided=true)– This tells GWT not to call GWT.create(), up to you to wire it– Field must be <strong>in</strong>stantiated before call<strong>in</strong>g <strong>in</strong>itWidget(…)• Usually, this approach is used with some factorythat produces s<strong>in</strong>gleton widget– Only one <strong>in</strong>stance of it exists– This allows the entire UI to share the same <strong>in</strong>stance of the widget• Disadvantage– UI XML cannot use attributes correspond<strong>in</strong>g to constructor argsExample Preparation• Use FancyOptionalTextBox fromGwt<strong>Custom</strong>Widgets2 project• Exported Gwt<strong>Custom</strong>Widgets2 Java JAR <strong>in</strong>tothe GwtUiB<strong>in</strong>der1/war/WEB-INF/lib directory– S<strong>in</strong>ce this JAR has no server-side <strong>com</strong>ponents, it can really beplaced anywhere outside the war directory so it doesn’t getdeployed with the web app but only <strong>com</strong>piled <strong>in</strong>to JavaScript– Adjusted GwtUiB<strong>in</strong>der1 project properties (Libraries) to<strong>in</strong>clude the gwtcustomwidgets2.jar• Inserted this l<strong>in</strong>e <strong>in</strong>to GwtUiB<strong>in</strong>der1.gwt.xml:30


Example: ConstructorArg1.java...public class ConstructorArg1 extends Composite {private static ConstructorArg1UiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(ConstructorArg1UiB<strong>in</strong>der.class);<strong>in</strong>terface ConstructorArg1UiB<strong>in</strong>derextends UiB<strong>in</strong>der {}@UiField(provided = true)FancyOptionalTextBox shared;Important! This must be <strong>in</strong>stantiated beforecall<strong>in</strong>g <strong>in</strong>itWidget(…).public ConstructorArg1() {// You would usually retrieve this from a factoryshared = new FancyOptionalTextBox("Shar<strong>in</strong>g is car<strong>in</strong>g!",true, false);<strong>in</strong>itWidget(uiB<strong>in</strong>der.createAndB<strong>in</strong>dUi(this));}...31Example: ContructorArg1.ui.xml...Us<strong>in</strong>g@UiField(provided=&quot;true&quot;):...Attributes correspond<strong>in</strong>g to setters can still beused as usual. This one corresponds tosetClearTextOnDisable(…).32


Example: GwtUiB<strong>in</strong>der1.html andEntry Class...Optional Textboxes with Non-defaultConstructors...Same as before s<strong>in</strong>ce we areus<strong>in</strong>g the same UI XML file.33public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...ConstructorArg1 constArg1 = new ConstructorArg1();RootPanel.get("optional-textbox-constructor").add(constArg1);...}}Example: Result34


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Special UiB<strong>in</strong>der Syntaxfor Panels35<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.Overview• Idea– Many Panel types have subelements that require specialsyntax• Most <strong>com</strong>mon issues– Panels have sub-elements that cannot be bound to Javacode– Panels may or may not allow raw HTML as body contentof subelements36


Example• Syntax for DockLayoutPanelTopBodySidebarUnlike Label, HTML, etc., north, south, center,west, east do NOT represent runtime objects.They can’t be bound.(Be<strong>in</strong>g all lower case serves as a h<strong>in</strong>t)Not all widgets know what to do with raw HTML.Only those that implement HasHTML know howto handle HTML.This is the reason we need to wrap our ‘west’sidebar <strong>in</strong> an HTML widget that knows how todeal with raw HTML.37© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Work<strong>in</strong>g with CSS <strong>in</strong>UiB<strong>in</strong>der<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.


Associat<strong>in</strong>g CSS Files: Choices39• Host page– Have to make sure GWT doesn’t override your styles– Forces the browser to make another request• More on that when we discuss image load<strong>in</strong>g– Docs say this is deprecated <strong>in</strong> GWT!• GWT Module XML file with – Old way, and GWT says it’s now deprecated• Us<strong>in</strong>g CssResource <strong>in</strong>side ClientBundle– Very efficient (more on that later)– Compile-time verification!– No more global CSS scope – goodbye CSS nam<strong>in</strong>g collisions!• Us<strong>in</strong>g <strong>in</strong>l<strong>in</strong>e <strong>in</strong> UiB<strong>in</strong>der template– Automatically creates a ClientBundle with CssResourceExample: InStyle1.ui.xml40.table {border: 2px blue solid;}.table tr+tr+tr {background: p<strong>in</strong>k;}.randomNumber {font-style: italic;width: 250px;}.randomNumber {background-color: green;padd<strong>in</strong>g: 0px 4px 0px 4px;text-align: center;color: red;...}All tags must be direct children of.Both can be used with<strong>in</strong> this page. The f<strong>in</strong>alname of the CSS class will be obfuscated tomake sure these don’t collide.Prefix (i.e., namespace) for all stylesconta<strong>in</strong>ed <strong>in</strong> this style block.If ‘field’ is omitted, the prefix defaults to‘style’. There can only be one default styleblock as above.


Example: InStyle1.ui.xml (cont)User ControlResultShow Random NumberNum will go hereN/AHurts your eyes,doesn't it?CSS classes can be applied to regular HTMLelements just like a regular CSS class.Multiple classes are separated by space.Adds another class to the exist<strong>in</strong>g CSSclasses assigned to this widget. Note theplural addStyleNames.41Example: InStyle1.java...public class InStyle1 extends Composite {private static InStyle1UiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(InStyle1UiB<strong>in</strong>der.class);<strong>in</strong>terface InStyle1UiB<strong>in</strong>derextends UiB<strong>in</strong>der {}@UiField Button numberButton;@UiField HTML numberResult;public InStyle1() {<strong>in</strong>itWidget(uiB<strong>in</strong>der.createAndB<strong>in</strong>dUi(this));}42}@UiHandler("numberButton")void displayRandomNumber(ClickEvent event) {numberResult.setText("Number: " + Math.random() * 10);}


Example: GwtUiB<strong>in</strong>der.html andEntry Class...Basic CSS with UiB<strong>in</strong>der...public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...InStyle1 <strong>in</strong>Style1 = new InStyle1();RootPanel.get("<strong>in</strong>-style1").add(<strong>in</strong>Style1);...}}43Example: Result44


Example: InStyle2.ui.xml45.moveRight {Specifies a relative path to the CSS file.padd<strong>in</strong>g-left: 100px;}.moveLeft {padd<strong>in</strong>g-left: 2px;This gives us the ability to dynamicallyassign <strong>in</strong>l<strong>in</strong>e styles to widgets <strong>in</strong> the}template.User ControlResultDifferent=GoodNot always!Specifies that there will be a bound field<strong>in</strong>side InStyle2.java of type MyStyle that canaccess the classes def<strong>in</strong>ed <strong>in</strong> this style block.Example: InStyle2.java...public class InStyle2 extends Composite {private static InStyle2UiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(InStyle2UiB<strong>in</strong>der.class);<strong>in</strong>terface InStyle2UiB<strong>in</strong>derextends UiB<strong>in</strong>der {}<strong>in</strong>terface MyStyle extends CssResource {Str<strong>in</strong>g moveRight();Str<strong>in</strong>g moveLeft();}@UiFieldMyStyle dynamicStyle;Must extend CssResource <strong>in</strong>terface.Method names must match the names of theclasses <strong>in</strong>side the UI XML style block.There is no requirement that all CSS classeswith<strong>in</strong> the style block be mapped here....46


Example: InStyle2.javapublic InStyle2() {<strong>in</strong>itWidget(uiB<strong>in</strong>der.createAndB<strong>in</strong>dUi(this));}47}@UiHandler("diffButton")void moveNotAlways(ClickEvent event) {if (movedRight) {notAlways.removeStyleName(dynamicStyle.moveRight());notAlways.addStyleName(dynamicStyle.moveLeft());movedRight = false;} else {notAlways.removeStyleName(dynamicStyle.moveLeft());notAlways.addStyleName(dynamicStyle.moveRight());movedRight = true;}}Example: res/mystyles.css.different {width: 100px;height: 30px;border: 1px outset red;background: papayawhip;}That’s a real/valid color! Who knew?48


Example: GwtUiB<strong>in</strong>der.html andEntry Class...Dynamic and External CSS with UiB<strong>in</strong>der...public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...InStyle2 <strong>in</strong>Style2 = new InStyle2();RootPanel.get("<strong>in</strong>-style2").add(<strong>in</strong>Style2);...}}49Example: Result50


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Work<strong>in</strong>g with Images <strong>in</strong>UiB<strong>in</strong>der<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.Problem• Large overhead– Web apps mostly use small images for icons– HTTP request for each image – most traffic is overhead• Useless if-modified-s<strong>in</strong>ce checks– Even cached image needs to be checked if updated• Browser typically gets a 304 “Not Modified”– Images rarely change, so this is a waste• Limited HTTP connections– HTTP 1.1 requires browser to use no more than 2connections per doma<strong>in</strong>/port– Large number of small images tie up the connections52


Big Idea53• ClientBundle solves the problem– Intelligently stiches multiple images <strong>in</strong>to one <strong>com</strong>posite– Only 1 round trip to the server• Filename of the <strong>com</strong>posite is a hash of file’scontents– Browser can safely cache it permanently• If anyth<strong>in</strong>g changes, the file name will change as well• No more standard size place holders forimages– Avoids “bouncy page” affect as the size of the image isdiscovered when bundle is created – size can be explicitly setwhen images from bundles are used• Bundles can conta<strong>in</strong> other types of resources– CSS, text, <strong>PDF</strong>, etc.Example: Resources.javapackage coreservlets.client.ui.res;Forces GWT to create concrete ClientBundle....public <strong>in</strong>terface Resources extends ClientBundle {public static f<strong>in</strong>al Resources INSTANCE =GWT.create(Resources.class);Relative path to the location of this <strong>in</strong>terface.@Source("red-ajax-loader.gif")ImageResource redLoader();@Source("gwtDuke.jpg")ImageResource gwtDuke();@Source("duke.gif")ImageResource duke();@Source("resStyles.css")Style style();Includ<strong>in</strong>g a CSS file <strong>in</strong> the bundle. The style()method is the accessor for the CSS classwired methods <strong>in</strong> the Style <strong>in</strong>terface.54public <strong>in</strong>terface Style extends CssResource {Str<strong>in</strong>g different();Str<strong>in</strong>g loader();Str<strong>in</strong>g gwtDuke();Str<strong>in</strong>g duke();}...


Example: resStyles.css.different {width: 180px;height: 30px;border: 1px outset red;background: papayawhip;}@sprite .loader {gwt-image: "redLoader";}@sprite .gwtDuke {gwt-image: "gwtDuke";}@sprite .duke {gwt-image: "duke";}Tells GWT to pull out a “clip” out of the<strong>com</strong>posite image which is referenced“redLoader”. The gwt-image is a special GWTproperty for this purpose.The value of gwt-image must match the nameof the method <strong>in</strong> Resources.55Example: InStyle3.ui.xml56User ControlResultResource Styled!GWT DukeRegular Duke


Example: InStyle3.java...public class InStyle3 extends Composite {private static InStyle3UiB<strong>in</strong>der uiB<strong>in</strong>der =GWT.create(InStyle3UiB<strong>in</strong>der.class);<strong>in</strong>terface InStyle3UiB<strong>in</strong>derextends UiB<strong>in</strong>der {}}public InStyle3() {<strong>in</strong>itWidget(uiB<strong>in</strong>der.createAndB<strong>in</strong>dUi(this));Resources.INSTANCE.style().ensureInjected();}Important! Often forgotten!Make sure CSS resources get <strong>in</strong>jected <strong>in</strong>tothe DOM of the browser.Call<strong>in</strong>g this method multiple times has noeffect, so it’s OK to call it from multipletemplates.57Example: GwtUiB<strong>in</strong>der.html andEntry Class...External ClientBundle with Sprited Images...public class GwtUiB<strong>in</strong>der1 implements EntryPo<strong>in</strong>t {public void onModuleLoad() {...InStyle3 <strong>in</strong>Style3 = new InStyle3();RootPanel.get("<strong>in</strong>-style3").add(<strong>in</strong>Style3);...}}58


Example: Result59Example: Result (Firebug)60


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Wrap-Up<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.Summary62• Use XML attributes that correspond towidget’s setter methods (like a JavaBean)– Can pass primitives (or wrappers) or Str<strong>in</strong>gs• Use @UiConstructor if possible– If you wrote the Widget, you can use @UiConstructor <strong>in</strong>Widget code. Then constructor can be used from XML.• Look at JavaDocs before us<strong>in</strong>g a panel– To see if special syntax is required for UI XML• Use CSS with – Use its ‘src’ attribute to po<strong>in</strong>t to a central CSS file• Use ClientBundle approach– To group images and thus improve HTTP performance


© 2013 Marty Hall & Yaakov Chaik<strong>in</strong>Questions?JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spr<strong>in</strong>g, Hibernate, Servlets, JSP, GWT, and other Java EE tra<strong>in</strong><strong>in</strong>g.<strong>Custom</strong>ized Java EE Tra<strong>in</strong><strong>in</strong>g: http://courses.coreservlets.<strong>com</strong>/GWT, Java 7 and 8, JSF 2, PrimeFaces, HTML5, Servlets, JSP, Ajax, jQuery, Spr<strong>in</strong>g, Hibernate, REST, Hadoop, Android.Developed and taught by well-known author and developer. At public venues or onsite at your location.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!