KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > FieldWidget


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.widget;
31
32 import com.caucho.util.L10N;
33
34 import java.io.IOException JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 public class FieldWidget
39   extends WidgetContainer
40 {
41   private static L10N L = new L10N( FieldWidget.class );
42
43   static protected final Logger JavaDoc log =
44     Logger.getLogger( FieldWidget.class.getName() );
45
46   private LocaleString _displayName;
47   private LocaleString _shortDescription;
48
49   public FieldWidget()
50   {
51   }
52
53   public FieldWidget( String JavaDoc id )
54   {
55     super( id );
56   }
57
58   public FieldWidget( Widget parent )
59   {
60     super( parent );
61   }
62
63   public FieldWidget( Widget parent, String JavaDoc id )
64   {
65     super( parent, id );
66   }
67
68   /**
69    * The name to display
70    */

71   public void setDisplayName( String JavaDoc displayName )
72   {
73     _displayName = LocaleString.add( _displayName, displayName );
74   }
75
76   /**
77    * The name to display
78    */

79   public void addDisplayName( LocaleString displayName )
80   {
81     _displayName = LocaleString.add( _displayName, displayName );
82   }
83
84   /**
85    * The name to display
86    */

87   public String JavaDoc getDisplayName( Locale JavaDoc locale )
88   {
89     return LocaleString.get( _displayName, locale );
90   }
91
92   /**
93    * The name to display
94    */

95   public String JavaDoc getDisplayName()
96   {
97     return LocaleString.get( _displayName );
98   }
99
100   /**
101    * A short description, up to one sentence in length.
102    */

103   public void setShortDescription( String JavaDoc shortDescription )
104   {
105     _shortDescription = LocaleString.add( _shortDescription, shortDescription );
106   }
107
108   /**
109    * A short description, up to one sentence in length.
110    */

111   public void addShortDescription( LocaleString shortDescription )
112   {
113     _shortDescription = LocaleString.add( _shortDescription, shortDescription );
114   }
115
116   /**
117    * A short description, up to one sentence in length.
118    */

119   public String JavaDoc getShortDescription()
120   {
121     return LocaleString.get( _shortDescription );
122   }
123
124   /**
125    * A short description, up to one sentence in length.
126    */

127   public String JavaDoc getShortDescription( Locale JavaDoc locale )
128   {
129     return LocaleString.get( _shortDescription, locale );
130   }
131
132   protected String JavaDoc calculateId( Widget child )
133   {
134     String JavaDoc id = null;
135
136     int size = size();
137
138     if ( size == 0 )
139       id = "value";
140     else
141       id = "value" + size;
142
143     return id;
144   }
145
146   public void init()
147     throws WidgetException
148   {
149     super.init();
150
151     if ( getDisplayName() == null )
152       throw new IllegalStateException JavaDoc(
153           L.l( "`{0}' is required", "display-name" ) );
154   }
155
156   protected FieldWidgetState createState( WidgetConnection connection )
157     throws WidgetException
158   {
159     return new FieldWidgetState();
160   }
161
162   protected WidgetState decodeChild( WidgetConnection connection,
163                                      WidgetState thisState,
164                                      Widget child )
165     throws WidgetException
166   {
167     WidgetState childState
168       = super.decodeChild( connection, thisState, child );
169
170     FieldWidgetState state = (FieldWidgetState) thisState;
171
172     state.setChildWithValue( childState );
173
174     return childState;
175   }
176
177   public void renderTextHtml( WidgetConnection connection,
178                               FieldWidgetState widgetState )
179     throws WidgetException, IOException JavaDoc
180   {
181     WidgetWriter writer = connection.getWriter();
182
183     Locale JavaDoc locale = connection.getLocale();
184
185     String JavaDoc displayName = getDisplayName( locale );
186     String JavaDoc shortDescription = getShortDescription( locale );
187
188     writer.startElement( "div", true );
189     writer.writeAttribute( "id", getClientId() );
190     writer.writeAttribute( "class", getCssClass() );
191
192     // name
193

194     writer.startElement( "div", true );
195     writer.writeAttribute( "id", getClientId() );
196     writer.writeAttribute( "class", "name" );
197
198     if ( shortDescription != null )
199       writer.writeAttribute( "title", shortDescription );
200
201     if ( displayName != null )
202       writer.writeText( displayName );
203
204     writer.endElement( "div", true );
205
206
207     // XXX:
208
// warnings
209
// errors
210

211     // value
212

213     writer.startElement( "div", true );
214     writer.writeAttribute( "id", getClientId() );
215     writer.writeAttribute( "class", "value" );
216
217     super.renderChildren( connection, widgetState );
218
219     writer.endElement( "div", true );
220
221     //
222

223     writer.endElement( "div", true );
224   }
225 }
226
Popular Tags