/[classpath]/classpath/javax/swing/text/PlainDocument.java
ViewVC logotype

Diff of /classpath/javax/swing/text/PlainDocument.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by rabbit78, Thu Oct 27 14:53:42 2005 UTC revision 1.16 by abalkiss, Tue Nov 15 19:45:46 2005 UTC
# Line 105  public class PlainDocument extends Abstr Line 105  public class PlainDocument extends Abstr
105      return root;      return root;
106    }    }
107    
108    protected void insertUpdate(DefaultDocumentEvent event, AttributeSet attributes)    protected void insertUpdate(DefaultDocumentEvent event,
109                                  AttributeSet attributes)
110    {    {
111      reindex();      int offset = event.getOffset();
112        int end = offset + event.getLength();
113        int elementIndex = rootElement.getElementIndex(offset);
114        
115        // added and removed are Element arrays used to add an ElementEdit
116        // to the DocumentEvent if there were entire lines added or removed.
117        Element[] removed = new Element[1];
118        Element[] added;
119        try
120          {
121            String str = content.getString(0, content.length());
122            ArrayList elts = new ArrayList();
123    
124            // Determine how many NEW lines were added by finding the newline
125            // characters within the newly inserted text
126            int j = offset;
127            int i = str.indexOf('\n', j);
128            while (i != -1 && i <= end)
129              {            
130                // For each new line, create a new element
131                elts.add(createLeafElement(rootElement, SimpleAttributeSet.EMPTY,
132                                           j, i + 1));
133                j = i + 1;
134                if (j >= str.length())
135                  break;
136                i = str.indexOf('\n', j);
137              }
138            // If there were new lines added we have to add an ElementEdit to
139            // the DocumentEvent and we have to call rootElement.replace to
140            // insert the new lines
141            if (elts.size() != 0)
142              {
143                // Set up the ElementEdit by filling the added and removed
144                // arrays with the proper Elements
145                added = new Element[elts.size()];
146                for (int k = 0; k < elts.size(); ++k)
147                  added[k] = (Element) elts.get(k);
148                removed[0] = rootElement.getElement(elementIndex);
149                
150                // Now create and add the ElementEdit
151                ElementEdit e = new ElementEdit(rootElement, elementIndex, removed,
152                                                added);
153                event.addEdit(e);
154                
155                // And call replace to actually make the changes
156                ((BranchElement) rootElement).replace(elementIndex, 1, added);
157              }
158          }
159        catch (BadLocationException e)
160          {
161            // This shouldn't happen so we throw an AssertionError
162            AssertionError ae = new AssertionError();
163            ae.initCause(e);
164            throw ae;
165          }
166      super.insertUpdate(event, attributes);      super.insertUpdate(event, attributes);
167    }    }
168    
# Line 116  public class PlainDocument extends Abstr Line 170  public class PlainDocument extends Abstr
170    {    {
171      super.removeUpdate(event);      super.removeUpdate(event);
172    
173        // added and removed are Element arrays used to add an ElementEdit
174        // to the DocumentEvent if there were entire lines added or removed
175        // from the Document
176        Element[] added = new Element[1];
177        Element[] removed;
178      int p0 = event.getOffset();      int p0 = event.getOffset();
     int len = event.getLength();  
     int p1 = len + p0;  
179    
180      // check if we must collapse some elements      // check if we must collapse some elements
181      int i1 = rootElement.getElementIndex(p0);      int i1 = rootElement.getElementIndex(p0);
182      int i2 = rootElement.getElementIndex(p1);      int i2 = rootElement.getElementIndex(p0 + event.getLength());
183      if (i1 != i2)      if (i1 != i2)
184        {        {
185          Element el1 = rootElement.getElement(i1);          // If there were lines removed then we have to add an ElementEdit
186          Element el2 = rootElement.getElement(i2);          // to the DocumentEvent so we set it up now by filling the Element
187          int start = el1.getStartOffset();          // arrays "removed" and "added" appropriately
188          int end = el2.getEndOffset();          removed = new Element [i2 - i1 + 1];
189          // collapse elements if the removal spans more than 1 line          for (int i = i1; i <= i2; i++)
190          Element newEl = createLeafElement(rootElement,            removed[i-i1] = rootElement.getElement(i);
191            
192            int start = rootElement.getElement(i1).getStartOffset();
193            int end = rootElement.getElement(i2).getEndOffset();        
194            added[0] = createLeafElement(rootElement,
195                                            SimpleAttributeSet.EMPTY,                                            SimpleAttributeSet.EMPTY,
196                                            start, end);                                            start, end);
197          rootElement.replace(i1, i2 - i1 + 1, new Element[]{ newEl });  
198            // Now create and add the ElementEdit
199            ElementEdit e = new ElementEdit(rootElement, i1, removed, added);
200            event.addEdit(e);
201    
202            // collapse elements if the removal spans more than 1 line
203            rootElement.replace(i1, i2 - i1 + 1, added);
204        }        }
205    }    }
206    

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.16

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26