001package com.hfg.html.custom;
002
003import com.hfg.html.HTMLTag;
004import com.hfg.html.InputCheckbox;
005import com.hfg.html.InputRadio;
006import com.hfg.html.Span;
007import com.hfg.html.attribute.HTMLColor;
008
009//------------------------------------------------------------------------------
010/**
011 Container for a checkbox or radio button plus its label. Clicking on the label
012 checks / unchecks the checkbox.
013 <div>
014 @author J. Alex Taylor, hairyfatguy.com
015 </div>
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037
038public class CheckboxSpan extends Span
039{
040   private InputCheckbox mCheckbox;
041   private InputRadio    mRadioBtn;
042   private String        mDisabledClass;
043
044   //##########################################################################
045   // CONSTRUCTORS
046   //##########################################################################
047
048   //--------------------------------------------------------------------------
049   public CheckboxSpan(InputCheckbox inCheckbox, String inLabel)
050   {
051      super();
052
053      mCheckbox = inCheckbox;
054      addSubtag(inCheckbox.appendToOnClick("event.cancelBubble=true;"));
055      addSpan(inLabel).setStyle("margin-left: 5px;");
056      appendToOnClick("this.children[0].click();");
057   }
058
059   //--------------------------------------------------------------------------
060   public CheckboxSpan(InputCheckbox inCheckbox, HTMLTag inLabel)
061   {
062      super();
063
064      mCheckbox = inCheckbox;
065      addSubtag(inCheckbox.appendToOnClick("event.cancelBubble=true;"));
066      addSubtag(inLabel.addStyle("margin-left: 5px;"));
067      appendToOnClick("this.children[0].click();");
068   }
069
070   //--------------------------------------------------------------------------
071   public CheckboxSpan(InputRadio inRadioButton, String inLabel)
072   {
073      super();
074
075      mRadioBtn = inRadioButton;
076      addSubtag(inRadioButton.appendToOnClick("event.cancelBubble=true;"));
077      addSpan(inLabel).setStyle("margin-left: 5px;");
078      appendToOnClick("this.children[0].click();");
079   }
080
081   //--------------------------------------------------------------------------
082   public CheckboxSpan(InputRadio inRadioButton, HTMLTag inLabel)
083   {
084      super();
085
086      mRadioBtn = inRadioButton;
087      addSubtag(inRadioButton.appendToOnClick("event.cancelBubble=true;"));
088      addSubtag(inLabel.addStyle("margin-left: 5px;"));
089      appendToOnClick("this.children[0].click();");
090   }
091
092
093   //##########################################################################
094   // PUBLIC METHODS
095   //##########################################################################
096
097   //--------------------------------------------------------------------------
098   @Override
099   public CheckboxSpan addClass(String inValue)
100   {
101      return (CheckboxSpan) super.addClass(inValue);
102   }
103
104   //--------------------------------------------------------------------------
105   @Override
106   public CheckboxSpan addStyle(String inValue)
107   {
108      return (CheckboxSpan) super.addStyle(inValue);
109   }
110
111   //--------------------------------------------------------------------------
112   public CheckboxSpan setDisabledClass(String inValue)
113   {
114      mDisabledClass = inValue;
115      return this;
116   }
117
118   //--------------------------------------------------------------------------
119   public String getDisabledClass()
120   {
121      return mDisabledClass;
122   }
123
124   //--------------------------------------------------------------------------
125   public CheckboxSpan setDisabled(boolean inValue)
126   {
127      if (mCheckbox != null)
128      {
129         mCheckbox.setDisabled(inValue);
130      }
131
132      if (mRadioBtn != null)
133      {
134         mRadioBtn.setDisabled(inValue);
135      }
136
137      if (mDisabledClass != null)
138      {
139         if (inValue)
140         {
141            addClass(mDisabledClass);
142         }
143         else
144         {
145            removeClass(mDisabledClass);
146         }
147      }
148      else
149      {
150         setStyleColor(inValue ? HTMLColor.DARK_GRAY : null);
151      }
152
153      return this;
154   }
155
156   //--------------------------------------------------------------------------
157   public CheckboxSpan setOnChange(String inValue)
158   {
159      if (mCheckbox != null)
160      {
161         mCheckbox.setOnChange(inValue);
162      }
163      else if (mRadioBtn != null)
164      {
165         mRadioBtn.setOnChange(inValue);
166      }
167
168      return this;
169   }
170
171}