PageRenderTime 50ms CodeModel.GetById 44ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/infoviewer/History.java

#
Java | 164 lines | 80 code | 16 blank | 68 comment | 8 complexity | 3cc2c1d8bcb27f5188337bcd079aab2f MD5 | raw file
  1/*
  2 * History.java - Model for an URL History
  3 * Copyright (C) 1999-2001 Dirk Moebius
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package infoviewer;
 21
 22import java.util.Iterator;
 23import java.util.Stack;
 24import java.util.Vector;
 25import org.gjt.sp.jedit.jEdit;
 26
 27/**
 28 * this class maintains a list of visitid URLs and remembers the current entry,
 29 * that is being viewed.
 30 */
 31public class History
 32{
 33
 34	private Stack backStack = new Stack();
 35	private Stack forwardStack = new Stack();
 36	// private Vector entries = new Vector();
 37
 38//	private int currentPos = -1;
 39
 40	public History()
 41	{
 42	}
 43
 44	/**
 45	 * add a new entry to the history. The new entry is made the current
 46	 * entry of the history.
 47	 */
 48	public synchronized void add(TitledURLEntry e)
 49	{
 50		if (e == null)
 51			return;
 52		backStack.push(e);
 53		forwardStack.clear();
 54	}
 55
 56	/** returns the current URL of the history, as String. */
 57	public String getCurrent()
 58	{
 59		if (backStack.isEmpty()) return null;
 60		TitledURLEntry entry = (TitledURLEntry)backStack.lastElement();
 61		return entry.getURL();
 62	}
 63
 64	
 65	public int getHistoryPos() {
 66		return backStack.size();
 67	}
 68	/**
 69	 * sets the internal state of the history to the next entry and returns
 70	 * its URL.
 71	 * 
 72	 * @return the next URL as String, or null if the end of the history is
 73	 *         reached.
 74	 */
 75	public synchronized TitledURLEntry getNext(TitledURLEntry current)
 76	{
 77		if (forwardStack.isEmpty()) return null;
 78		TitledURLEntry element = (TitledURLEntry)forwardStack.pop();
 79		backStack.push(current);
 80		return element;
 81	}
 82
 83	/** return true, if there is a next entry in the history. */
 84	public boolean hasNext()
 85	{
 86		return !forwardStack.isEmpty();
 87	}
 88
 89	/**
 90	 * sets the internal state of the history to the previous entry and
 91	 * returns its URL.
 92	 * 
 93	 * @return the previous URL as String, or null if the beginning of the
 94	 *         history is reached.
 95	 */
 96	public synchronized TitledURLEntry getPrevious(TitledURLEntry current)
 97	{
 98		TitledURLEntry element = (TitledURLEntry)backStack.pop();
 99		forwardStack.push(current);
100		return element;
101	}
102
103	/** return true, if there is a previous entry in the history. */
104	public boolean hasPrevious()
105	{
106		return !backStack.isEmpty();
107	}
108
109
110	/**
111	 * get the last entries from the history, but now more than specified in
112	 * the property 'infoviewer.max_go_menu'. The entries are such that the
113	 * current entry is among them.
114	 */
115	public TitledURLEntry[] getGoMenuEntries()
116	{
117		int max = getMaxVisibleMenuEntries();
118		int count = backStack.size();
119		if (count > max) count = max;
120		TitledURLEntry[] entries = new TitledURLEntry[count];
121		Iterator itr = backStack.iterator();
122		int i=0;
123		while (itr.hasNext() && (i < count)) {
124			TitledURLEntry ent = (TitledURLEntry) itr.next();
125			entries[i++]=ent;
126		}
127		return entries;
128	}
129/*
130	private TitledURLEntry[] getEntries(int from, int to)
131	{
132		Vector v = new Vector();
133		for (int i = from; i <= to; i++)
134		{
135			TitledURLEntry e = getEntry(i);
136			if (e != null)
137			{
138				e.setHistoryPos(i);
139				v.addElement(e);
140			}
141		}
142		TitledURLEntry[] entr = new TitledURLEntry[v.size()];
143		v.copyInto(entr);
144		return entr;
145	}
146*/
147	private int getMaxVisibleMenuEntries()
148	{
149		String history = jEdit.getProperty("history");
150		int max;
151		try
152		{
153			max = Integer.parseInt(history);
154			if (max < 1)
155				throw new NumberFormatException();
156		}
157		catch (NumberFormatException e)
158		{
159			max = 20;
160		}
161		return max;
162	}
163
164}