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.OMSVGElement;
21  import org.vectomatic.dom.svg.OMSVGEllipseElement;
22  import org.vectomatic.dom.svg.OMSVGPoint;
23  import org.vectomatic.dom.svg.OMSVGSVGElement;
24  import org.vectomatic.dom.svg.OMSVGTransform;
25  import org.vectomatic.dom.svg.impl.SVGElement;
26  import org.vectomatic.dom.svg.utils.SVGConstants;
27  import org.vectomatic.svg.edit.client.SvgrealApp;
28  import org.vectomatic.svg.edit.client.command.FactoryInstantiatorBase;
29  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
30  import org.vectomatic.svg.edit.client.event.MouseDownProcessor;
31  import org.vectomatic.svg.edit.client.event.MouseMoveProcessor;
32  import org.vectomatic.svg.edit.client.event.MouseUpProcessor;
33  import org.vectomatic.svg.edit.client.model.ModelConstants;
34  
35  import com.google.gwt.core.client.GWT;
36  import com.google.gwt.event.dom.client.MouseDownEvent;
37  import com.google.gwt.event.dom.client.MouseMoveEvent;
38  import com.google.gwt.event.dom.client.MouseUpEvent;
39  
40  /**
41   * Command factory to add new ellipses to the the SVG model.
42   * @author laaglu
43   */
44  public class AddEllipseCommandFactory extends AddCommandFactoryBase implements MouseDownProcessor, MouseMoveProcessor, MouseUpProcessor {
45  	@SuppressWarnings("serial")
46  	public static final IFactoryInstantiator<AddEllipseCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<AddEllipseCommandFactory>(ModelConstants.INSTANCE.addEllipseCmdFactory(), ModelConstants.INSTANCE.addEllipseCmdFactoryDesc()) {
47  		@Override
48  		public AddEllipseCommandFactory create() {
49  			return new AddEllipseCommandFactory();
50  		}
51  	};
52  
53  	/**
54  	 * The new ellipse element
55  	 */
56  	protected OMSVGEllipseElement ellipse;
57  	/**
58  	 * The ellipse center
59  	 */
60  	protected OMSVGPoint c;
61  	/**
62  	 * The ellipse corner
63  	 */
64  	protected OMSVGPoint p;
65  
66  	@Override
67  	public IFactoryInstantiator<?> getInstantiator() {
68  		return INSTANTIATOR;
69  	}
70  	
71   	@Override
72  	public void start(Object requester) {
73  		GWT.log("AddEllipseCommandFactory.start(" + requester + ")");
74  		super.start(requester);
75  		updateStatus(ModelConstants.INSTANCE.addEllipseCmdFactory1());
76  	}
77  
78  	@Override
79  	public boolean processMouseDown(MouseDownEvent event) {
80  		if (owner != null && ellipse != null) {
81  			owner.getTwinGroup().removeChild(ellipse);
82  		}
83  		updateStatus(ModelConstants.INSTANCE.addEllipseCmdFactory2());
84  		owner = SvgrealApp.getApp().getActiveModel();
85  		OMSVGElement parent = owner.getTwinGroup();
86  		OMSVGSVGElement svg = parent.getOwnerSVGElement();
87  		c = owner.getCoordinates(event, true);
88  		p = owner.getSvgElement().createSVGPoint(c);
89  		ellipse = new OMSVGEllipseElement(c.getX(), c.getY(), 0, 0);
90  		OMSVGTransform xform = svg.createSVGTransform();
91  		xform.setRotate(-owner.getRotation(), c.getX(), c.getY());
92  		ellipse.getTransform().getBaseVal().appendItem(xform);
93  		applyCssContextStyle((SVGElement) ellipse.getElement().cast());
94  		ellipse.getStyle().setSVGProperty(SVGConstants.CSS_VISIBILITY_PROPERTY, SVGConstants.CSS_VISIBLE_VALUE);
95  		parent.appendChild(ellipse);
96  		return true;
97  	}
98  	
99  	@Override
100 	public boolean processMouseUp(MouseUpEvent event) {
101 		if (owner != null) {
102 			updateStatus(ModelConstants.INSTANCE.addEllipseCmdFactory1());
103 			if (ellipse.getRx().getBaseVal().getValue() == 0 || ellipse.getRy().getBaseVal().getValue() == 0) {
104 				owner.getTwinGroup().removeChild(ellipse);
105 			} else {
106 				createCommand(ellipse);
107 			}
108 			ellipse = null;
109 			owner = null;
110 		}
111 		return true;
112 	}
113 	
114 	@Override
115 	public boolean processMouseMove(MouseMoveEvent event) {
116 		if (owner != null) {
117 			p = owner.getCoordinates(event, true);
118 			float xmin = Math.min(c.getX(), p.getX());
119 			float ymin = Math.min(c.getY(), p.getY());
120 			float xmax = Math.max(c.getX(), p.getX());
121 			float ymax = Math.max(c.getY(), p.getY());
122 			ellipse.getRx().getBaseVal().setValue(xmax - xmin);
123 			ellipse.getRy().getBaseVal().setValue(ymax - ymin);
124 		}
125 		return true;
126 	}
127 }