View Javadoc

1   // $Id: SameNameDifferentTypeCommand.java 164 2009-05-27 18:08:13Z 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.Map;
23  import java.util.concurrent.ExecutionException;
24  
25  import javax.swing.JFrame;
26  import javax.xml.namespace.QName;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.xerces.xs.XSModel;
31  import org.apache.xerces.xs.XSObjectList;
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.SameNameDifferentTypeFinderWorker;
37  import de.mindcrimeilab.xsanalyzer.XsModelWalker;
38  import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
39  
40  /**
41   * 
42   * @author Michael Engelhardt<me@mindcrime-ilab.de>
43   * @author $Author: agony $
44   * @version $Revision: 164 $
45   * 
46   */
47  public class SameNameDifferentTypeCommand extends ApplicationWindowAwareCommand {
48  
49      private static final String ID = "sameNameDifferentTypeCommand";
50  
51      /** logger */
52      private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
53  
54      /**
55       * ctor()
56       */
57      public SameNameDifferentTypeCommand() {
58          super(SameNameDifferentTypeCommand.ID);
59      }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.springframework.richclient.command.ActionCommand#doExecuteCommand()
65       */
66      @Override
67      protected void doExecuteCommand() {
68          final XsAnalyzerApplicationModel model = (XsAnalyzerApplicationModel) Application.instance().getApplicationContext().getBean("applicationModel");
69          final JFrame parent = Application.instance().getActiveWindow().getControl();
70          final VisualProgressWorker<Map<QName, ? extends XSObjectList>, Map.Entry<QName, ? extends XSObjectList>> worker = new VisualProgressWorker<Map<QName, ? extends XSObjectList>, Map.Entry<QName, ? extends XSObjectList>>(parent) {
71  
72              @Override
73              protected Map<QName, ? extends XSObjectList> doInBackground() throws Exception {
74                  final XSModel xsmodel = model.getSchemaModel();
75  
76                  final PropertyChangeListener changeListener = new PropertyChangeListener() {
77  
78                      @Override
79                      public void propertyChange(PropertyChangeEvent evt) {
80                          setDetailMessage("Examining type:" + evt.getNewValue());
81                      }
82  
83                  };
84  
85                  final SameNameDifferentTypeFinderWorker sameNameFinder = new SameNameDifferentTypeFinderWorker();
86                  final XsModelWalker walker = new XsModelWalker();
87                  walker.addPropertyChangeListener(changeListener);
88                  walker.addWorker(sameNameFinder);
89                  walker.walkModel(xsmodel);
90  
91                  return sameNameFinder.getTypesWithSameName();
92              }
93  
94              @SuppressWarnings("unchecked")
95              @Override
96              protected void done() {
97                  try {
98                      Map<QName, XSObjectList> sameNames = (Map<QName, XSObjectList>) get();
99                      logger.debug("Found different types with same name [" + sameNames + "]");
100                     model.setSameNamesDifferentTypes(sameNames);
101                     Application.instance().getActiveWindow().getPage().showView("sameNameDifferentTypesView");
102                     Application.instance().getApplicationContext().publishEvent(new XsAnalyzerApplicationEvent(EventType.SAME_NAMES, this));
103                 }
104                 catch (InterruptedException e) {
105                     logger.error("Failed to analyze schema for same names with different types.", e);
106                     e.printStackTrace();
107                 }
108                 catch (ExecutionException e) {
109                     logger.error("Failed to analyze schema for same names with different types.", e);
110                     e.printStackTrace();
111                 }
112                 finally {
113                     super.done();
114                 }
115             }
116 
117         };
118         worker.setTitle("Operation in progress...");
119         worker.setActionText("Searching for similar type definitions");
120         worker.showProgressDialog();
121         worker.execute();
122     }
123 }