KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2005 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 package com.caucho.widget;
30
31 import com.caucho.util.L10N;
32
33 import java.io.IOException;
34
35 public class LabelWidget
36   extends XulWidget
37 {
38   private static final L10N L = new L10N(LabelWidget.class);
39
40   private static final String NULL_VALUE = "";
41
42   private VarDefinition valueDef = new XulVarDefinition(this, "value", String.class);
43   private DisabledProperty _disabledProperty = new DisabledProperty(this);
44
45   public LabelWidget()
46   {
47   }
48
49   public LabelWidget(String id)
50   {
51     setId(id);
52   }
53
54   public LabelWidget(String id, String initialValue)
55   {
56     setId(id);
57     setValue(initialValue);
58   }
59
60   public void setValue(String value)
61   {
62     valueDef.setValue(value);
63   }
64
65   public String getValue()
66   {
67     return valueDef.getValue();
68   }
69
70   public void setDisabled(boolean isDisabled)
71   {
72     _disabledProperty.setValue(isDisabled);
73   }
74
75   public boolean isDisabled()
76   {
77     return _disabledProperty.getValue();
78   }
79
80   public void init(WidgetInit init)
81     throws WidgetException
82   {
83     super.init(init);
84
85     _disabledProperty.init(init);
86
87     if (valueDef.getValue() == null)
88       valueDef.setValue(NULL_VALUE);
89
90     init.addVarDefinition(valueDef);
91
92   }
93
94   /**
95    * A null value becomes the empty string.
96    */

97   public void setValue(VarContext context, String value)
98   {
99     if (value == null)
100       value = NULL_VALUE;
101
102     context.setVar(this, valueDef, value);
103   }
104
105   /**
106    * A null value is never returned, LabelWidget treats null as an empty
107    * string.
108    */

109   public String getValue(VarContext context)
110   {
111     return context.getVar(this, valueDef);
112   }
113
114   public void setDisabled(VarContext context, boolean isDisabled)
115   {
116     _disabledProperty.setValue(context, isDisabled);
117   }
118
119   public boolean isDisabled(VarContext context)
120   {
121     return _disabledProperty.getValue(context);
122   }
123
124   public void invocation(WidgetInvocation invocation)
125     throws WidgetException
126   {
127     super.invocation(invocation);
128
129     if (!valueDef.isReadOnly()) {
130       String value = invocation.getParameter();
131
132       if (value != null)
133         setValue(invocation, value);
134     }
135   }
136
137   public void url(WidgetURL url)
138     throws WidgetException
139   {
140     if (isTransient(url))
141       return;
142
143     super.url(url);
144
145     if (!valueDef.isReadOnly()) {
146       String value = getValue(url);
147
148       if (value == null)
149         value = NULL_VALUE;
150
151       if (!value.equals(valueDef.getValue()))
152         url.setParameter(value);
153     }
154   }
155
156   public void response(WidgetResponse response)
157     throws WidgetException, IOException
158   {
159     String value = getValue(response);
160
161     if (value == null)
162       return;
163
164     String id = getId();
165     String cssClass = getCssClass(response);
166     boolean isDisabled = isDisabled(response);
167
168     WidgetWriter out = response.getWriter();
169
170     out.startElement("span");
171     out.writeAttribute("id", id);
172
173     String disabledAttribute = isDisabled ? "disabled" : null;
174
175     out.writeAttribute("class", cssClass, disabledAttribute);
176
177     out.writeText(value);
178
179     out.endElement("span");
180   }
181 }
182
Popular Tags