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.model.svg;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.dom.svg.impl.SVGElement;
24  import org.vectomatic.dom.svg.impl.SVGRectElement;
25  import org.vectomatic.dom.svg.utils.SVGConstants;
26  import org.vectomatic.svg.edit.client.AppBundle;
27  import org.vectomatic.svg.edit.client.command.EditGeometryCommandFactory;
28  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
29  import org.vectomatic.svg.edit.client.engine.SVGModel;
30  import org.vectomatic.svg.edit.client.gxt.binding.FormPanelUtils;
31  import org.vectomatic.svg.edit.client.inspector.GenericSectionFactory;
32  import org.vectomatic.svg.edit.client.model.IPropertyAccessor;
33  import org.vectomatic.svg.edit.client.model.IValidator;
34  import org.vectomatic.svg.edit.client.model.JSMetadata;
35  import org.vectomatic.svg.edit.client.model.MetaModel;
36  import org.vectomatic.svg.edit.client.model.MetadataBase;
37  import org.vectomatic.svg.edit.client.model.ModelCategory;
38  import org.vectomatic.svg.edit.client.model.ModelConstants;
39  import org.vectomatic.svg.edit.client.model.ValidationConstants;
40  import org.vectomatic.svg.edit.client.model.ValidationError;
41  import org.vectomatic.svg.edit.client.model.ValidationError.Severity;
42  
43  /**
44   * Viewbox model class.
45   * @author laaglu
46   */
47  public class SVGViewBoxElementModel extends SVGElementModel {
48  	private static MetaModel<SVGElement> metaModel;
49  
50  	public SVGViewBoxElementModel(SVGModel owner, SVGElement element, SVGElement twin) {
51  		super(owner, element, twin);
52  	}
53  
54  	@Override
55  	public MetaModel<SVGElement> getMetaModel() {
56  		return getViewBoxElementMetaModel();
57  	}
58  	
59  	@Override
60  	public <X> X get(String property) {
61  		if (SVGConstants.SVG_TITLE_TAG.equals(property)) {
62  			return (X)ModelConstants.INSTANCE.viewBox();
63  		}
64  		return super.<X>get(property);
65  	}
66  
67  	@Override
68  	public <X> X set(String property, X value) {
69  		value = super.set(property, value);
70  		owner.updateTransform();
71  		return value;
72  	}
73  
74  	public static MetaModel<SVGElement> getViewBoxElementMetaModel() {
75  		if (metaModel == null) {
76  			metaModel = new MetaModel<SVGElement>();
77  			ModelConstants constants = ModelConstants.INSTANCE;
78  
79  			ModelCategory<SVGElement> geometricCategory = new ModelCategory<SVGElement>(
80  					ModelCategory.GEOMETRY, 
81  					constants.geometry(), 
82  					GenericSectionFactory.INSTANCE);
83  			MetadataBase<Float, SVGElement> x = new JSMetadata<Float, SVGElement>(
84  				SVGConstants.SVG_X_ATTRIBUTE, 
85  				constants.rectX(),
86  				FormPanelUtils.NUMBER_FIELD_FACTORY,
87  				new IPropertyAccessor<Float, SVGElement>() {
88  					@Override
89  					public Float get(SVGElement element) {
90  						return ((SVGRectElement)element.cast()).getX().getBaseVal().getValue();
91  					}
92  
93  					@Override
94  					public void set(SVGElement element, Float value) {
95  						((SVGRectElement)element.cast()).getX().getBaseVal().setValue(value);
96  					}
97  				},
98  				EditGeometryCommandFactory.INSTANTIATOR,
99  				null);
100 			MetadataBase<Float, SVGElement> y = new JSMetadata<Float, SVGElement>(
101 				SVGConstants.SVG_Y_ATTRIBUTE, 
102 				constants.rectY(),
103 				FormPanelUtils.NUMBER_FIELD_FACTORY,
104 				new IPropertyAccessor<Float, SVGElement>() {
105 					@Override
106 					public Float get(SVGElement element) {
107 						return ((SVGRectElement)element.cast()).getY().getBaseVal().getValue();
108 					}
109 
110 					@Override
111 					public void set(SVGElement element, Float value) {
112 						((SVGRectElement)element.cast()).getY().getBaseVal().setValue(value);
113 					}
114 				},
115 				EditGeometryCommandFactory.INSTANTIATOR,
116 				null);
117 			MetadataBase<Float, SVGElement> width = new JSMetadata<Float, SVGElement>(
118 				SVGConstants.SVG_WIDTH_ATTRIBUTE, 
119 				constants.rectWidth(),
120 				FormPanelUtils.NUMBER_FIELD_FACTORY,
121 				new IPropertyAccessor<Float, SVGElement>() {
122 					@Override
123 					public Float get(SVGElement element) {
124 						return ((SVGRectElement)element.cast()).getWidth().getBaseVal().getValue();
125 					}
126 
127 					@Override
128 					public void set(SVGElement element, Float value) {
129 						((SVGRectElement)element.cast()).getWidth().getBaseVal().setValue(value);
130 					}
131 				},
132 				EditGeometryCommandFactory.INSTANTIATOR,
133 				new IValidator<Float, SVGElement>() {
134 					final ValidationError zeroWidth = new ValidationError(Severity.ERROR, ValidationConstants.INSTANCE.zeroWidth());
135 					final ValidationError negativeWidth = new ValidationError(Severity.ERROR, ValidationConstants.INSTANCE.negativeWidth());
136 					@Override
137 					public ValidationError validate(SVGElement model, Float value) {
138 						if (value == 0) {
139 							return zeroWidth;
140 						}
141 						if (value < 0) {
142 							return negativeWidth;
143 						}
144 						return null;
145 					}
146 				});
147 			MetadataBase<Float, SVGElement> height = new JSMetadata<Float, SVGElement>(
148 				SVGConstants.SVG_HEIGHT_ATTRIBUTE, 
149 				constants.rectHeight(),
150 				FormPanelUtils.NUMBER_FIELD_FACTORY,
151 				new IPropertyAccessor<Float, SVGElement>() {
152 					@Override
153 					public Float get(SVGElement element) {
154 						return ((SVGRectElement)element.cast()).getHeight().getBaseVal().getValue();
155 					}
156 
157 					@Override
158 					public void set(SVGElement element, Float value) {
159 						((SVGRectElement)element.cast()).getHeight().getBaseVal().setValue(value);
160 					}
161 				},
162 				EditGeometryCommandFactory.INSTANTIATOR,
163 				new IValidator<Float, SVGElement>() {
164 					final ValidationError zeroHeight = new ValidationError(Severity.ERROR, "zeroHeight");
165 					final ValidationError negativeHeight = new ValidationError(Severity.ERROR, "negativeHeight");
166 					@Override
167 					public ValidationError validate(SVGElement model, Float value) {
168 						if (value == 0) {
169 							return zeroHeight;
170 						}
171 						if (value < 0) {
172 							return negativeHeight;
173 						}
174 						return null;
175 					}
176 				});
177 			geometricCategory.addMetadata(x);
178 			geometricCategory.addMetadata(y);
179 			geometricCategory.addMetadata(width);
180 			geometricCategory.addMetadata(height);
181 			IFactoryInstantiator<?>[][] contextMenuFactories = new IFactoryInstantiator<?>[][] {
182 				{
183 					EditGeometryCommandFactory.INSTANTIATOR,
184 				}
185 			};
186 			List<ModelCategory<SVGElement>> categories = new ArrayList<ModelCategory<SVGElement>>();
187 			categories.add(geometricCategory);
188 			metaModel.init(
189 				constants.viewBox(),
190 				AppBundle.INSTANCE.viewBox(),
191 				categories,
192 				contextMenuFactories);
193 			
194 		}
195 		return metaModel;
196 	}
197 }