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