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.event.MouseAdapter;
22 import java.awt.event.MouseEvent;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.swing.JComponent;
27 import javax.swing.JPanel;
28 import javax.swing.JPopupMenu;
29 import javax.swing.JTree;
30 import javax.swing.ScrollPaneConstants;
31 import javax.swing.event.TreeSelectionEvent;
32 import javax.swing.event.TreeSelectionListener;
33 import javax.swing.tree.DefaultMutableTreeNode;
34 import javax.swing.tree.TreePath;
35
36 import org.apache.xerces.xs.XSObject;
37 import org.springframework.binding.value.ValueModel;
38 import org.springframework.context.ApplicationEvent;
39 import org.springframework.context.ApplicationListener;
40 import org.springframework.richclient.application.Application;
41 import org.springframework.richclient.application.PageComponentContext;
42 import org.springframework.richclient.application.statusbar.StatusBar;
43 import org.springframework.richclient.application.support.AbstractView;
44 import org.springframework.richclient.command.CommandGroup;
45 import org.springframework.richclient.command.support.GlobalCommandIds;
46 import org.springframework.richclient.util.PopupMenuMouseListener;
47
48 import de.mindcrimeilab.util.DefaultMutableTreeNodeAccessor;
49 import de.mindcrimeilab.xsanalyzer.actions.TypeInspectionCommand;
50 import de.mindcrimeilab.xsanalyzer.actions.TypeHierarchyCommand;
51 import de.mindcrimeilab.xsanalyzer.actions.TypeHierarchyExecutor;
52 import de.mindcrimeilab.xsanalyzer.actions.XsAnalyzerApplicationEvent;
53 import de.mindcrimeilab.xsanalyzer.actions.XsElementPropertiesExecutor;
54 import de.mindcrimeilab.xsanalyzer.model.JTreeValueModelAdapter;
55 import de.mindcrimeilab.xsanalyzer.model.TreeModelAdapter;
56 import de.mindcrimeilab.xsanalyzer.model.TreeSelectionValueModelAdapter;
57 import de.mindcrimeilab.xsanalyzer.model.TreeSingleSelectionGuard;
58 import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
59 import de.mindcrimeilab.xsanalyzer.ui.renderer.SchemaElementsRenderer;
60
61
62
63
64
65
66
67 public class OutlineToolWindow extends AbstractView implements ApplicationListener {
68
69 private JTree jtStructure;
70
71 private XsAnalyzerApplicationModel model;
72
73 private final XsElementPropertiesExecutor xsElementPropertiesExecutor = new XsElementPropertiesExecutor();
74
75 private final TypeHierarchyExecutor typeHierarchyExecutor = new TypeHierarchyExecutor();
76
77 @Override
78 protected void registerLocalCommandExecutors(PageComponentContext context) {
79 context.register(GlobalCommandIds.PROPERTIES, xsElementPropertiesExecutor);
80 }
81
82
83
84
85 public XsAnalyzerApplicationModel getModel() {
86 return model;
87 }
88
89
90
91
92
93 public void setModel(XsAnalyzerApplicationModel model) {
94 this.model = model;
95 if (null != this.model.getSchemaModel() && null != jtStructure) {
96 jtStructure.setModel(new TreeModelAdapter(this.model.getSchemaModel()));
97 }
98 }
99
100
101
102
103
104
105
106
107
108 @Override
109 protected JComponent createControl() {
110 createTree();
111 JPanel jpOutline = getComponentFactory().createPanel(new BorderLayout());
112 jpOutline.add(getComponentFactory().createScrollPane(jtStructure, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
113 return jpOutline;
114 }
115
116 @Override
117 public void onApplicationEvent(ApplicationEvent event) {
118 logger.debug(event);
119 if (event instanceof XsAnalyzerApplicationEvent) {
120 switch (((XsAnalyzerApplicationEvent) event).getEventType()) {
121 case OPEN:
122 jtStructure.setModel(new TreeModelAdapter(model.getSchemaModel()));
123 StatusBar bar = Application.instance().getLifecycleAdvisor().getStatusBar();
124 bar.setMessage(getMessage("statusBar.label.action.open", new Object[] { model.getSchemaFile().getAbsolutePath()}));
125 break;
126 }
127 }
128 }
129
130 private void createTree() {
131 jtStructure = new JTree(new DefaultMutableTreeNode(getMessage("outlineView.structureTree.initialMessage")));
132 jtStructure.setCellRenderer(new SchemaElementsRenderer());
133
134 final ValueModel selectionHolder = new TreeSelectionValueModelAdapter(jtStructure.getSelectionModel());
135 final ValueModel vModel = new JTreeValueModelAdapter((TreeSelectionValueModelAdapter) selectionHolder);
136
137 final DefaultMutableTreeNodeAccessor accessor = new DefaultMutableTreeNodeAccessor();
138 final CommandGroup group = getWindowCommandManager().createCommandGroup("schemaElementsCommandGroup", new Object[] { new TypeHierarchyCommand(vModel), new TypeInspectionCommand(vModel), "separator", "propertiesCommand"});
139
140 jtStructure.addMouseListener(new PopupMenuMouseListener() {
141
142 @Override
143 protected JPopupMenu getPopupMenu(MouseEvent e) {
144 return group.createPopupMenu();
145 }
146 });
147 jtStructure.addMouseListener(new MouseAdapter() {
148
149 @Override
150 public void mouseClicked(MouseEvent evt) {
151 if (evt.getClickCount() == 2) {
152 final Object selectedObject = jtStructure.getLastSelectedPathComponent();
153 if (selectedObject instanceof XSObject) {
154 Map<String, XSObject> parameters = new HashMap<String, XSObject>();
155 parameters.put(TypeHierarchyExecutor.TARGET_OBJECT, (XSObject) selectedObject);
156 typeHierarchyExecutor.execute(parameters);
157 }
158 }
159 }
160 });
161
162 jtStructure.addTreeSelectionListener(new TreeSelectionListener() {
163
164 @Override
165 public void valueChanged(TreeSelectionEvent e) {
166 TreePath path = e.getPath();
167 if (null != path) {
168 Map<String, XSObject> parameters = new HashMap<String, XSObject>();
169
170 final Object lastPathComponent = path.getLastPathComponent();
171 if (lastPathComponent instanceof XSObject) {
172 parameters.put(XsElementPropertiesExecutor.OBJECT, (XSObject) lastPathComponent);
173 xsElementPropertiesExecutor.execute(parameters);
174 }
175 }
176
177 }
178
179 });
180
181 new TreeSingleSelectionGuard(selectionHolder, xsElementPropertiesExecutor);
182 new TreeSingleSelectionGuard(selectionHolder, typeHierarchyExecutor);
183
184
185 }
186 }