001: /*
002: * Autosave.java - Autosave manager
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: //{{{ Imports
026: import javax.swing.Timer;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import org.gjt.sp.util.Log;
030:
031: //}}}
032:
033: /**
034: * @author Slava Pestov
035: * @version $Id: Autosave.java 10818 2007-10-05 20:07:56Z kpouer $
036: */
037: class Autosave implements ActionListener {
038: //{{{ setInterval() method
039: public static void setInterval(int interval) {
040: if (interval == 0) {
041: if (timer != null) {
042: timer.stop();
043: timer = null;
044: }
045:
046: return;
047: }
048:
049: interval *= 1000;
050:
051: if (timer == null) {
052: timer = new Timer(interval, new Autosave());
053: timer.start();
054: } else
055: timer.setDelay(interval);
056: } //}}}
057:
058: //{{{ stop() method
059: public static void stop() {
060: if (timer != null)
061: timer.stop();
062: } //}}}
063:
064: //{{{ actionPerformed() method
065: public void actionPerformed(ActionEvent evt) {
066: if (jEdit.getIntegerProperty("autosave", 0) == 0)
067: return;
068: // might come in handy useful some time
069: /* Runtime runtime = Runtime.getRuntime();
070: int freeMemory = (int)(runtime.freeMemory() / 1024);
071: int totalMemory = (int)(runtime.totalMemory() / 1024);
072: int usedMemory = (totalMemory - freeMemory);
073:
074: Log.log(Log.DEBUG,this,"Java heap: " + usedMemory + "Kb / "
075: + totalMemory + "Kb, " + (usedMemory * 100 / totalMemory)
076: + "%"); */
077:
078: // save list of open files
079: if (jEdit.getViewCount() != 0
080: && PerspectiveManager.isPerspectiveDirty()) {
081: PerspectiveManager.setPerspectiveDirty(false);
082: PerspectiveManager.savePerspective(true);
083: }
084: boolean autosaveUntitled = jEdit
085: .getBooleanProperty("autosaveUntitled");
086: Buffer[] bufferArray = jEdit.getBuffers();
087: for (int i = 0; i < bufferArray.length; i++) {
088: Buffer buffer = bufferArray[i];
089: if (autosaveUntitled || !buffer.isUntitled())
090: buffer.autosave();
091: }
092:
093: // flush log
094: Log.flushStream();
095: } //}}}
096:
097: //{{{ Private members
098: private static Timer timer;
099:
100: private Autosave() {
101: }
102: //}}}
103: }
|