PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/HistoryModel.java

#
Java | 333 lines | 207 code | 44 blank | 82 comment | 38 complexity | 7082fa56e11d392b1d19daa587d7197d 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, 2003 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.AbstractListModel;
  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. Note that the list model implementation is incomplete; no events
  34. * are fired when the history model changes.
  35. * @author Slava Pestov
  36. * @version $Id: HistoryModel.java 5053 2004-05-29 01:55:26Z spestov $
  37. */
  38. public class HistoryModel extends AbstractListModel
  39. {
  40. //{{{ HistoryModel constructor
  41. /**
  42. * Creates a new history list. Calling this is normally not
  43. * necessary.
  44. */
  45. public HistoryModel(String name)
  46. {
  47. this.name = name;
  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. modified = true;
  61. int index = data.indexOf(text);
  62. if(index != -1)
  63. data.removeElementAt(index);
  64. data.insertElementAt(text,0);
  65. while(getSize() > max)
  66. data.removeElementAt(data.size() - 1);
  67. } //}}}
  68. //{{{ getItem() method
  69. /**
  70. * Returns an item from the history list.
  71. * @param index The index
  72. */
  73. public String getItem(int index)
  74. {
  75. return (String)data.elementAt(index);
  76. } //}}}
  77. //{{{ getElementAt() method
  78. /**
  79. * Returns an item from the history list. This method returns the
  80. * same thing as {@link #getItem(int)} and only exists so that
  81. * <code>HistoryModel</code> instances can be used as list models.
  82. * @param index The index
  83. * @since jEdit 4.2pre2
  84. */
  85. public Object getElementAt(int index)
  86. {
  87. return getItem(index);
  88. } //}}}
  89. //{{{ clear() method
  90. /**
  91. * Removes all entries from this history model.
  92. * @since jEdit 4.2pre2
  93. */
  94. public void clear()
  95. {
  96. modified = true;
  97. data.removeAllElements();
  98. } //}}}
  99. //{{{ getSize() method
  100. /**
  101. * Returns the number of elements in this history list.
  102. */
  103. public int getSize()
  104. {
  105. return data.size();
  106. } //}}}
  107. //{{{ getName() method
  108. /**
  109. * Returns the name of this history list. This can be passed
  110. * to the HistoryTextField constructor.
  111. */
  112. public String getName()
  113. {
  114. return name;
  115. } //}}}
  116. //{{{ getModel() method
  117. /**
  118. * Returns a named model. If the specified model does not
  119. * already exist, it will be created.
  120. * @param name The model name
  121. */
  122. public static HistoryModel getModel(String name)
  123. {
  124. if(models == null)
  125. models = new Hashtable();
  126. HistoryModel model = (HistoryModel)models.get(name);
  127. if(model == null)
  128. {
  129. model = new HistoryModel(name);
  130. models.put(name,model);
  131. }
  132. return model;
  133. } //}}}
  134. //{{{ loadHistory() method
  135. public static void loadHistory()
  136. {
  137. String settingsDirectory = jEdit.getSettingsDirectory();
  138. if(settingsDirectory == null)
  139. return;
  140. history = new File(MiscUtilities.constructPath(
  141. settingsDirectory,"history"));
  142. if(!history.exists())
  143. return;
  144. historyModTime = history.lastModified();
  145. Log.log(Log.MESSAGE,HistoryModel.class,"Loading history");
  146. if(models == null)
  147. models = new Hashtable();
  148. BufferedReader in = null;
  149. try
  150. {
  151. in = new BufferedReader(new FileReader(history));
  152. HistoryModel currentModel = null;
  153. String line;
  154. while((line = in.readLine()) != null)
  155. {
  156. if(line.startsWith("[") && line.endsWith("]"))
  157. {
  158. if(currentModel != null)
  159. {
  160. models.put(currentModel.getName(),
  161. currentModel);
  162. }
  163. String modelName = MiscUtilities
  164. .escapesToChars(line.substring(
  165. 1,line.length() - 1));
  166. currentModel = new HistoryModel(
  167. modelName);
  168. }
  169. else if(currentModel == null)
  170. {
  171. throw new IOException("History data starts"
  172. + " before model name");
  173. }
  174. else
  175. {
  176. currentModel.data.addElement(MiscUtilities
  177. .escapesToChars(line));
  178. }
  179. }
  180. if(currentModel != null)
  181. {
  182. models.put(currentModel.getName(),currentModel);
  183. }
  184. }
  185. catch(FileNotFoundException fnf)
  186. {
  187. //Log.log(Log.DEBUG,HistoryModel.class,fnf);
  188. }
  189. catch(IOException io)
  190. {
  191. Log.log(Log.ERROR,HistoryModel.class,io);
  192. }
  193. finally
  194. {
  195. try
  196. {
  197. if(in != null)
  198. in.close();
  199. }
  200. catch(IOException io)
  201. {
  202. }
  203. }
  204. } //}}}
  205. //{{{ saveHistory() method
  206. public static void saveHistory()
  207. {
  208. if(!modified)
  209. return;
  210. Log.log(Log.MESSAGE,HistoryModel.class,"Saving history");
  211. File file1 = new File(MiscUtilities.constructPath(
  212. jEdit.getSettingsDirectory(), "#history#save#"));
  213. File file2 = new File(MiscUtilities.constructPath(
  214. jEdit.getSettingsDirectory(), "history"));
  215. if(file2.exists() && file2.lastModified() != historyModTime)
  216. {
  217. Log.log(Log.WARNING,HistoryModel.class,file2
  218. + " changed on disk; will not save history");
  219. return;
  220. }
  221. jEdit.backupSettingsFile(file2);
  222. String lineSep = System.getProperty("line.separator");
  223. BufferedWriter out = null;
  224. try
  225. {
  226. out = new BufferedWriter(new FileWriter(file1));
  227. if(models != null)
  228. {
  229. Enumeration modelEnum = models.elements();
  230. while(modelEnum.hasMoreElements())
  231. {
  232. HistoryModel model = (HistoryModel)modelEnum
  233. .nextElement();
  234. if(model.getSize() == 0)
  235. continue;
  236. out.write('[');
  237. out.write(MiscUtilities.charsToEscapes(
  238. model.getName(),TO_ESCAPE));
  239. out.write(']');
  240. out.write(lineSep);
  241. for(int i = 0; i < model.getSize(); i++)
  242. {
  243. out.write(MiscUtilities.charsToEscapes(
  244. model.getItem(i),
  245. TO_ESCAPE));
  246. out.write(lineSep);
  247. }
  248. }
  249. }
  250. out.close();
  251. /* to avoid data loss, only do this if the above
  252. * completed successfully */
  253. file2.delete();
  254. file1.renameTo(file2);
  255. modified = false;
  256. }
  257. catch(IOException io)
  258. {
  259. Log.log(Log.ERROR,HistoryModel.class,io);
  260. }
  261. finally
  262. {
  263. try
  264. {
  265. if(out != null)
  266. out.close();
  267. }
  268. catch(IOException e)
  269. {
  270. }
  271. }
  272. historyModTime = file2.lastModified();
  273. } //}}}
  274. //{{{ propertiesChanged() method
  275. public static void propertiesChanged()
  276. {
  277. max = jEdit.getIntegerProperty("history",25);
  278. } //}}}
  279. //{{{ Private members
  280. private static final String TO_ESCAPE = "\r\n\t\\\"'[]";
  281. private static int max;
  282. private String name;
  283. private Vector data;
  284. private static Hashtable models;
  285. private static boolean modified;
  286. private static File history;
  287. private static long historyModTime;
  288. //}}}
  289. }