01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.util;
16:
17: import java.util.Map;
18:
19: import org.apache.tapestry.MarkupWriter;
20: import org.apache.tapestry.OptionGroupModel;
21: import org.apache.tapestry.OptionModel;
22: import org.apache.tapestry.SelectModelVisitor;
23: import org.apache.tapestry.ValueEncoder;
24:
25: public class SelectModelRenderer implements SelectModelVisitor {
26: private final MarkupWriter _writer;
27:
28: private final ValueEncoder _encoder;
29:
30: public SelectModelRenderer(final MarkupWriter writer,
31: ValueEncoder encoder) {
32: _writer = writer;
33: _encoder = encoder;
34: }
35:
36: public void beginOptionGroup(OptionGroupModel groupModel) {
37: _writer.element("optgroup", "label", groupModel.getLabel());
38:
39: writeDisabled(groupModel.isDisabled());
40: writeAttributes(groupModel.getAttributes());
41: }
42:
43: public void endOptionGroup(OptionGroupModel groupModel) {
44: _writer.end(); // select
45: }
46:
47: @SuppressWarnings("unchecked")
48: public void option(OptionModel optionModel) {
49: Object optionValue = optionModel.getValue();
50:
51: String clientValue = _encoder.toClient(optionValue);
52:
53: _writer.element("option", "value", clientValue);
54:
55: if (isOptionSelected(optionModel))
56: _writer.attributes("selected", "selected");
57:
58: writeDisabled(optionModel.isDisabled());
59: writeAttributes(optionModel.getAttributes());
60:
61: _writer.write(optionModel.getLabel());
62:
63: _writer.end();
64: }
65:
66: private void writeDisabled(boolean disabled) {
67: if (disabled)
68: _writer.attributes("disabled", "disabled");
69: }
70:
71: private void writeAttributes(Map<String, String> attributes) {
72: if (attributes == null)
73: return;
74:
75: for (Map.Entry<String, String> e : attributes.entrySet())
76: _writer.attributes(e.getKey(), e.getValue());
77: }
78:
79: /**
80: * If true, then the selected attribute will be written. This implementation always returns
81: * false.
82: */
83: protected boolean isOptionSelected(OptionModel optionModel) {
84: return false;
85: }
86:
87: }
|