PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/HistoryModel.java

#
Java | 240 lines | 139 code | 27 blank | 74 comment | 25 complexity | b25484e34b3eae9efe62152548003da7 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. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.io.*;
  26. import java.util.*;
  27. import org.gjt.sp.jedit.jEdit;
  28. import org.gjt.sp.jedit.MiscUtilities;
  29. import org.gjt.sp.util.Log;
  30. //}}}
  31. /**
  32. * A history list. One history list can be used by several history text
  33. * fields.
  34. * @author Slava Pestov
  35. * @version $Id: HistoryModel.java 4299 2002-08-12 16:55:59Z spestov $
  36. */
  37. public class HistoryModel
  38. {
  39. //{{{ HistoryModel constructor
  40. /**
  41. * Creates a new history list. Calling this is normally not
  42. * necessary.
  43. */
  44. public HistoryModel(String name)
  45. {
  46. this.name = name;
  47. max = jEdit.getIntegerProperty("history",25);
  48. data = new Vector(max);
  49. } //}}}
  50. //{{{ addItem() method
  51. /**
  52. * Adds an item to the end of this history list, trimming the list
  53. * to the maximum number of items if necessary.
  54. * @param text The item
  55. */
  56. public void addItem(String text)
  57. {
  58. if(text == null || text.length() == 0)
  59. return;
  60. int index = data.indexOf(text);
  61. if(index != -1)
  62. data.removeElementAt(index);
  63. data.insertElementAt(text,0);
  64. while(getSize() > max)
  65. data.removeElementAt(data.size() - 1);
  66. } //}}}
  67. //{{{ getItem() method
  68. /**
  69. * Returns an item from the history list.
  70. * @param index The index
  71. */
  72. public String getItem(int index)
  73. {
  74. return (String)data.elementAt(index);
  75. } //}}}
  76. //{{{ getSize() method
  77. /**
  78. * Returns the number of elements in this history list.
  79. */
  80. public int getSize()
  81. {
  82. return data.size();
  83. } //}}}
  84. //{{{ getName() method
  85. /**
  86. * Returns the name of this history list. This can be passed
  87. * to the HistoryTextField constructor.
  88. */
  89. public String getName()
  90. {
  91. return name;
  92. } //}}}
  93. //{{{ getModel() method
  94. /**
  95. * Returns a named model. If the specified model does not
  96. * already exist, it will be created.
  97. * @param name The model name
  98. */
  99. public static HistoryModel getModel(String name)
  100. {
  101. if(models == null)
  102. models = new Hashtable();
  103. HistoryModel model = (HistoryModel)models.get(name);
  104. if(model == null)
  105. {
  106. model = new HistoryModel(name);
  107. models.put(name,model);
  108. }
  109. return model;
  110. } //}}}
  111. //{{{ loadHistory() method
  112. /**
  113. * Loads the history from the specified file. jEdit calls this
  114. * on startup.
  115. * @param The file
  116. */
  117. public static void loadHistory(File file)
  118. {
  119. if(models == null)
  120. models = new Hashtable();
  121. try
  122. {
  123. BufferedReader in = new BufferedReader(new FileReader(file));
  124. HistoryModel currentModel = null;
  125. String line;
  126. while((line = in.readLine()) != null)
  127. {
  128. if(line.startsWith("[") && line.endsWith("]"))
  129. {
  130. if(currentModel != null)
  131. {
  132. models.put(currentModel.getName(),
  133. currentModel);
  134. }
  135. currentModel = new HistoryModel(line
  136. .substring(1,line.length() - 1));
  137. }
  138. else if(currentModel == null)
  139. {
  140. throw new IOException("History data starts"
  141. + " before model name");
  142. }
  143. else
  144. {
  145. currentModel.data.addElement(MiscUtilities
  146. .escapesToChars(line));
  147. }
  148. }
  149. if(currentModel != null)
  150. {
  151. models.put(currentModel.getName(),currentModel);
  152. }
  153. in.close();
  154. }
  155. catch(FileNotFoundException fnf)
  156. {
  157. Log.log(Log.DEBUG,HistoryModel.class,fnf);
  158. }
  159. catch(IOException io)
  160. {
  161. Log.log(Log.ERROR,HistoryModel.class,io);
  162. }
  163. } //}}}
  164. //{{{ saveHistory() method
  165. /**
  166. * Saves the history to the specified file. jEdit calls this when
  167. * it is exiting.
  168. * @param file The file
  169. */
  170. public static void saveHistory(File file)
  171. {
  172. String lineSep = System.getProperty("line.separator");
  173. try
  174. {
  175. BufferedWriter out = new BufferedWriter(
  176. new FileWriter(file));
  177. if(models == null)
  178. {
  179. out.close();
  180. return;
  181. }
  182. Enumeration modelEnum = models.elements();
  183. while(modelEnum.hasMoreElements())
  184. {
  185. HistoryModel model = (HistoryModel)modelEnum
  186. .nextElement();
  187. out.write('[');
  188. out.write(model.getName());
  189. out.write(']');
  190. out.write(lineSep);
  191. for(int i = 0; i < model.getSize(); i++)
  192. {
  193. out.write(MiscUtilities.charsToEscapes(
  194. model.getItem(i),
  195. TO_ESCAPE));
  196. out.write(lineSep);
  197. }
  198. }
  199. out.close();
  200. }
  201. catch(IOException io)
  202. {
  203. Log.log(Log.ERROR,HistoryModel.class,io);
  204. }
  205. } //}}}
  206. //{{{ Private members
  207. private static final String TO_ESCAPE = "\n\t\\\"'[]";
  208. private String name;
  209. private int max;
  210. private Vector data;
  211. private static Hashtable models;
  212. //}}}
  213. }