1 11 package org.eclipse.ui.internal.intro.impl.model; 12 13 import java.util.List ; 14 import java.util.Vector ; 15 16 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser; 17 18 21 public class History { 22 private Vector history = new Vector (); 25 private int navigationLocation = 0; 26 27 28 class HistoryObject { 36 AbstractIntroPage page; 37 String iframeUrl; 38 String url; 39 40 HistoryObject(Object location) { 41 if (location instanceof String ) 42 this.url = (String ) location; 43 44 if (location instanceof AbstractIntroPage) { 45 this.page = (AbstractIntroPage) location; 46 this.iframeUrl = this.page.getIFrameURL(); 48 } 49 } 50 51 56 AbstractIntroPage getPage() { 57 if (page.isIFramePage()) 58 page.setIFrameURL(getIFrameUrl()); 62 return page; 63 } 64 65 String getPageId() { 66 return page.getId(); 67 } 68 69 String getIFrameUrl() { 70 return iframeUrl; 71 } 72 73 String getUrl() { 74 return url; 75 } 76 77 boolean isURL() { 78 return (url != null) ? true : false; 79 } 80 81 boolean isIntroPage() { 82 return (page != null) ? true : false; 83 } 84 85 boolean isIFramePage() { 86 return (iframeUrl != null) ? true : false; 87 } 88 89 } 90 91 92 97 public void updateHistory(String location) { 98 if (!history.isEmpty() && isSameLocation(location)) 100 return; 102 doUpdateHistory(location); 103 } 104 105 110 public void updateHistory(AbstractIntroPage page) { 111 if (!history.isEmpty() && isSameLocation(page)) 113 return; 115 doUpdateHistory(page); 116 } 117 118 private void doUpdateHistory(Object location) { 119 if (navigationLocation == getHistoryEndPosition()) 122 pushToHistory(location); 124 else 125 trimHistory(location); 128 } 129 130 131 private boolean isSameLocation(Object location) { 132 HistoryObject currentLocation = getCurrentLocation(); 133 if (location instanceof String && currentLocation.isURL()) 134 return currentLocation.getUrl().equals(location); 135 136 if (location instanceof AbstractIntroPage 137 && currentLocation.isIntroPage()) { 138 139 AbstractIntroPage locationPage = (AbstractIntroPage) location; 140 if (!currentLocation.getPageId().equals(locationPage.getId())) 142 return false; 143 144 if (currentLocation.isIFramePage() && locationPage.isIFramePage()) 147 return currentLocation.getIFrameUrl().equals( 148 locationPage.getIFrameURL()); 149 150 return true; 152 } 153 154 return false; 155 } 156 157 158 159 160 private void pushToHistory(Object location) { 161 history.add(new HistoryObject(location)); 162 navigationLocation = getHistoryEndPosition(); 164 } 165 166 public void removeLastHistory() { 167 history.remove(getHistoryEndPosition()); 168 navigationLocation = getHistoryEndPosition(); 170 } 171 172 private void trimHistory(Object location) { 173 List newHistory = history.subList(0, navigationLocation + 1); 174 history = new Vector (newHistory); 175 history.add(new HistoryObject(location)); 176 navigationLocation = getHistoryEndPosition(); 178 } 179 180 187 private int getHistoryEndPosition() { 188 if (history.isEmpty()) 189 return 0; 190 return history.size() - 1; 191 } 192 193 public void navigateHistoryBackward() { 194 if (badNavigationLocation(navigationLocation - 1)) 195 return; 197 --navigationLocation; 198 } 199 200 205 public void navigateHistoryForward() { 206 if (badNavigationLocation(navigationLocation + 1)) 207 return; 209 ++navigationLocation; 210 } 211 212 213 private boolean badNavigationLocation(int navigationLocation) { 214 if (navigationLocation < 0 || navigationLocation >= history.size()) 215 return true; 217 return false; 218 } 219 220 221 227 private HistoryObject getCurrentLocation() { 228 return (HistoryObject) history.elementAt(navigationLocation); 229 } 230 231 public boolean canNavigateForward() { 232 return navigationLocation != getHistoryEndPosition() ? true : false; 233 } 234 235 public boolean canNavigateBackward() { 236 return navigationLocation == 0 ? false : true; 237 } 238 239 public boolean currentLocationIsUrl() { 240 return getCurrentLocation().isURL(); 241 } 242 243 public String getCurrentLocationAsUrl() { 244 return getCurrentLocation().getUrl(); 245 } 246 247 public AbstractIntroPage getCurrentLocationAsPage() { 248 return getCurrentLocation().getPage(); 249 } 250 251 public static boolean isURL(String aString) { 252 IntroURLParser parser = new IntroURLParser(aString); 253 if (parser.hasProtocol()) 254 return true; 255 return false; 256 } 257 258 259 public void clear() { 260 history.clear(); 261 navigationLocation = 0; 262 } 263 264 265 266 267 } 268 | Popular Tags |