|                                                                                                              1
 19
 20  package org.netbeans.lib.editor.codetemplates;
 21
 22  import java.util.ArrayList
  ; 23  import java.util.Collections
  ; 24  import java.util.List
  ; 25  import javax.swing.event.DocumentEvent
  ; 26  import javax.swing.event.DocumentListener
  ; 27  import javax.swing.text.BadLocationException
  ; 28  import javax.swing.text.Document
  ; 29  import javax.swing.text.Position
  ; 30  import org.netbeans.lib.editor.util.CharSequenceUtilities;
 31  import org.netbeans.lib.editor.util.swing.DocumentUtilities;
 32  import org.netbeans.lib.editor.util.swing.MutablePositionRegion;
 33  import org.netbeans.lib.editor.util.swing.PositionRegion;
 34  import org.openide.ErrorManager;
 35
 36
 41  public final class SyncDocumentRegion {
 42
 43      private Document
  doc; 44
 45      private List
  regions; 46
 47      private List
  sortedRegions; 48
 49      private boolean regionsSortPerformed;
 50
 51
 59      public SyncDocumentRegion(Document
  doc, List  regions) { 60          this.doc = doc;
 61          this.regions = regions;
 62                  regionsSortPerformed = PositionRegion.isRegionsSorted(regions);
 64          if (regionsSortPerformed) {
 65              sortedRegions = regions;
 66          } else {
 67              sortedRegions = new ArrayList
  (regions); 68              Collections.sort(sortedRegions, PositionRegion.getComparator());
 69          }
 70      }
 71
 72      public int getRegionCount() {
 73          return regions.size();
 74      }
 75
 76      public MutablePositionRegion getRegion(int regionIndex) {
 77          return (MutablePositionRegion)regions.get(regionIndex);
 78      }
 79
 80      public int getFirstRegionStartOffset() {
 81          return getRegion(0).getStartOffset();
 82      }
 83
 84      public int getFirstRegionEndOffset() {
 85          return getRegion(0).getEndOffset();
 86      }
 87
 88      public int getFirstRegionLength() {
 89          return getFirstRegionEndOffset() - getFirstRegionStartOffset();
 90      }
 91
 92
 98      public MutablePositionRegion getSortedRegion(int regionIndex) {
 99           return (MutablePositionRegion)sortedRegions.get(regionIndex);
 100     }
 101
 102
 109     public void sync(int moveStartDownLength) {
 110         if (moveStartDownLength != 0) {
 111                         MutablePositionRegion firstRegion = getRegion(0);
 113             try {
 114                 Position
  newStartPos = doc.createPosition( 115                         firstRegion.getStartOffset() - moveStartDownLength);
 116                 firstRegion.setStartPosition(newStartPos);
 117
 118             } catch (BadLocationException
  e) { 119                 ErrorManager.getDefault().notify(e);
 120             }
 121
 122         }
 123
 124         String
  firstRegionText = getFirstRegionText(); 125         if (firstRegionText != null) {
 126             int regionCount = getRegionCount();
 127             for (int i = 1; i < regionCount; i++) {
 128                 MutablePositionRegion region = getRegion(i);
 129                 int offset = region.getStartOffset();
 130                 int length = region.getEndOffset() - offset;
 131                 try {
 132                     if (!CharSequenceUtilities.textEquals(firstRegionText, DocumentUtilities.getText(doc, offset, length))) {
 133                         doc.remove(offset, length);
 134                         if (firstRegionText.length() > 0) {
 135                             doc.insertString(offset, firstRegionText, null);
 136                         }
 137                     }
 138                                         Position
  newStartPos = doc.createPosition(offset); 140                     region.setStartPosition(newStartPos);
 141                 } catch (BadLocationException
  e) { 142                     ErrorManager.getDefault().notify(e);
 143                 }
 144
 145             }
 146         }
 147     }
 148
 149     private String
  getFirstRegionText() { 150         return getRegionText(0);
 151     }
 152
 153     private String
  getRegionText(int regionIndex) { 154         try {
 155             MutablePositionRegion region = getRegion(regionIndex);
 156             int offset = region.getStartOffset();
 157             int length = region.getEndOffset() - offset;
 158             return doc.getText(offset, length);
 159         } catch (BadLocationException
  e) { 160             ErrorManager.getDefault().notify(e);
 161             return null;
 162         }
 163     }
 164
 165 }
 166
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |