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.canvas;
21 
22 import js.Js;
23 import jsx.Configurable;
24 import jsx.core.ObjectLikes;
25 import jsx.ui.dd.Mouse;
26 import jsx.ui.dd.event.DragMove;
27 import jsx.ui.dd.event.DragStop;
28 import jsx.ui.vect.Canvas;
29 
30 /**
31  * <p>Defines a drawing wrapper that interactively draws lines on a wrapped canvas with 
32  * the help of a cached draft canvas.</p>
33  * <p>A drawing tool of this class wraps a {@link Canvas} component and draws lines in 
34  * accordance with the {@link Mouse} handle to which it listens mouse events but shows 
35  * the intermediate drawings on the draft canvas.</p>
36  * <p>A {@link CanvasLine} widget is {@link Configurable} and is also an event source 
37  * which 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 CanvasLine extends CanvasDraft
42 {
43     /**
44      * <p>A typical constructor that constructs a wrapper widget of this type and forces 
45      * constructors of subclasses to pass a {@link Canvas} widget.</p>
46      * <p>This constructor invokes the typical constructor of the superclass passing 
47      * the component wrapped by the specified {@link Canvas} widget that is set to the 
48      * configurable property {@link CanvasDraw#CANVAS} of the widget to be constructed.</p>
49      * @param c A canvas widget.
50      * @since 1.0
51      */
52     public CanvasLine(Canvas c) {
53         super(c);
54     }
55 
56     /**
57      * <p>Performs an action on the dispatched event.</p>
58      * <p>This method invokes the overridden method of the superclass to clear the draft 
59      * canvas. If the current canvas drawing tool is in a drawing mode this method draws 
60      * a line on the draft canvas from the cached mouse press event to the currently 
61      * specified mouse move event.</p>
62      * @param evt The event dispatched to this listener.
63      * @since 1.0
64      */
65     @Override
66     public void onEvent(DragMove evt) {
67         super.onEvent(evt);
68         if (Js.be(ini(this).var(START))) {
69             Canvas t = getDraft(this);
70             Canvas.beginPath(t);
71             moveTo(this, t, ini(this).var(PRESS));
72             lineTo(this, t, evt);
73         }
74     }
75 
76     /**
77      * <p>Performs an action on the dispatched event.</p>
78      * <p>This method invokes the overridden method of the superclass to delete the draft 
79      * canvas. If the current canvas drawing tool is in a drawing mode this method draws 
80      * a line on the wrapped canvas from the cached mouse press event to the currently 
81      * specified mouse release event, and exits the drawing mode.</p>
82      * @param evt The event dispatched to this listener.
83      * @since 1.0
84      */
85     @Override
86     public void onEvent(DragStop evt) {
87         super.onEvent(evt);
88         if (Js.be(ini(this).var(START))) {
89             ObjectLikes.delete(ini(this), START);
90             lineTo(this, canvas(), evt);
91         }
92     }
93 }