View Javadoc
1 /* 2 * Angkor Web Framework 3 * 4 * Distributable under LGPL license. 5 * See terms of license at gnu.org. 6 */ 7 8 package com.tirsen.angkor.widget; 9 10 import com.tirsen.angkor.RenderContext; 11 import com.tirsen.angkor.Component; 12 13 import java.io.IOException; 14 import java.text.Format; 15 import java.text.MessageFormat; 16 import java.util.ArrayList; 17 import java.util.Iterator; 18 import java.util.List; 19 20 /*** 21 * Displays text to the user. 22 * 23 * Has the following functionality: 24 * <li> fetches data from a list of {@link ValueModel}s. 25 * <li> the data is formated according to a {@link Format}. 26 * <li> if the format is not a {@link MessageFormat} only the data from the first model is used. 27 * <li> if the format is {@link MessageFormat} all values are fetched from all models. 28 * <li> if setting the as text it is specified according to the syntax of {@link MessageFormat}. 29 * <li> renders the format in a SPAN-tag with the correct id so it can easily be accessed while testing. 30 * 31 * <br> 32 * 33 * Future functionality: 34 * <li> support for fonts. 35 * <li> support for rendering of png-images with the text according to a specified font 36 * on the server (as a truetype-file or an os-font). 37 * <li> support for cascading style sheets. 38 * 39 * <!-- $Id: TextLabel.java,v 1.6 2002/10/13 19:59:23 tirsen Exp $ --> 40 * 41 * @author $Author: tirsen $ 42 * @version $Revision: 1.6 $ 43 */ 44 public class TextLabel extends Component 45 { 46 private static final MessageFormat CONSTANT_FORMAT = new MessageFormat("{0}"); 47 48 private List models; 49 private Format format = CONSTANT_FORMAT; 50 51 public TextLabel() 52 { 53 } 54 55 public TextLabel(String label) 56 { 57 setLabel(label); 58 } 59 60 public TextLabel(ValueModel model) 61 { 62 setModel(model); 63 } 64 65 public TextLabel(String label, ValueModel model) 66 { 67 setLabel(label); 68 setModel(model); 69 } 70 71 public void setLabel(String label) 72 { 73 format = new MessageFormat(label); 74 } 75 76 /*** 77 * Sets a single model to fetch data from. 78 */ 79 public void setModel(ValueModel model) 80 { 81 this.models = new ArrayList(1); 82 addModel(model); 83 } 84 85 /*** 86 * Adds a model. 87 * If the format is not a {@link MessageFormat} only the first model is used. 88 */ 89 public void addModel(ValueModel model) 90 { 91 if (models == null) models = new ArrayList(1); 92 models.add(model); 93 } 94 95 /*** 96 * Gets the text formatted correctly and with the values fetched from the models. 97 */ 98 public String getFormattedText() 99 { 100 String display; 101 if (models != null && !models.isEmpty()) 102 { 103 if (format instanceof MessageFormat) 104 { 105 Object[] values; 106 107 values = new Object[models.size()]; 108 Iterator it = models.iterator(); 109 for (int i = 0; it.hasNext(); i++) 110 { 111 ValueModel model = (ValueModel) it.next(); 112 values[i] = model.getValue(); 113 } 114 115 display = format.format(values); 116 } 117 else 118 { 119 // this is not exactly 100% good design, might need some refactoring 120 // if we don't have a message-format format the string with only the first models value 121 Object value = ((ValueModel) models.get(0)).getValue(); 122 display = format.format(value); 123 } 124 } 125 else 126 { 127 display = format.format(null); 128 } 129 130 return display; 131 } 132 133 /*** 134 * Sets a custom format. For example a currency format or a date format. 135 */ 136 public void setFormat(Format format) 137 { 138 this.format = format; 139 } 140 141 public Format getFormat() 142 { 143 return format; 144 } 145 146 public String toString() 147 { 148 return getClass().getName() + "[" + format.toString() + "]"; 149 } 150 151 public void render(RenderContext context) throws IOException 152 { 153 if (isVisible()) 154 { 155 context.startTag("SPAN id = \"" + uniqueId(context) + "\""); 156 context.println(getFormattedText()); 157 context.endTag("SPAN"); 158 } 159 } 160 }

This page was automatically generated by Maven