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.textEditor;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  import sk.uniba.euromath.editor.textEditor.lang.Messages;
20  
21  /***
22   * Container for ITextPieceKeepers. As logical unit represents one text node.
23   * Text of that node is formated into more lines or line segments(first and last
24   * can be segments). Each line or line segment keeps one ITextPieceKeeper and is
25   * located (or displayed) by ITextLocator. Ordering keepers is like lines of
26   * text are ordered in node and only in this ordering keepers can be added.
27   * 
28   * @author Tomáš Studva 29.9.2005
29   */
30  public class TextPieceContainer implements ITextPieceContainer {
31  
32      /***
33       * Id of text node.
34       */
35      private String nodeID;
36  
37      /***
38       * Text of text node(s).
39       */
40      private String fullText;
41  
42      /***
43       * List of keepers in this container.
44       */
45      private final List<ITextPieceKeeper> keepers;
46  
47      /***
48       * Maps keepers to their ITextPieceInfo.
49       */
50      private final Map<ITextPieceKeeper, ITextPieceInfo> keeperInfoMap;
51  
52      /***
53       * Constructor.
54       * 
55       * @param firstPieceKeeper
56       *            first TextPieceKeeper in this container
57       * @param id
58       *            id of node corresponding to this TextPieceContainer
59       * @param fullText
60       *            fullText of this container
61       */
62      public TextPieceContainer(ITextPieceKeeper firstPieceKeeper, String id,
63              String fullText) {
64          super();
65          assert (id != null);
66          assert (firstPieceKeeper != null);
67  
68          // TODO: Kollar(asi pre teba) Studva please explain this
69          // vysvetlenie: keeperov text sa musi nachadzat vo full texte
70          // ale nie je to tak asi koli medzeram, nerozumiem tomu
71  //      if (fullText.indexOf(firstPieceKeeper.getText()) < 0)
72  //            throw new IllegalArgumentException(
73  //                    "Invalid fullText or firstPieceKeeper's text."); //$NON-NLS-1$
74          setNodeID(id);
75          this.fullText = fullText;
76          this.keepers = new ArrayList<ITextPieceKeeper>();
77          this.keeperInfoMap = new HashMap<ITextPieceKeeper, ITextPieceInfo>();
78  
79          add(firstPieceKeeper);
80      }
81  
82      /***
83       * Adds ITextPieceKeeper to container. Returns for that keeper
84       * ITextPieceInfo.
85       * 
86       * @param pieceKeeper
87       *            keeper to add
88       * @return ITextPieceInfo for pieceKeeper
89       */
90      public ITextPieceInfo add(ITextPieceKeeper pieceKeeper) {
91          // check id
92          assert (pieceKeeper.getTextLocator().getID() == getNodeID());
93  
94          // if is already in this container, do nothing
95          if (this.keeperInfoMap.get(pieceKeeper) != null)
96              return this.keeperInfoMap.get(pieceKeeper);
97  
98          this.keepers.add(pieceKeeper);
99          String text = pieceKeeper.getText();
100 
101         int prevOffset = 0;
102         int prevLength = 0;
103         if (getPieceCount() > 1) {
104             prevOffset = getPiece(getPieceCount() - 2).getTextPieceInfo()
105                     .getOffset();
106             prevLength = getPiece(getPieceCount() - 2).getText().length();
107         }
108         int index = prevOffset + prevLength;
109         if (index == -1)
110             throw new IllegalStateException(Messages
111                     .getString("Fail when determinig offset of ITextPieceInfo")); //$NON-NLS-1$
112         TextPieceInfoImpl pieceInfo = new TextPieceInfoImpl(getNodeID(), index,
113                 this.fullText.indexOf(text), text);
114         this.keeperInfoMap.put(pieceKeeper, pieceInfo);
115         return pieceInfo;
116     }
117 
118     /***
119      * @param index
120      *            index of piece
121      * @return ITextPieceKeeper or null if index is out of range
122      * @see ITextPieceContainer#getPiece(int)
123      */
124     public ITextPieceKeeper getPiece(int index) {
125         if ((index < 0) || (index >= this.keepers.size()))
126             return null;
127         return this.keepers.get(index);
128     }
129 
130     /***
131      * @see sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceContainer#getPieceCount()
132      */
133     public int getPieceCount() {
134         return this.keepers.size();
135     }
136 
137     /***
138      * @see sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceContainer#getInfo(sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceKeeper)
139      */
140     public ITextPieceInfo getInfo(ITextPieceKeeper forPiece) {
141         return this.keeperInfoMap.get(forPiece);
142     }
143 
144     /***
145      * 
146      * @see sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceContainer#getNext(sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceKeeper)
147      */
148     public ITextPieceKeeper getNext(ITextPieceKeeper piece) {
149         if (indexOf(piece) >= getPieceCount() - 1)
150             return null;
151         return getPiece(indexOf(piece) + 1);
152     }
153 
154     /***
155      * 
156      * @see sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceContainer#getPrevious(sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceKeeper)
157      */
158     public ITextPieceKeeper getPrevious(ITextPieceKeeper piece) {
159         if (indexOf(piece) <= 0)
160             return null;
161         return getPiece(indexOf(piece) - 1);
162     }
163 
164     /***
165      * @see sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceContainer#indexOf(sk.uniba.euromath.editor.textEditor.xmlTexts.TextPieceKeeper)
166      */
167     public int indexOf(ITextPieceKeeper piece) {
168         return this.keepers.indexOf(piece);
169     }
170 
171     /***
172      * @return Returns the nodeID.
173      */
174     public String getNodeID() {
175         return this.nodeID;
176     }
177 
178     /***
179      * @param nodeID
180      *            The nodeID to set.
181      */
182     public void setNodeID(String nodeID) {
183         this.nodeID = nodeID;
184     }
185 
186     public List<ITextPieceKeeper> getAll() {
187         return this.keepers;
188     }
189 
190     /***
191      * @return Returns the fullText.
192      */
193     protected String getFullText() {
194         return this.fullText;
195     }
196 
197     /***
198      * 
199      * @param offset offset of whole text in textual Node
200      * @return ITextPieceKeeper in that is this offset
201      */
202 	public ITextPieceKeeper OffsetInPieceKeeper(int offset) {
203 		if ( (offset < 0) || (offset > fullText.length()) )
204 			throw new IllegalArgumentException("Container does not contain this offset");
205 		
206 		for (ITextPieceKeeper keeper : getAll()){
207 			if ( (keeper.getTextPieceInfo().getOffset() >= offset) && 
208 				 (keeper.getTextPieceInfo().getLastIndex() <= offset) )
209 				return keeper;
210 		}
211 		return null;
212 	}
213 
214     /***
215      * @see ITextPieceContainer#getWholeContainerText()
216      */
217     public String getWholeContainerText() {
218         if (getPieceCount() < 0)
219             throw new IllegalStateException(
220                     "Container is in illegal state, because doesn't conatain this piece keeper, but should."); //$NON-NLS-1$
221         StringBuilder result = new StringBuilder();
222         for (int i = 0; i < getPieceCount(); i++) {
223             result.append(getPiece(i).getText());
224         }
225         return result.toString();
226     }
227 
228 }