Here is the class. Some observations:

Format
----------

You will provide a string with properties enclosed in braces like this: 
${propertyName}. You coud include as much properties as you want. 
"${firstName} ${secondName} SomeHarcodeString ${surname}" its totally valid.

NullElement
-----------------

If this property is true, then selection will have one element with an empty 
string as label, and it's value will be "null". If false, only the 
collection elements will be there.

Here is the class:

/**
* 
* @author Pablo Ruggia
* @author Ignacio Blanco
* 
*/
public class QPropertySelectionModel implements IPropertySelectionModel {

//*******************************************************
// Fields
//*******************************************************

List _sourceList;

String _format;

//*******************************************************
// Constructors
//*******************************************************

public QPropertySelectionModel(Collection col, boolean nullElement) {

_sourceList = new ArrayList();

if (nullElement) {
_sourceList.add(null);
}

_sourceList.addAll(col);
}

public QPropertySelectionModel(Collection col, boolean nullElement,
String formatString) {

if (col == null) {
col = new ArrayList();
}

_sourceList = new ArrayList();

if (nullElement) {
_sourceList.add(null);
}

_sourceList.addAll(col);

_format = formatString.trim();

}

//*******************************************************
// Logic
//*******************************************************

public static QPropertySelectionModel emptyModel() {
return new QPropertySelectionModel(new ArrayList(), true);
}

public int getOptionCount() {
return _sourceList.size();
}

public Object getOption(int index) {
return _sourceList.get(index);
}

public String getLabel(int index) {
if (_format != null && !_format.equals("")) {
if (_sourceList.get(index) != null) {
return format(_sourceList.get(index));
} else {
return "";
}
} else {
if (_sourceList.get(index) != null) {
return _sourceList.get(index).toString();
} else {
return "";
}
}

}

private String format(Object object) {

String result = _format;

int startPos = 0, endPos = 0;

while (true) {

endPos = 0;

String propertyName;

startPos = result.indexOf("${", endPos);

endPos = result.indexOf("}", startPos + 1);

if(startPos == -1 || endPos == -1){
break; 
}

if (startPos >= endPos)
break;

propertyName = result.substring(startPos + 2, endPos);

result = result.replaceAll("\\$\\{" + propertyName + "\\}",
getPropertyValue(object, propertyName)
.toString());

}

return result;
}

public Object getPropertyValue(Object object, String property) {
Object realValue;

Object parsedExpr = OgnlUtils.getParsedExpression(property);
try {
realValue = Ognl.getValue(parsedExpr, object);
} catch (OgnlException ex) {
throw new RuntimeException("Error obtiendo la propiedad del resultado"
+ property, ex);
}
return realValue;
}


public String getValue(int index) {
return "" + index;
}

public Object translateValue(String value) {
int pos = Integer.parseInt(value);
if (pos < _sourceList.size())
return _sourceList.get(pos);
else
return null;
}

} 


On 7/27/05, Robert Zeigler <[EMAIL PROTECTED]> wrote:
> 
> That sounds pretty sweet. :) I'm interested... :)
> 
> Robert
> 
> Pablo Ruggia wrote:
> > If you have the list, you can have the index in the list as id. Then, 
> for
> > description, you can pass a string to get info from the objects, like:
> > "${name} - ${company.description}"
> > So you will pass to your property selection the collection and the 
> format
> > string. I've been using this way and it's very simple. No interfases and 
> you
> > can use two different descriptions for the same object.
> > If you want this implementation of PropertySelectionModel tell me.
> >
> > On 7/26/05, Robert Zeigler <[EMAIL PROTECTED]> wrote:
> >
> >>Incidentally, there is a generic "ListPropertySelectionModel"
> >>available on tassel
> >>
> >>
> http://equalitylearning.org/Tassel/app?service=external/ViewComponent&sp=SListPropertySelectionModel
> >>
> >>
> >>It uses the interface approach; classes which implement the interface
> >>must implement the "public String getItemName" method.
> >>
> >>Robert
> >>
> >>Eric DIEP wrote:
> >>
> >>>OK - thanks, this was the way I was originally planning to do (create a
> >>>common interface).
> >>>
> >>>-- X. Y. ZHAO
> >>>
> >>>On 26-Jul-05, at 161611, Mark Dillon wrote:
> >>>
> >>>
> >>>>There is an example in the mailing list archives of a reflection
> >>>>implementation of the IPropertySelectionModel. That's one option. I
> >>>>prefer to have all of my domain objects implement an interface that
> >>>>specifies a getDescription and a getID method, and have the
> >>>>IPropertySelectionModel just deal with the interface.
> >>>>
> >>>>Cheers,
> >>>>Mark
> >>>>
> >>>>On 7/26/05, X. Y. Zhao <[EMAIL PROTECTED]> wrote:
> >>>>
> >>>>
> >>>>>Hi:
> >>>>>I have several Lists of various domain objects which I would like to
> >>>>>use with the PropertySelection component. However, to use them, I
> >>>>>have to create an implementation of IPropertySelectionModel for each
> >>>>>domain object type (for the reason that I need to do xxx.getName() in
> >>>>>the IPropertySelectionModel's getLabel() for each impl.). Is there a
> >>>>>generic way to handle this situation without the need of creating all
> >>>>>these redundant classes?
> >>>>>
> >>>>>Thank you.
> >>>>>
> >>>>>-- X. Y. ZHAO
> >>>>>
> >>>>>
> >>>>>---------------------------------------------------------------------
> >>>>>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>>>>For additional commands, e-mail: tapestry-user-
> >>
> >>[EMAIL PROTECTED]
> >>
> >>>>>
> >>>>>
> >>>>---------------------------------------------------------------------
> >>>>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>>>For additional commands, e-mail: [EMAIL PROTECTED]
> >>>>
> >>>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>---------------------------------------------------------------------
> >>>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>>For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
>

Reply via email to