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.ui.Component;
25 import jsx.ui.Control;
26 import jsx.ui.event.Click;
27 
28 /**
29  * <p>A base class for clickable widgets.</p>
30  * 
31  * @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>
32  */
33 public class Clickable extends Control
34 {
35     /**
36      * <p>Typically constructs a clickable widget.</p>
37      * @param html The HTML text of the control widget.
38      * @since 1.0
39      */
40     public Clickable(String html) {
41         this(new Component(html));
42     }
43 
44     private final static Id<Clickable> CLICKABLE = new Id<Clickable>();
45 
46     /**
47      * <p>Gets the {@link Clickable} widget associated with a component.</p>
48      * <p>If the specified component does not have an associated {@link Clickable} widget,
49      * this method will creates one based on that component and associate them.</p>
50      * @param e A component that may have a {@link Clickable} widget based on it.
51      * @return The {@link Clickable} widget associated with <tt>e</tt>.
52      * @since 1.0
53      */
54     public static Clickable get(Component e) {
55         Clickable c = ini(Control.get(e)).var(CLICKABLE);
56         return Js.be(c) ? c : new Clickable(e);
57     }
58 
59     /**
60      * <p>Constructs a clickable widget.</p>
61      * @param e The underlying component for the widget.
62      * @since 1.0
63      */
64     protected Clickable(Component e) {
65         super(e);
66         sub(Control.get(e), this, CLICKABLE);
67     }
68 
69     /**
70      * <p>Initializes the current widget for the newly rendered underlying HTML element.</p>
71      * <p>This method firstly invokes the overridden method of super class for the "focus" and "blur"
72      * to work and then makes the widget fires {@link Click} event from itself on a browser
73      * "click" event over the underlying HTML element.</p>
74      * @since 1.0
75      */
76     @Override
77     protected void init() {
78         super.init();
79         attachEvent("click", new Click(unwrap()), PREVENTCLICK);
80     }
81 }