001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui.vect.draw;
021 
022 import js.Js;
023 import jsx.Configurable;
024 import jsx.core.ObjectLikes;
025 import jsx.ui.Component;
026 import jsx.ui.dd.Mouse;
027 import jsx.ui.dd.event.DragMove;
028 import jsx.ui.dd.event.DragStart;
029 import jsx.ui.dd.event.DragStop;
030 import jsx.ui.event.Position;
031 import jsx.ui.vect.Graphic;
032 
033 /**
034  * <p>Defines a drawing wrapper that paints interactively.</p>
035  * <p>A drawing tool of this class wraps either a VML component for IE or a SVG component 
036  * for other browsers and paints in accordance with the {@link Mouse} handle to which it 
037  * listens mouse events.</p>
038  * <p>A {@link Paint} widget is {@link Configurable} and is also an event source which 
039  * may fire high level events.</p>
040  * 
041  * @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>
042  */
043 public class Paint extends Draw
044 {
045     /**
046      * <p>The default constructor that constructs a wrapper widget of this type.</p>
047      * <p>This constructor invokes the default constructor of the superclass to create 
048      * a default underlying graphic component.</p>
049      * @since 1.0
050      */
051     public Paint() {}
052 
053     /**
054      * <p>Performs an action on the dispatched event.</p>
055      * <p>This method invokes the overridden method of the superclass to initialize the 
056      * drawing tool and paints at the argument mouse position with a call to the method 
057      * {@link #paint(Paint, Position)} if the current drawing widget is in a drawing mode.</p>
058      * @param evt The event dispatched to this listener.
059      * @since 1.0
060      */
061     @Override
062     public void onEvent(DragStart evt) {
063         super.onEvent(evt);
064         if (Js.be(ini(this).var(START))) {
065             paint(this, evt);
066         }
067     }
068 
069     /**
070      * <p>Performs an action on the dispatched event.</p>
071      * <p>This method paints at the argument mouse position with a call to the method 
072      * {@link #paint(Paint, Position)} if the current drawing widget is in a drawing mode.</p>
073      * @param evt The event dispatched to this listener.
074      * @since 1.0
075      */
076     public void onEvent(DragMove evt) {
077         if (Js.be(ini(this).var(START))) {
078             paint(this, evt);
079         }
080     }
081 
082     /**
083      * <p>Paints a specified painting wrapper at a specified mouse position.</p>
084      * <p>A subclass may also call this method to meet its particular needs.</p>
085      * @param p A painting tool.
086      * @param evt A mouse position event.
087      * @since 1.0
088      */
089     protected static final void paint(Paint p, Position<?> evt) {
090         double x = ini(evt).var(Position.X).doubleValue() - ini(p).var(X);
091         double y = ini(evt).var(Position.Y).doubleValue() - ini(p).var(Y);
092         int r = 4;
093         Component e = Graphic.rect(
094                 x - 2,
095                 y - 2,
096                 r,
097                 r
098         );
099         Graphic.fill(e, "red");
100         Graphic.add(p, e);
101     }
102 
103     /**
104      * <p>Performs an action on the dispatched event.</p>
105      * <p>This method paints at the argument mouse position with a call to the method 
106      * {@link #paint(Paint, Position)} if the current drawing widget is in a drawing 
107      * mode and exits the drawing mode.</p>
108      * @param evt The event dispatched to this listener.
109      * @since 1.0
110      */
111     public void onEvent(DragStop evt) {
112         if (Js.be(ini(this).var(START))) {
113             ObjectLikes.delete(ini(this), START);
114             paint(this, evt);
115         }
116     }
117 }