KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > text > DefaultDocument


1 /* $Id $ */
2 /*
3  * $Id: DefaultDocument.java,v 1.3 2004/12/01 07:54:29 hengels Exp $
4  * Copyright 2000,2005 wingS development team.
5  *
6  * This file is part of wingS (http://www.j-wings.org).
7  *
8  * wingS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * Please see COPYING for the complete licence.
14  */

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 JavaDoc;
21 import javax.swing.text.BadLocationException JavaDoc;
22
23 /**
24  * @author hengels
25  * @version $Revision: 1.3 $
26  */

27 public class DefaultDocument implements SDocument {
28     private final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
29     private EventListenerList JavaDoc listeners = null;
30
31     public DefaultDocument() {
32     }
33
34     public DefaultDocument(String JavaDoc text) {
35         buffer.append(text);
36     }
37
38     public void setText(String JavaDoc text) {
39         String JavaDoc 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 JavaDoc getText() {
50         return buffer.toString();
51     }
52
53     public String JavaDoc getText(int offset, int length) throws BadLocationException JavaDoc {
54         try {
55             return buffer.substring(offset, length);
56         } catch (IndexOutOfBoundsException JavaDoc e) {
57             throw new BadLocationException JavaDoc(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 JavaDoc {
66         if (length == 0) {
67             return;
68         }
69         try {
70             buffer.delete(offset, offset + length);
71             fireRemoveUpdate(offset, length);
72         } catch (IndexOutOfBoundsException JavaDoc e) {
73             throw new BadLocationException JavaDoc(e.getMessage(), offset);
74         }
75     }
76
77     public void insert(int offset, String JavaDoc string) throws BadLocationException JavaDoc {
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 JavaDoc e) {
85             throw new BadLocationException JavaDoc(e.getMessage(), offset);
86         }
87     }
88
89     public void addDocumentListener(SDocumentListener listener) {
90         if (listeners == null)
91             listeners = new EventListenerList JavaDoc();
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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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