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.svg.edit.client.command.IFactoryInstantiator;
21  
22  /**
23   * Metadata class based on a java script property or a function
24   * @author laaglu
25   * @param <T>
26   * The metadata type
27   * @param <U>
28   * The javascript type which provides the property or object
29   */
30  public class JSMetadata<T,U> extends MetadataBase<T,U> {
31  	protected IPropertyAccessor<T,U> accessor;
32  	public JSMetadata(String propertyName, String description, IFieldFactory fieldFactory, IPropertyAccessor<T,U> accessor, IFactoryInstantiator<?> factory, IValidator<T,U> validator) {
33  		super(propertyName, description, fieldFactory, factory, validator);
34  		this.accessor = accessor;
35  	}
36  
37  	@Override
38  	public T get(U element) {
39  		return accessor.get(element);
40  	}
41  
42  	@Override
43  	public T set(U element, T value) {
44  		T oldValue = accessor.get(element);
45  		accessor.set(element, value);
46  		return oldValue;
47  	}
48  
49  	@Override
50  	public T remove(U element) {
51  		throw new IllegalStateException("Property " + propertyName + " cannot be removed");
52  	}
53  	
54  	@Override
55  	public String toString() {
56  		StringBuilder builder = new StringBuilder("JSMetadata(");
57  		builder.append(propertyName);
58  		builder.append(")");
59  		return builder.toString();
60  	}
61  }