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.inspector;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.svg.edit.client.model.AbstractModel;
24  import org.vectomatic.svg.edit.client.model.MetaModel;
25  import org.vectomatic.svg.edit.client.model.ModelCategory;
26  
27  import com.extjs.gxt.ui.client.core.El;
28  import com.extjs.gxt.ui.client.widget.Container;
29  import com.extjs.gxt.ui.client.widget.ContentPanel;
30  import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
31  import com.google.gwt.core.client.GWT;
32  
33  /**
34   * Class to provide an editable view of a metamodel. The inspector
35   * itself is organized in sections, which correspond to the various
36   * categories on the metamodel
37   * @author laaglu
38   * @param <M>
39   * The model being associated to this inspector
40   */
41  public class Inspector<M extends AbstractModel<?>> extends ContentPanel {
42  	private List<IInspectorSection<M>> sections;
43  	private IInspectorSection<M> currentSection;
44  	private MetaModel<?> metamodel;
45  	private M model;
46  	
47  	public Inspector(MetaModel<?> metamodel) {
48  		this.metamodel = metamodel;
49  		setHeaderVisible(false);
50  		setBodyBorder(false);
51  		setLayout(new AccordionLayout() {
52  			/**
53  			 * Invoked when the section becomes visible. It binds
54  			 * the section to the model so that the section displays
55  			 * the proper data
56  			 */
57  			@Override
58  			protected void onLayout(Container<?> container, El target) {
59  				if (currentSection != null) {
60  					currentSection.unbind();
61  				}
62  				for (IInspectorSection<M> section : sections) {
63  					if (section.getPanel() == activeItem) {
64  						section.bind(model);
65  						currentSection = section;
66  						break;
67  					}
68  				}
69  				super.onLayout(container, target);
70  			}
71  		});
72  		
73  		sections = new ArrayList<IInspectorSection<M>>();
74  		for (ModelCategory<?> category : metamodel.getCategories()) {
75  			IInspectorSection<M> section = category.getInspectorSection();
76  			if (section != null) {
77  				sections.add(section);
78  				add(section.getPanel());
79  			}
80  		}
81  	}
82  	
83  	public void bind(M model) {
84  		GWT.log("Inspector.bind(" + model + ")");
85  		this.model = model;
86  		if (currentSection != null) {
87  			currentSection.unbind();
88  			currentSection.bind(model);
89  		}
90  	}
91  	
92  	public void unbind() {
93  		GWT.log("unbind()");
94  		if (currentSection != null) {
95  			currentSection.unbind();
96  		}
97  		this.model = null;
98  	}
99  	@Override
100 	public String toString() {
101 		StringBuilder builder = new StringBuilder("SVGInspector(");
102 		builder.append(metamodel);
103 		builder.append(")");
104 		return builder.toString();
105 	}
106 
107 }