/[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.4.2.7 by gnu_andrew, Wed Nov 2 00:44:03 2005 UTC revision 1.4.2.8 by gnu_andrew, Sun Nov 27 21:00:42 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        Element firstElement = rootElement.getElement(elementIndex);
115        
116        // added and removed are Element arrays used to add an ElementEdit
117        // to the DocumentEvent if there were entire lines added or removed.
118        Element[] removed = new Element[1];
119        Element[] added;
120        try
121          {
122            String str = content.getString(0, content.length());
123            ArrayList elts = new ArrayList();
124    
125            // Determine how many NEW lines were added by finding the newline
126            // characters within the newly inserted text
127            int j = firstElement.getStartOffset();
128            int i = str.indexOf('\n', offset);
129            while (i != -1 && i <= end)
130              {            
131                // For each new line, create a new element
132                elts.add(createLeafElement(rootElement, SimpleAttributeSet.EMPTY,
133                                           j, i + 1));
134                j = i + 1;
135                if (j >= str.length())
136                  break;
137                i = str.indexOf('\n', j);
138              }
139            // If there were new lines added we have to add an ElementEdit to
140            // the DocumentEvent and we have to call rootElement.replace to
141            // insert the new lines
142            if (elts.size() != 0)
143              {
144                // Set up the ElementEdit by filling the added and removed
145                // arrays with the proper Elements
146                added = new Element[elts.size()];
147                for (int k = 0; k < elts.size(); ++k)
148                  added[k] = (Element) elts.get(k);
149                removed[0] = firstElement;
150                
151                // Now create and add the ElementEdit
152                ElementEdit e = new ElementEdit(rootElement, elementIndex, removed,
153                                                added);
154                event.addEdit(e);
155                
156                // And call replace to actually make the changes
157                ((BranchElement) rootElement).replace(elementIndex, 1, added);
158              }
159          }
160        catch (BadLocationException e)
161          {
162            // This shouldn't happen so we throw an AssertionError
163            AssertionError ae = new AssertionError();
164            ae.initCause(e);
165            throw ae;
166          }
167      super.insertUpdate(event, attributes);      super.insertUpdate(event, attributes);
168    }    }
169    
# Line 116  public class PlainDocument extends Abstr Line 171  public class PlainDocument extends Abstr
171    {    {
172      super.removeUpdate(event);      super.removeUpdate(event);
173    
174        // added and removed are Element arrays used to add an ElementEdit
175        // to the DocumentEvent if there were entire lines added or removed
176        // from the Document
177        Element[] added = new Element[1];
178        Element[] removed;
179      int p0 = event.getOffset();      int p0 = event.getOffset();
     int len = event.getLength();  
     int p1 = len + p0;  
180    
181      // check if we must collapse some elements      // check if we must collapse some elements
182      int i1 = rootElement.getElementIndex(p0);      int i1 = rootElement.getElementIndex(p0);
183      int i2 = rootElement.getElementIndex(p1);      int i2 = rootElement.getElementIndex(p0 + event.getLength());
184      if (i1 != i2)      if (i1 != i2)
185        {        {
186          Element el1 = rootElement.getElement(i1);          // If there were lines removed then we have to add an ElementEdit
187          Element el2 = rootElement.getElement(i2);          // to the DocumentEvent so we set it up now by filling the Element
188          int start = el1.getStartOffset();          // arrays "removed" and "added" appropriately
189          int end = el2.getEndOffset();          removed = new Element [i2 - i1 + 1];
190          // collapse elements if the removal spans more than 1 line          for (int i = i1; i <= i2; i++)
191          Element newEl = createLeafElement(rootElement,            removed[i-i1] = rootElement.getElement(i);
192            
193            int start = rootElement.getElement(i1).getStartOffset();
194            int end = rootElement.getElement(i2).getEndOffset();        
195            added[0] = createLeafElement(rootElement,
196                                            SimpleAttributeSet.EMPTY,                                            SimpleAttributeSet.EMPTY,
197                                            start, end);                                            start, end);
198          rootElement.replace(i1, i2 - i1 + 1, new Element[]{ newEl });  
199            // Now create and add the ElementEdit
200            ElementEdit e = new ElementEdit(rootElement, i1, removed, added);
201            event.addEdit(e);
202    
203            // collapse elements if the removal spans more than 1 line
204            rootElement.replace(i1, i2 - i1 + 1, added);
205        }        }
206    }    }
207    
# Line 167  public class PlainDocument extends Abstr Line 235  public class PlainDocument extends Abstr
235      throws BadLocationException      throws BadLocationException
236    {    {
237      String string = str;      String string = str;
238      if (Boolean.TRUE.equals(getProperty("filterNewlines")))      if (str != null && Boolean.TRUE.equals(getProperty("filterNewlines")))
239        string = str.replaceAll("\n", " ");        string = str.replaceAll("\n", " ");
240      super.insertString(offs, string, atts);      super.insertString(offs, string, atts);
241    }    }

Legend:
Removed from v.1.4.2.7  
changed lines
  Added in v.1.4.2.8

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