/plugins/JPyDbg/trunk/src/org/jymc/jpydebug/utils/HistoryModel.java

# · Java · 319 lines · 190 code · 43 blank · 86 comment · 35 complexity · 5f1766de85941fd02374b26863d4ae23 MD5 · raw file

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