Source code: com/virtuosotechnologies/asaph/standardmodel/BaseStringList.java
1 /*
2 ================================================================================
3
4 FILE: BaseStringList.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Abstract base implementation of StringList
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.standardmodel;
43
44
45 import java.io.IOException;
46 import javax.swing.event.UndoableEditListener;
47 import org.xml.sax.Attributes;
48 import org.xml.sax.SAXException;
49 import org.xml.sax.ErrorHandler;
50 import org.xml.sax.Locator;
51
52 import com.virtuosotechnologies.lib.util.CollectingCompoundEdit;
53 import com.virtuosotechnologies.lib.xml.XMLUnparser;
54
55 import com.virtuosotechnologies.asaph.model.StringList;
56 import com.virtuosotechnologies.asaph.model.SimpleString;
57
58
59 /**
60 * Abstract base implementation of StringList
61 */
62 /*package*/ abstract class BaseStringList
63 extends BaseSongMember
64 implements
65 StringList
66 {
67 private ListHelper stringList_;
68
69
70 /*package*/ BaseStringList(
71 BaseSongMember parent)
72 {
73 super(parent);
74 stringList_ = new ListHelper(this);
75 }
76
77
78 /*package*/ void unparse(
79 XMLUnparser unparser,
80 String element)
81 throws
82 IOException
83 {
84 unparser.startMultiLineElement(element);
85 unparseAttributes(unparser);
86 for (StdSimpleString member = (StdSimpleString)stringList_.getNextObject(null);
87 member != null; member = (StdSimpleString)stringList_.getNextObject(member))
88 {
89 member.unparse(unparser, XMLConstants.STRINGLISTMEMBER_ELEMENT);
90 }
91 unparser.endElement(element);
92 }
93
94
95 /*package*/ void unparseAttributes(
96 XMLUnparser unparser)
97 throws
98 IOException
99 {
100 }
101
102
103 /*package*/ class ParseHandler
104 extends ParseHandlerBase
105 {
106 /*package*/ ParseHandler(
107 ErrorHandler errorHandler,
108 Locator locator,
109 String localElement)
110 {
111 super(errorHandler, locator, localElement);
112 }
113
114 /*package*/ ParseHandlerBase localStartElement(
115 String uri,
116 String localName,
117 String qName,
118 Attributes attributes)
119 throws
120 SAXException
121 {
122 if (localName.equals(XMLConstants.STRINGLISTMEMBER_ELEMENT))
123 {
124 StdSimpleString nstring = new StdSimpleString(BaseStringList.this, "");
125 stringList_.appendObject(nstring, null);
126 return nstring.new ParseHandler(getErrorHandler(), getDocumentLocator(),
127 XMLConstants.STRINGLISTMEMBER_ELEMENT);
128 }
129 else
130 {
131 return super.localStartElement(uri, localName, qName, attributes);
132 }
133 }
134 }
135
136
137 //-------------------------------------------------------------------------
138 // Methods of StringList
139 //-------------------------------------------------------------------------
140
141 /**
142 * Get the number of strings in the list
143 *
144 * @return number of strings
145 */
146 public int getStringCount()
147 {
148 return stringList_.getCount();
149 }
150
151
152 /**
153 * Get the nth string
154 *
155 * @param n index
156 * @return SimpleString
157 */
158 public SimpleString getNthString(
159 int n)
160 {
161 return (StdSimpleString)stringList_.getNthObject(n);
162 }
163
164
165 /**
166 * Get the next string following reference.
167 * If reference is null, returns the first string.
168 * If reference is the last string, returns null;
169 *
170 * @param reference reference SimpleString
171 * @return next string
172 * @exception IllegalArgumentException reference is not a member
173 */
174 public SimpleString getNextString(
175 SimpleString reference)
176 {
177 return (StdSimpleString)stringList_.getNextObject(reference);
178 }
179
180
181 /**
182 * Get the previous string preceding reference.
183 * If reference is null, returns the last string.
184 * If reference is the first string, returns null;
185 *
186 * @param reference reference SimpleString
187 * @return previous string
188 * @exception IllegalArgumentException reference is not a member
189 */
190 public SimpleString getPreviousString(
191 SimpleString reference)
192 {
193 return (StdSimpleString)stringList_.getPreviousObject(reference);
194 }
195
196
197 /**
198 * Add a string to the list at the given position
199 *
200 * @param before insert before this string, or at the end if null
201 * @param str string value
202 * @param undoListener listener to notify if an undoable edit is generated,
203 * or null to suppress generation of undoable edits
204 * @return SimpleString for new string
205 * @exception IllegalArgumentException before is not a member
206 * @exception NullPointerException str was null
207 */
208 public SimpleString insertStringBefore(
209 SimpleString before,
210 String str,
211 UndoableEditListener undoListener)
212 {
213 StdSimpleString nstring = new StdSimpleString(this, str);
214 stringList_.insertObjectBefore(nstring, before, undoListener);
215 return nstring;
216 }
217
218
219 /**
220 * Add a string to the list at the given position
221 *
222 * @param after insert after this string, or at the beginning if null
223 * @param str string value
224 * @param undoListener listener to notify if an undoable edit is generated,
225 * or null to suppress generation of undoable edits
226 * @return SimpleString for new string
227 * @exception IllegalArgumentException after is not a member
228 * @exception NullPointerException str was null
229 */
230 public SimpleString insertStringAfter(
231 SimpleString after,
232 String str,
233 UndoableEditListener undoListener)
234 {
235 StdSimpleString nstring = new StdSimpleString(this, str);
236 stringList_.insertObjectAfter(nstring, after, undoListener);
237 return nstring;
238 }
239
240
241 /**
242 * Remove a SimpleString from the list.
243 *
244 * @param str string to remove
245 * @param undoListener listener to notify if an undoable edit is generated,
246 * or null to suppress generation of undoable edits
247 * @exception IllegalArgumentException str is not a member
248 * @exception NullPointerException str was null
249 */
250 public void removeString(
251 SimpleString str,
252 UndoableEditListener undoListener)
253 {
254 stringList_.removeObject(str, undoListener);
255 }
256
257
258 /**
259 * Clear the string list
260 *
261 * @param undoListener listener to notify if an undoable edit is generated,
262 * or null to suppress generation of undoable edits
263 */
264 public void clear(
265 UndoableEditListener undoListener)
266 {
267 CollectingCompoundEdit editCollector = null;
268 if (undoListener != null)
269 {
270 editCollector = new CollectingCompoundEdit();
271 }
272
273 stringList_.clear(editCollector);
274
275 if (undoListener != null)
276 {
277 editCollector.end();
278 internalReportUndoableEdit(undoListener, editCollector);
279 }
280 }
281 }