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;
19  
20  import java.util.Map;
21  
22  import org.vectomatic.dom.svg.OMSVGElement;
23  import org.vectomatic.dom.svg.OMSVGTransform;
24  import org.vectomatic.dom.svg.itf.ISVGTransformable;
25  import org.vectomatic.dom.svg.utils.SVGConstants;
26  import org.vectomatic.svg.edit.client.model.ModelConstants;
27  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
28  import org.vectomatic.svg.edit.client.model.svg.Transformation;
29  
30  import com.extjs.gxt.ui.client.data.BeanModel;
31  import com.extjs.gxt.ui.client.data.BeanModelLookup;
32  import com.extjs.gxt.ui.client.util.Format;
33  
34  /**
35   * Command to represent changes in the svg transform attribute of an element.
36   */
37  public class EditTransformCommand extends GenericEditCommand {
38  	private OMSVGTransform told, tnew, rold, rnew, sold, snew;
39  	public EditTransformCommand(CommandFactoryBase factory, SVGElementModel model, Map<String, Object> oldValues) {
40  		super(factory, model, oldValues, ModelConstants.INSTANCE.transformCmd());
41  		OMSVGElement element = model.getElementWrapper();
42  		element.setAttribute(SVGConstants.SVG_TRANSFORM_ATTRIBUTE, (String) oldValues.get(SVGConstants.SVG_TRANSFORM_ATTRIBUTE));
43  		Transformation xform0 = Transformation.decompose((ISVGTransformable)element);
44  		told = xform0.getT();
45  		rold = xform0.getR();
46  		sold = xform0.getS();
47  		element.setAttribute(SVGConstants.SVG_TRANSFORM_ATTRIBUTE, (String) newValues.get(SVGConstants.SVG_TRANSFORM_ATTRIBUTE));
48  		Transformation xform1 = Transformation.decompose((ISVGTransformable)element);
49  		tnew = xform1.getT();
50  		rnew = xform1.getR();
51  		snew = xform1.getS();
52  	}
53  	
54  	@Override
55  	public String getDescription() {
56  		StringBuilder changes = new StringBuilder();
57  		if (!sameTranslation(told, tnew)) {
58  			changes.append(tnew.getDescription());
59  		}
60  		if (!sameRotation(rold, rnew)) {
61  			if (changes.length() > 0) {
62  				changes.append("; ");
63  			}
64  			changes.append(rnew.getDescription());
65  		}
66  		if (!sameScaling(sold, snew)) {
67  			if (changes.length() > 0) {
68  				changes.append("; ");
69  			}
70  			changes.append(snew.getDescription());
71  		}
72  		return Format.substitute(ModelConstants.INSTANCE.transformCmd(), model.get(SVGConstants.SVG_TITLE_TAG), changes);
73  	}
74  	public static final boolean sameRotation(OMSVGTransform t1, OMSVGTransform t2) {
75  		return sameValue(t1.getAngle(), t2.getAngle());
76  	}
77  	public static final boolean sameScaling(OMSVGTransform t1, OMSVGTransform t2) {
78  		return sameValue(t1.getMatrix().getA(), t2.getMatrix().getA()) && sameValue(t1.getMatrix().getD(), t2.getMatrix().getD());
79  	}
80  	public static final boolean sameTranslation(OMSVGTransform t1, OMSVGTransform t2) {
81  		return sameValue(t1.getMatrix().getE(), t2.getMatrix().getE()) && sameValue(t1.getMatrix().getF(), t2.getMatrix().getF());
82  	}
83  	public static final boolean sameValue(float f1, float f2) {
84  		return Math.abs(f1 - f2) < 1e-03f;
85  	}
86  	
87  	@Override
88  	public BeanModel asModel() {
89  		return BeanModelLookup.get().getFactory(EditTransformCommand.class).createModel(this);
90  	}
91  
92  }