KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > app > browser > HistoryManager


1 package org.joshy.html.app.browser;
2
3 import org.joshy.u;
4 import java.net.URL JavaDoc;
5 import org.w3c.dom.Document JavaDoc;
6 import java.util.ArrayList JavaDoc;
7
8 /**
9
10 The history manager keeps track of all of the documents that have been
11 loaded, and the order in which they have been loaded (the page navigation
12 trail). This allows the browser to go forwards and backwards through the
13 list. It caches the root document object of each page, along with the
14 location URL. There is one history manager in memory per browser view (ie,
15 per window or tab), since each view has it's own navigation trail.
16
17 */

18
19
20 public class HistoryManager {
21     protected ArrayList JavaDoc entries = new ArrayList JavaDoc();
22     protected int index = -1;
23     
24     protected class Entry {
25         public Document JavaDoc doc;
26         public URL JavaDoc url;
27         public String JavaDoc toString() {
28             return "HistoryEntry: " + doc;
29         }
30     }
31     
32     protected Entry getEntry(int i) {
33         return (Entry)entries.get(i);
34     }
35     
36     public Document JavaDoc getCurrentDocument() {
37         return getEntry(index).doc;
38     }
39     
40     public URL JavaDoc getCurrentURL() {
41         return getEntry(index).url;
42     }
43     
44     public void goPrevious() {
45         index--;
46     }
47     public boolean hasPrevious() {
48         if(index > 0) {
49             return true;
50         } else {
51             return false;
52         }
53     }
54     
55     public boolean hasNext() {
56         if(index+1 < entries.size() && index >= 0) {
57             return true;
58         } else {
59             return false;
60         }
61     }
62     public void goNext() {
63         index++;
64     }
65     public void goNewDocument(Document JavaDoc doc) {
66         goNewDocument(doc,null);
67     }
68     public void goNewDocument(Document JavaDoc doc, URL JavaDoc url) {
69         Entry entry = new Entry();
70         entry.doc = doc;
71         entry.url = url;
72         // clear out array list after index
73

74         int len = entries.size();
75         for(int i=index+1; i<len; i++) {
76             entries.remove(index+1);
77         }
78         
79         entries.add(entry);
80         index++;
81         //dumpHistory();
82
}
83     
84     public void dumpHistory() {
85         u.p(entries);
86         u.p("current index = " + index);
87     }
88
89 }
90
Popular Tags