1 15 package org.apache.tapestry.contrib.palette; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.Comparator ; 20 import java.util.List ; 21 22 import org.apache.tapestry.IMarkupWriter; 23 import org.apache.tapestry.IRender; 24 import org.apache.tapestry.IRequestCycle; 25 26 32 public class PaletteColumn implements IRender 33 { 34 private String _name; 35 private int _rows; 36 private List _options = new ArrayList (); 37 38 private static class ValueComparator implements Comparator 39 { 40 public int compare(Object o1, Object 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 50 { 51 public int compare(Object o1, Object o2) 52 { 53 PaletteOption option1 = (PaletteOption) o1; 54 PaletteOption option2 = (PaletteOption) o2; 55 56 return option1.getLabel().compareTo(option2.getLabel()); 57 } 58 } 59 60 64 public PaletteColumn(String 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 80 public void sortByValue() 81 { 82 Collections.sort(_options, new ValueComparator()); 83 } 84 85 89 public void sortByLabel() 90 { 91 Collections.sort(_options, new LabelComparator()); 92 } 93 94 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 |