View Javadoc

1   // $Id: TypeUsageCommand.java 165 2009-05-28 21:46:38Z agony $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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   * @author Michael Engelhardt<me@mindcrime-ilab.de>
44   * @author $Author: agony $
45   * @version $Revision: 165 $
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       * (non-Javadoc)
58       * 
59       * @see org.springframework.richclient.command.ActionCommand#doExecuteCommand()
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 }