1 2 15 package org.wings.text; 16 17 import org.wings.event.SDocumentEvent; 18 import org.wings.event.SDocumentListener; 19 20 import javax.swing.event.EventListenerList ; 21 import javax.swing.text.BadLocationException ; 22 23 27 public class DefaultDocument implements SDocument { 28 private final StringBuffer buffer = new StringBuffer (); 29 private EventListenerList listeners = null; 30 31 public DefaultDocument() { 32 } 33 34 public DefaultDocument(String text) { 35 buffer.append(text); 36 } 37 38 public void setText(String text) { 39 String origText = buffer.toString(); 40 if (origText.equals(text)) { 41 return; 42 } 43 buffer.setLength(0); 44 if (text != null) 45 buffer.append(text); 46 fireChangeUpdate(0, buffer.length()); 47 } 48 49 public String getText() { 50 return buffer.toString(); 51 } 52 53 public String getText(int offset, int length) throws BadLocationException { 54 try { 55 return buffer.substring(offset, length); 56 } catch (IndexOutOfBoundsException e) { 57 throw new BadLocationException (e.getMessage(), offset); 58 } 59 } 60 61 public int getLength() { 62 return buffer.length(); 63 } 64 65 public void remove(int offset, int length) throws BadLocationException { 66 if (length == 0) { 67 return; 68 } 69 try { 70 buffer.delete(offset, offset + length); 71 fireRemoveUpdate(offset, length); 72 } catch (IndexOutOfBoundsException e) { 73 throw new BadLocationException (e.getMessage(), offset); 74 } 75 } 76 77 public void insert(int offset, String string) throws BadLocationException { 78 if (string == null || string.length() == 0) { 79 return; 80 } 81 try { 82 buffer.insert(offset, string); 83 fireInsertUpdate(offset, string.length()); 84 } catch (IndexOutOfBoundsException e) { 85 throw new BadLocationException (e.getMessage(), offset); 86 } 87 } 88 89 public void addDocumentListener(SDocumentListener listener) { 90 if (listeners == null) 91 listeners = new EventListenerList (); 92 listeners.add(SDocumentListener.class, listener); 93 } 94 95 public void removeDocumentListener(SDocumentListener listener) { 96 if (listeners == null) 97 return; 98 listeners.remove(SDocumentListener.class, listener); 99 } 100 101 protected void fireInsertUpdate(int offset, int length) { 102 if (listeners == null || listeners.getListenerCount() == 0) 103 return; 104 105 SDocumentEvent e = new SDocumentEvent(this, offset, length, SDocumentEvent.INSERT); 106 107 Object [] listeners = this.listeners.getListenerList(); 108 for (int i = listeners.length - 2; i >= 0; i -= 2) { 109 ((SDocumentListener) listeners[i + 1]).insertUpdate(e); 110 } 111 } 112 113 protected void fireRemoveUpdate(int offset, int length) { 114 if (listeners == null || listeners.getListenerCount() == 0) 115 return; 116 117 SDocumentEvent e = new SDocumentEvent(this, offset, length, SDocumentEvent.INSERT); 118 119 Object [] listeners = this.listeners.getListenerList(); 120 for (int i = listeners.length - 2; i >= 0; i -= 2) { 121 ((SDocumentListener) listeners[i + 1]).removeUpdate(e); 122 } 123 } 124 125 protected void fireChangeUpdate(int offset, int length) { 126 if (listeners == null || listeners.getListenerCount() == 0) 127 return; 128 129 SDocumentEvent e = new SDocumentEvent(this, offset, length, SDocumentEvent.INSERT); 130 131 Object [] listeners = this.listeners.getListenerList(); 132 for (int i = listeners.length - 2; i >= 0; i -= 2) { 133 ((SDocumentListener) listeners[i + 1]).changedUpdate(e); 134 } 135 } 136 } 137 | Popular Tags |