View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.xmlEditor;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import sk.uniba.euromath.editor.xmlEditor.editParts.XMLEditPart;
22  import sk.uniba.euromath.editor.xmlEditor.lang.Messages;
23  
24  /***
25   * Holds relation between node's ids from source XML document to EditParts which
26   * visualize them. One id can be visualized by one EditPart, many or by none.
27   * 
28   * @author Tomáš Studva 4.11.2005
29   */
30  public class IdVisualInfo {
31  
32      /***
33       * Ids currently in map - not only element's ids as elementIDs.
34       */
35      protected final Set<String> idSet;
36  
37      /***
38       * Ids of elements currently in map.
39       */
40      private final Set<String> elementIDs;
41  
42      /***
43       * Maps id to EditParts visually representing node with given id.
44       */
45      protected final Map<String, Set<XMLEditPart>> parts;
46  
47      /***
48       * Conatructor.
49       */
50      public IdVisualInfo() {
51          super();
52          this.idSet = new HashSet<String>();
53          this.parts = new HashMap<String, Set<XMLEditPart>>();
54          this.elementIDs = new HashSet<String>();
55      }
56  
57      /***
58       * Adds vusualPart to set of visualizers of xml node with id <code>id</code>.
59       * 
60       * @param id
61       *            Unique identifier of a node from XML data source, not null
62       * @param visualPart
63       *            An XMLEditPart visually representing node with given id, not
64       *            null
65       */
66      public void add(String id, XMLEditPart visualPart) {
67          if (id == null)
68              throw new IllegalArgumentException(Messages.getString("IdVisualInfo.0")); //$NON-NLS-1$
69          if (visualPart == null)
70              throw new IllegalArgumentException(Messages.getString("IdVisualInfo.1")); //$NON-NLS-1$
71  
72          if (!getIdSet().contains(id)) {
73              addNewID(id);
74          }
75  
76          getParts().get(id).add(visualPart);
77      }
78  
79      /***
80       * Adds id to this info. Helper method.
81       * 
82       * @param id
83       *            not null and not contained by info
84       */
85      private void addNewID(String id) {
86          getIdSet().add(id);
87          getParts().put(id, new HashSet<XMLEditPart>());
88          if ((id.indexOf(';') == -1) && (id.indexOf('@') == -1))
89              getElementIDs().add(id);
90      }
91  
92      /***
93       * Clears out holded info. Object state is same as after construction.
94       */
95      public void clear() {
96          getIdSet().clear();
97          getParts().clear();
98          getElementIDs().clear();
99      }
100 
101     /***
102      * Returns true if holds some info about desired id.
103      * 
104      * @param id
105      *            of node not null
106      * @return true if contains the id
107      */
108     public boolean contains(String id) {
109         if (id == null)
110             throw new IllegalArgumentException(Messages.getString("IdVisualInfo.2")); //$NON-NLS-1$
111         return getIdSet().contains(id);
112     }
113 
114     /***
115      * Returns how many EditParts represent id (this information is taken from
116      * object's state). If id was not yet added, zero is returned.
117      * 
118      * @param id
119      *            of node not null
120      * @return how many EditParts represent id
121      */
122     public int count(String id) {
123         if (id == null)
124             throw new IllegalArgumentException(Messages.getString("IdVisualInfo.3")); //$NON-NLS-1$
125         if (contains(id))
126             return getVisualParts(id).size();
127         return 0;
128     }
129 
130     /***
131      * Returns list of XMLEditParts which visualize node with id <code>id</code>.
132      * 
133      * @param id
134      *            of xml node, not null
135      * @return EditParts representing id in new instance of ArrayList
136      */
137     public List<XMLEditPart> getVisualParts(String id) {
138         if (id == null)
139             throw new IllegalArgumentException(Messages.getString("IdVisualInfo.4")); //$NON-NLS-1$
140         // id is not in info so empty array list is returned
141         if (!contains(id))
142             return new ArrayList<XMLEditPart>();
143         
144         return new ArrayList<XMLEditPart>(getParts().get(id));
145     }
146 
147     /***
148      * Returns set containing all element ids holded by info.
149      * 
150      * @return elements ids set by copy
151      */
152     public Set<String> getElementIds() {
153         return new HashSet<String>(this.elementIDs);
154     }
155 
156     /***
157      * Returns set containing all ids holded by info.
158      * 
159      * @return ids set by copy
160      */
161     public Set<String> getIds() {
162         return new HashSet<String>(this.idSet);
163     }
164 
165     /***
166      * @return Returns the elementIDs.
167      */
168     protected Set<String> getElementIDs() {
169         return this.elementIDs;
170     }
171 
172     /***
173      * @return Returns the idSet.
174      */
175     protected Set<String> getIdSet() {
176         return this.idSet;
177     }
178 
179     /***
180      * @return Returns the parts.
181      */
182     protected Map<String, Set<XMLEditPart>> getParts() {
183         return this.parts;
184     }
185 
186  /*   public String getNextElementID(String id) {
187         try {
188             int _id = Integer.parseInt(id);
189             Set ids = getElementIDs();
190             while (_id < maxID) {
191                 if (ids.contains(String.valueOf(++_id)))
192                     return String.valueOf(_id);
193             }
194         } catch (NumberFormatException nfe) {
195 
196         }
197         return null;
198     }*/
199 }