1 31 32 33 package org.antlr.works.navigation; 34 35 import java.util.ArrayList ; 36 import java.util.List ; 37 38 public class GoToHistory { 39 40 protected List <Integer > history = new ArrayList <Integer >(); 41 protected int currentIndex = 0; 42 43 public GoToHistory() { 44 45 } 46 47 public void addPosition(int pos) { 48 int index = history.size()-1; 52 while(index >= currentIndex) { 53 history.remove(index); 54 index--; 55 } 56 history.add(pos); 57 currentIndex = history.size(); 58 } 59 60 public boolean canGoBack() { 61 return !history.isEmpty() && currentIndex > 0; 62 } 63 64 public boolean canGoForward() { 65 return !history.isEmpty() && currentIndex < history.size()-1; 66 } 67 68 public int getBackPosition(int currentPosition) { 69 if(currentIndex == history.size()) { 70 history.add(currentPosition); 74 } 75 76 currentIndex--; 77 if(currentIndex < 0) 78 currentIndex = 0; 79 return history.get(currentIndex); 80 } 81 82 public int getForwardPosition() { 83 currentIndex++; 84 if(currentIndex >= history.size()) 85 currentIndex = history.size()-1; 86 return history.get(currentIndex); 87 } 88 } 89 | Popular Tags |