source: josm/trunk/src/org/openstreetmap/josm/gui/help/HelpBrowserHistory.java@ 10281

Last change on this file since 10281 was 10210, checked in by Don-vip, 8 years ago

see #11924 - Java 9 - replace calls to deprecated classes java.util.Observable / java.util.Observer by a new class ChangeNotifier + swing's ChangeListener

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.help;
3
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7
8import org.openstreetmap.josm.gui.util.ChangeNotifier;
9
10/**
11 * Help browser history.
12 * @since 2274
13 */
14public 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 boolean add = true;
83
84 if (historyPos >= 0 && historyPos < history.size() && history.get(historyPos).equals(url)) {
85 add = false;
86 } else if (historyPos == history.size() -1) {
87 // do nothing just append
88 } else if (historyPos == 0 && !history.isEmpty()) {
89 history = new ArrayList<>(Collections.singletonList(history.get(0)));
90 } else if (historyPos < history.size() -1 && historyPos > 0) {
91 history = new ArrayList<>(history.subList(0, historyPos));
92 } else {
93 history = new ArrayList<>();
94 }
95 if (add) {
96 history.add(url);
97 historyPos = history.size()-1;
98 }
99 fireStateChanged();
100 }
101}
Note: See TracBrowser for help on using the repository browser.