1 22 23 package org.gjt.sp.jedit.buffer; 24 25 import javax.swing.event.ListDataListener ; 26 import java.util.*; 27 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.helpers.DefaultHandler ; 31 32 import org.gjt.sp.jedit.gui.MutableListModel; 33 import org.gjt.sp.util.XMLUtilities; 34 35 40 public class KillRing implements MutableListModel 41 { 42 public static KillRing getInstance() 44 { 45 return killRing; 46 } 48 public static void setInstance(KillRing killRing) 50 { 51 KillRing.killRing = killRing; 52 } 54 public void propertiesChanged(int historySize) 56 { 57 int newSize = Math.max(1, historySize); 58 if(ring == null) 59 ring = new UndoManager.Remove[newSize]; 60 else if(newSize != ring.length) 61 { 62 UndoManager.Remove[] newRing = new UndoManager.Remove[ 63 newSize]; 64 int newCount = Math.min(getSize(),newSize); 65 for(int i = 0; i < newCount; i++) 66 { 67 newRing[i] = (UndoManager.Remove)getElementAt(i); 68 } 69 ring = newRing; 70 count = newCount; 71 wrap = false; 72 } 73 74 if(count == ring.length) 75 { 76 count = 0; 77 wrap = true; 78 } 79 } 81 public void load() {} 82 83 public void save() {} 84 85 public void addListDataListener(ListDataListener listener) {} 87 88 public void removeListDataListener(ListDataListener listener) {} 89 90 public Object getElementAt(int index) 92 { 93 return ring[virtualToPhysicalIndex(index)]; 94 } 96 public int getSize() 98 { 99 if(wrap) 100 return ring.length; 101 else 102 return count; 103 } 105 public boolean removeElement(Object value) 107 { 108 for(int i = 0; i < getSize(); i++) 109 { 110 if(ring[i].equals(value)) 111 { 112 remove(i); 113 return true; 114 } 115 } 116 return false; 117 } 119 public void insertElementAt(Object value, int index) 121 { 122 125 remove(index); 126 add((UndoManager.Remove)value); 127 } 129 131 133 void changed(UndoManager.Remove rem) 135 { 136 if(rem.inKillRing) 137 { 138 int length = (wrap ? ring.length : count); 140 int kill = -1; 141 142 for(int i = 0; i < length; i++) 143 { 144 if(ring[i] != rem 145 && ring[i].hashcode == rem.hashcode 146 && ring[i].str.equals(rem.str)) 147 { 148 kill = i; 151 break; 152 } 153 } 154 155 if(kill != -1) 156 remove(kill); 157 } 158 else 159 add(rem); 160 } 162 void add(UndoManager.Remove rem) 164 { 165 int length = (wrap ? ring.length : count); 167 for(int i = 0; i < length; i++) 168 { 169 if(ring[i].hashcode == rem.hashcode) 170 { 171 if(ring[i].str.equals(rem.str)) 173 { 174 return; 177 } 178 } 179 } 180 181 boolean allWhitespace = true; 183 for(int i = 0; i < rem.str.length(); i++) 184 { 185 if(!Character.isWhitespace(rem.str.charAt(i))) 186 { 187 allWhitespace = false; 188 break; 189 } 190 } 191 192 if(allWhitespace) 193 return; 194 195 rem.inKillRing = true; 196 197 if(ring[count] != null) 198 ring[count].inKillRing = false; 199 200 ring[count] = rem; 201 if(++count >= ring.length) 202 { 203 wrap = true; 204 count = 0; 205 } 206 } 208 void remove(int i) 210 { 211 if(wrap) 212 { 213 UndoManager.Remove[] newRing = new UndoManager.Remove[ 214 ring.length]; 215 int newCount = 0; 216 for(int j = 0; j < ring.length; j++) 217 { 218 int index = virtualToPhysicalIndex(j); 219 220 if(i == index) 221 { 222 ring[index].inKillRing = false; 223 continue; 224 } 225 226 newRing[newCount++] = ring[index]; 227 } 228 ring = newRing; 229 count = newCount; 230 wrap = false; 231 } 232 else 233 { 234 System.arraycopy(ring,i + 1,ring,i,count - i - 1); 235 count--; 236 } 237 } 239 241 protected UndoManager.Remove[] ring; 243 protected int count; 244 protected boolean wrap; 245 protected long killRingModTime; 246 private static KillRing killRing = new KillRing(); 247 248 253 private int virtualToPhysicalIndex(int index) 254 { 255 if(wrap) 256 { 257 if(index < count) 258 return count - index - 1; 259 else 260 return count + ring.length - index - 1; 261 } 262 else 263 return count - index - 1; 264 } 266 268 public static class KillRingHandler extends DefaultHandler 270 { 271 public List<UndoManager.Remove> list = new LinkedList<UndoManager.Remove>(); 272 273 public InputSource resolveEntity(String publicId, String systemId) 274 { 275 return XMLUtilities.findEntity(systemId, "killring.dtd", getClass()); 276 } 277 278 public void startElement(String uri, String localName, 279 String qName, Attributes attrs) 280 { 281 inEntry = qName.equals("ENTRY"); 282 } 283 284 public void endElement(String uri, String localName, String name) 285 { 286 if(name.equals("ENTRY")) 287 { 288 list.add(new UndoManager.Remove(null,0,0,charData.toString())); 289 inEntry = false; 290 charData.setLength(0); 291 } 292 } 293 294 public void characters(char[] ch, int start, int length) 295 { 296 if (inEntry) 297 charData.append(ch, start, length); 298 } 299 300 private StringBuffer charData = new StringBuffer (); 301 private boolean inEntry; 302 } } 304 | Popular Tags |