1 22 23 package org.gjt.sp.jedit.help; 24 25 30 public class HelpHistoryModel 31 { 32 public HelpHistoryModel(int size) 34 { 35 int 36 historyPos = 0; 37 history = new HistoryEntry[size]; 38 listeners = new java.util.Vector (); 39 } 41 public String forward() 43 { 44 if(history.length - historyPos <= 1) 45 return null; 46 if (history[historyPos] == null) 47 return null; 48 String url = history[historyPos].url; 49 historyPos++; 50 fireUpdate(); 51 return url; 52 } 54 public boolean hasNext() 56 { 57 return !(history.length - historyPos <= 1 || 58 history[historyPos] == null); 59 } 61 public String back() 63 { 64 if (historyPos<=1) 65 return null; 66 String url = history[--historyPos - 1].url; 67 fireUpdate(); 68 return url; 69 } 71 public boolean hasPrevious() 73 { 74 return (historyPos>1); 75 } 77 public void addToHistory(String url) 79 { 80 history[historyPos] = new HistoryEntry(url,url); 81 if(historyPos + 1 == history.length) 82 { 83 System.arraycopy(history,1,history, 84 0,history.length - 1); 85 history[historyPos] = null; 86 } 87 else 88 { 89 historyPos++; 90 for (int i = historyPos;i<history.length;i++) 91 { 92 history[i] = null; 93 } 94 } 95 fireUpdate(); 96 } 98 public void setCurrentEntry(HistoryEntry entry) 100 { 101 for (int i=0;i<history.length;i++) 102 { 103 if (history[i]!=null && history[i].equals(entry)) 104 { 105 historyPos = i+1; 106 fireUpdate(); 107 break; 108 } 109 } 110 } 113 public void updateTitle(String url, String title) 115 { 116 for (int i=0;i<history.length;i++) 117 { 118 if (history[i]!=null && history[i].url.equals(url)) 119 { 120 history[i].title = title; 121 } 122 } 123 fireUpdate(); 124 } 126 public HistoryEntry[] getPreviousURLs() 128 { 129 if (historyPos<=1) 130 return new HelpHistoryModel.HistoryEntry[0]; 131 HistoryEntry[] previous = new HistoryEntry[historyPos-1]; 132 System.arraycopy(history,0,previous,0,historyPos-1); 133 return previous; 134 } 136 public HistoryEntry[] getNextURLs() 138 { 139 if (history.length - historyPos <= 1) 140 return new HelpHistoryModel.HistoryEntry[0]; 141 if (history[historyPos] == null) 142 return new HelpHistoryModel.HistoryEntry[0]; 143 HistoryEntry[] next = new HistoryEntry[history.length-historyPos]; 144 System.arraycopy(history,historyPos,next,0,history.length-historyPos); 145 return next; 146 } 148 public void addHelpHistoryModelListener(HelpHistoryModelListener hhml) 150 { 151 listeners.add(hhml); 152 } 154 public void removeHelpHistoryModelListener(HelpHistoryModelListener hhml) 156 { 157 listeners.remove(hhml); 158 } 160 public void fireUpdate() 162 { 163 for (int i=0;i<listeners.size();i++) 164 ((HelpHistoryModelListener)listeners.elementAt(i)).historyUpdated(); 165 } 167 private int historyPos; 169 private HistoryEntry[] history; 170 private java.util.Vector listeners; 171 173 class HistoryEntry 175 { 176 String url; 177 String title; 178 HistoryEntry(String url,String title) 179 { 180 this.url = url; 181 this.title = title; 182 } 183 public boolean equals(HistoryEntry he) 184 { 185 return he.url.equals(this.url) && 186 he.title.equals(this.title); 187 } 188 } 189 } 191 192 193 | Popular Tags |