01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.ui.ctrl;
21 
22 import js.Id;
23 import js.Js;
24 import jsx.dom.Markups;
25 import jsx.ui.Component;
26 import jsx.ui.event.Click;
27 import jsx.ui.event.Uncheck;
28 
29 /**
30  * <p>A base class for uncheckable widgets resembling HTML check-boxes.</p>
31  * <p>An uncheckable widget is firstly a {@link Checkable} that fires and listens to high level events.</p>
32  * 
33  * @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>
34  */
35 public class Uncheckable extends Checkable
36 {
37     /**
38      * <p>Constructs an uncheckable widget.</p>
39      * <p>This constructor invokes the typical constructor {@link #Uncheckable(Component)}.</p>
40      * @param label The label of the uncheckable.
41      * @since 1.0
42      */
43     public Uncheckable(String label) {
44         this(new Component(Markups.span(label)));
45     }
46 
47     /**
48      * <p>Typically constructs an uncheckable widget.</p>
49      * <p>This constructor invokes super constructor with the component as argument.</p>
50      * @param e The underlying component for the widget.
51      * @since 1.0
52      */
53     protected Uncheckable(Component e) {
54         super(e);
55         sub(Checkable.get(e), this, UNCHECKABLE);
56     }
57 
58     private final static Id<Uncheckable> UNCHECKABLE = new Id<Uncheckable>();
59 
60     /**
61      * <p>Gets the {@link Uncheckable} widget associated with a component.</p>
62      * <p>If the specified component does not have an associated {@link Uncheckable} widget,
63      * this method will creates one based on that component and associate them.</p>
64      * @param e A component that may have a {@link Uncheckable} widget based on it.
65      * @return The {@link Uncheckable} widget associated with <tt>e</tt>.
66      * @since 1.0
67      */
68     public static Uncheckable get(Component e) {
69         Uncheckable c = ini(Checkable.get(e)).var(UNCHECKABLE);
70         return Js.be(c) ? c : new Uncheckable(e);
71     }
72 
73     /**
74      * <p>Performs an action on the dispatched event.</p>
75      * <p>This method fires {@link Uncheck} event from itself if it is checked; Otherwise, it
76      * simply invokes the super listener with the dispatched event as argument.</p>
77      * @param evt The event dispatched to this listener.
78      * @since 1.0
79      */
80     @Override
81     public void onEvent(Click evt) {
82         if (checked()) {
83             exec(new Uncheck(unwrap()));
84         } else {
85             super.onEvent(evt);
86         }
87     }
88 }