PageRenderTime 118ms CodeModel.GetById 112ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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