1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.beans.*; 25 import java.io.*; 26 import javax.swing.text.BadLocationException ; 27 28 import org.openide.nodes.Node; 29 import org.openide.ErrorManager; 30 import org.openide.text.PositionBounds; 31 32 33 40 public abstract class Element implements Serializable { 41 42 43 private transient PropertyChangeSupport support = new PropertyChangeSupport(this); 44 45 47 protected PositionBounds bounds; 48 49 50 51 protected Element(PositionBounds bounds) { 52 this.bounds = bounds; 53 } 54 55 56 57 public PositionBounds getBounds() { 58 return bounds; 59 } 60 61 65 void update(Element elem) { 66 this.bounds = elem.bounds; 67 } 68 69 74 protected final void firePropertyChange(String name, Object o, Object n) { 75 support.firePropertyChange (name, o, n); 76 } 77 78 79 public void addPropertyChangeListener (PropertyChangeListener l) { 80 support.addPropertyChangeListener (l); 81 } 82 83 84 public void removePropertyChangeListener (PropertyChangeListener l) { 85 support.removePropertyChangeListener (l); 86 } 87 88 91 public final void print() { 92 if (bounds == null) { 93 return; 94 } 95 try { 96 bounds.setText(getDocumentString()); 97 } catch (BadLocationException e) { 98 ErrorManager.getDefault().notify(e); 99 } catch (IOException e) { 100 ErrorManager.getDefault().notify(e); 101 } 102 } 103 104 109 public abstract String getDocumentString(); 110 111 115 public String toString() { 116 if (bounds == null) { 117 return "(no bounds)"; 118 } 119 return new StringBuffer (16) 120 .append('(') 121 .append(bounds.getBegin().getOffset()) 122 .append(", ") .append(bounds.getEnd().getOffset()) 124 .append(')') 125 .toString(); 126 } 127 128 129 130 public static abstract class Basic extends Element { 131 132 133 protected String value; 134 135 136 protected Basic(PositionBounds bounds, String value) { 137 super(bounds); 138 this.value = value; 139 } 140 141 145 void update(Element elem) { 146 super.update(elem); 147 this.value = ((Basic)elem).value; 148 } 149 150 153 public String toString() { 154 return value + " " + super.toString(); } 156 157 161 public String getValue() { 162 return value; 163 } 164 165 171 public void setValue(String value) { 172 this.value = value; 173 this.print(); 174 } 175 176 } 178 179 180 public static class KeyElem extends Basic { 181 182 183 static final long serialVersionUID =6828294289485744331L; 184 185 186 187 protected KeyElem(PositionBounds bounds, String value) { 188 super(bounds, value); 189 } 190 191 192 195 public String getDocumentString() { 196 return UtilConvert.saveConvert(value, true) + "="; 197 } 198 } 200 201 202 public static class ValueElem extends Basic { 203 204 205 static final long serialVersionUID =4662649023463958853L; 206 207 208 protected ValueElem(PositionBounds bounds, String value) { 209 super(bounds, value); 210 } 211 212 215 public String getDocumentString() { 216 return UtilConvert.saveConvert(value) + "\n"; 218 } 219 } 221 226 public static class CommentElem extends Basic { 227 228 229 static final long serialVersionUID =2418308580934815756L; 230 231 232 237 protected CommentElem(PositionBounds bounds, String value) { 238 super(bounds, value); 239 } 240 241 242 246 public String getDocumentString() { 247 if (value == null || value.length() == 0) 248 return ""; else { 250 StringBuffer sb = new StringBuffer (value); 253 if (sb.charAt(sb.length() - 1) != '\n') { 255 sb.append('\n'); 256 } 257 int lineStart = 0; 258 boolean hasCommentChar = false; 259 for (int i=0; i<sb.length(); i++) { 260 char aChar = sb.charAt(i); 261 if (aChar == '\n') { 263 String line = sb.substring(lineStart, i); 264 String convertedLine = UtilConvert.saveConvert(line); 265 sb.replace(lineStart, i, convertedLine); 266 267 i += convertedLine.length() - line.length(); 269 270 lineStart = i + 1; 272 273 hasCommentChar = false; 274 } else if (!hasCommentChar 275 && UtilConvert.whiteSpaceChars.indexOf(aChar) == -1) { 276 if ((aChar == '#') || (aChar == '!')) { 278 lineStart = i + 1; 279 } else { 280 sb.insert(lineStart, '#'); 282 i++; 283 lineStart = i; 284 } 285 hasCommentChar = true; 286 } 287 } 288 return sb.toString(); 289 } 290 } 291 } 293 294 298 public static class ItemElem extends Element implements Node.Cookie { 299 300 301 private KeyElem key; 302 303 304 private ValueElem value; 305 306 307 private CommentElem comment; 308 309 310 private PropertiesStructure parent; 311 312 313 public static final String PROP_ITEM_KEY = "key"; 315 public static final String PROP_ITEM_VALUE = "value"; 317 public static final String PROP_ITEM_COMMENT = "comment"; 319 320 static final long serialVersionUID =1078147817847520586L; 321 322 323 324 protected ItemElem(PositionBounds bounds, KeyElem key, ValueElem value, CommentElem comment) { 325 super(bounds); 326 this.key = key; 327 this.value = value; 328 this.comment = comment; 329 } 330 331 332 333 void setParent(PropertiesStructure ps) { 334 parent = ps; 335 } 336 337 339 private PropertiesStructure getParent() { 340 if(parent == null) { 341 throw new IllegalStateException ("Resource Bundle: Parent is missing"); } 343 344 return parent; 345 } 346 347 350 public String toString() { 351 return comment.toString() + "\n" + ((key == null) ? "" : key.toString()) + "\n" + ((value == null) ? "" : value.toString()) + "\n"; } 355 356 357 public KeyElem getKeyElem() { 358 return key; 359 } 360 361 362 public ValueElem getValueElem() { 363 return value; 364 } 365 366 367 public CommentElem getCommentElem() { 368 return comment; 369 } 370 371 void update(Element elem) { 372 super.update(elem); 373 if (this.key == null) 374 this.key = ((ItemElem)elem).key; 375 else 376 this.key.update(((ItemElem)elem).key); 377 378 if (this.value == null) 379 this.value = ((ItemElem)elem).value; 380 else 381 this.value.update(((ItemElem)elem).value); 382 383 this.comment.update(((ItemElem)elem).comment); 384 } 385 386 public String getDocumentString() { 387 return comment.getDocumentString() + 388 ((key == null) ? "" : key.getDocumentString()) + ((value == null) ? "" : value.getDocumentString()); } 391 392 395 public String getKey() { 396 return (key == null) ? null : key.getValue(); 397 } 398 399 402 public void setKey(String newKey) { 403 String oldKey = key.getValue(); 404 if (!oldKey.equals(newKey)) { 405 key.setValue(newKey); 406 getParent().itemKeyChanged(oldKey, this); 407 this.firePropertyChange(PROP_ITEM_KEY, oldKey, newKey); 408 } 409 } 410 411 412 public String getValue() { 413 return (value == null) ? null : value.getValue(); 414 } 415 416 419 public void setValue(String newValue) { 420 String oldValue = value.getValue(); 421 if (!oldValue.equals(newValue)) { 422 423 if(oldValue.equals("")) key.print(); 426 427 value.setValue(newValue); 428 getParent().itemChanged(this); 429 this.firePropertyChange(PROP_ITEM_VALUE, oldValue, newValue); 430 } 431 } 432 433 434 public String getComment() { 435 return (comment == null) ? null : comment.getValue(); 436 } 437 438 443 public void setComment(String newComment) { 444 String oldComment = comment.getValue(); 445 if ((oldComment == null && newComment != null) || (oldComment != null && !oldComment.equals(newComment))) { 446 comment.setValue(newComment); 447 getParent().itemChanged(this); 448 this.firePropertyChange(PROP_ITEM_COMMENT, oldComment, newComment); 449 } 450 } 451 452 453 public boolean equals(Object item) { 454 if (item == null || !(item instanceof ItemElem)) 455 return false; 456 ItemElem ie = (ItemElem)item; 457 if ( ((key==null && ie.getKeyElem()==null) || (key!=null && ie.getKeyElem()!=null && getKey().equals(ie.getKey())) ) && 458 ((value==null && ie.getValueElem()==null) || (value!=null && ie.getValueElem()!=null && getValue().equals(ie.getValue())) ) && 459 ((comment==null && ie.getCommentElem()==null) || (comment!=null && ie.getCommentElem()!=null && getComment().equals(ie.getComment())) ) ) 460 return true; 461 return false; 462 } 463 } } 465 | Popular Tags |