1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer;
19
20 import java.awt.BorderLayout;
21 import java.awt.Dimension;
22 import java.beans.IntrospectionException;
23 import java.beans.Introspector;
24 import java.beans.PropertyDescriptor;
25 import java.util.Collection;
26
27 import javax.swing.DefaultListModel;
28 import javax.swing.JComponent;
29 import javax.swing.JList;
30 import javax.swing.JPanel;
31 import javax.swing.JScrollPane;
32 import javax.swing.JSplitPane;
33 import javax.swing.ListSelectionModel;
34 import javax.swing.ScrollPaneConstants;
35 import javax.swing.SwingUtilities;
36 import javax.swing.event.ListSelectionEvent;
37 import javax.swing.event.ListSelectionListener;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.xerces.xs.StringList;
42 import org.apache.xerces.xs.XSComplexTypeDefinition;
43 import org.apache.xerces.xs.XSElementDeclaration;
44 import org.apache.xerces.xs.XSModelGroup;
45 import org.apache.xerces.xs.XSModelGroupDefinition;
46 import org.apache.xerces.xs.XSObjectList;
47 import org.apache.xerces.xs.XSSimpleTypeDefinition;
48 import org.springframework.context.ApplicationEvent;
49 import org.springframework.context.ApplicationListener;
50 import org.springframework.richclient.application.support.AbstractView;
51
52 import com.l2fprod.common.propertysheet.Property;
53 import com.l2fprod.common.propertysheet.PropertySheetPanel;
54 import com.l2fprod.common.propertysheet.PropertySheetTable;
55
56 import de.mindcrimeilab.xsanalyzer.actions.XsAnalyzerApplicationEvent;
57 import de.mindcrimeilab.xsanalyzer.ui.renderer.SchemaElementsRenderer;
58 import de.mindcrimeilab.xsanalyzer.ui.renderer.XSRenderFactory;
59 import de.mindcrimeilab.xsanalyzer.util.SchemaElementsPropertyFactory;
60 import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
61
62
63
64
65
66
67
68
69 public class XsPropertyToolWindow extends AbstractView implements ApplicationListener {
70
71 private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
72
73 private PropertySheetPanel propertySheetPanel;
74
75 private JList jlPropertyDetails;
76
77
78
79
80
81
82 @Override
83 protected JComponent createControl() {
84 createPropertySheet();
85 createDetailList();
86 JPanel jpProperties = getComponentFactory().createPanel(new BorderLayout());
87
88 JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
89 jsp.setTopComponent(getComponentFactory().createScrollPane(propertySheetPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED));
90
91 JPanel jpDetails = getComponentFactory().createPanel(new BorderLayout());
92
93 final JScrollPane jspPropertyDetails = getComponentFactory().createScrollPane(jlPropertyDetails, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
94 jpDetails.add(getComponentFactory().createLabelFor("propertySheetPanel.detail.label", jspPropertyDetails), BorderLayout.NORTH);
95 jpDetails.add(jspPropertyDetails, BorderLayout.CENTER);
96
97 jsp.setBottomComponent(jpDetails);
98 jsp.setDividerLocation(0.5);
99 jpProperties.add(jsp, BorderLayout.CENTER);
100 return jpProperties;
101 }
102
103 @Override
104 public void onApplicationEvent(ApplicationEvent event) {
105 XsPropertyToolWindow.logger.debug(event);
106 if (event instanceof XsAnalyzerApplicationEvent) {
107 switch (((XsAnalyzerApplicationEvent) event).getEventType()) {
108 case SHOW_SCHEMA_ELEMENT_DETAIL:
109 showProperties(event.getSource());
110 ((DefaultListModel) jlPropertyDetails.getModel()).clear();
111 break;
112 }
113 }
114 }
115
116 private void showProperties(Object selectedObject) {
117 try {
118 if (selectedObject instanceof XSComplexTypeDefinition) {
119 final Property[] p = SchemaElementsPropertyFactory.getComplexTypeProperties((XSComplexTypeDefinition) selectedObject);
120 propertySheetPanel.setProperties(p);
121 propertySheetPanel.readFromObject(selectedObject);
122 }
123 else if (selectedObject instanceof XSSimpleTypeDefinition) {
124 final Property[] p = SchemaElementsPropertyFactory.getSimpleTypeProperties((XSSimpleTypeDefinition) selectedObject);
125 propertySheetPanel.setProperties(p);
126 propertySheetPanel.readFromObject(selectedObject);
127 }
128 else if (selectedObject instanceof XSModelGroupDefinition) {
129 final Property[] p = SchemaElementsPropertyFactory.getModelGroupProperties((XSModelGroupDefinition) selectedObject);
130 propertySheetPanel.setProperties(p);
131 propertySheetPanel.readFromObject(selectedObject);
132 }
133 else if (selectedObject instanceof XSElementDeclaration) {
134 final Property[] p = SchemaElementsPropertyFactory.getElementProperties((XSElementDeclaration) selectedObject);
135 propertySheetPanel.setProperties(p);
136 propertySheetPanel.readFromObject(selectedObject);
137 }
138 else if (selectedObject instanceof String) {
139
140
141 }
142 else {
143 XsPropertyToolWindow.logger.debug("------------------- Fallback -----------------");
144 XsPropertyToolWindow.logger.debug("Fallback for class [" + selectedObject.getClass().getName() + "]");
145 final PropertyDescriptor[] pd = Introspector.getBeanInfo(selectedObject.getClass()).getPropertyDescriptors();
146
147 propertySheetPanel.setProperties(pd);
148 propertySheetPanel.readFromObject(selectedObject);
149 }
150 }
151 catch (IntrospectionException e) {
152 XsPropertyToolWindow.logger.warn("valueChanged(TreeSelectionEvent)", e);
153 e.printStackTrace();
154 }
155 }
156
157 private void createPropertySheet() {
158 propertySheetPanel = new PropertySheetPanel();
159 propertySheetPanel.setMinimumSize(new Dimension(200, 200));
160 propertySheetPanel.setPreferredSize(new Dimension(200, 200));
161 propertySheetPanel.setSize(new Dimension(200, 200));
162 propertySheetPanel.setRendererFactory(new XSRenderFactory());
163
164 final PropertySheetTable table = propertySheetPanel.getTable();
165 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
166 table.getSelectionModel().addListSelectionListener(new TableSelectionControl());
167 table.setCellEditor(null);
168 }
169
170 private void createDetailList() {
171 jlPropertyDetails = getComponentFactory().createList();
172 jlPropertyDetails.setModel(new DefaultListModel());
173 jlPropertyDetails.setCellRenderer(new SchemaElementsRenderer());
174 }
175
176
177
178
179
180
181
182
183
184 private class TableSelectionControl implements ListSelectionListener {
185
186
187
188
189
190
191 @Override
192 public void valueChanged(ListSelectionEvent evt) {
193 final PropertySheetTable table = propertySheetPanel.getTable();
194 final DefaultListModel model = (DefaultListModel) jlPropertyDetails.getModel();
195
196 SwingUtilities.invokeLater(new Runnable() {
197
198 @SuppressWarnings("unchecked")
199 @Override
200 public void run() {
201 int rowIndex = table.getSelectedRow();
202 if (0 > rowIndex) { return; }
203 Object value = table.getValueAt(rowIndex, 1);
204
205 model.clear();
206
207 if (null == value) {
208 model.addElement("<null> value!");
209 }
210 else if (value instanceof Collection) {
211 for (Object item : (Collection) value) {
212 model.addElement(item);
213 }
214 }
215 else if (value instanceof StringList) {
216 StringList strList = (StringList) value;
217 for (int i = 0; i < strList.getLength(); ++i) {
218 model.addElement(strList.item(i));
219 }
220 }
221 else if (value instanceof XSModelGroup) {
222 XSModelGroup group = (XSModelGroup) value;
223 XSObjectList list = group.getParticles();
224 Collection collection = XSModelHelper.flat(list);
225 for (Object item : collection) {
226 model.addElement(item);
227 }
228 }
229 else {
230 model.addElement(value.toString());
231 }
232
233 }
234 });
235
236 }
237 }
238 }