PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/Autosave.java

#
Java | 110 lines | 55 code | 12 blank | 43 comment | 16 complexity | 715e80cebc38642cb4ed046d1790915b 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. * Autosave.java - Autosave manager
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 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;
  23. //{{{ Imports
  24. import javax.swing.Timer;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import org.gjt.sp.util.Log;
  28. //}}}
  29. /**
  30. * @author Slava Pestov
  31. * @version $Id: Autosave.java 12504 2008-04-22 23:12:43Z ezust $
  32. */
  33. class Autosave implements ActionListener
  34. {
  35. //{{{ setInterval() method
  36. public static void setInterval(int interval)
  37. {
  38. if(interval == 0)
  39. {
  40. if(timer != null)
  41. {
  42. timer.stop();
  43. timer = null;
  44. }
  45. return;
  46. }
  47. interval *= 1000;
  48. if(timer == null)
  49. {
  50. timer = new Timer(interval,new Autosave());
  51. timer.start();
  52. }
  53. else
  54. timer.setDelay(interval);
  55. } //}}}
  56. //{{{ stop() method
  57. public static void stop()
  58. {
  59. if(timer != null)
  60. timer.stop();
  61. } //}}}
  62. //{{{ actionPerformed() method
  63. public void actionPerformed(ActionEvent evt)
  64. {
  65. if (jEdit.getIntegerProperty("autosave",0) == 0)
  66. return;
  67. // might come in handy useful some time
  68. /* Runtime runtime = Runtime.getRuntime();
  69. int freeMemory = (int)(runtime.freeMemory() / 1024);
  70. int totalMemory = (int)(runtime.totalMemory() / 1024);
  71. int usedMemory = (totalMemory - freeMemory);
  72. Log.log(Log.DEBUG,this,"Java heap: " + usedMemory + "Kb / "
  73. + totalMemory + "Kb, " + (usedMemory * 100 / totalMemory)
  74. + "%"); */
  75. // save list of open files
  76. if(jEdit.getViewCount() != 0
  77. && PerspectiveManager.isPerspectiveDirty())
  78. {
  79. PerspectiveManager.setPerspectiveDirty(false);
  80. PerspectiveManager.savePerspective(true);
  81. }
  82. boolean autosaveUntitled = jEdit.getBooleanProperty("autosaveUntitled");
  83. Buffer[] bufferArray = jEdit.getBuffers();
  84. for(int i = 0; i < bufferArray.length; i++)
  85. {
  86. Buffer buffer = bufferArray[i];
  87. if (autosaveUntitled || !buffer.isUntitled())
  88. buffer.autosave();
  89. }
  90. // flush log
  91. Log.flushStream();
  92. } //}}}
  93. //{{{ Private members
  94. private static Timer timer;
  95. private Autosave() {}
  96. //}}}
  97. }