1 19 20 package org.openide.src; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.beans.PropertyVetoException ; 26 import java.beans.VetoableChangeListener ; 27 import java.beans.VetoableChangeSupport ; 28 import java.io.PrintWriter ; 29 import java.io.Serializable ; 30 import java.io.StringWriter ; 31 import javax.swing.JEditorPane ; 32 import javax.swing.text.DefaultEditorKit ; 33 import javax.swing.text.Document ; 34 import javax.swing.text.EditorKit ; 35 import javax.swing.text.StyledDocument ; 36 import org.openide.ErrorManager; 37 import org.openide.nodes.Node; 38 import org.openide.text.FilterDocument; 39 import org.openide.text.IndentEngine; 40 41 49 public abstract class Element extends Object 50 implements Serializable , ElementProperties, Node.Cookie { 51 52 53 protected Impl impl; 54 55 56 protected Impl2 impl2; 57 58 static final long serialVersionUID =967040188302141522L; 59 64 protected Element(Impl impl) { 65 this.impl = impl; 66 if (impl instanceof Impl2) { 67 this.impl2 = (Impl2)impl; 68 } 69 impl.attachedToElement (this); 70 } 71 72 76 public final void addPropertyChangeListener (PropertyChangeListener l) { 77 impl.addPropertyChangeListener (l); 78 } 79 80 84 public final void removePropertyChangeListener (PropertyChangeListener l) { 85 impl.removePropertyChangeListener (l); 86 } 87 88 94 public final void addVetoableChangeListener(VetoableChangeListener l) { 95 if (impl2 != null) 96 impl2.addVetoableChangeListener(l); 97 } 98 99 103 public final void removeVetoableChangeListener(VetoableChangeListener l) { 104 if (impl2 != null) 105 impl2.removeVetoableChangeListener(l); 106 } 107 108 113 public void markCurrent(boolean beforeAfter) { 114 impl.markCurrent(beforeAfter); 115 } 116 117 127 public Node.Cookie getCookie(Class type) { 128 Node.Cookie c = impl.getCookie(type); 129 if ((c == null) && type.isAssignableFrom(getClass())) 130 c = this; 131 132 return c; 133 } 134 135 protected Object writeReplace() { 136 return impl; 137 } 138 139 143 public abstract void print(ElementPrinter printer) throws ElementPrinterInterruptException; 144 145 151 static boolean print(Element[] el, ElementPrinter printer) throws ElementPrinterInterruptException { 152 for (int i = 0; i < el.length; i++) { 153 if (i > 0) { 154 printer.println(""); printer.println(""); } 157 el[i].print(printer); 158 } 159 return (el.length > 0); 160 } 161 162 169 static void printJavaDoc(JavaDoc doc, ElementPrinter printer) throws ElementPrinterInterruptException { 170 if (doc.isEmpty()) 171 return; 172 173 String javaText = doc.getRawText(); 175 String lines[]=javaText.split("\\n"); int i; 177 178 printer.print("/**\n"); 179 for (i=0;i<lines.length;i++) { 180 String line=lines[i]; 181 182 printer.print("*"); 183 if (line.length() > 0 && line.charAt(0) != ' ') 184 printer.print(" "); 185 printer.println(line); 186 } 187 printer.print(" */"); } 189 190 194 StyledDocument createDocument() { 195 EditorKit kit = JEditorPane.createEditorKitForContentType("text/x-java"); if (kit == null) { 197 kit = new DefaultEditorKit (); 198 } 199 Document doc = kit.createDefaultDocument(); 200 if (doc instanceof StyledDocument ) { 201 return (StyledDocument )doc; 202 } 203 return new FilterDocument(doc); 204 } 205 206 209 void throwSourceException(String message) throws SourceException { 210 SourceException ex = new SourceException(message); 211 ErrorManager.getDefault().annotate(ex, ErrorManager.USER, null, message, null, null); 212 throw ex; 213 } 214 215 void throwSourceException(Throwable nested) throws SourceException { 216 SourceException ex = new SourceException(nested.getMessage()); 217 ErrorManager.getDefault().annotate(ex, nested); 218 throw ex; 219 } 220 221 226 public String toString() { 227 StringWriter sw = new StringWriter (); 228 StyledDocument doc = createDocument(); 229 IndentEngine indentator = IndentEngine.find(doc); 230 PrintWriter pw = new PrintWriter (indentator.createWriter(doc, 0, sw)); 231 try { 233 print(new DefaultElementPrinter(pw)); 234 } 235 catch (ElementPrinterInterruptException e) { 236 } 238 pw.close(); 239 return sw.toString(); 240 } 241 242 245 public interface Impl extends Serializable { 246 247 long serialVersionUID = -3246061193296761293L; 248 256 public void attachedToElement (Element el); 257 258 261 public void addPropertyChangeListener (PropertyChangeListener l); 262 263 266 public void removePropertyChangeListener (PropertyChangeListener l); 267 268 274 public Object readResolve(); 275 276 283 public Node.Cookie getCookie(Class type); 284 285 290 public void markCurrent(boolean beforeAfter); 291 } 292 293 300 public interface Impl2 extends Impl { 301 304 public void addVetoableChangeListener(VetoableChangeListener l); 305 306 309 public void removeVetoableChangeListener(VetoableChangeListener l); 310 311 317 public boolean isValid(); 318 } 319 320 323 static abstract class Memory implements Element.Impl2, Node.Cookie { 324 325 protected Element element; 326 327 328 private PropertyChangeSupport support; 329 330 333 private VetoableChangeSupport vetoSupport; 334 335 static final long serialVersionUID =7734412320645883859L; 336 337 public void attachedToElement (Element element) { 338 this.element = element; 339 } 340 341 346 protected final void firePropertyChange (String name, Object o, Object n) { 347 if (support != null) { 348 support.firePropertyChange (name, o, n); 349 } 350 } 351 352 protected final void firePropertyChange(PropertyChangeEvent evt) { 353 if (support != null) 354 support.firePropertyChange(evt); 355 } 356 357 358 public synchronized void addPropertyChangeListener (PropertyChangeListener l) { 359 if (support == null) { 360 synchronized (this) { 361 if (support == null) { 363 support = new PropertyChangeSupport (element); 364 } 365 } 366 } 367 support.addPropertyChangeListener (l); 368 } 369 370 371 public void removePropertyChangeListener (PropertyChangeListener l) { 372 if (support != null) { 373 support.removePropertyChangeListener (l); 374 } 375 } 376 377 public void addVetoableChangeListener(VetoableChangeListener l) { 378 if (vetoSupport == null) { 379 synchronized (this) { 380 if (vetoSupport == null) 381 vetoSupport = new VetoableChangeSupport (this); 382 } 383 } 384 vetoSupport.addVetoableChangeListener(l); 385 } 386 387 public void removeVetoableChangeListener(VetoableChangeListener l) { 388 if (vetoSupport != null) 389 vetoSupport.removeVetoableChangeListener(l); 390 } 391 392 protected void fireVetoableChange(String name, Object o, Object v) throws PropertyVetoException { 393 if (vetoSupport != null) 394 vetoSupport.fireVetoableChange(name, o, v); 395 } 396 397 protected void fireVetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { 398 if (vetoSupport != null) 399 vetoSupport.fireVetoableChange(evt); 400 } 401 402 406 public Node.Cookie getCookie(Class type) { 407 if (type.isAssignableFrom(getClass())) { 408 return this; 409 } else { 410 return null; 411 } 412 } 413 414 419 public void markCurrent(boolean beforeAfter) { 420 } 422 423 public boolean isValid() { 424 return true; 425 } 426 } 427 } 428 | Popular Tags |