1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.awt.event.ActionListener ; 23 import java.awt.event.ActionEvent ; 24 import java.io.BufferedOutputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.OutputStreamWriter ; 28 import java.io.Writer ; 29 import java.net.URISyntaxException ; 30 import java.text.ParseException ; 31 import java.util.logging.Level ; 32 import javax.swing.Timer ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.event.EventListenerList ; 36 import net.fortuna.ical4j.model.ValidationException; 37 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 38 import org.netbeans.modules.tasklist.usertasks.translators.ICalExportFormat; 39 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 40 import org.openide.DialogDisplayer; 41 import org.openide.NotifyDescriptor; 42 import org.openide.NotifyDescriptor.Message; 43 import org.openide.filesystems.FileLock; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 49 55 public class AutoSaver { 56 private UserTaskList utl; 57 private EventListenerList listeners = new EventListenerList (); 58 59 62 private boolean modified = false; 63 64 65 private FileObject file = null; 66 67 71 private Timer runTimer = null; 72 73 private boolean enabled = false; 74 private DataObject do_; 75 76 82 public AutoSaver(UserTaskList utl, FileObject file) { 83 try { 84 this.do_ = DataObject.find(file); 85 } catch (DataObjectNotFoundException e) { 86 UTUtils.LOGGER.log(Level.SEVERE, e.getMessage(), e); 87 } 88 this.utl = utl; 89 this.file = file; 90 this.utl.addChangeListener(new ChangeListener () { 91 public void stateChanged(ChangeEvent ev) { 92 do_.setModified(true); 93 if (modified != true) { 94 UTUtils.LOGGER.fine("modified = true"); modified = true; 96 fireChange(); 97 } 98 if (enabled) 99 scheduleWrite(); 100 } 101 }); 102 } 103 104 109 public boolean isEnabled() { 110 return enabled; 111 } 112 113 118 public void setEnabled(boolean enabled) { 119 this.enabled = enabled; 120 } 121 122 125 public FileObject getFile() { 126 return file; 127 } 128 129 132 private void scheduleWrite() { 133 if (runTimer != null) { 136 runTimer.stop(); 137 runTimer = null; 138 } 139 ActionListener al = new ActionListener () { 140 public void actionPerformed(ActionEvent evt) { 141 runTimer = null; 142 try { 143 save(); 144 } catch (IOException ioe) { 145 DialogDisplayer.getDefault().notify(new Message(ioe, 146 NotifyDescriptor.ERROR_MESSAGE)); 147 } 148 } 149 }; 150 runTimer = new Timer (300, al); 152 runTimer.setRepeats(false); 153 runTimer.setCoalesce(true); 154 runTimer.start(); 155 } 156 157 162 public boolean isModified() { 163 return modified; 164 } 165 166 169 public void save() throws IOException { 170 ICalExportFormat io = new ICalExportFormat(); 171 172 FileLock lock = this.file.lock(); 173 try { 174 Writer w = new OutputStreamWriter (new BufferedOutputStream ( 175 file.getOutputStream(lock)), "UTF-8"); try { 177 io.writeList(utl, w, false); 178 } catch (ParseException e) { 179 throw new IOException (e.getMessage()); 180 } catch (URISyntaxException e) { 181 throw new IOException (e.getMessage()); 182 } catch (ValidationException e) { 183 throw new IOException (e.getMessage()); 184 } finally { 185 try { 186 w.close(); 187 } catch (IOException e) { 188 UTUtils.LOGGER.log(Level.WARNING, 189 "failed closing file", e); } 191 } 192 } finally { 193 lock.releaseLock(); 194 } 195 196 if (new File ("/bin/chmod").exists()) { try { 200 Runtime.getRuntime().exec( 201 new String [] {"/bin/chmod", "go-rwx", FileUtil.toFile(this.file).getAbsolutePath()}); 203 } catch (Exception e) { 204 UTUtils.LOGGER.log(Level.INFO, 206 "chmod call failed", e); } 208 } 209 210 if (modified != false) { 211 modified = false; 212 do_.setModified(false); 213 UTUtils.LOGGER.fine("modified = false"); fireChange(); 215 } 216 } 217 218 223 public UserTaskList getUserTaskList() { 224 return utl; 225 } 226 227 228 234 public void addChangeListener(ChangeListener l) { 235 listeners.add(ChangeListener .class, l); 236 } 237 238 243 public void removeChangeListener(ChangeListener l) { 244 listeners.remove(ChangeListener .class, l); 245 } 246 247 250 void fireChange() { 251 Object [] list = listeners.getListenerList(); 253 254 ChangeEvent changeEvent = null; 257 for (int i = list.length - 2; i >= 0; i -= 2) { 258 if (list[i] == ChangeListener .class) { 259 if (changeEvent == null) 261 changeEvent = new ChangeEvent (this); 262 ((ChangeListener ) list[i+1]).stateChanged(changeEvent); 263 } 264 } 265 } 266 } 267 | Popular Tags |