001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.contrib.palette;
016
017 import java.util.ArrayList;
018 import java.util.Collections;
019 import java.util.Comparator;
020 import java.util.List;
021
022 import org.apache.tapestry.IMarkupWriter;
023 import org.apache.tapestry.IRender;
024 import org.apache.tapestry.IRequestCycle;
025
026 /**
027 * One of the two columns in a Palette component: the left column lists available options, the right
028 * column lists the selected columns.
029 *
030 * @author Howard Lewis Ship
031 */
032 public class PaletteColumn implements IRender
033 {
034 private String _name;
035
036 private String _clientId;
037
038 private int _rows;
039
040 private List _options = new ArrayList();
041
042 /**
043 *
044 * @author hls
045 */
046 private static class ValueComparator implements Comparator
047 {
048 public int compare(Object o1, Object o2)
049 {
050 PaletteOption option1 = (PaletteOption) o1;
051 PaletteOption option2 = (PaletteOption) o2;
052
053 return option1.getValue().compareTo(option2.getValue());
054 }
055 }
056
057 /**
058 *
059 * @author hls
060 */
061 private static class LabelComparator implements Comparator
062 {
063 public int compare(Object o1, Object o2)
064 {
065 PaletteOption option1 = (PaletteOption) o1;
066 PaletteOption option2 = (PaletteOption) o2;
067
068 return option1.getLabel().compareTo(option2.getLabel());
069 }
070 }
071
072 /**
073 * @param name
074 * the name of the column (the name attribute of the <select>)
075 * @param rows
076 * the number of visible rows (the size attribute of the <select>)
077 */
078 public PaletteColumn(String name, String clientId, int rows)
079 {
080 _name = name;
081 _clientId = clientId;
082 _rows = rows;
083 }
084
085 public void addOption(PaletteOption option)
086 {
087 _options.add(option);
088 }
089
090 /**
091 * Sorts the options by value (the hidden value for the option that represents the object
092 * value). This should be invoked before rendering this PaletteColumn.
093 */
094 public void sortByValue()
095 {
096 Collections.sort(_options, new ValueComparator());
097 }
098
099 /**
100 * Sorts the options by the label visible to the user. This should be invoked before rendering
101 * this PaletteColumn.
102 */
103 public void sortByLabel()
104 {
105 Collections.sort(_options, new LabelComparator());
106 }
107
108 /**
109 * Renders the <select> and <option> tags for this column.
110 */
111 public void render(IMarkupWriter writer, IRequestCycle cycle)
112 {
113 writer.begin("select");
114 writer.attribute("multiple", "multiple");
115 writer.attribute("name", _name);
116
117 if (_clientId != null)
118 writer.attribute("id", _clientId);
119
120 writer.attribute("size", _rows);
121 writer.println();
122
123 int count = _options.size();
124 for (int i = 0; i < count; i++)
125 {
126 PaletteOption o = (PaletteOption) _options.get(i);
127
128 o.render(writer, cycle);
129 }
130
131 writer.end();
132 }
133
134 }