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.OMSVGLineElement;
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 lines to the the SVG model.
39   * @author laaglu
40   */
41  public class AddLineCommandFactory extends AddCommandFactoryBase implements MouseDownProcessor, MouseMoveProcessor, MouseUpProcessor {
42  	@SuppressWarnings("serial")
43  	public static final IFactoryInstantiator<AddLineCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<AddLineCommandFactory>(ModelConstants.INSTANCE.addLineCmdFactory(), ModelConstants.INSTANCE.addLineCmdFactoryDesc()) {
44  		@Override
45  		public AddLineCommandFactory create() {
46  			return new AddLineCommandFactory();
47  		}
48  	};
49  
50  	/**
51  	 * The new line element
52  	 */
53  	protected OMSVGLineElement line;
54  	/**
55  	 * The line second point
56  	 */
57  	protected OMSVGPoint p;
58  	
59  	@Override
60  	public IFactoryInstantiator<?> getInstantiator() {
61  		return INSTANTIATOR;
62  	}
63  
64  	@Override
65  	public void start(Object requester) {
66  		GWT.log("AddLineCommandFactory.start(" + requester + ")");
67  		super.start(requester);
68  		updateStatus(ModelConstants.INSTANCE.addLineCmdFactory1());
69  	}
70  
71  	@Override
72  	public boolean processMouseDown(MouseDownEvent event) {
73  		if (owner != null && line != null) {
74  			owner.getTwinGroup().removeChild(line);
75  		}
76  		updateStatus(ModelConstants.INSTANCE.addLineCmdFactory2());
77  		owner = SvgrealApp.getApp().getActiveModel();
78  		p = owner.getCoordinates(event, true);
79  		line = new OMSVGLineElement(p.getX(), p.getY(), p.getX(), p.getY());
80  		applyCssContextStyle((SVGElement) line.getElement().cast());
81  		line.getStyle().setSVGProperty(SVGConstants.CSS_VISIBILITY_PROPERTY, SVGConstants.CSS_VISIBLE_VALUE);
82  		owner.getTwinGroup().appendChild(line);
83  		return true;
84  	}
85  	
86  	@Override
87  	public boolean processMouseUp(MouseUpEvent event) {
88  		if (owner != null) {
89  			updateStatus(ModelConstants.INSTANCE.addLineCmdFactory1());
90  			if (line.getX1().getBaseVal() == line.getX2().getBaseVal()
91  			 && line.getY1().getBaseVal() == line.getY2().getBaseVal()) {
92  				owner.getTwinGroup().removeChild(line);
93  			} else {
94  				createCommand(line);
95  			}
96  			line = null;
97  			owner = null;
98  		}
99  		return true;
100 	}
101 	
102 	@Override
103 	public boolean processMouseMove(MouseMoveEvent event) {
104 		if (owner != null) {
105 			p = owner.getCoordinates(event, true);
106 			line.getX2().getBaseVal().setValue(p.getX());
107 			line.getY2().getBaseVal().setValue(p.getY());
108 		}
109 		return true;
110 	}
111 }