PageRenderTime 83ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package infoviewer;
  20. import java.util.Iterator;
  21. import java.util.Stack;
  22. import java.util.Vector;
  23. import org.gjt.sp.jedit.jEdit;
  24. /**
  25. * this class maintains a list of visitid URLs and remembers the current entry,
  26. * that is being viewed.
  27. */
  28. public class History
  29. {
  30. private Stack backStack = new Stack();
  31. private Stack forwardStack = new Stack();
  32. // private Vector entries = new Vector();
  33. // private int currentPos = -1;
  34. public History()
  35. {
  36. }
  37. /**
  38. * add a new entry to the history. The new entry is made the current
  39. * entry of the history.
  40. */
  41. public synchronized void add(TitledURLEntry e)
  42. {
  43. if (e == null)
  44. return;
  45. backStack.push(e);
  46. forwardStack.clear();
  47. }
  48. /** returns the current URL of the history, as String. */
  49. public String getCurrent()
  50. {
  51. if (backStack.isEmpty()) return null;
  52. TitledURLEntry entry = (TitledURLEntry)backStack.lastElement();
  53. return entry.getURL();
  54. }
  55. public int getHistoryPos() {
  56. return backStack.size();
  57. }
  58. /**
  59. * sets the internal state of the history to the next entry and returns
  60. * its URL.
  61. *
  62. * @return the next URL as String, or null if the end of the history is
  63. * reached.
  64. */
  65. public synchronized TitledURLEntry getNext(TitledURLEntry current)
  66. {
  67. if (forwardStack.isEmpty()) return null;
  68. TitledURLEntry element = (TitledURLEntry)forwardStack.pop();
  69. backStack.push(current);
  70. return element;
  71. }
  72. /** return true, if there is a next entry in the history. */
  73. public boolean hasNext()
  74. {
  75. return !forwardStack.isEmpty();
  76. }
  77. /**
  78. * sets the internal state of the history to the previous entry and
  79. * returns its URL.
  80. *
  81. * @return the previous URL as String, or null if the beginning of the
  82. * history is reached.
  83. */
  84. public synchronized TitledURLEntry getPrevious(TitledURLEntry current)
  85. {
  86. TitledURLEntry element = (TitledURLEntry)backStack.pop();
  87. forwardStack.push(current);
  88. return element;
  89. }
  90. /** return true, if there is a previous entry in the history. */
  91. public boolean hasPrevious()
  92. {
  93. return !backStack.isEmpty();
  94. }
  95. /**
  96. * get the last entries from the history, but now more than specified in
  97. * the property 'infoviewer.max_go_menu'. The entries are such that the
  98. * current entry is among them.
  99. */
  100. public TitledURLEntry[] getGoMenuEntries()
  101. {
  102. int max = getMaxVisibleMenuEntries();
  103. int count = backStack.size();
  104. if (count > max) count = max;
  105. TitledURLEntry[] entries = new TitledURLEntry[count];
  106. Iterator itr = backStack.iterator();
  107. int i=0;
  108. while (itr.hasNext() && (i < count)) {
  109. TitledURLEntry ent = (TitledURLEntry) itr.next();
  110. entries[i++]=ent;
  111. }
  112. return entries;
  113. }
  114. /*
  115. private TitledURLEntry[] getEntries(int from, int to)
  116. {
  117. Vector v = new Vector();
  118. for (int i = from; i <= to; i++)
  119. {
  120. TitledURLEntry e = getEntry(i);
  121. if (e != null)
  122. {
  123. e.setHistoryPos(i);
  124. v.addElement(e);
  125. }
  126. }
  127. TitledURLEntry[] entr = new TitledURLEntry[v.size()];
  128. v.copyInto(entr);
  129. return entr;
  130. }
  131. */
  132. private int getMaxVisibleMenuEntries()
  133. {
  134. String history = jEdit.getProperty("history");
  135. int max;
  136. try
  137. {
  138. max = Integer.parseInt(history);
  139. if (max < 1)
  140. throw new NumberFormatException();
  141. }
  142. catch (NumberFormatException e)
  143. {
  144. max = 20;
  145. }
  146. return max;
  147. }
  148. }