001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.help;
003
004 import java.util.ArrayList;
005 import java.util.Collections;
006 import java.util.Observable;
007
008 public class HelpBrowserHistory extends Observable {
009 private HelpBrowser browser;
010 private ArrayList<String> history;
011 private int historyPos = 0;
012
013 public HelpBrowserHistory(HelpBrowser browser) {
014 this.browser = browser;
015 history = new ArrayList<String>();
016 }
017
018 public void clear() {
019 history.clear();
020 historyPos = 0;
021 setChanged();
022 notifyObservers();
023 }
024
025 public boolean canGoBack() {
026 return historyPos > 0;
027 }
028
029 public boolean canGoForward() {
030 return historyPos + 1 < history.size();
031 }
032
033 public void back() {
034 historyPos--;
035 if (historyPos < 0) return;
036 String url = history.get(historyPos);
037 browser.openUrl(url);
038 setChanged();
039 notifyObservers();
040 }
041
042 public void forward() {
043 historyPos++;
044 if (historyPos >= history.size()) return;
045 String url = history.get(historyPos);
046 browser.openUrl(url);
047 setChanged();
048 notifyObservers();
049 }
050
051 public void setCurrentUrl(String url) {
052 boolean add=true;
053
054 if (historyPos >= 0 && historyPos < history.size() && history.get(historyPos).toString().equals(url.toString())) {
055 add = false;
056 } else if (historyPos == history.size() -1) {
057 // do nothing just append
058 } else if (historyPos ==0 && history.size() > 0) {
059 history = new ArrayList<String>(Collections.singletonList(history.get(0)));
060 } else if (historyPos < history.size() -1 && historyPos > 0) {
061 history = new ArrayList<String>(history.subList(0, historyPos));
062 } else {
063 history = new ArrayList<String>();
064 }
065 if(add)
066 {
067 history.add(url);
068 historyPos = history.size()-1;
069 }
070 setChanged();
071 notifyObservers();
072 }
073 }