001package com.hfg.html;
002
003
004import com.hfg.util.collection.CollectionUtil;
005import com.hfg.xml.XMLNode;
006
007import java.util.List;
008
009//------------------------------------------------------------------------------
010/**
011 * Represents an html select (<select>) tag.
012 *
013 * @author J. Alex Taylor, hairyfatguy.com
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class Select extends HTMLTagWithCoreEvents
037{
038   private String mSelectedValue;
039
040   //##########################################################################
041   // PRIVATE FIELDS
042   //##########################################################################
043
044
045   //##########################################################################
046   // CONSTRUCTORS
047   //##########################################################################
048
049   //--------------------------------------------------------------------------
050   public Select()
051   {
052      super(HTML.SELECT);
053   }
054
055   //--------------------------------------------------------------------------
056   public Select(String inName)
057   {
058      this();
059      setName(inName);
060   }
061
062   //--------------------------------------------------------------------------
063   public Select(XMLNode inXMLNode)
064   {
065      this();
066      initFromXMLNode(inXMLNode);
067   }
068
069   //##########################################################################
070   // PUBLIC METHODS
071   //##########################################################################
072
073   //--------------------------------------------------------------------------
074   public Option addOption(String inText)
075   {
076      Option option = new Option(inText);
077      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
078      addSubtag(option);
079
080      return option;
081   }
082
083   //--------------------------------------------------------------------------
084   public Option addOption(int inText)
085   {
086      Option option = new Option(inText);
087      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
088      addSubtag(option);
089
090      return option;
091   }
092
093   //--------------------------------------------------------------------------
094   public Option addOption(long inText)
095   {
096      Option option = new Option(inText);
097      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
098      addSubtag(option);
099
100      return option;
101   }
102
103   //--------------------------------------------------------------------------
104   public Option addOption(float inText)
105   {
106      Option option = new Option(inText);
107      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
108      addSubtag(option);
109
110      return option;
111   }
112
113   //--------------------------------------------------------------------------
114   public Option addOption(double inText)
115   {
116      Option option = new Option(inText);
117      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
118      addSubtag(option);
119
120      return option;
121   }
122
123   //--------------------------------------------------------------------------
124   public Option addOption(String inText, String inValue)
125   {
126      Option option = new Option(inText, inValue);
127      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
128      addSubtag(option);
129
130      return option;
131   }
132
133   //--------------------------------------------------------------------------
134   public Option addOption(String inText, int inValue)
135   {
136      Option option = new Option(inText, inValue);
137      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
138      addSubtag(option);
139
140      return option;
141   }
142
143   //--------------------------------------------------------------------------
144   public Option addOption(String inText, long inValue)
145   {
146      Option option = new Option(inText, inValue);
147      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
148      addSubtag(option);
149
150      return option;
151   }
152
153   //--------------------------------------------------------------------------
154   public Option addOption(String inText, float inValue)
155   {
156      Option option = new Option(inText, inValue);
157      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
158      addSubtag(option);
159
160      return option;
161   }
162
163   //--------------------------------------------------------------------------
164   public Option addOption(String inText, double inValue)
165   {
166      Option option = new Option(inText, inValue);
167      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
168      addSubtag(option);
169
170      return option;
171   }
172
173   //--------------------------------------------------------------------------
174   public Option addOption(String inText, Object inValue)
175   {
176      Option option = new Option(inText, inValue);
177      if (mSelectedValue != null) option.setSelected(option.getValue().equals(mSelectedValue));
178      addSubtag(option);
179
180      return option;
181   }
182
183   //--------------------------------------------------------------------------
184   public Option addOption(String inText, boolean inSelected)
185   {
186      Option option = new Option(inText, inSelected);
187      addSubtag(option);
188
189      return option;
190   }
191
192   //--------------------------------------------------------------------------
193   public Option addOption(String inText, String inValue, boolean inSelected)
194   {
195      Option option = new Option(inText, inValue, inSelected);
196      addSubtag(option);
197
198      return option;
199   }
200
201   //--------------------------------------------------------------------------
202   public Option addOption(String inText, int inValue, boolean inSelected)
203   {
204      Option option = new Option(inText, inValue, inSelected);
205      addSubtag(option);
206
207      return option;
208   }
209
210   //--------------------------------------------------------------------------
211   public Option addOption(String inText, long inValue, boolean inSelected)
212   {
213      Option option = new Option(inText, inValue, inSelected);
214      addSubtag(option);
215
216      return option;
217   }
218
219   //--------------------------------------------------------------------------
220   public Option addOption(String inText, float inValue, boolean inSelected)
221   {
222      Option option = new Option(inText, inValue, inSelected);
223      addSubtag(option);
224
225      return option;
226   }
227
228   //--------------------------------------------------------------------------
229   public Option addOption(String inText, double inValue, boolean inSelected)
230   {
231      Option option = new Option(inText, inValue, inSelected);
232      addSubtag(option);
233
234      return option;
235   }
236
237   //--------------------------------------------------------------------------
238   public Option addOption(String inText, Object inValue, boolean inSelected)
239   {
240      Option option = new Option(inText, inValue, inSelected);
241      addSubtag(option);
242
243      return option;
244   }
245
246   //--------------------------------------------------------------------------
247   public List<Option> getOptions()
248   {
249      return (List<Option>) (Object) getSubtagsByName(HTML.OPTION);
250   }
251
252   //--------------------------------------------------------------------------
253   public int getNumOptions()
254   {
255      List<HTMLTag> optionTags = getSubtagsByName(HTML.OPTION);
256      return (optionTags != null ? optionTags.size() : 0);
257   }
258
259   //--------------------------------------------------------------------------
260   public OptGroup addOptGroup(String inLabel)
261   {
262      OptGroup optgroup = new OptGroup(inLabel);
263      addSubtag(optgroup);
264
265      return optgroup;
266   }
267 
268   //--------------------------------------------------------------------------
269   public Select setSelectedValue(String inValue)
270   {
271      mSelectedValue = inValue != null ? inValue : "";
272
273      List<XMLNode> options = getSubtagsByName(HTML.OPTION);
274      if (CollectionUtil.hasValues(options))
275      {
276         for (XMLNode optionTag : options)
277         {
278            Option option = (Option) optionTag;
279            option.setSelected(option.getValue().equals(mSelectedValue));
280         }
281      }
282
283      return this;
284   }
285
286   //--------------------------------------------------------------------------
287   public Select setName(String inValue)
288   {
289      setAttribute(HTML.NAME, inValue);
290      return this;
291   }
292
293   //--------------------------------------------------------------------------
294   public String getName()
295   {
296      return getAttributeValue(HTML.NAME);
297   }
298
299   //--------------------------------------------------------------------------
300   public Select setIsMultiple(boolean inValue)
301   {
302      if (inValue)
303      {
304         setAttribute(HTML.MULTIPLE, "true");
305      }
306      else
307      {
308         removeAttribute(HTML.MULTIPLE);
309      }
310
311      return this;
312   }
313
314   //--------------------------------------------------------------------------
315   public boolean isMultiple()
316   {
317      return hasAttribute(HTML.MULTIPLE);
318   }
319
320   //--------------------------------------------------------------------------
321   public Select setSize(int inValue)
322   {
323      setAttribute(HTML.SIZE, Integer.toString(inValue));
324
325      return this;
326   }
327
328   //--------------------------------------------------------------------------
329   public String getSize()
330   {
331      return getAttributeValue(HTML.SIZE);
332   }
333
334
335   //--------------------------------------------------------------------------
336   public Select setDisabled(boolean inValue)
337   {
338      if (inValue)
339      {
340         setAttribute(HTML.DISABLED, "1");
341      }
342      else
343      {
344         removeAttribute(HTML.DISABLED);
345      }
346
347      return this;
348   }
349
350   //--------------------------------------------------------------------------
351   public boolean isDisabled()
352   {
353      return hasAttribute(HTML.DISABLED);
354   }
355
356
357   //--------------------------------------------------------------------------
358   public Select setRequired(boolean inValue)
359   {
360      if (inValue)
361      {
362         setAttribute(HTML.REQUIRED, "1");
363      }
364      else
365      {
366         removeAttribute(HTML.REQUIRED);
367      }
368
369      return this;
370   }
371
372   //--------------------------------------------------------------------------
373   public boolean isRequired()
374   {
375      return hasAttribute(HTML.REQUIRED);
376   }
377
378
379   //--------------------------------------------------------------------------
380   public Select setOnBlur(String inValue)
381   {
382      setAttribute(HTML.ONBLUR, inValue);
383
384      return this;
385   }
386
387   //--------------------------------------------------------------------------
388   public Select setOnChange(String inValue)
389   {
390      setAttribute(HTML.ONCHANGE, inValue);
391
392      return this;
393   }
394
395   //--------------------------------------------------------------------------
396   public Select setOnFocus(String inValue)
397   {
398      setAttribute(HTML.ONFOCUS, inValue);
399
400      return this;
401   }
402
403
404   // Overrides for HTMLTag setters to allow method chaining.
405
406   //--------------------------------------------------------------------------
407   @Override
408   public Select addClass(String inValue)
409   {
410      return (Select) super.addClass(inValue);
411   }
412
413   //--------------------------------------------------------------------------
414   @Override
415   public Select setClass(String inValue)
416   {
417      return (Select) super.setClass(inValue);
418   }
419
420   //--------------------------------------------------------------------------
421   @Override
422   public Select setId(String inValue)
423   {
424      return (Select) super.setId(inValue);
425   }
426
427   //--------------------------------------------------------------------------
428   @Override
429   public Select setStyle(CharSequence inValue)
430   {
431      return (Select) super.setStyle(inValue);
432   }
433
434   //--------------------------------------------------------------------------
435   @Override
436   public Select addStyle(String inValue)
437   {
438      return (Select) super.addStyle(inValue);
439   }
440
441   // Overrides for HTMLTagWithCoreEvents setters to allow method chaining.
442
443   //--------------------------------------------------------------------------
444   @Override
445   public Select setOnClick(String inValue)
446   {
447      return (Select) super.setOnClick(inValue);
448   }
449
450   //--------------------------------------------------------------------------
451   @Override
452   public Select setOnDblClick(String inValue)
453   {
454      return (Select) super.setOnDblClick(inValue);
455   }
456
457   //--------------------------------------------------------------------------
458   @Override
459   public Select setOnMouseDown(String inValue)
460   {
461      return (Select) super.setOnMouseDown(inValue);
462   }
463
464   //--------------------------------------------------------------------------
465   @Override
466   public Select setOnMouseMove(String inValue)
467   {
468      return (Select) super.setOnMouseMove(inValue);
469   }
470
471   //--------------------------------------------------------------------------
472   @Override
473   public Select appendToOnMouseOut(String inValue)
474   {
475      return (Select) super.appendToOnMouseOut(inValue);
476   }
477
478   //--------------------------------------------------------------------------
479   @Override
480   public Select setOnMouseOut(String inValue)
481   {
482      return (Select) super.setOnMouseOut(inValue);
483   }
484
485   //--------------------------------------------------------------------------
486   @Override
487   public Select appendToOnMouseOver(String inValue)
488   {
489      return (Select) super.appendToOnMouseOver(inValue);
490   }
491
492   //--------------------------------------------------------------------------
493   @Override
494   public Select setOnMouseOver(String inValue)
495   {
496      return (Select) super.setOnMouseOver(inValue);
497   }
498
499   //--------------------------------------------------------------------------
500   @Override
501   public Select setOnMouseUp(String inValue)
502   {
503      return (Select) super.setOnMouseUp(inValue);
504   }
505
506   //--------------------------------------------------------------------------
507   @Override
508   public Select setOnKeyDown(String inValue)
509   {
510      return (Select) super.setOnKeyDown(inValue);
511   }
512
513   //--------------------------------------------------------------------------
514   @Override
515   public Select setOnKeyPress(String inValue)
516   {
517      return (Select) super.setOnKeyPress(inValue);
518   }
519
520   //--------------------------------------------------------------------------
521   @Override
522   public Select setOnKeyUp(String inValue)
523   {
524      return (Select) super.setOnKeyUp(inValue);
525   }
526}