001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui;
021 
022 import js.Id;
023 import js.Js;
024 import jsx.Configurable;
025 import jsx.ui.Component;
026 import jsx.ui.ctrl.Checkable;
027 import jsx.ui.event.Check;
028 import jsx.ui.event.OnCheck;
029 import jsx.ui.event.OnUncheck;
030 import jsx.ui.event.Uncheck;
031 
032 /**
033  * <p>A base class for toggle widgets.</p>
034  * <p>A toggle widget is a {@link Widget} that toggles its display state by listening to a 
035  * {@link Checkable} for {@link Check} and {@link Uncheck} events.</p>
036  * <p>A {@link Toggle} widget is {@link Configurable} and is also an event source which 
037  * fires {@link Widget.Event} events. It is meanwhile an event listener that handles 
038  * events fired from its underlying component.</p>
039  * 
040  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
041  */
042 public class Toggle extends Widget implements OnCheck, OnUncheck
043 {
044     private final static Id<Toggle> TOGGLE = new Id<Toggle>();
045 
046     /**
047      * <p>Gets the toggle widget from a component.</p>
048      * @param e A component to toggle.
049      * @return The toggle widget associate with the component.
050      * @since 1.0
051      */
052     public static final Toggle get(Component e) {
053         Toggle toggle = ini(e).var(TOGGLE);
054         if (Js.not(toggle)) {
055             toggle = new Toggle(e);
056             ini(e).var(TOGGLE, toggle);
057         }
058         return toggle;
059     }
060 
061     /**
062      * <p>Typically constructs a toggle widget.</p>
063      * <p>This constructor associates this widget with the component.</p>
064      * @param e A component to toggle.
065      * @since 1.0
066      */
067     protected Toggle(Component e) {
068         super(e);
069         ini(e).var(TOGGLE, this);
070     }
071 
072     /**
073      * <p>Listens to a checkable widget.</p>
074      * <p>This adds listeners to listen to the checkable widget for {@link Check} 
075      * and {@link Uncheck} events.</p>
076      * @param c A checkable widget.
077      * @since 1.0
078      */
079     public void listen(Checkable c) {
080         c.addListener(Check.class, this);
081         c.addListener(Uncheck.class, this);
082     }
083 
084     /**
085      * <p>Stops listening to a checkable widget.</p>
086      * <p>This removes listeners for listening to the checkable widget for {@link Check} 
087      * and {@link Uncheck} events.</p>
088      * @param c A checkable widget.
089      * @since 1.0
090      */
091     public void unlisten(Checkable c) {
092         c.removeListener(Check.class, this);
093         c.removeListener(Uncheck.class, this);
094     }
095 
096     /**
097      * <p>Performs an action on the dispatched event.</p>
098      * <p>This method sets up mouse event handlers for the underlying HTML element on the 
099      * browser level.</p>
100      * @param evt The event dispatched to this listener.
101      * @since 1.0
102      */
103     public void onEvent(Uncheck evt) {
104         unwrap().pseudo(HIDE, "hide").getAdder().invoke();
105     }
106 
107     /**
108      * <p>Performs an action on the dispatched event.</p>
109      * <p>This method sets up mouse event handlers for the underlying HTML element on the 
110      * browser level.</p>
111      * @param evt The event dispatched to this listener.
112      * @since 1.0
113      */
114     public void onEvent(Check evt) {
115         unwrap().pseudo(HIDE, "hide").getRemover().invoke();
116     }
117 }