Hi Ryan,

Not sure wether I understand ur question, so I will just post a working version of a page class, a page property file and a html template. All using the IPropertySelection implementation I posted earlier. I stripped out some code specific to my app, so beware of any compilation errors.

Hope this will get you going. Good luck.

Ciao,
Martijn

Here goes:

(snippets of) Page class:

 public abstract String getStringProperty();
 public abstract ValueLabelPropertySelectionModel getStringModel();
public abstract void setStringModel(ValueLabelPropertySelectionModel model);

 public abstract String getValueProperty();
 public abstract ValueLabelPropertySelectionModel getValueLabelModel();
public abstract void setValueLabelModel(ValueLabelPropertySelectionModel model);
 public void pageBeginRender(PageEvent event) {
   log.debug("Begin rendering");

   // A StringModel, i.e. <option value="String">String</option>
   if( getStringModel()==null ) {
     log.debug("creating StringModel");
     List stringList = new ArrayList();
     int i=0;
     stringList.add(i++,"A");
     stringList.add(i++,"B");
     stringList.add(i++,"C");
ValueLabelPropertySelectionModel stringModel = new ValueLabelPropertySelectionModel(stringList);
     setStringModel(stringModel);
   }

// A ValueLabelModel, i.e. <option value="value">label</option> if( getValueLabelModel()==null ) {
     log.debug("creating ValueLabelModel");
     List valueLabelList = new ArrayList();
     int i=0;
valueLabelList.add(i++, new IValueLabel() { public String getValue() { return "AA"; } public String getLabel() { return "Label for AA"; }
                                                }
                       );
valueLabelList.add(i++, new IValueLabel() { public String getValue() { return "BB"; } public String getLabel() { return "Label for BB"; }
                                                }
                       );
valueLabelList.add(i++, new IValueLabel() { public String getValue() { return "CC"; } public String getLabel() { return "Label for CC"; }
                                                }
                       );
ValueLabelPropertySelectionModel valueLabelModel = new ValueLabelPropertySelectionModel(valueLabelList);
     setValueLabelModel(valueLabelModel);
}


 }


 public void onSubmit() {
   log.debug("onSubmit");
log.debug("StringPropertySelection: submitted value="+getStringProperty()); log.debug("ValueLabelPropertySelection: submitted value="+getValueProperty()); }

Page property file:

<?xml version="1.0"?>
<!DOCTYPE page-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd";>
<page-specification class="com.cumquatit.t4.Home">

 <!-- The form -->
 <component id="form" type="Form">
   <binding name="listener" value="listener:onSubmit"/>
 </component>

 <!-- The *string* property to which the stringPoplist is bound -->
 <property name="stringProperty"/>
 <!-- The stringPoplist, ie a poplist that has a model in which
      each <option> has the same (string) value for its value as
      its body, ie:
      <option value="[value]">[value]</option>
      -->
 <component id="stringPopList" type="PropertySelection">
   <binding name="model" value="ognl:stringModel"/>
   <binding name="value" value="ognl:stringProperty"/>
 </component>

 <!-- The *value* property to which the valueLabel poplist is bound -->
 <property name="valueProperty"/>
 <!-- The valueLabelPoplist, ie a poplist that has a model in which
      each <option> has a distinct (string) value for its value as
      its body, ie:
      <option value="[value]">[label]</option>
      -->
 <component id="valueLabelPopList" type="PropertySelection">
   <binding name="model" value="ognl:valueLabelModel"/>
   <binding name="value" value="ognl:valueProperty"/>
 </component>



</page-specification>

The html template:

<html>
<head>
   <title>Hello world!</title>
</head>
<body jwcid="@Body">

<h2>Hello, some poplists here:</h2>


<form jwcid="form">

 <table border="1" cellpadding="10">
   <tr>
     <td>
       <h3>StringModel:</h3>
       <select jwcid="stringPopList">
         <option value="A">A</option>
         <option value="B">B</option>
         <option value="C">C</option>
       </select>
       <p/>
Selected string: <span jwcid="@Insert" value="ognl:stringProperty">object</span>
     </td>
     <td>
       <h3>ValueLabelModel:</h3>
       <select jwcid="valueLabelPopList">
         <option value="A">This is A</option>
         <option value="B">This is B</option>
         <option value="C">This is C</option>
       </select>
       <p/>
Selected Value: <span jwcid="@Insert" value="ognl:valueProperty">object</span>
     </td>
   </tr>
 </table>
 <input type="submit" value="go!"/>

</form>


</body>

</html>

Ryan Pan wrote:

Hi Martijn,
   Thanks for your suggestion,and I made a model for myself,but I
encountered another issue.
   So would you please tell me how can I  or which method can set the value
loaded from DB to the PropertySelection component while I could do nothing
for setting the value back to UI.

Regards,
Ryan.

2005/12/22, Martijn Hinten <[EMAIL PROTECTED]>:
You will have to create yout own implementation of
IPropertySelectionModel, of which several are available via the WIKI.
Also see pages 141, 142 of Tapestry in Action.

This is what I created myself and is working fine. The only
bug/feature is that the value to which the PropertySelection
component is bound *has* to be a String.

The constructor of ValueLabelPropertySelectionModel accepts any List
of Objects that either implement the IValueLabel interface (which
should serve your needs) or are just plain Strings. In the latter
case the value and label in the poplist will be the same.

Good luck,
Martijn

public class ValueLabelPropertySelectionModel implements
   IPropertySelectionModel {

 /**
  * The list of options held by this poplist<br/> Options can be: -
Strings or
  * objects that implement IValueLabel
  *
  */
 List _options;

 Map _optionsMap;


 public ValueLabelPropertySelectionModel() {
 }


 public ValueLabelPropertySelectionModel(List l) {
   set_options(l);
   if( _options != null ) {
     _optionsMap = new HashMap();
     for( int i = 0; i < _options.size(); i++ ) {
       Object option = _options.get(i);

        if( option instanceof IValueLabel ) {
         IValueLabel vl = (IValueLabel)_options.get(i);
         // _optionsMap.put(vl.getValue(), vl.getLabel());
         _optionsMap.put(vl.getValue(), vl);
       } else {
         _optionsMap.put(option.toString(), option.toString());
       }
     }
   }
 }


 /**
  * @see
org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
  */
 public int getOptionCount() {
   if( _options == null ) {
     return 0;
   } else {
     return _options.size();
   }
 }


 /**
  * @see
org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
  */
 public Object getOption(int i) {
   if( _options == null ) {
     return null;
   } else {
     Object option = _options.get(i);
     if( option instanceof IValueLabel ) {
       return ((IValueLabel)option).getValue();
       // return option;
     } else {
       return option;
     }
   }
 }


 /**
  * @see
org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
  */
 public String getLabel(int i) {
   if( _options == null ) {
     return null;
   } else {
     Object option = _options.get(i);
     if( option instanceof IValueLabel ) {
       return ((IValueLabel)option).getLabel();
     } else {
       return option.toString();
     }
   }

 }


 /**
  * @see
org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
  */
 public String getValue(int i) {
   if( _options == null ) {
     return null;
   } else {
     Object option = _options.get(i);
     if( option instanceof IValueLabel ) {
       return ((IValueLabel)option).getValue();
     } else {
       return option.toString();
     }
   }

 }


 /**
  * @see
org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.l
ang.String)
  */
 public Object translateValue(String value) {
   if( _options == null ) {
     return null;
   } else {
     Object o = _optionsMap.get(value);
     if( o instanceof IValueLabel ) {
       return ((IValueLabel)o).getValue();
     } else {
       return _optionsMap.get(value);
     }
   }
 }


 /**
  * @return Returns the _options.
  */
 public List get_options() {
   return _options;
 }


 /**
  * @param _options The _options to set.
  */
 public void set_options(List _options) {
   this._options = _options;
 }


public interface IValueLabel {

 public String getLabel();
 public String getValue();

}


public class ValueLabel implements IValueLabel{

 String label;
 String value;

 public ValueLabel(String l, String v){

 setLabel(l);
 setValue(v);

 }

 public void setLabel(String label) {
   this.label = label;
 }

 public void setValue(String value) {
   this.value = value;
 }

 public String getLabel() {

   return label;
 }

 public String getValue() {

   return value;
 }

}


---- Original Message ----
From: [EMAIL PROTECTED]
To: [email protected]
Subject: RE: selection problem
Date: Thu, 22 Dec 2005 18:33:11 +0800

Hi all,
  I have a issue about using PropertySelection component.
  So I wanna whether can PropertySelection seperate his list value
and
list key.Because I may need to display the value,while using the key
of  the
selection.
  So  is there any  one who know how to do?

Thanks,
Ryan.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--


*Cumquat Information Technology*
De Dreef 19
3706 BR Zeist
T +31 (0)30 - 6940490
F +31 (0)10 - 6940499
http://www.cumquat.nl <http://www.cumquat.nl/>

[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
M +31 6 22 384 318


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to