Wednesday, March 3, 2010

Nip/Tuck (not the show)

I call this part Nip/Tuck (not the show) because now we are going to make this thing look good. It will be in two parts.

We are going to add some javascript to make it look good and use a CSS framework. When I say look good is we are going to add effects that so popular with Web2.0 websites like slide, fade and curl. For this to happen we are going to use JQuery (and probably JQueryUI) and maybe 960.gs for our CSS framework.

I know that Tapestry ships with Prototype but *whine on* I don't like prototype *whine off*. I know I might get flamed for this but guess what, my project, my stuff, go away.

Let's start with JQuery....

Now, it absolutely makes no sense in recreating the wheel on how to add JQuery into our Tapestry project. With that, we are going use somebody's else's wheel: ioko + maven.

Ioko-tapestry-commons' project, a series of GPL licensed components to assist in building tapestry websites and the easiest way to use them is to add them is by using maven (or other build systems) dependencies. Remember, we are only interested in the JQuery lib. So, power up your Netbeans6.8 and open up your pom.xml and we simply add ioko as a project dependency. Refer to the ioko-tapestry-commons website if you get lost, somehow.

Just look for the dependencies section of the pom.xml and add these 5 line. That's right five lines!

 <dependency>  
   <groupId>uk.co.ioko</groupId>  
   <artifactId>tapestry-jquery<artifactId>  
   <version>1.5.0-jquery-1.3.2</version>  
  </dependency>  

The module builds on the tapestry-javascript stack support and automatically includes jQuery in your pages. It automatically calls jQuery.noConflict() so be aware that you will need to write any jQuery code using the 'jQuery' or map it to another short hand form to avoid conflicts with Scriptaculous.

Run with it and have fun with JQuery.

Sunday, January 24, 2010

Getting that layout just right

Getting that layout just right or more like changing the layout into something else I like. That's right, changing the look of our web application. Now, since this is tutorial - we are not going to reinvent the wheel. So, no wires, drafts, sketches or whatever remotely artistic. We go the cowards way and look for a cop out. So drop that pad and pen (or pencil) and open you're browser and point it to freecsstemplates.org or oswd.org. We are going to pick one out and use it. so browse, pick and download a design.


For me I am using a fixed-width design with two-columns from freecsstemplates.org. Its called Republic.

Now, you should be able to read html and some css for this part. When you have figured out how the republic template works, you know its time to use it and replace ours.

So, we open up Tapestry's Layout component. Almost everything in Tapestry is a component. The Layout component comes in two parts - the Layout.tml (just like you see in the pic) and the Layout.java (which is the java class). Layout.java (if haven't figured it out) is in the source packages.

Now we are going to do a simple find and replace for this part, but not everything. For example:

 <div class="post">  
   <div class="title">  
      <h2>${title}</h2>  
   </div>  
    <div class="entry">  
      <t:body/>  
    </div>  
 </div>  

If you see tags like a ${title} and the <t:body/>, you should be aware that these are page variables that Tapestry will replace. So you will have to work around them to get the look you want. It should be fairly clear that anything with a "$" or "t:" prefix is connected to something in our Tapestry application.

That should be easy.

After recoding the html, now you need to replace the css. Now within the Web pages there should be a Layout.css file in a (duh) layout folder. Open that and then open (use notepad) layout.css of the republic template. Copy and paste over. Don't forget about the image folder. And BAM! you got a new look.

BTW, you might need to clean the cache of your web browser and/or application server (assuming that you're using something else other than jetty *big grin*)



This is how it should look now after a few minutes of playing around with the HTML and CSS. Don't worry if you don't like it - you could always change how the thing looks quite easily.

Remember, if you want to get your grubby hands on the code you need to learn how to use SVN. Again, this tutorial project is hosted by kenai.com at this URL: http://kenai.com/projects/addressbook-tap.

Hoots!


Sunday, November 22, 2009

Automatic Housing: Tapestry's Scaffolding Components

Tapestry5, if you haven't caught on, is a component-based web application framework. This means that most of its workings is based on discreet components. And the beauty of it is that we can quickly create a front-facing webpage (ala user interface) using Tapestry's "scaffolding" components. We will be using:
  • BeanEditForm - Generates a simple UI for editing the properties of a JavaBean, with the flavor of UI for each property (text field, checkbox, drop down list) determined from the property type (or by other means, such as an annotation), and the order and validation for the properties determined from annotations on the property's getter and setter methods.
  • Grid - A grid presents tabular data. It is a composite component, created in terms of several sub-components. The sub-components are statically wired to the Grid, as it provides access to the data and other models that they need. A Grid may operate inside a form.
We start with rewriting the code in four places; index(tml and java) and another(tml and java). Let's start with the index. Within the index.java source, delete all the code except for anything that has to do with the current time. Do the same with the index.tml source.
 package edu.addressbook.pages;  
 import java.util.Date;  
 import org.apache.tapestry5.annotations.InjectPage;  
 /**  
  * Start page of application AddressBook.  
  */  
 public class Index {  
   public Date getCurrentTime() {  
     return new Date();  
   }  
 }  
After all of that, we add our first scaffolding component which is the grid. The index.tml file should look like this:
 <html t:type="layout" title="AddressBook Index"  
    t:sidebarTitle="Current Time"  
    xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"  
    xmlns:p="tapestry:parameter">  
     <!-- Most of the page content, including <head>, <body>, etc. tags, comes from Layout.tml -->  
   <p>${message:greeting}</p>  
   <h1>List of Contacts</h1>  
   <t:grid source="addresses" row="address">  
   </t:grid>  
   <p>  
     <t:pagelink page="another">Add Contact</t:pagelink>  
   </p>  
   <p:sidebar>  
     <p>  
       Just to prove this is live:  
     </p>  
     <p>The current time is: ${currentTime}.</p>  
     <p>  
       [<t:pagelink page="Index">refresh</t:pagelink>]  
     </p>  
   </p:sidebar>  
 </html>  
And the index.java will contain this:
 package edu.addressbook.pages;  
 import edu.addressbook.entities.Address;  
 import edu.addressbook.entities.dao.AddressDAO;  
 import java.util.Date;  
 import java.util.List;  
 import org.apache.tapestry5.ioc.annotations.Inject;  
 /**  
  * Start page of application AddressBook.  
  */  
 public class Index {  
   @Inject  
   private AddressDAO addressDAO;  
   private Address address;  
   public Address getAddress() {  
     return address;  
   }  
   public void setAddress(Address address) {  
     this.address = address;  
   }  
   public List<Address> getAddresses(){  
     return addressDAO.retrieveAll();  
   }  
   public Date getCurrentTime() {  
     return new Date();  
   }  
 }  
When you reload the browser, it should go off without a hitch. Don't worry yet, it shouldn't display anything since there is no data to display. Remember, your database table is empty. We will deal with that. You will notice that there is an "Add Contact" pagelink within the index.tml file. We will use that. Edit the another.tml file to look like this.
 <html t:type="layout" title="Another"  
    t:sidebarTitle="Current Time"  
    xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"  
    xmlns:p="tapestry:parameter">  
     <!-- Most of the page content, including <head>, <body>, etc. tags, comes from Layout.tml -->  
   <h1>Create New Address</h1>  
   <t:BeanEditForm t:id="address" />  
   <p:sidebar>  
   </p:sidebar>  
 </html>  
You'll notice that we now are using the BeanEditForm component bind to the address object. Here's the another.java source.
 /*  
  * To change this template, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 package edu.addressbook.pages;  
 import edu.addressbook.entities.Address;  
 import edu.addressbook.entities.dao.AddressDAO;  
 import org.apache.tapestry5.ioc.annotations.Inject;  
 /**  
  *  
  * @author killertilapia  
  */  
 public class Another {  
   private Address address;  
   @Inject  
   private AddressDAO addressDAO;  
   public Address getAddress() {  
     return address;  
   }  
   public void setAddress(Address address) {  
     this.address = address;  
   }  
   Object onSuccess(){  
     addressDAO.add(address);  
     return Index.class;  
   }  
 }  
You should be quick to notice that the create/update button is linked to the onSuccess() method. When the insert is successful it should return to the index page and show the newly added record. Here is a screenshot:


And you should have also noticed that the grid component of ours spills out of the page. It just has too many fields to display. You can exclude that to look like this:
 <h1>List of Contacts</h1>  
   <t:grid source="addresses" row="address" exclude="address1,address2, middlename,landphone">  
   </t:grid>  
The should fix it.

Get the latest code from the repository. And remember happy weaving.