1 19 package org.netbeans.tax; 20 21 import java.util.StringTokenizer ; 22 23 28 public abstract class TreeData extends TreeChild { 29 30 31 public static final String PROP_DATA = "data"; 33 34 private String data; 35 36 37 41 45 protected TreeData (String data) throws InvalidArgumentException { 46 super (); 47 48 checkData (data); 49 this.data = data; 50 } 51 52 53 protected TreeData (TreeData data) { 54 super (data); 55 56 this.data = data.data; 57 } 58 59 60 64 66 public boolean equals (Object 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 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 exc) { 88 throw new CannotMergeException (treeObject, exc); 89 } 90 } 91 92 93 97 99 public final String getData () { 100 return data; 101 } 102 103 105 private final void setDataImpl (String newData) { 106 String oldData = this.data; 107 108 this.data = newData; 109 110 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeData::setDataImpl: firing data change " + oldData + " => " +newData); 112 firePropertyChange (PROP_DATA, oldData, newData); 113 } 114 115 119 public final void setData (String newData) throws ReadOnlyException, InvalidArgumentException { 120 if ( Util.equals (this.data, newData) ) 124 return; 125 checkReadOnly (); 126 checkData (newData); 127 128 setDataImpl (newData); 132 } 133 134 135 137 protected abstract void checkData (String data) throws InvalidArgumentException; 138 139 141 public final int getLength () { 142 return data.length (); 143 } 144 145 146 149 public final String substringData (int offset, int count) throws InvalidArgumentException { 150 try { 151 return data.substring (offset, offset + count); 152 } catch (IndexOutOfBoundsException ex) { 153 throw new InvalidArgumentException (ex); 154 } 155 } 156 157 161 public final void appendData (String appData) throws ReadOnlyException, InvalidArgumentException { 162 setData (data + appData); 163 } 164 165 169 public final void insertData (int offset, String inData) throws ReadOnlyException, InvalidArgumentException { 170 checkReadOnly (); 171 try { 172 String preData = data.substring (0, offset); 173 String postData = data.substring (offset, data.length ()); 174 setData (preData + inData + postData); 175 } catch (IndexOutOfBoundsException ex) { 176 throw new InvalidArgumentException (ex); 177 } 178 } 179 180 184 public final void deleteData (int offset, int count) throws ReadOnlyException, InvalidArgumentException { 185 checkReadOnly (); 186 try { 187 String preData = data.substring (0, offset); 188 String postData = data.substring (offset + count, data.length ()); 189 setData (preData + postData); 190 } catch (IndexOutOfBoundsException ex) { 191 throw new InvalidArgumentException (ex); 192 } 193 } 194 195 199 public final void replaceData (int offset, int count, String repData) throws ReadOnlyException, InvalidArgumentException { 200 checkReadOnly (); 201 try { 202 String preData = data.substring (0, offset); 203 String postData = data.substring (offset + count, data.length ()); 204 setData (preData + repData + postData); 205 } catch (IndexOutOfBoundsException ex) { 206 throw new InvalidArgumentException (ex); 207 } 208 } 209 210 214 public final TreeData splitData (int offset) throws ReadOnlyException, InvalidArgumentException { 215 checkReadOnly (); 216 TreeData splitedData; 217 try { 218 String preData = data.substring (0, offset); 219 String postData = data.substring (offset, data.length ()); 220 splitedData = createData (preData); 221 setData (postData); 222 } catch (IndexOutOfBoundsException ex) { 223 throw new InvalidArgumentException (ex); 224 } 225 return splitedData; 226 } 227 228 231 protected abstract TreeData createData (String data) throws InvalidArgumentException; 232 233 235 public final boolean onlyWhiteSpaces () { 236 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeData::onlyWhiteSpaces: data = '" + data + "'"); 238 String trimed = data.trim (); 239 240 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" ::onlyWhiteSpaces: trimed = '" + trimed + "'"); 242 return (trimed.length () == 0); 243 } 244 245 } 246 | Popular Tags |