| Home >> All >> org >> bdgp >> apps >> dagedit >> [ gui Javadoc ] |
Source code: org/bdgp/apps/dagedit/gui/AutosaveThread.java
1 package org.bdgp.apps.dagedit.gui; 2 3 import org.bdgp.apps.dagedit.dataadapter.*; 4 import javax.swing.*; 5 import java.io.*; 6 import java.util.*; 7 8 public class AutosaveThread extends Thread { 9 10 protected Controller controller; 11 protected boolean halt = false; 12 13 public AutosaveThread(Controller controller) { 14 setDaemon(true); 15 this.controller = controller; 16 } 17 18 protected static String getFileName(Calendar calendar) { 19 return "dagedit-save-"+ 20 calendar.get(Calendar.DAY_OF_MONTH)+"-"+ 21 calendar.get(Calendar.MONTH)+"-"+ 22 calendar.get(Calendar.YEAR)+"-"+ 23 calendar.get(Calendar.HOUR_OF_DAY)+"-"+ 24 calendar.get(Calendar.MINUTE)+ 25 ".autosave"; 26 } 27 28 public void halt() { 29 try { 30 interrupt(); 31 } catch (Exception e) {} 32 halt = true; 33 } 34 35 public void run() { 36 halt = false; 37 final GOSerialAdapter adapter = new GOSerialAdapter(); 38 try { 39 while(!halt) { 40 sleep((long) (controller.getAutosaveWaitTime()*60000)); 41 final GregorianCalendar calendar = new GregorianCalendar(); 42 SwingUtilities.invokeAndWait(new Runnable() { 43 public void run() { 44 try { 45 if (!controller.getAutosavePath().exists()) { 46 if (!controller.getAutosavePath(). 47 mkdirs()) { 48 System.err.println("Couldn't create "+ 49 "autosave "+ 50 "directory. "+ 51 "Autosave "+ 52 "disabled."); 53 halt = true; 54 return; 55 } 56 57 } 58 File saveFile = 59 new File(controller. 60 getAutosavePath(), 61 getFileName(calendar)); 62 adapter.setPath(saveFile); 63 adapter.write(Controller.getController(). 64 getHistory()); 65 } catch (Exception e) { 66 System.err.println("Couldn't autosave "+ 67 "because of "+ 68 e.getMessage()+". "+ 69 "Autosaved disabled. "+ 70 "Stack trace follows..."); 71 e.printStackTrace(); 72 } 73 } 74 }); 75 if (controller.getAutosaveExpiration() > 0) { 76 GregorianCalendar expireFileCalendar = new 77 GregorianCalendar(); 78 File [] files = controller.getAutosavePath().listFiles(); 79 for(int i=0; i < files.length; i++) { 80 expireFileCalendar.setTime(new Date(files[i]. 81 lastModified())); 82 expireFileCalendar.add(Calendar.DAY_OF_YEAR, 83 controller. 84 getAutosaveExpiration()); 85 if (calendar.after(expireFileCalendar)) { 86 if (!files[i].delete()) 87 System.err.println("Couldn't delete expired "+ 88 "autosave file "+files[i]); 89 } 90 } 91 } 92 } 93 } catch (InterruptedException e) { 94 System.err.println("Autosave disabled by user"); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 98 } 99 } 100