DynamicSelectDemo2 : Source Code

DynamicSelectDemo2.tml:
  <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
	<head>
		<title>DynamicSelect Demo 2</title>
	</head>
	<body>
		<h1>DynamicSelect Demo 2</h1>

		A simple example on ParentSelect and ChildSelect. In this case, OptionModel.value being the Java class Bike.
           Note that when OptionModel.value is a custom Java class, it must override the equals method.
           The source code of this demo
            is <a href="#" t:type="PageLink" t:page="test/components/DynamicSelectDemo2Source" target="_blank">here</a>.

		<br />
		<br />
		<form t:type="form" t:id="myForm">
		
			<br />
				
			Makes : <select t:type="lombok/ParentSelect" t:id="makes" model="'BMW,Harley Davidsion,Honda,Yamaha'" value="prop:make"  
                                    />   

			
                        Models: <select t:type="lombok/ChildSelect" t:id="bikes" parent="makes" models="childSelectModels" 
                               value="bike" />
		        <br/>

			<input type="submit" value="Submit" />
		</form>

                 <t:if test="bike">                              
			You have selected  : <br/>
			Brand :   <b>${make} </b>
				<br/>
			Mode : 
					<table border="1">
			
							<thead>
								<tr>
								<th scope="col"> Name </th>
								<th scope="col"> Capacity </th>
								<th scope="col"> Max Speed </th>
								</tr>
			
							</thead>
							<tbody>
								<tr>
								<td> ${bike.name} </td>
								<td> ${bike.engineCapacity} </td>
								<td>  ${bike.maxSpeed} </td>
			
								</tr>				
							</tbody>
				</table>
   

                 </t:if>  
		<br />
		<br />
		
	</body>
</html>
 
DynamicSelectDemo2.java
   package com.man.tapestry5Demo.pages.test.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.internal.SelectModelImpl;

import com.man.tapestry5Demo.pagehelpers.Bike;



@SuppressWarnings("unused")
public class DynamicSelectDemo2 {

	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private String make;

	@Property
	@Persist(PersistenceConstants.FLASH)
	private Bike bike;

	private List<SelectModel> childSelectModels;

	public List<SelectModel> getChildSelectModels() {
		if (childSelectModels == null) {
			childSelectModels = new ArrayList<SelectModel>();
			childSelectModels.add(getSelectModle(getBMWs()));
			childSelectModels.add(getSelectModle(getHDs()));
			childSelectModels.add(getSelectModle(getHondas()));
			childSelectModels.add(getSelectModle(getYamahas()));
		}
		return childSelectModels;
	}

	protected SelectModel getSelectModle(List<Bike> bikes) {
		return new SelectModelImpl(null, getOptionModels(bikes));
	}

	protected List<OptionModel> getOptionModels(List<Bike> bikes) {

		List<OptionModel> optionModels = new ArrayList<OptionModel>();
		for (Iterator<Bike> it = bikes.iterator(); it.hasNext();) {
			Bike bike = it.next();
			optionModels.add(new OptionModelImpl(bike.getName(), bike));
		}

		return optionModels;
	}

	private List<Bike> getBMWs() {
		List<Bike> list = new ArrayList<Bike>();
		list.add(new Bike("F650", "650cc", 100));
		list.add(new Bike("K1100RS", "1100cc", 220));
		list.add(new Bike("R100RT", "1000cc", 200));
		list.add(new Bike("R100RT", "1000cc", 200));
		list.add(new Bike("R80GS", "800cc", 180));
		return list;
	}

	private List<Bike> getHDs() {
		List<Bike> list = new ArrayList<Bike>();
		list.add(new Bike("Fat Boy", "700cc", 120));
		list.add(new Bike("FXD Superglide", "1100cc", 150));
		list.add(new Bike("Sportster", "850cc", 150));
		return list;
	}

	private List<Bike> getHondas() {
		List<Bike> list = new ArrayList<Bike>();
		list.add(new Bike("CBR600F", "600cc", 250));
		list.add(new Bike("FMX650", "650cc", 150));
		list.add(new Bike("GoldWing", "1500cc", 260));
		list.add(new Bike("VFR800", "800cc", 260));
		list.add(new Bike("VTR1000", "1000cc", 250));
		return list;
	}

	private List<Bike> getYamahas() {
		List<Bike> list = new ArrayList<Bike>();
		list.add(new Bike("Fazer 600", "600cc", 200));
		list.add(new Bike("VMax", "1200cc", 180));
		list.add(new Bike("YZR-R1", "1000cc", 250));
		list.add(new Bike("YZR-R6", "600cc", 240));
		return list;
	}

	
}

      
Bike.java
   package com.man.tapestry5Demo.pagehelpers;

public class Bike {
	private String name;

	private String engineCapacity;

	private int maxSpeed;

		
	public Bike(String name, String engineCapacity, int maxSpeed) {		
		this.name = name;
		this.engineCapacity = engineCapacity;
		this.maxSpeed = maxSpeed;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEngineCapacity() {
		return engineCapacity;
	}

	public void setEngineCapacity(String engineCapacity) {
		this.engineCapacity = engineCapacity;
	}

	public int getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}
	
	public boolean equals(Object obj){
		
		
		if (obj ==null || !(obj instanceof Bike )){
			return false;
		}
		
		Bike bike = (Bike) obj;
		if (!name.equals(bike.getName())){
			return false;
		}
		
		if (!engineCapacity.equals(bike.getEngineCapacity())){
			return false;
		}
		if (maxSpeed != bike.getMaxSpeed()){
			return false;
		}
		
		return true;
	}

}