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 java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
28  import org.vectomatic.svg.edit.client.gxt.widget.CommandFactoryMenuItem;
29  
30  import com.extjs.gxt.ui.client.widget.menu.Item;
31  import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
32  import com.google.gwt.resources.client.ImageResource;
33  
34  /**
35   * Metamodel contains information which enable the introspection
36   * of a model. Notably, all the model properties are described
37   * by metadata. The model properties are grouped into logical
38   * categories
39   * @author laaglu
40   * @param <U>
41   * The model class
42   */
43  public class MetaModel<U> {
44  	private String name;
45  	private Map<String, ModelCategory<U>> nameToCategory;
46  	private Map<String, IMetadata<?, U>> nameToMetadata;
47  	private List<ModelCategory<U>> categories;
48  	private IFactoryInstantiator<?>[][] contextMenuFactories;
49  	private ImageResource icon;
50  	private List<Item> modelItems;
51  	
52  	public void init(String name, ImageResource icon, List<ModelCategory<U>> categories, IFactoryInstantiator<?>[][] contextMenuFactories) {
53  		this.name = name;
54  		this.icon = icon;
55  		this.categories = categories;
56  		this.contextMenuFactories = contextMenuFactories;
57  		nameToCategory = new HashMap<String, ModelCategory<U>>();
58  		nameToMetadata = new HashMap<String, IMetadata<?,U>>();
59  		for (ModelCategory<U> c : categories) {
60  			assert !nameToCategory.containsKey(c.getName());
61  			nameToCategory.put(c.getName(), c);
62  			for (IMetadata<?,U> m : c.getMetadata()) {
63  				assert !nameToMetadata.containsKey(m.getName());
64  				nameToMetadata.put(m.getName(), m);
65  			}
66  		}
67  	}
68  	
69  	public String getName() {
70  		return name;
71  	}
72  	
73  	public List<ModelCategory<U>> getCategories() {
74  		return categories;
75  	}
76  
77  	public ModelCategory<U> getCategory(String name) {
78  		return nameToCategory.get(name);
79  	}
80  
81  	public ImageResource getIcon() {
82  		return icon;
83  	}
84  
85  	public IMetadata<?,U> getMetadata(String name) {
86  		return nameToMetadata.get(name);
87  	}
88  	
89  	public Map<String, Object> getProperties(U model) {
90  		Map<String, Object> properties = new HashMap<String, Object>();
91  		for (Entry<String, IMetadata<?,U>> entry : nameToMetadata.entrySet()) {
92  			properties.put(entry.getKey(), entry.getValue().get(model));
93  		}
94  		return properties;
95  	}
96  	
97  	public Collection<String> getPropertyNames() {
98  		return nameToMetadata.keySet();
99  	}
100 	
101 	@Override
102 	public String toString() {
103 		StringBuilder builder = new StringBuilder("SVGMetaModel(");
104 		builder.append(categories);
105 		builder.append(")");
106 		return builder.toString();
107 	}
108 
109 	public List<Item> getContextMenuItems() {
110 		if (modelItems == null) {
111 			modelItems = new ArrayList<Item>();
112 			if (contextMenuFactories != null) {
113 				for (int i = 0; i < contextMenuFactories.length; i++) {
114 					for (int j = 0; j < contextMenuFactories[i].length; j++) {
115 						modelItems.add(new CommandFactoryMenuItem((contextMenuFactories[i][j])));
116 					}
117 					if (i < contextMenuFactories.length - 1) {
118 						modelItems.add(new SeparatorMenuItem());
119 					}
120 				}
121 			}
122 		}
123 		return modelItems;
124 	}
125 }