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.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.vectomatic.dom.svg.impl.SVGElement;
25  import org.vectomatic.dom.svg.utils.SVGConstants;
26  import org.vectomatic.svg.edit.client.SvgrealApp;
27  import org.vectomatic.svg.edit.client.event.SelectionChangedProcessor;
28  import org.vectomatic.svg.edit.client.event.SelectionChangedProxy;
29  import org.vectomatic.svg.edit.client.model.MetaModel;
30  import org.vectomatic.svg.edit.client.model.ModelConstants;
31  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
32  
33  import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
34  import com.extjs.gxt.ui.client.event.SelectionService;
35  import com.extjs.gxt.ui.client.util.Format;
36  import com.extjs.gxt.ui.client.widget.ContentPanel;
37  import com.extjs.gxt.ui.client.widget.Window;
38  import com.extjs.gxt.ui.client.widget.layout.CardLayout;
39  import com.extjs.gxt.ui.client.widget.layout.FitLayout;
40  import com.google.gwt.core.client.GWT;
41  
42  /**
43   * Class to manage the inspector window. The inspector window contains
44   * many inspectors (each inspector is dedicated to displaying the properties
45   * of a given metamodel). The inspector window displays one inspector at a
46   * time, corresponding to the currently selected object 
47   * @author laaglu
48   */
49  public class InspectorWindow extends Window implements SelectionChangedProcessor<SVGElementModel> {
50  	private ContentPanel contentPanel;
51  	private CardLayout cardLayout;
52  	private ContentPanel noSelection;
53  	private ContentPanel multipleSelection;
54  	private Inspector<SVGElementModel> currentInspector;
55  	private static Map<MetaModel<SVGElement>, Inspector<SVGElementModel>> metaModelToInspector;
56  	private SelectionChangedProxy<SVGElementModel> selChangeProxy = new SelectionChangedProxy<SVGElementModel>(this);
57  	
58  	public InspectorWindow() {
59  		contentPanel = new ContentPanel();
60  		cardLayout = new CardLayout();
61  		contentPanel.setHeaderVisible(false);
62  		contentPanel.setLayout(cardLayout);
63  		
64  		noSelection = new ContentPanel();
65  		noSelection.setHeaderVisible(false);
66  		noSelection.setAnimCollapse(false);  
67  		noSelection.addText("No selection");  
68  		contentPanel.add(noSelection); 
69  
70  		multipleSelection = new ContentPanel();  
71  		multipleSelection.setHeaderVisible(false);
72  		multipleSelection.setAnimCollapse(false);  
73  		multipleSelection.addText("Multiple selection");  
74  		contentPanel.add(multipleSelection); 
75  
76  		setPlain(true);
77  		setMaximizable(true);
78  		setSize(500, 300);
79  		setMinWidth(200);
80  		setMinHeight(170);
81  		setLayout(new FitLayout());
82  		add(contentPanel);		
83  	}
84  	
85  	@Override
86  	public void onShow() {
87  		GWT.log("InspectorWindow.onShow()");
88  		super.onShow();
89  		SelectionService.get().addListener(selChangeProxy);
90  	}
91  	
92  	@Override
93  	public void onHide() {
94  		GWT.log("InspectorWindow.onHide()");
95  		super.onHide();
96  		SelectionService.get().removeListener(selChangeProxy);
97  	}
98  	
99  	@Override
100 	public boolean processSelectionChanged(SelectionChangedEvent<SVGElementModel> se) {
101 		ContentPanel contentPanel = noSelection;
102 		StringBuilder heading = new StringBuilder(Format.capitalize(ModelConstants.INSTANCE.inspectorWindow()));
103 		heading.append(": ");
104 		SVGElementModel model = null;
105 		if (currentInspector != null) {
106 			currentInspector.unbind();
107 			currentInspector = null;
108 		}
109 		List<SVGElementModel> models = se.getSelection();
110 		if (models != null) {
111 			if (models.size() > 1) {
112 				contentPanel = multipleSelection;
113 				heading.append(ModelConstants.INSTANCE.inspectorMultiSelection());
114 			} else {
115 				model = models.size() == 1 ? models.get(0) : SvgrealApp.getApp().getCssContext();
116 				currentInspector = getInspector(model);
117 				currentInspector.bind(model);
118 				contentPanel = currentInspector;
119 				heading.append(model.get(SVGConstants.SVG_TITLE_TAG).toString());
120 			}
121 		}
122 		cardLayout.setActiveItem(contentPanel);
123 		setHeading(heading.toString());
124 		return true;
125 	}
126 
127 	private Inspector<SVGElementModel> getInspector(SVGElementModel model) {
128 		if (metaModelToInspector == null) {
129 			metaModelToInspector = new HashMap<MetaModel<SVGElement>, Inspector<SVGElementModel>>();
130 		}
131 		MetaModel<SVGElement> metaModel = model.getMetaModel();
132 		Inspector<SVGElementModel> inspector = metaModelToInspector.get(metaModel);
133 		if (inspector == null) {
134 			inspector = new Inspector<SVGElementModel>(model.getMetaModel());
135 			metaModelToInspector.put(metaModel, inspector);
136 			contentPanel.add(inspector);
137 		}
138 		return inspector;
139 	}
140 }