1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 import java.util.Vector ; 37 38 import edu.rice.cs.util.UnexpectedException; 39 40 57 public class ReducedModelControl implements BraceReduction { 58 59 volatile ReducedModelBrace _rmb; 60 volatile ReducedModelComment _rmc; 61 volatile int _offset; 62 63 69 private ReducedModelBrace getRMB() { 70 if (_rmb == null) _rmb = new ReducedModelBrace(this); 71 return _rmb; 72 } 73 74 private ReducedModelComment getRMC() { 75 if (_rmc == null) _rmc = new ReducedModelComment(); 76 return _rmc; 77 } 78 79 public void insertChar(char ch) { 80 getRMB().insertChar(ch); 81 getRMC().insertChar(ch); 82 } 83 84 88 public void move(int count) { 89 try { 90 getRMB().move(count); 91 getRMC().move(count); 92 } 93 catch(IllegalArgumentException e) { 94 resetLocation(); 95 throw new UnexpectedException(e); 96 } 97 } 98 99 103 public void delete(int count) { 104 getRMB().delete(count); 105 getRMC().delete(count); 106 } 107 108 112 public int balanceForward() { return getRMB().balanceForward(); } 113 114 118 public int balanceBackward() { return getRMB().balanceBackward(); } 119 120 123 public ReducedModelState moveWalkerGetState(int relDistance) { return getRMC().moveWalkerGetState(relDistance); } 124 125 130 public void resetLocation() { 131 getRMC().resetWalkerLocationToCursor(); 132 } 133 134 140 public ReducedToken currentToken() { 141 ReducedToken rmcToken = getRMC().current(); 143 if (! rmcToken.isGap()) return rmcToken; 144 ReducedToken rmbToken = getRMB().current(); 146 if (! rmbToken.isGap()) { 147 rmbToken.setState(getRMC().getStateAtCurrent()); 148 return rmbToken; 149 } 150 int size = getSize(rmbToken,rmcToken); 152 return new Gap(size, getRMC().getStateAtCurrent()); 153 } 154 155 159 public ReducedModelState getStateAtCurrent() { 160 return getRMC().getStateAtCurrent(); 161 } 162 163 167 String getType() { 168 ReducedToken rmcToken = getRMC().current(); 169 if (! rmcToken.isGap()) 170 return rmcToken.getType(); 171 172 ReducedToken rmbToken = getRMB().current(); 173 if (! rmbToken.isGap()) { 174 return rmbToken.getType(); 175 } 176 return ""; } 178 179 187 int getSize() { 188 return getSize(getRMB().current(),getRMC().current()); 189 } 190 191 int getSize(ReducedToken rmbToken, ReducedToken rmcToken) { 192 int rmb_offset = getRMB().getBlockOffset(); 193 int rmc_offset = getRMC().getBlockOffset(); 194 int rmb_size = rmbToken.getSize(); 195 int rmc_size = rmcToken.getSize(); 196 int size; 197 if (rmb_offset < rmc_offset) { 198 size = rmb_offset; 199 _offset = size; 200 } 201 else { 202 size = rmc_offset; 203 _offset = size; 204 } 205 206 if (rmb_size - rmb_offset < rmc_size - rmc_offset) { 207 size += (rmb_size - rmb_offset); 208 } 209 else { 210 size += (rmc_size - rmc_offset); 211 } 212 return size; 213 } 214 215 218 void next() { 219 if (getRMC()._cursor.atStart()) { 220 getRMC().next(); 221 getRMB().next(); 222 return; 223 } 224 int size = getSize(getRMB().current(), getRMC().current()); 225 getRMC().move(size - _offset); 226 getRMB().move(size - _offset); 227 } 228 229 232 void prev() { 233 int size; 234 if (getRMC()._cursor.atEnd()) { 235 getRMC().prev(); 236 getRMB().prev(); 237 if (getRMC()._cursor.atStart()) { 238 return; } 240 241 if (getRMC().current().getSize() < getRMB().current().getSize()) { 242 size = -getRMC().current().getSize(); 243 } 244 else { 245 size = -getRMB().current().getSize(); 246 } 247 getRMC().next(); 248 getRMB().next(); 249 move(size); 250 } 251 else if (getRMB().getBlockOffset() < getRMC().getBlockOffset()) { 252 getRMB().prev(); 253 size = getRMB().current().getSize() + getRMB().getBlockOffset(); 254 getRMB().next(); 255 if (size < getRMC().getBlockOffset()) { 256 move(-size); 257 } 258 else { 259 move(-getRMC().getBlockOffset()); 260 } 261 } 262 else if (getRMB().getBlockOffset() == getRMC().getBlockOffset()) { 263 getRMB().prev(); 264 getRMC().prev(); 265 getRMB().setBlockOffset(0); 266 getRMC().setBlockOffset(0); 267 } 268 else { 269 getRMC().prev(); 270 size = getRMC().current().getSize() + getRMC().getBlockOffset(); 271 getRMC().next(); 272 if (size < getRMB().getBlockOffset()) { 273 move(-size); 274 } 275 else { 276 move(-getRMB().getBlockOffset()); 277 } 278 } 279 } 280 281 284 public ReducedToken prevItem() { 285 int rmbOffset = getRMB().getBlockOffset(); 286 int rmcOffset = getRMC().getBlockOffset(); 287 288 prev(); 289 ReducedToken temp = currentToken(); 290 next(); 291 292 getRMB().setBlockOffset(rmbOffset); 293 getRMC().setBlockOffset(rmcOffset); 294 return temp; 295 } 296 297 300 public ReducedToken nextItem() { 301 int rmbOffset = getRMB().getBlockOffset(); 302 int rmcOffset = getRMC().getBlockOffset(); 303 next(); 304 ReducedToken temp = currentToken(); 305 prev(); 306 getRMB().setBlockOffset(rmbOffset); 307 getRMC().setBlockOffset(rmcOffset); 308 return temp; 309 } 310 311 312 boolean atEnd() { return (getRMB()._cursor.atEnd() || getRMC()._cursor.atEnd()); } 313 314 315 boolean atStart() { return (getRMB()._cursor.atStart() || getRMC()._cursor.atStart()); } 316 317 318 int getBlockOffset() { 319 if (getRMB().getBlockOffset() < getRMC().getBlockOffset()) return getRMB().getBlockOffset(); 320 return getRMC().getBlockOffset(); 321 } 322 323 324 public int absOffset() { return getRMC().absOffset(); } 325 326 327 328 public String simpleString() { 329 return "\n********\n" + getRMB().simpleString() + "\n________\n" + getRMC().simpleString(); 330 } 331 332 337 public IndentInfo getIndentInformation() { 338 IndentInfo braceInfo = new IndentInfo(); 339 getRMC().getDistToPreviousNewline(braceInfo); 341 getRMB().getDistToEnclosingBrace(braceInfo); 343 getRMC().getDistToIndentNewline(braceInfo); 345 getRMB().getDistToEnclosingBraceCurrent(braceInfo); 347 getRMC().getDistToCurrentBraceNewline(braceInfo); 349 return braceInfo; 350 } 351 352 353 public int getDistToPreviousNewline(int relLoc) { 354 return getRMC().getDistToPreviousNewline(relLoc); 355 } 356 357 public int getDistToNextNewline() { 358 return getRMC().getDistToNextNewline(); 359 } 360 361 367 public Vector <HighlightStatus> getHighlightStatus(final int start, final int length) { 368 Vector <HighlightStatus> vec = new Vector <HighlightStatus>(); 369 370 int curState; 371 int curLocation; 372 int curLength; 373 374 TokenList.Iterator cursor = getRMC()._cursor._copy(); 375 curLocation = start; 377 if (cursor.atEnd() || cursor.atStart()) { curLength = length; 382 curState = 0; 383 } 384 else { 385 curLength = cursor.current().getSize() - getRMC().getBlockOffset(); 386 curState = cursor.current().getHighlightState(); 387 } 388 389 while ((curLocation + curLength) < (start + length)) { 390 cursor.next(); 391 if (cursor.atEnd()) break; 394 int nextState = cursor.current().getHighlightState(); 395 396 if (nextState == curState) { 397 curLength += cursor.current().getSize(); 399 } 400 else { 401 vec.add(new HighlightStatus(curLocation, curLength, curState)); 403 curLocation += curLength; curLength = cursor.current().getSize(); 405 curState = nextState; 406 } 407 } 408 409 int requestEnd = start + length; 413 if ((curLocation + curLength) > requestEnd) { 414 curLength = requestEnd - curLocation; 415 } 416 417 vec.add(new HighlightStatus(curLocation, curLength, curState)); 419 420 cursor.dispose(); 421 422 return vec; 423 } 424 } 425 | Popular Tags |