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 |
|
---|
8 | import org.openstreetmap.josm.gui.util.ChangeNotifier;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Help browser history.
|
---|
12 | * @since 2274
|
---|
13 | */
|
---|
14 | public class HelpBrowserHistory extends ChangeNotifier {
|
---|
15 | private final IHelpBrowser browser;
|
---|
16 | private List<String> history;
|
---|
17 | private int historyPos;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Constructs a new {@code HelpBrowserHistory}.
|
---|
21 | * @param browser help browser
|
---|
22 | */
|
---|
23 | public HelpBrowserHistory(IHelpBrowser browser) {
|
---|
24 | this.browser = browser;
|
---|
25 | history = new ArrayList<>();
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Clears the history.
|
---|
30 | */
|
---|
31 | public void clear() {
|
---|
32 | history.clear();
|
---|
33 | historyPos = 0;
|
---|
34 | fireStateChanged();
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Determines if the help browser can go back.
|
---|
39 | * @return {@code true} if a previous history position exists
|
---|
40 | */
|
---|
41 | public boolean canGoBack() {
|
---|
42 | return historyPos > 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Determines if the help browser can go forward.
|
---|
47 | * @return {@code true} if a following history position exists
|
---|
48 | */
|
---|
49 | public boolean canGoForward() {
|
---|
50 | return historyPos + 1 < history.size();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Go back.
|
---|
55 | */
|
---|
56 | public void back() {
|
---|
57 | historyPos--;
|
---|
58 | if (historyPos < 0)
|
---|
59 | return;
|
---|
60 | String url = history.get(historyPos);
|
---|
61 | browser.openUrl(url);
|
---|
62 | fireStateChanged();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Go forward.
|
---|
67 | */
|
---|
68 | public void forward() {
|
---|
69 | historyPos++;
|
---|
70 | if (historyPos >= history.size())
|
---|
71 | return;
|
---|
72 | String url = history.get(historyPos);
|
---|
73 | browser.openUrl(url);
|
---|
74 | fireStateChanged();
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Remembers the new current URL.
|
---|
79 | * @param url the new current URL
|
---|
80 | */
|
---|
81 | public void setCurrentUrl(String url) {
|
---|
82 | if (url != null) {
|
---|
83 | boolean add = true;
|
---|
84 |
|
---|
85 | if (historyPos >= 0 && historyPos < history.size() && history.get(historyPos).equals(url)) {
|
---|
86 | add = false;
|
---|
87 | } else if (historyPos == history.size() -1) {
|
---|
88 | // do nothing just append
|
---|
89 | } else if (historyPos == 0 && !history.isEmpty()) {
|
---|
90 | history = new ArrayList<>(Collections.singletonList(history.get(0)));
|
---|
91 | } else if (historyPos < history.size() -1 && historyPos > 0) {
|
---|
92 | history = new ArrayList<>(history.subList(0, historyPos));
|
---|
93 | } else {
|
---|
94 | history = new ArrayList<>();
|
---|
95 | }
|
---|
96 | if (add) {
|
---|
97 | history.add(url);
|
---|
98 | historyPos = history.size()-1;
|
---|
99 | }
|
---|
100 | fireStateChanged();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|