001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax;
042:
043: import java.util.StringTokenizer;
044:
045: /**
046: *
047: * @author Libor Kramolis
048: * @version 0.1
049: */
050: public abstract class TreeData extends TreeChild {
051:
052: /** */
053: public static final String PROP_DATA = "data"; // NOI18N
054:
055: /** */
056: private String data;
057:
058: //
059: // init
060: //
061:
062: /**
063: * Creates new TreeData.
064: * @throws InvalidArgumentException
065: */
066: protected TreeData(String data) throws InvalidArgumentException {
067: super ();
068:
069: checkData(data);
070: this .data = data;
071: }
072:
073: /** Creates new TreeData -- copy constructor. */
074: protected TreeData(TreeData data) {
075: super (data);
076:
077: this .data = data.data;
078: }
079:
080: //
081: // from TreeObject
082: //
083:
084: /**
085: */
086: public boolean equals(Object object, boolean deep) {
087: if (!!!super .equals(object, deep))
088: return false;
089:
090: TreeData peer = (TreeData) object;
091: if (!!!Util.equals(this .getData(), peer.getData()))
092: return false;
093:
094: return true;
095: }
096:
097: /*
098: * Merge data property.
099: */
100: public void merge(TreeObject treeObject)
101: throws CannotMergeException {
102: super .merge(treeObject);
103:
104: TreeData peer = (TreeData) treeObject;
105:
106: try {
107: setDataImpl(peer.getData());
108: } catch (Exception exc) {
109: throw new CannotMergeException(treeObject, exc);
110: }
111: }
112:
113: //
114: // itself
115: //
116:
117: /**
118: */
119: public final String getData() {
120: return data;
121: }
122:
123: /**
124: */
125: private final void setDataImpl(String newData) {
126: String oldData = this .data;
127:
128: this .data = newData;
129:
130: if (Util.THIS.isLoggable()) /* then */
131: Util.THIS
132: .debug("TreeData::setDataImpl: firing data change "
133: + oldData + " => " + newData); // NOI18N
134:
135: firePropertyChange(PROP_DATA, oldData, newData);
136: }
137:
138: /**
139: * @throws ReadOnlyException
140: * @throws InvalidArgumentException
141: */
142: public final void setData(String newData) throws ReadOnlyException,
143: InvalidArgumentException {
144: //
145: // check new value
146: //
147: if (Util.equals(this .data, newData))
148: return;
149: checkReadOnly();
150: checkData(newData);
151:
152: //
153: // set new value
154: //
155: setDataImpl(newData);
156: }
157:
158: /**
159: */
160: protected abstract void checkData(String data)
161: throws InvalidArgumentException;
162:
163: /**
164: */
165: public final int getLength() {
166: return data.length();
167: }
168:
169: /**
170: * @throws InvalidArgumentException
171: */
172: public final String substringData(int offset, int count)
173: throws InvalidArgumentException {
174: try {
175: return data.substring(offset, offset + count);
176: } catch (IndexOutOfBoundsException ex) {
177: throw new InvalidArgumentException(ex);
178: }
179: }
180:
181: /**
182: * @throws ReadOnlyException
183: * @throws InvalidArgumentException
184: */
185: public final void appendData(String appData)
186: throws ReadOnlyException, InvalidArgumentException {
187: setData(data + appData);
188: }
189:
190: /**
191: * @throws ReadOnlyException
192: * @throws InvalidArgumentException
193: */
194: public final void insertData(int offset, String inData)
195: throws ReadOnlyException, InvalidArgumentException {
196: checkReadOnly();
197: try {
198: String preData = data.substring(0, offset);
199: String postData = data.substring(offset, data.length());
200: setData(preData + inData + postData);
201: } catch (IndexOutOfBoundsException ex) {
202: throw new InvalidArgumentException(ex);
203: }
204: }
205:
206: /**
207: * @throws ReadOnlyException
208: * @throws InvalidArgumentException
209: */
210: public final void deleteData(int offset, int count)
211: throws ReadOnlyException, InvalidArgumentException {
212: checkReadOnly();
213: try {
214: String preData = data.substring(0, offset);
215: String postData = data.substring(offset + count, data
216: .length());
217: setData(preData + postData);
218: } catch (IndexOutOfBoundsException ex) {
219: throw new InvalidArgumentException(ex);
220: }
221: }
222:
223: /**
224: * @throws ReadOnlyException
225: * @throws InvalidArgumentException
226: */
227: public final void replaceData(int offset, int count, String repData)
228: throws ReadOnlyException, InvalidArgumentException {
229: checkReadOnly();
230: try {
231: String preData = data.substring(0, offset);
232: String postData = data.substring(offset + count, data
233: .length());
234: setData(preData + repData + postData);
235: } catch (IndexOutOfBoundsException ex) {
236: throw new InvalidArgumentException(ex);
237: }
238: }
239:
240: /**
241: * @throws ReadOnlyException
242: * @throws InvalidArgumentException
243: */
244: public final TreeData splitData(int offset)
245: throws ReadOnlyException, InvalidArgumentException {
246: checkReadOnly();
247: TreeData splitedData;
248: try {
249: String preData = data.substring(0, offset);
250: String postData = data.substring(offset, data.length());
251: splitedData = createData(preData);
252: setData(postData);
253: } catch (IndexOutOfBoundsException ex) {
254: throw new InvalidArgumentException(ex);
255: }
256: return splitedData;
257: }
258:
259: /**
260: * @throws InvalidArgumentException
261: */
262: protected abstract TreeData createData(String data)
263: throws InvalidArgumentException;
264:
265: /**
266: */
267: public final boolean onlyWhiteSpaces() {
268: if (Util.THIS.isLoggable()) /* then */
269: Util.THIS.debug("TreeData::onlyWhiteSpaces: data = '"
270: + data + "'"); // NOI18N
271:
272: String trimed = data.trim();
273:
274: if (Util.THIS.isLoggable()) /* then */
275: Util.THIS.debug(" ::onlyWhiteSpaces: trimed = '"
276: + trimed + "'"); // NOI18N
277:
278: return (trimed.length() == 0);
279: }
280:
281: }
|