001: /*
002: * HelpHistoryModel.java - History Model for Help GUI
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2005 Nicholas O'Leary
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.help;
024:
025: import java.net.URL;
026:
027: /**
028: * History model used by the help browser
029: * @author Nicholas O'Leary
030: * @version $Id: HelpHistoryModel.java 9197 2007-03-22 14:35:05Z Vampire0 $
031: */
032: public class HelpHistoryModel {
033: //{{{ HelpHistoryModel constructor
034: public HelpHistoryModel(int size) {
035: int historyPos = 0;
036: history = new HistoryEntry[size];
037: listeners = new java.util.Vector();
038: } //}}}
039:
040: //{{{ forward() method
041: public HistoryEntry forward(HelpViewer helpViewer) {
042: if (history.length - historyPos <= 1) {
043: return null;
044: }
045: if (history[historyPos] == null) {
046: return null;
047: }
048: setCurrentScrollPosition(helpViewer.getCurrentPage(),
049: helpViewer.getCurrentScrollPosition());
050: HistoryEntry result = new HistoryEntry(history[historyPos]);
051: historyPos++;
052: fireUpdate();
053: return result;
054: } //}}}
055:
056: //{{{ hasNext() method
057: public boolean hasNext() {
058: return !((history.length - historyPos <= 1) || (history[historyPos] == null));
059: } //}}}
060:
061: //{{{ back() method
062: public HistoryEntry back(HelpViewer helpViewer) {
063: if (historyPos <= 1) {
064: return null;
065: }
066: setCurrentScrollPosition(helpViewer.getCurrentPage(),
067: helpViewer.getCurrentScrollPosition());
068: HistoryEntry result = new HistoryEntry(
069: history[--historyPos - 1]);
070: fireUpdate();
071: return result;
072: } //}}}
073:
074: //{{{ hasPrevious() method
075: public boolean hasPrevious() {
076: return (historyPos > 1);
077: } //}}}
078:
079: //{{{ addToHistory() method
080: public void addToHistory(String url) {
081: history[historyPos] = new HistoryEntry(url, url, 0);
082: if (historyPos + 1 == history.length) {
083: System
084: .arraycopy(history, 1, history, 0,
085: history.length - 1);
086: history[historyPos] = null;
087: } else {
088: historyPos++;
089: for (int i = historyPos; i < history.length; i++) {
090: history[i] = null;
091: }
092: }
093: fireUpdate();
094: } //}}}
095:
096: //{{{ setCurrentScrollPosition() method
097: public void setCurrentScrollPosition(URL currentPage,
098: int scrollPosition) {
099: if ((null != currentPage)
100: && (historyPos >= 1)
101: && (currentPage.toString()
102: .equals(history[historyPos - 1].url))) {
103: history[historyPos - 1].scrollPosition = scrollPosition;
104: }
105: } //}}}
106:
107: //{{{ setCurrentEntry() method
108: public void setCurrentEntry(HistoryEntry entry) {
109: for (int i = 0; i < history.length; i++) {
110: if ((history[i] != null) && (history[i].equals(entry))) {
111: historyPos = i + 1;
112: fireUpdate();
113: break;
114: }
115: }
116: // Do nothing?
117: } //}}}
118:
119: //{{{ updateTitle() method
120: public void updateTitle(String url, String title) {
121: for (int i = 0; i < history.length; i++) {
122: if ((history[i] != null) && history[i].url.equals(url)) {
123: history[i].title = title;
124: }
125: }
126: fireUpdate();
127: }//}}}
128:
129: //{{{ getPreviousURLs() method
130: public HistoryEntry[] getPreviousURLs() {
131: if (historyPos <= 1) {
132: return new HelpHistoryModel.HistoryEntry[0];
133: }
134: HistoryEntry[] previous = new HistoryEntry[historyPos - 1];
135: System.arraycopy(history, 0, previous, 0, historyPos - 1);
136: return previous;
137: } //}}}
138:
139: //{{{ getNextURLs() method
140: public HistoryEntry[] getNextURLs() {
141: if (history.length - historyPos <= 1) {
142: return new HelpHistoryModel.HistoryEntry[0];
143: }
144: if (history[historyPos] == null) {
145: return new HelpHistoryModel.HistoryEntry[0];
146: }
147: HistoryEntry[] next = new HistoryEntry[history.length
148: - historyPos];
149: System.arraycopy(history, historyPos, next, 0, history.length
150: - historyPos);
151: return next;
152: } //}}}
153:
154: //{{{ addHelpHistoryModelListener() method
155: public void addHelpHistoryModelListener(
156: HelpHistoryModelListener hhml) {
157: listeners.add(hhml);
158: } //}}}
159:
160: //{{{ removeHelpHistoryModelListener() method
161: public void removeHelpHistoryModelListener(
162: HelpHistoryModelListener hhml) {
163: listeners.remove(hhml);
164: } //}}}
165:
166: //{{{ fireUpdate() method
167: public void fireUpdate() {
168: for (int i = 0; i < listeners.size(); i++) {
169: ((HelpHistoryModelListener) listeners.elementAt(i))
170: .historyUpdated();
171: }
172: } //}}}
173:
174: //{{{ Private members
175: private int historyPos;
176: private HistoryEntry[] history;
177: private java.util.Vector listeners;
178:
179: //}}}
180:
181: //{{{ Inner Classes
182:
183: //{{{ HistoryEntry class
184: static class HistoryEntry {
185: String url;
186: String title;
187: int scrollPosition;
188:
189: //{{{ HistoryEntry constructor
190: HistoryEntry(String url, String title) {
191: this (url, title, 0);
192: } //}}}
193:
194: //{{{ HistoryEntry constructor
195: HistoryEntry(HistoryEntry original) {
196: this (original.url, original.title, original.scrollPosition);
197: } //}}}
198:
199: //{{{ HistoryEntry constructor
200: HistoryEntry(String url, String title, int scrollPosition) {
201: this .url = url;
202: this .title = title;
203: this .scrollPosition = scrollPosition;
204: } //}}}
205:
206: //{{{ equals() method
207: public boolean equals(HistoryEntry he) {
208: return he.url.equals(this .url)
209: && he.title.equals(this .title)
210: && (he.scrollPosition == scrollPosition);
211: } //}}}
212:
213: //{{{ toString() method
214: public String toString() {
215: return getClass().getName() + "[url=" + url + ",title="
216: + title + ",scrollPosition=" + scrollPosition + "]";
217: } //}}}
218: } //}}}
219:
220: //}}}
221: }
|