PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/HistoryModel.java

#
Java | 229 lines | 141 code | 28 blank | 60 comment | 26 complexity | 357936784c97550d1b785b1153e842d8 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. * HistoryModel.java - History list model
  3. * Copyright (C) 1999 Slava Pestov
  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 org.gjt.sp.jedit.gui;
  20. import javax.swing.*;
  21. import java.io.*;
  22. import java.util.*;
  23. import org.gjt.sp.jedit.jEdit;
  24. import org.gjt.sp.jedit.MiscUtilities;
  25. import org.gjt.sp.util.Log;
  26. /**
  27. * A history list. One history list can be used by several history text
  28. * fields.
  29. * @author Slava Pestov
  30. * @version $Id: HistoryModel.java 3805 2001-09-10 08:46:23Z spestov $
  31. */
  32. public class HistoryModel
  33. {
  34. /**
  35. * Creates a new history list. Calling this is normally not
  36. * necessary.
  37. */
  38. public HistoryModel(String name)
  39. {
  40. this.name = name;
  41. max = jEdit.getIntegerProperty("history",25);
  42. data = new Vector(max);
  43. }
  44. /**
  45. * Adds an item to the end of this history list, trimming the list
  46. * to the maximum number of items if necessary.
  47. * @param text The item
  48. */
  49. public void addItem(String text)
  50. {
  51. if(text == null || text.length() == 0)
  52. return;
  53. int index = data.indexOf(text);
  54. if(index != -1)
  55. data.removeElementAt(index);
  56. data.insertElementAt(text,0);
  57. if(getSize() > max)
  58. data.removeElementAt(getSize() - 1);
  59. }
  60. /**
  61. * Returns an item from the history list.
  62. * @param index The index
  63. */
  64. public String getItem(int index)
  65. {
  66. return (String)data.elementAt(index);
  67. }
  68. /**
  69. * Returns the number of elements in this history list.
  70. */
  71. public int getSize()
  72. {
  73. return data.size();
  74. }
  75. /**
  76. * Returns the name of this history list. This can be passed
  77. * to the HistoryTextField constructor.
  78. */
  79. public String getName()
  80. {
  81. return name;
  82. }
  83. /**
  84. * Returns a named model. If the specified model does not
  85. * already exist, it will be created.
  86. * @param name The model name
  87. */
  88. public static HistoryModel getModel(String name)
  89. {
  90. if(models == null)
  91. models = new Hashtable();
  92. HistoryModel model = (HistoryModel)models.get(name);
  93. if(model == null)
  94. {
  95. model = new HistoryModel(name);
  96. models.put(name,model);
  97. }
  98. return model;
  99. }
  100. /**
  101. * Loads the history from the specified file. jEdit calls this
  102. * on startup.
  103. * @param The file
  104. */
  105. public static void loadHistory(File file)
  106. {
  107. if(models == null)
  108. models = new Hashtable();
  109. try
  110. {
  111. BufferedReader in = new BufferedReader(new FileReader(file));
  112. HistoryModel currentModel = null;
  113. String line;
  114. while((line = in.readLine()) != null)
  115. {
  116. if(line.startsWith("[") && line.endsWith("]"))
  117. {
  118. if(currentModel != null)
  119. {
  120. models.put(currentModel.getName(),
  121. currentModel);
  122. }
  123. currentModel = new HistoryModel(line
  124. .substring(1,line.length() - 1));
  125. }
  126. else if(currentModel == null)
  127. {
  128. throw new IOException("History data starts"
  129. + " before model name");
  130. }
  131. else
  132. {
  133. currentModel.addItemToEnd(MiscUtilities
  134. .escapesToChars(line));
  135. }
  136. }
  137. if(currentModel != null)
  138. {
  139. models.put(currentModel.getName(),currentModel);
  140. }
  141. in.close();
  142. }
  143. catch(FileNotFoundException fnf)
  144. {
  145. Log.log(Log.DEBUG,HistoryModel.class,fnf);
  146. }
  147. catch(IOException io)
  148. {
  149. Log.log(Log.ERROR,HistoryModel.class,io);
  150. }
  151. }
  152. /**
  153. * Saves the history to the specified file. jEdit calls this when
  154. * it is exiting.
  155. * @param file The file
  156. */
  157. public static void saveHistory(File file)
  158. {
  159. String lineSep = System.getProperty("line.separator");
  160. try
  161. {
  162. BufferedWriter out = new BufferedWriter(
  163. new FileWriter(file));
  164. if(models == null)
  165. {
  166. out.close();
  167. return;
  168. }
  169. Enumeration modelEnum = models.elements();
  170. while(modelEnum.hasMoreElements())
  171. {
  172. HistoryModel model = (HistoryModel)modelEnum
  173. .nextElement();
  174. out.write('[');
  175. out.write(model.getName());
  176. out.write(']');
  177. out.write(lineSep);
  178. for(int i = 0; i < model.getSize(); i++)
  179. {
  180. out.write(MiscUtilities.charsToEscapes(
  181. model.getItem(i),true));
  182. out.write(lineSep);
  183. }
  184. }
  185. out.close();
  186. }
  187. catch(IOException io)
  188. {
  189. Log.log(Log.ERROR,HistoryModel.class,io);
  190. }
  191. }
  192. // private members
  193. private String name;
  194. private int max;
  195. private Vector data;
  196. private static Hashtable models;
  197. private void addItemToEnd(String item)
  198. {
  199. data.addElement(item);
  200. }
  201. }