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