Clover coverage report - angkor - 0.4
Coverage timestamp: ti okt 15 2002 22:32:48 CEST
file stats: LOC: 203   Methods: 17
NCLOC: 133   Classes: 1
Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
TextInput.java 59,1% 70,4% 47,1% 63,4%
 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   
 import com.tirsen.angkor.event.ChangeListener;
 13   
 import com.tirsen.angkor.event.ChangeSource;
 14   
 import com.tirsen.angkor.event.ChangeSourceHelper;
 15   
 
 16   
 import java.beans.PropertyEditor;
 17   
 import java.io.IOException;
 18   
 
 19   
 /**
 20   
  * Takes input from user as text.
 21   
  *
 22   
  * <p>
 23   
  *
 24   
  * Has the following functionality:
 25   
  * <li> fetches its value from a {@link ValueModel}.
 26   
  * <li> updates its value to the {@link ValueModel}.
 27   
  * <li> has a prompt as a {@link TextLabel}.
 28   
  * <li> has a configurable size.
 29   
  *
 30   
  *
 31   
  * <!-- $Id: TextInput.java,v 1.5 2002/10/13 19:59:23 tirsen Exp $ -->
 32   
  *
 33   
  * @author $Author: tirsen $
 34   
  * @version $Revision: 1.5 $
 35   
  * @see ValueModel
 36   
  * @see TextLabel
 37   
  */
 38   
 public class TextInput extends Component implements ChangeSource
 39   
 {
 40   
     private ValueModel model;
 41   
     private TextLabel prompt;
 42   
     private int size = -1;
 43   
     private ChangeSourceHelper changeSourceHelper = new ChangeSourceHelper(this);
 44   
     private String text = null;
 45   
     private boolean password = false;
 46   
 
 47  0
     public TextInput(String id)
 48   
     {
 49  0
         super(id);
 50   
     }
 51   
 
 52  5
     public TextInput()
 53   
     {
 54   
     }
 55   
 
 56  0
     public TextInput(String prompt, ValueModel model)
 57   
     {
 58  0
         setPrompt(prompt);
 59  0
         this.model = model;
 60   
     }
 61   
 
 62  0
     public TextInput(ValueModel model)
 63   
     {
 64  0
         this.model = model;
 65   
     }
 66   
 
 67  3
     public void addChangeListener(ChangeListener listener)
 68   
     {
 69  3
         changeSourceHelper.addChangeListener(listener);
 70   
     }
 71   
 
 72  0
     public void removeChangeListener(ChangeListener listener)
 73   
     {
 74  0
         changeSourceHelper.removeChangeListener(listener);
 75   
     }
 76   
 
 77  2
     public void setSize(int size)
 78   
     {
 79  2
         this.size = size;
 80   
     }
 81   
 
 82   
     /**
 83   
      * Set the prompt as a @{link TextLabel} widget. This enables more sofisticated display
 84   
      * of a prompt than by using only a <code>String</code>.
 85   
      * The prompt is displayed above the input-box.
 86   
      */
 87  2
     public void setPrompt(TextLabel prompt)
 88   
     {
 89  2
         this.prompt = prompt;
 90   
     }
 91   
 
 92   
     /**
 93   
      * Sets the text of the prompt. The prompt is displayed above the input-box.
 94   
      */
 95  0
     public void setPrompt(String promptText)
 96   
     {
 97  0
         prompt = new TextLabel();
 98  0
         prompt.setLabel(promptText);
 99   
     }
 100   
 
 101  0
     public void setVisible(boolean visible)
 102   
     {
 103  0
         super.setVisible(visible);
 104  0
         prompt.setVisible(visible);
 105   
     }
 106   
 
 107  5
     public void setModel(ValueModel model)
 108   
     {
 109  5
         this.model = model;
 110   
     }
 111   
 
 112   
     /**
 113   
      * This widget is interested in parsing events.
 114   
      * @return <code>true</code>
 115   
      */
 116  0
     public boolean isParsing()
 117   
     {
 118  0
         return true;
 119   
     }
 120   
 
 121   
     /**
 122   
      * Retrieves the value in the parameter named by {@link #getId()} and updates
 123   
      * the model with this value. The PropertyEditor specified by the model is used to
 124   
      * parse the <code>String</code> into an <code>Object</code>.
 125   
      */
 126  7
     public void parse(RenderContext context)
 127   
     {
 128  7
         super.parse(context);
 129  7
         if (isVisible())
 130   
         {
 131  7
             text = context.getRequestParameter(getId());
 132  7
             if (text != null)
 133   
             {
 134  7
                 PropertyEditor editor = getModel().getPropertyEditor();
 135  7
                 editor.setAsText(text);
 136  7
                 Object newValue = editor.getValue();
 137  7
                 getModel().setValue(newValue);
 138  7
                 text = null;
 139  7
                 changeSourceHelper.signalChangeEvent(context);
 140   
             }
 141   
         }
 142   
     }
 143   
 
 144  8
     public void render(RenderContext context) throws IOException
 145   
     {
 146  8
         if (isVisible())
 147   
         {
 148  1
             if (prompt != null) prompt.render(context);
 149   
 
 150  8
             context.emptyTag("BR");
 151   
 
 152  8
             if (text == null)
 153   
             {
 154  8
                 Object value = getModel().getValue();
 155  8
                 PropertyEditor editor = getModel().getPropertyEditor();
 156  8
                 if (editor != null)
 157   
                 {
 158  8
                     if (value != null)
 159   
                     {
 160  8
                         editor.setValue(value);
 161  8
                         text = editor.getAsText();
 162   
                     }
 163   
                     else
 164   
                     {
 165  0
                         text = "";
 166   
                     }
 167   
                 }
 168   
                 else
 169  0
                     text = "<error, no editor>";
 170  0
                 if (text == null) text = "";
 171   
             }
 172   
 
 173   
             // TODO: recalculate width for different browsers here...
 174  8
             String type = password ? " type=\"password\"" : " type=\"text\"";
 175  8
             String sizeAttr = "";
 176  1
             if (size != -1) sizeAttr = " size=\"" + size + "\"";
 177  8
             String name = " name=\"" + uniqueId(context) + "\"";
 178  8
             String value = " value=\"" + text + "\"";
 179  8
             String id = " id=\"" + uniqueId(context) + "\"";
 180   
 
 181  8
             context.emptyTag("INPUT" + type + id + sizeAttr + name + value);
 182   
 
 183  8
             if (getModel() == null || !getModel().isReadOnly()) context.registerParsingComponent(this);
 184   
         }
 185   
     }
 186   
 
 187  0
     public void setPassword(boolean password)
 188   
     {
 189  0
         this.password = password;
 190   
     }
 191   
 
 192  48
     public ValueModel getModel()
 193   
     {
 194  0
         if (model == null) model = createDefaultModel();
 195  48
         return model;
 196   
     }
 197   
 
 198  0
     private BasicValueModel createDefaultModel()
 199   
     {
 200  0
         return new BasicValueModel();
 201   
     }
 202   
 }
 203