KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > palette > PaletteColumn


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.contrib.palette;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.tapestry.IMarkupWriter;
23 import org.apache.tapestry.IRender;
24 import org.apache.tapestry.IRequestCycle;
25
26 /**
27  * One of the two columns in a Palette component: the left column lists
28  * available options, the right column lists the selected columns.
29  *
30  * @author Howard Lewis Ship
31  */

32 public class PaletteColumn implements IRender
33 {
34     private String JavaDoc _name;
35     private int _rows;
36     private List JavaDoc _options = new ArrayList JavaDoc();
37
38     private static class ValueComparator implements Comparator JavaDoc
39     {
40         public int compare(Object JavaDoc o1, Object JavaDoc o2)
41         {
42             PaletteOption option1 = (PaletteOption) o1;
43             PaletteOption option2 = (PaletteOption) o2;
44
45             return option1.getValue().compareTo(option2.getValue());
46         }
47     }
48
49     private static class LabelComparator implements Comparator JavaDoc
50     {
51         public int compare(Object JavaDoc o1, Object JavaDoc o2)
52         {
53             PaletteOption option1 = (PaletteOption) o1;
54             PaletteOption option2 = (PaletteOption) o2;
55
56             return option1.getLabel().compareTo(option2.getLabel());
57         }
58     }
59
60     /**
61      * @param name the name of the column (the name attribute of the <select>)
62      * @param rows the number of visible rows (the size attribute of the <select>)
63      */

64     public PaletteColumn(String JavaDoc name, int rows)
65     {
66         _name = name;
67         _rows = rows;
68     }
69
70     public void addOption(PaletteOption option)
71     {
72         _options.add(option);
73     }
74
75     /**
76      * Sorts the options by value (the hidden value for the option
77      * that represents the object value). This should be invoked
78      * before rendering this PaletteColumn.
79      */

80     public void sortByValue()
81     {
82         Collections.sort(_options, new ValueComparator());
83     }
84
85     /**
86      * Sorts the options by the label visible to the user. This should be invoked
87      * before rendering this PaletteColumn.
88      */

89     public void sortByLabel()
90     {
91         Collections.sort(_options, new LabelComparator());
92     }
93
94     /**
95      * Renders the <select> and <option> tags for
96      * this column.
97      */

98     public void render(IMarkupWriter writer, IRequestCycle cycle)
99     {
100         writer.begin("select");
101         writer.attribute("multiple", "multiple");
102         writer.attribute("name", _name);
103         writer.attribute("size", _rows);
104         writer.println();
105
106         int count = _options.size();
107         for (int i = 0; i < count; i++)
108         {
109             PaletteOption o = (PaletteOption) _options.get(i);
110
111             o.render(writer, cycle);
112         }
113
114         writer.end();
115     }
116
117 }
118
Popular Tags