View Javadoc

1   // $Id: SimilarTypesListModelAdapter.java 167 2009-05-29 17:43:07Z 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.model;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.TreeMap;
25  import java.util.Map.Entry;
26  
27  import javax.swing.AbstractListModel;
28  import javax.swing.ListModel;
29  
30  import org.apache.xerces.xs.XSObject;
31  import org.apache.xerces.xs.XSObjectList;
32  
33  import de.mindcrimeilab.xsanalyzer.util.NameComparator;
34  
35  /**
36   * Adapts the {@code Map} of similar types to a {@code ListModel}.
37   * 
38   * @author Michael Engelhardt<me@mindcrime-ilab.de>
39   * @author $Author: agony $
40   * @version $Revision: 167 $
41   * 
42   */
43  public class SimilarTypesListModelAdapter extends AbstractListModel implements ListModel {
44  
45      /**
46       * serial version uid
47       */
48      private static final long serialVersionUID = -525227687405772834L;
49  
50      /**
51       * Represents the list model view to the expanded values of the given map of similar types.
52       */
53      private final List<Entry<XSObject, String>> model;
54  
55      /**
56       * Copy of the map containing the similar types. This property is used to fill the result of this model with all
57       * information.
58       */
59      private final Map<String, ? extends XSObjectList> data;
60  
61      /**
62       * ctor()
63       * 
64       * @param similarTypes
65       *            Map of similar types.
66       */
67      public SimilarTypesListModelAdapter(final Map<String, ? extends XSObjectList> similarTypes) {
68          data = Collections.unmodifiableMap(similarTypes);
69  
70          // use TreeMap to provide a sorted list view to the user
71          Map<XSObject, String> map = new TreeMap<XSObject, String>(new NameComparator());
72          // expand all values of the map and construct a list representing each
73          // value of the map associated to its key.
74          for (Entry<String, ? extends XSObjectList> entry : data.entrySet()) {
75              XSObjectList list = entry.getValue();
76              for (int i = 0; null != list && i < list.getLength(); ++i) {
77                  map.put(list.item(i), entry.getKey());
78              }
79          }
80          model = new ArrayList<Entry<XSObject, String>>(map.entrySet());
81      }
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see javax.swing.ListModel#getElementAt(int)
87       */
88      @Override
89      public Object getElementAt(int index) {
90          Entry<XSObject, String> entry = model.get(index);
91          return new SimilarTypeListModelEntry(entry.getKey(), entry.getValue(), this.data.get(entry.getValue()));
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see javax.swing.ListModel#getSize()
98       */
99      @Override
100     public int getSize() {
101         return model.size();
102     }
103 }