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.OMSVGPointList;
22  import org.vectomatic.dom.svg.OMSVGPolygonElement;
23  import org.vectomatic.dom.svg.impl.SVGElement;
24  import org.vectomatic.dom.svg.utils.SVGConstants;
25  import org.vectomatic.svg.edit.client.SvgrealApp;
26  import org.vectomatic.svg.edit.client.command.FactoryInstantiatorBase;
27  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
28  import org.vectomatic.svg.edit.client.event.MouseDownProcessor;
29  import org.vectomatic.svg.edit.client.event.MouseMoveProcessor;
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  
36  /**
37   * Command factory to add new polygons to the the SVG model.
38   * @author laaglu
39   */
40  public class AddPolygonCommandFactory extends AddCommandFactoryBase implements MouseDownProcessor, MouseMoveProcessor {
41  	@SuppressWarnings("serial")
42  	public static final IFactoryInstantiator<AddPolygonCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<AddPolygonCommandFactory>(ModelConstants.INSTANCE.addPolygonCmdFactory(), ModelConstants.INSTANCE.addPolygonCmdFactoryDesc()) {
43  		@Override
44  		public AddPolygonCommandFactory create() {
45  			return new AddPolygonCommandFactory();
46  		}
47  	};
48  
49  	/**
50  	 * The new polygon element
51  	 */
52  	protected OMSVGPolygonElement polygon;
53  	protected OMSVGPointList points;
54  
55  	
56  	@Override
57  	public IFactoryInstantiator<?> getInstantiator() {
58  		return INSTANTIATOR;
59  	}
60  
61  	@Override
62  	public void start(Object requester) {
63  		GWT.log("AddPolygonCommandFactory.start(" + requester + ")");
64  		super.start(requester);
65  		updateStatus(ModelConstants.INSTANCE.addPolygonCmdFactory1());
66  	}
67  
68  	@Override
69  	public void stop() {
70  		GWT.log("AddPolygonCommandFactory.stop()");
71  		if (polygon != null) {
72  			OMSVGElement parent = owner.getTwinGroup();
73  			if (polygon != null) {
74  				parent.removeChild(polygon);		
75  				polygon = null;
76  			}
77  			points = null;
78  			owner = null;
79  		}
80  		super.stop();
81  	}
82  
83  
84  	@Override
85  	public boolean processMouseDown(MouseDownEvent event) {
86  		if (polygon == null) {
87  			owner = SvgrealApp.getApp().getActiveModel();
88  			OMSVGElement parent = owner.getTwinGroup();
89  			polygon = new OMSVGPolygonElement();
90  			points = polygon.getPoints();
91  			applyCssContextStyle((SVGElement) polygon.getElement().cast());
92  //			polygon.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLACK_VALUE);
93  //			polygon.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_NONE_VALUE);
94  			polygon.getStyle().setSVGProperty(SVGConstants.CSS_VISIBILITY_PROPERTY, SVGConstants.CSS_VISIBLE_VALUE);
95  			parent.appendChild(polygon);
96  			points.appendItem(owner.getCoordinates(event, true));
97  		}
98  		points.appendItem(owner.getCoordinates(event, true));
99  		int count = points.getNumberOfItems();
100 		if (count > 2 && points.getItem(count - 1).substract(points.getItem(count - 3), owner.getSvgElement().createSVGPoint()).length() < 3) {
101 			points.removeItem(count - 1);
102 			points.removeItem(count - 2);
103 //			polygon.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_LIGHTCORAL_VALUE);
104 			OMSVGPolygonElement newPolygon = polygon;
105 			polygon = null;
106 			createCommand(newPolygon);
107 			owner = null;
108 		}
109 		return true;
110 	}
111 	
112 	@Override
113 	public boolean processMouseMove(MouseMoveEvent event) {
114 		if (owner != null) {
115 			int count = points.getNumberOfItems();
116 			owner.getCoordinates(event, true).assignTo(points.getItem(count - 1));
117 		}
118 		return true;
119 	}
120 }