KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > Autosave


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
23 package org.gjt.sp.jedit;
24
25 //{{{ Imports
26
import javax.swing.Timer JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import org.gjt.sp.util.Log;
30 //}}}
31

32 /**
33  * @author Slava Pestov
34  * @version $Id: Autosave.java 5037 2004-05-06 22:35:11Z spestov $
35  */

36 class Autosave implements ActionListener JavaDoc
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 JavaDoc(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 JavaDoc evt)
72     {
73         // might come in handy useful some time
74
/* Runtime runtime = Runtime.getRuntime();
75         int freeMemory = (int)(runtime.freeMemory() / 1024);
76         int totalMemory = (int)(runtime.totalMemory() / 1024);
77         int usedMemory = (totalMemory - freeMemory);
78
79         Log.log(Log.DEBUG,this,"Java heap: " + usedMemory + "Kb / "
80             + totalMemory + "Kb, " + (usedMemory * 100 / totalMemory)
81             + "%"); */

82
83         // save list of open files
84
if(jEdit.getViewCount() != 0
85             && PerspectiveManager.isPerspectiveDirty())
86         {
87             PerspectiveManager.setPerspectiveDirty(false);
88             PerspectiveManager.savePerspective(true);
89         }
90
91         Buffer[] bufferArray = jEdit.getBuffers();
92         for(int i = 0; i < bufferArray.length; i++)
93             bufferArray[i].autosave();
94
95         // flush log
96
Log.flushStream();
97     } //}}}
98

99     //{{{ Private members
100
private static Timer JavaDoc timer;
101
102     private Autosave() {}
103     //}}}
104
}
105
Popular Tags