1 19 20 21 package org.openide.text; 22 23 import java.beans.VetoableChangeListener ; 24 import javax.swing.text.*; 25 import javax.swing.undo.UndoManager ; 26 27 33 class NbLikeEditorKit extends DefaultEditorKit { 34 public javax.swing.text.Document createDefaultDocument() { 35 return new Doc (); 36 } 37 38 class Doc extends PlainDocument 39 implements NbDocument.WriteLockable, StyledDocument { 40 43 public Doc() { 44 super (new StringContent ()); 45 46 putProperty ("supportsModificationListener", Boolean.TRUE); 48 } 49 50 public void runAtomic (Runnable r) { 51 try { 52 runAtomicAsUser (r); 53 } catch (BadLocationException ex) { 54 } 56 } 57 58 public void runAtomicAsUser (Runnable r) throws BadLocationException { 59 insOrRemoveOrRunnable (-1, null, null, -1, false, r); 60 } 61 62 public javax.swing.text.Style getLogicalStyle(int p) { 63 return null; 64 } 65 66 public javax.swing.text.Style getStyle(java.lang.String nm) { 67 return null; 68 } 69 70 public javax.swing.text.Style addStyle(java.lang.String nm, javax.swing.text.Style parent) { 71 return null; 72 } 73 74 public void setParagraphAttributes(int offset, int length, javax.swing.text.AttributeSet s, boolean replace) { 75 } 76 77 public void setCharacterAttributes(int offset, int length, javax.swing.text.AttributeSet s, boolean replace) { 78 } 79 80 public void removeStyle(java.lang.String nm) { 81 } 82 83 public java.awt.Font getFont(javax.swing.text.AttributeSet attr) { 84 return null; 85 } 86 87 public java.awt.Color getBackground(javax.swing.text.AttributeSet attr) { 88 return null; 89 } 90 91 public javax.swing.text.Element getCharacterElement(int pos) { 92 return null; 93 } 94 95 public void setLogicalStyle(int pos, javax.swing.text.Style s) { 96 } 97 98 public java.awt.Color getForeground(javax.swing.text.AttributeSet attr) { 99 return null; 100 } 101 102 private int changes; 103 public void insertString (int offs, String str, AttributeSet a) throws BadLocationException { 104 insOrRemoveOrRunnable (offs, str, a, 0, true, null); 105 } 106 107 public void remove (int offs, int len) throws BadLocationException { 108 insOrRemoveOrRunnable (offs, null, null, len, false, null); 109 } 110 111 112 private void insOrRemoveOrRunnable (int offset, String str, AttributeSet set, int len, boolean insert, Runnable run) 113 throws BadLocationException { 114 boolean alreadyInsideWrite = getCurrentWriter () == Thread.currentThread (); 115 if (alreadyInsideWrite) { 116 if (run != null) { 117 run.run (); 118 } else { 119 assertOffset (offset); 120 if (insert) { 121 super.insertString (offset, str, set); 122 } else { 123 super.remove(offset, len); 124 } 125 } 126 return; 127 } 128 129 Object o = getProperty ("modificationListener"); 130 131 if (run != null) { 132 writeLock (); 133 int prevChanges = changes; 134 try { 135 run.run (); 136 } finally { 137 writeUnlock (); 138 } 139 if (changes > prevChanges) { 140 try { 141 notifyModified (o, offset); 142 } catch (BadLocationException ex) { 143 } 145 } 146 } else { 147 assertOffset (offset); 148 notifyModified (o, offset); 149 try { 150 if (insert) { 151 super.insertString (offset, str, set); 152 } else { 153 super.remove(offset, len); 154 } 155 } catch (BadLocationException ex) { 156 if (o instanceof VetoableChangeListener ) { 157 VetoableChangeListener l = (VetoableChangeListener )o; 158 try { 159 l.vetoableChange (new java.beans.PropertyChangeEvent (this, "modified", null, Boolean.FALSE)); 160 } catch (java.beans.PropertyVetoException ignore) { 161 } 162 } 163 throw ex; 164 } 165 } 166 } 167 168 private void assertOffset (int offset) throws BadLocationException { 169 if (offset < 0) throw new BadLocationException ("", offset); 170 } 171 172 private void notifyModified (Object o, int offset) throws BadLocationException { 173 if (o instanceof VetoableChangeListener ) { 174 VetoableChangeListener l = (VetoableChangeListener )o; 175 try { 176 l.vetoableChange (new java.beans.PropertyChangeEvent (this, "modified", null, Boolean.TRUE)); 177 } catch (java.beans.PropertyVetoException ex) { 178 throw new BadLocationException("Document modification vetoed", offset); 179 } 180 } 181 } 182 183 protected void fireRemoveUpdate (javax.swing.event.DocumentEvent e) { 184 super.fireRemoveUpdate(e); 185 changes++; 186 } 187 188 protected void fireInsertUpdate (javax.swing.event.DocumentEvent e) { 189 super.fireInsertUpdate(e); 190 changes++; 191 } 192 193 protected void fireChangedUpdate (javax.swing.event.DocumentEvent e) { 194 super.fireChangedUpdate(e); 195 changes++; 196 } 197 198 } } 200 | Popular Tags |