KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > ReusableHelpPartHistory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.ui.internal.views;
12
13 import java.util.LinkedList JavaDoc;
14
15 public class ReusableHelpPartHistory {
16     private static final int CAPACITY = 50;
17     private LinkedList JavaDoc queue;
18     private int cursor = -1;
19     private boolean blocked;
20
21     public ReusableHelpPartHistory() {
22         queue = new LinkedList JavaDoc();
23     }
24
25     public void addEntry(HistoryEntry entry) {
26         if (cursor!= -1) {
27             // If we are adding a new entry while
28
// the cursor is not at the end, discard
29
// all the entries after the cursor.
30
int extra = queue.size()-1 -cursor;
31             if (extra>0) {
32                 for (int i=extra; i>0; i--) {
33                     queue.removeLast();
34                 }
35             }
36         }
37         queue.add(entry);
38         if (queue.size()>CAPACITY)
39             queue.removeFirst();
40         cursor = queue.size()-1;
41     }
42
43     public boolean hasNext() {
44         return cursor != -1 && cursor < queue.size()-1;
45     }
46
47     public boolean hasPrev() {
48         return cursor != -1 && cursor > 0;
49     }
50     
51     public HistoryEntry getNext() {
52         return hasNext()?(HistoryEntry)queue.get(cursor+1):null;
53     }
54     
55     public HistoryEntry getPrev() {
56         return hasPrev() ? (HistoryEntry)queue.get(cursor-1):null;
57     }
58
59     public HistoryEntry next() {
60         if (hasNext()) {
61             return (HistoryEntry)queue.get(++cursor);
62         }
63         return null;
64     }
65     public HistoryEntry prev() {
66         if (hasPrev()) {
67             return (HistoryEntry)queue.get(--cursor);
68         }
69         return null;
70     }
71     /**
72      * @return Returns the blocked.
73      */

74     public boolean isBlocked() {
75         return blocked;
76     }
77     /**
78      * @param blocked The blocked to set.
79      */

80     public void setBlocked(boolean blocked) {
81         this.blocked = blocked;
82     }
83 }
84
Popular Tags