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.vect.draw;
21 
22 import js.Id;
23 import js.Js;
24 import js.ObjectLike;
25 import jsx.Configurable;
26 import jsx.core.ObjectLikes;
27 import jsx.ui.dd.Mouse;
28 import jsx.ui.dd.event.DragMove;
29 import jsx.ui.dd.event.DragStop;
30 
31 /**
32  * <p>Defines a drawing wrapper that sketches interactively.</p>
33  * <p>A drawing tool of this class wraps either a VML component for IE or a SVG component 
34  * for other browsers and sketches in accordance with the {@link Mouse} handle to which it 
35  * listens mouse events.</p>
36  * <p>A {@link Sketch} widget is {@link Configurable} and is also an event source which 
37  * may fire high level events.</p>
38  * 
39  * @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>
40  */
41 public class Sketch extends Draw
42 {
43     private final static Id<DragMove> LAST = new Id<DragMove>();
44 
45     /**
46      * <p>The default constructor that constructs a wrapper widget of this type.</p>
47      * <p>This constructor invokes the default constructor of the superclass to create 
48      * a default underlying graphic component.</p>
49      * @since 1.0
50      */
51     public Sketch() {}
52 
53     /**
54      * <p>Performs an action on the dispatched event.</p>
55      * <p>This method sketches to the argument mouse position if the current drawing 
56      * widget is in a drawing mode.</p>
57      * @param evt The event dispatched to this listener.
58      * @since 1.0
59      */
60     public void onEvent(DragMove evt) {
61         ObjectLike ini = ini(this);
62         if (Js.be(ini.var(START))) {
63             DragMove last = ini.var(LAST);
64             if (Js.be(last)) {
65                 line(this, last, evt);
66             } else {
67                 line(this, ini.var(START), evt);
68             }
69             ini.var(LAST, evt);
70         }
71     }
72 
73     /**
74      * <p>Performs an action on the dispatched event.</p>
75      * <p>This method sketches to the argument mouse position if the current drawing 
76      * widget is in a drawing mode and then exits the drawing mode.</p>
77      * @param evt The event dispatched to this listener.
78      * @since 1.0
79      */
80     public void onEvent(DragStop evt) {
81         ObjectLike ini = ini(this);
82         if (Js.be(ini.var(START))) {
83             DragMove last = ini.var(LAST);
84             if (Js.be(last)) {
85                 line(this, last, evt);
86             } else {
87                 line(this, ini.var(START), evt);
88             }
89             ObjectLikes.delete(ini, START);
90             ObjectLikes.delete(ini, LAST);
91         }
92     }
93 }