1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.help;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Observable;
|
---|
8 |
|
---|
9 | public class HelpBrowserHistory extends Observable {
|
---|
10 | private HelpBrowser browser;
|
---|
11 | private List<String> history;
|
---|
12 | private int historyPos = 0;
|
---|
13 |
|
---|
14 | public HelpBrowserHistory(HelpBrowser browser) {
|
---|
15 | this.browser = browser;
|
---|
16 | history = new ArrayList<>();
|
---|
17 | }
|
---|
18 |
|
---|
19 | public void clear() {
|
---|
20 | history.clear();
|
---|
21 | historyPos = 0;
|
---|
22 | setChanged();
|
---|
23 | notifyObservers();
|
---|
24 | }
|
---|
25 |
|
---|
26 | public boolean canGoBack() {
|
---|
27 | return historyPos > 0;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public boolean canGoForward() {
|
---|
31 | return historyPos + 1 < history.size();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void back() {
|
---|
35 | historyPos--;
|
---|
36 | if (historyPos < 0) return;
|
---|
37 | String url = history.get(historyPos);
|
---|
38 | browser.openUrl(url);
|
---|
39 | setChanged();
|
---|
40 | notifyObservers();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void forward() {
|
---|
44 | historyPos++;
|
---|
45 | if (historyPos >= history.size()) return;
|
---|
46 | String url = history.get(historyPos);
|
---|
47 | browser.openUrl(url);
|
---|
48 | setChanged();
|
---|
49 | notifyObservers();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void setCurrentUrl(String url) {
|
---|
53 | boolean add=true;
|
---|
54 |
|
---|
55 | if (historyPos >= 0 && historyPos < history.size() && history.get(historyPos).equals(url)) {
|
---|
56 | add = false;
|
---|
57 | } else if (historyPos == history.size() -1) {
|
---|
58 | // do nothing just append
|
---|
59 | } else if (historyPos ==0 && history.size() > 0) {
|
---|
60 | history = new ArrayList<>(Collections.singletonList(history.get(0)));
|
---|
61 | } else if (historyPos < history.size() -1 && historyPos > 0) {
|
---|
62 | history = new ArrayList<>(history.subList(0, historyPos));
|
---|
63 | } else {
|
---|
64 | history = new ArrayList<>();
|
---|
65 | }
|
---|
66 | if(add)
|
---|
67 | {
|
---|
68 | history.add(url);
|
---|
69 | historyPos = history.size()-1;
|
---|
70 | }
|
---|
71 | setChanged();
|
---|
72 | notifyObservers();
|
---|
73 | }
|
---|
74 | }
|
---|