KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  *
25  * @author Libor Kramolis
26  * @version 0.1
27  */

28 public abstract class TreeData extends TreeChild {
29
30     /** */
31     public static final String JavaDoc PROP_DATA = "data"; // NOI18N
32

33     /** */
34     private String JavaDoc data;
35
36
37     //
38
// init
39
//
40

41     /**
42      * Creates new TreeData.
43      * @throws InvalidArgumentException
44      */

45     protected TreeData (String JavaDoc data) throws InvalidArgumentException {
46         super ();
47
48         checkData (data);
49         this.data = data;
50     }
51     
52     /** Creates new TreeData -- copy constructor. */
53     protected TreeData (TreeData data) {
54         super (data);
55         
56         this.data = data.data;
57     }
58     
59     
60     //
61
// from TreeObject
62
//
63

64     /**
65      */

66     public boolean equals (Object JavaDoc object, boolean deep) {
67         if (!!! super.equals (object, deep))
68             return false;
69         
70         TreeData peer = (TreeData) object;
71         if (!!! Util.equals (this.getData (), peer.getData ()))
72             return false;
73         
74         return true;
75     }
76     
77     /*
78      * Merge data property.
79      */

80     public void merge (TreeObject treeObject) throws CannotMergeException {
81         super.merge (treeObject);
82         
83         TreeData peer = (TreeData) treeObject;
84         
85         try {
86             setDataImpl (peer.getData ());
87         } catch (Exception JavaDoc exc) {
88             throw new CannotMergeException (treeObject, exc);
89         }
90     }
91     
92     
93     //
94
// itself
95
//
96

97     /**
98      */

99     public final String JavaDoc getData () {
100         return data;
101     }
102     
103     /**
104      */

105     private final void setDataImpl (String JavaDoc newData) {
106         String JavaDoc oldData = this.data;
107         
108         this.data = newData;
109         
110         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeData::setDataImpl: firing data change " + oldData + " => " +newData); // NOI18N
111

112         firePropertyChange (PROP_DATA, oldData, newData);
113     }
114     
115     /**
116      * @throws ReadOnlyException
117      * @throws InvalidArgumentException
118      */

119     public final void setData (String JavaDoc newData) throws ReadOnlyException, InvalidArgumentException {
120         //
121
// check new value
122
//
123
if ( Util.equals (this.data, newData) )
124             return;
125         checkReadOnly ();
126         checkData (newData);
127         
128         //
129
// set new value
130
//
131
setDataImpl (newData);
132     }
133     
134     
135     /**
136      */

137     protected abstract void checkData (String JavaDoc data) throws InvalidArgumentException;
138     
139     /**
140      */

141     public final int getLength () {
142         return data.length ();
143     }
144     
145     
146     /**
147      * @throws InvalidArgumentException
148      */

149     public final String JavaDoc substringData (int offset, int count) throws InvalidArgumentException {
150         try {
151             return data.substring (offset, offset + count);
152         } catch (IndexOutOfBoundsException JavaDoc ex) {
153             throw new InvalidArgumentException (ex);
154         }
155     }
156     
157     /**
158      * @throws ReadOnlyException
159      * @throws InvalidArgumentException
160      */

161     public final void appendData (String JavaDoc appData) throws ReadOnlyException, InvalidArgumentException {
162         setData (data + appData);
163     }
164     
165     /**
166      * @throws ReadOnlyException
167      * @throws InvalidArgumentException
168      */

169     public final void insertData (int offset, String JavaDoc inData) throws ReadOnlyException, InvalidArgumentException {
170         checkReadOnly ();
171         try {
172             String JavaDoc preData = data.substring (0, offset);
173             String JavaDoc postData = data.substring (offset, data.length ());
174             setData (preData + inData + postData);
175         } catch (IndexOutOfBoundsException JavaDoc ex) {
176             throw new InvalidArgumentException (ex);
177         }
178     }
179     
180     /**
181      * @throws ReadOnlyException
182      * @throws InvalidArgumentException
183      */

184     public final void deleteData (int offset, int count) throws ReadOnlyException, InvalidArgumentException {
185         checkReadOnly ();
186         try {
187             String JavaDoc preData = data.substring (0, offset);
188             String JavaDoc postData = data.substring (offset + count, data.length ());
189             setData (preData + postData);
190         } catch (IndexOutOfBoundsException JavaDoc ex) {
191             throw new InvalidArgumentException (ex);
192         }
193     }
194     
195     /**
196      * @throws ReadOnlyException
197      * @throws InvalidArgumentException
198      */

199     public final void replaceData (int offset, int count, String JavaDoc repData) throws ReadOnlyException, InvalidArgumentException {
200         checkReadOnly ();
201         try {
202             String JavaDoc preData = data.substring (0, offset);
203             String JavaDoc postData = data.substring (offset + count, data.length ());
204             setData (preData + repData + postData);
205         } catch (IndexOutOfBoundsException JavaDoc ex) {
206             throw new InvalidArgumentException (ex);
207         }
208     }
209     
210     /**
211      * @throws ReadOnlyException
212      * @throws InvalidArgumentException
213      */

214     public final TreeData splitData (int offset) throws ReadOnlyException, InvalidArgumentException {
215         checkReadOnly ();
216         TreeData splitedData;
217         try {
218             String JavaDoc preData = data.substring (0, offset);
219             String JavaDoc postData = data.substring (offset, data.length ());
220             splitedData = createData (preData);
221             setData (postData);
222         } catch (IndexOutOfBoundsException JavaDoc ex) {
223             throw new InvalidArgumentException (ex);
224         }
225         return splitedData;
226     }
227     
228     /**
229      * @throws InvalidArgumentException
230      */

231     protected abstract TreeData createData (String JavaDoc data) throws InvalidArgumentException;
232     
233     /**
234      */

235     public final boolean onlyWhiteSpaces () {
236         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeData::onlyWhiteSpaces: data = '" + data + "'"); // NOI18N
237

238         String JavaDoc trimed = data.trim ();
239         
240         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::onlyWhiteSpaces: trimed = '" + trimed + "'"); // NOI18N
241

242         return (trimed.length () == 0);
243     }
244     
245 }
246
Popular Tags