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;
19  
20  import org.vectomatic.dom.svg.OMSVGStyle;
21  import org.vectomatic.dom.svg.impl.SVGElement;
22  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
23  
24  /**
25   * Metadata class based on CSS properties
26   * @author laaglu
27   * @param <T>
28   * The CSS property type
29   */
30  public class CssMetadata<T> extends MetadataBase<T, SVGElement>  {
31  	protected IConverter<String, T> converter;
32  	protected T defaultValue;
33  	
34  	public CssMetadata(String propertyName, String description, IFieldFactory fieldFactory, IConverter<String, T> converter, T defaultValue, IFactoryInstantiator<?> factory, IValidator<T, SVGElement> validator) {
35  		super(propertyName, description, fieldFactory, factory, validator);
36  		this.defaultValue = defaultValue;
37  		this.converter = converter;
38  	}
39  	
40  	@Override
41  	public T get(SVGElement element) {
42  		OMSVGStyle style = element.getStyle().cast();
43  		String value = style.getSVGProperty(propertyName);
44  		if (value == null || value.length() == 0) {
45  			value = element.getAttribute(propertyName);
46  		}
47  		if (value == null || value.length() == 0) {
48  			return defaultValue;
49  		}
50  		return converter.sourceToDest(value);
51  	}
52  	
53  	@Override
54  	public T set(SVGElement element, T value) {
55  		String str = converter.destToSource(value);
56  		if (str == null || str.length() == 0) {
57  			// This is to deal with CSS properties which are inherited
58  			// Setting the value to "" or null means the property should
59  			// be inherited
60  			return remove(element);
61  		}
62  		OMSVGStyle style = element.getStyle().cast();
63  		T oldValue = get(element);
64  		style.setSVGProperty(propertyName, str);
65  		element.removeAttribute(propertyName);
66  		return oldValue;
67  	}
68  	
69  	@Override
70  	public T remove(SVGElement element) {
71  		OMSVGStyle style = element.getStyle().cast();
72  		T oldValue = converter.sourceToDest(style.getSVGProperty(propertyName));
73  		style.clearSVGProperty(propertyName);
74  		if (oldValue == null) {
75  			oldValue = converter.sourceToDest(element.getAttribute(propertyName));
76  		}
77  		element.removeAttribute(propertyName);
78  		return oldValue;
79  	}
80  	
81  	public T getDefaultValue() {
82  		return defaultValue;
83  	}
84  	
85  	@Override
86  	public String toString() {
87  		StringBuilder builder = new StringBuilder("CSSMetadata(");
88  		builder.append(propertyName);
89  		builder.append(", ");
90  		builder.append(propertyName);
91  		builder.append(")");
92  		return builder.toString();
93  	}
94  }