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 private static class ValueComparator implements Comparator
043 {
044 public int compare(Object o1, Object o2)
045 {
046 PaletteOption option1 = (PaletteOption) o1;
047 PaletteOption option2 = (PaletteOption) o2;
048
049 return option1.getValue().compareTo(option2.getValue());
050 }
051 }
052
053 private static class LabelComparator implements Comparator
054 {
055 public int compare(Object o1, Object o2)
056 {
057 PaletteOption option1 = (PaletteOption) o1;
058 PaletteOption option2 = (PaletteOption) o2;
059
060 return option1.getLabel().compareTo(option2.getLabel());
061 }
062 }
063
064 /**
065 * @param name
066 * the name of the column (the name attribute of the <select>)
067 * @param rows
068 * the number of visible rows (the size attribute of the <select>)
069 */
070 public PaletteColumn(String name, String clientId, int rows)
071 {
072 _name = name;
073 _clientId = clientId;
074 _rows = rows;
075 }
076
077 public void addOption(PaletteOption option)
078 {
079 _options.add(option);
080 }
081
082 /**
083 * Sorts the options by value (the hidden value for the option that represents the object
084 * value). This should be invoked before rendering this PaletteColumn.
085 */
086 public void sortByValue()
087 {
088 Collections.sort(_options, new ValueComparator());
089 }
090
091 /**
092 * Sorts the options by the label visible to the user. This should be invoked before rendering
093 * this PaletteColumn.
094 */
095 public void sortByLabel()
096 {
097 Collections.sort(_options, new LabelComparator());
098 }
099
100 /**
101 * Renders the <select> and <option> tags for this column.
102 */
103 public void render(IMarkupWriter writer, IRequestCycle cycle)
104 {
105 writer.begin("select");
106 writer.attribute("multiple", "multiple");
107 writer.attribute("name", _name);
108
109 if (_clientId != null)
110 writer.attribute("id", _clientId);
111
112 writer.attribute("size", _rows);
113 writer.println();
114
115 int count = _options.size();
116 for (int i = 0; i < count; i++)
117 {
118 PaletteOption o = (PaletteOption) _options.get(i);
119
120 o.render(writer, cycle);
121 }
122
123 writer.end();
124 }
125
126 }