1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer.actions;
19
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.swing.JFrame;
27
28 import org.apache.xerces.xs.XSModel;
29 import org.apache.xerces.xs.XSNamespaceItem;
30 import org.apache.xerces.xs.XSNamespaceItemList;
31 import org.apache.xerces.xs.XSTypeDefinition;
32 import org.springframework.richclient.application.Application;
33 import org.springframework.richclient.command.support.ApplicationWindowAwareCommand;
34
35 import de.mindcrimeilab.swing.util.VisualProgressWorker;
36 import de.mindcrimeilab.xsanalyzer.TypeUsageWorker;
37 import de.mindcrimeilab.xsanalyzer.UnusedTypeFinderWorker;
38 import de.mindcrimeilab.xsanalyzer.XsModelWalker;
39 import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
40 import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
41
42
43
44
45
46
47
48 public class TypeUsageCommand extends ApplicationWindowAwareCommand {
49
50 private static final String ID = "typeUsageCommand";
51
52 public TypeUsageCommand() {
53 super(TypeUsageCommand.ID);
54 }
55
56
57
58
59
60
61 @Override
62 protected void doExecuteCommand() {
63 final XsAnalyzerApplicationModel model = (XsAnalyzerApplicationModel) Application.instance().getApplicationContext().getBean("applicationModel");
64 final JFrame parent = Application.instance().getActiveWindow().getControl();
65
66 final VisualProgressWorker worker = new VisualProgressWorker(parent) {
67
68 @Override
69 protected Object doInBackground() throws Exception {
70 final XSModel xsmodel = model.getSchemaModel();
71
72 final XSNamespaceItemList nsList = xsmodel.getNamespaceItems();
73 final List<XSNamespaceItem> namespaceItems = new LinkedList<XSNamespaceItem>();
74
75 for (int i = 0; i < nsList.getLength(); ++i) {
76 XSNamespaceItem item = nsList.item(i);
77 namespaceItems.add(item);
78 }
79
80 Set<? extends XSTypeDefinition> definedTypes = XSModelHelper.getComponents(xsmodel, namespaceItems);
81
82 TypeUsageWorker typeUsageFinder = new TypeUsageWorker();
83 UnusedTypeFinderWorker unusedTypeFinder = new UnusedTypeFinderWorker(definedTypes);
84
85 final PropertyChangeListener changeListener = new PropertyChangeListener() {
86
87 @Override
88 public void propertyChange(PropertyChangeEvent evt) {
89 setDetailMessage("Examining type:" + evt.getNewValue());
90 }
91
92 };
93
94 XsModelWalker walker = new XsModelWalker();
95 walker.addPropertyChangeListener(changeListener);
96 walker.addWorker(unusedTypeFinder);
97 walker.addWorker(typeUsageFinder);
98 walker.walkModel(xsmodel);
99 walker.removePropertyChangeListener(changeListener);
100 model.setUnusedTypes(unusedTypeFinder.getUnusedTypes());
101 model.setUsedTypes(typeUsageFinder.getUsedBy());
102 return model;
103 }
104
105 @Override
106 protected void done() {
107 Application.instance().getActiveWindow().getPage().showView("typeUsageView");
108 Application.instance().getApplicationContext().publishEvent(new XsAnalyzerApplicationEvent(EventType.TYPE_USAGE, this));
109
110 super.done();
111 }
112 };
113
114 worker.setTitle("Operation in progress...");
115 worker.setActionText("Searching for unused types & determining type usage");
116 worker.showProgressDialog();
117 worker.execute();
118 }
119 }