Tag de création de menu déroulant



template<class T>
class selectTag {
Map<T,String> option;
T currentKey;

String name;
ptr<Map<T,String> > options;
String defaultTo;
String id;
public:

class Setter {
selectTag& dd;
public:
Setter(selectTag& d) : dd(d) {}
selectTag& operator[](const T& key) {
dd.setCurrentKey(key);
return dd;
}
};
Setter set;
void setCurrentKey(const T& key) {
currentKey=key;
}
void operator()(const String& value) {
option[currentKey]=value;
}

selectTag() : name(""), options(), defaultTo(""), id(""), set(*this) {}
void setId(const String& str) { id=str; }
void setName(const String& str) { name=str; }
void setOptions(ptr<Map<T,String> > opts) { options=opts; }
void setDefaultTo(const T& str) { defaultTo=str; }
void doStart(HttpServletRequest& request, HttpServletResponse& response) {

bool selected_passed = false;

response << "<select name=\""<< name.escapeAttribute() <<"\">\n";
if ( ! id.empty() )
response <<"id =\""<<id.escapeAttribute()<<"\"\n";

for ( typename Map<T,String>::const_iterator it=option.begin();
it != option.end();
it++ ) {
if ( !defaultTo.empty() && it->first == defaultTo ) {
if ( selected_passed ) continue;
response << " <option value=\""<<it->first.escapeAttribute()<<"\" selected=\"selected\">"<<it->second.escapeHTML()<<"</option>\n";
selected_passed=true;
}
else {
response << " <option value=\""<<it->first.escapeAttribute()<<"\">"<<it->second.escapeHTML()<<"</option>\n";
}
}
if ( options )
for ( typename Map<T,String>::const_iterator it=options->begin();
it != options->end();
it++ ) {
if ( !defaultTo.empty() && it->first == defaultTo ) {
if ( selected_passed ) continue;
response << " <option value=\""<<it->first.escapeAttribute()<<"\" selected=\"selected\">"<<it->second.escapeHTML()<<"</option>\n";
selected_passed=true;
}
else {
response << " <option value=\""<<it->first.escapeAttribute()<<"\">"<<it->second.escapeHTML()<<"</option>\n";
}
}

response << "</select>\n";
}
};