View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.command.add;
19  
20  import org.vectomatic.dom.svg.OMSVGCircleElement;
21  import org.vectomatic.dom.svg.OMSVGPoint;
22  import org.vectomatic.dom.svg.impl.SVGElement;
23  import org.vectomatic.dom.svg.utils.SVGConstants;
24  import org.vectomatic.svg.edit.client.SvgrealApp;
25  import org.vectomatic.svg.edit.client.command.FactoryInstantiatorBase;
26  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
27  import org.vectomatic.svg.edit.client.event.MouseDownProcessor;
28  import org.vectomatic.svg.edit.client.event.MouseMoveProcessor;
29  import org.vectomatic.svg.edit.client.event.MouseUpProcessor;
30  import org.vectomatic.svg.edit.client.model.ModelConstants;
31  
32  import com.google.gwt.core.client.GWT;
33  import com.google.gwt.event.dom.client.MouseDownEvent;
34  import com.google.gwt.event.dom.client.MouseMoveEvent;
35  import com.google.gwt.event.dom.client.MouseUpEvent;
36  
37  /**
38   * Command factory to add new circles to the the SVG model.
39   * @author laaglu
40   */
41  public class AddCircleCommandFactory extends AddCommandFactoryBase implements MouseDownProcessor, MouseMoveProcessor, MouseUpProcessor {
42  	@SuppressWarnings("serial")
43  	public static final IFactoryInstantiator<AddCircleCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<AddCircleCommandFactory>(ModelConstants.INSTANCE.addCircleCmdFactory(), ModelConstants.INSTANCE.addCircleCmdFactoryDesc()) {
44  		@Override
45  		public AddCircleCommandFactory create() {
46  			return new AddCircleCommandFactory();
47  		}
48  	};
49  	
50  	/**
51  	 * The new circle element
52  	 */
53  	protected OMSVGCircleElement circle;
54  	/**
55  	 * The circle center
56  	 */
57  	protected OMSVGPoint c;
58  	
59  	@Override
60  	public IFactoryInstantiator<?> getInstantiator() {
61  		return INSTANTIATOR;
62  	}
63  	
64  	@Override
65  	public void start(Object requester) {
66  		GWT.log("AddCircleCommandFactory.start(" + requester + ")");
67  		super.start(requester);
68  		updateStatus(ModelConstants.INSTANCE.addCircleCmdFactory1());
69  	}
70  
71  	@Override
72  	public boolean processMouseDown(MouseDownEvent event) {
73  		if (owner != null && circle != null) {
74  			owner.getTwinGroup().removeChild(circle);
75  		}
76  		updateStatus(ModelConstants.INSTANCE.addCircleCmdFactory2());
77  		owner = SvgrealApp.getApp().getActiveModel();
78  		c = owner.getCoordinates(event, true);
79  		circle = new OMSVGCircleElement(c.getX(), c.getY(), 0);
80  		applyCssContextStyle((SVGElement) circle.getElement().cast());
81  		circle.getStyle().setSVGProperty(SVGConstants.CSS_VISIBILITY_PROPERTY, SVGConstants.CSS_VISIBLE_VALUE);
82  		owner.getTwinGroup().appendChild(circle);
83  		return true;
84  	}
85  	
86  	@Override
87  	public boolean processMouseUp(MouseUpEvent event) {
88  		if (owner != null) {
89  			if (circle.getR().getBaseVal().getValue() == 0) {
90  				owner.getTwinGroup().removeChild(circle);
91  			} else {
92  				createCommand(circle);
93  			}
94  			circle = null;
95  			owner = null;
96  			updateStatus(ModelConstants.INSTANCE.addCircleCmdFactory1());
97  		}
98  		return true;
99  	}
100 	
101 	@Override
102 	public boolean processMouseMove(MouseMoveEvent event) {
103 		if (owner != null) {
104 			OMSVGPoint p = owner.getCoordinates(event, true);
105 			circle.getR().getBaseVal().setValue(p.substract(c).length());
106 		}
107 		return true;
108 	}
109 }