1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 import java.net.URISyntaxException ; 28 import java.text.ParseException ; 29 import java.util.logging.Level ; 30 import net.fortuna.ical4j.model.ValidationException; 31 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 32 import org.netbeans.modules.tasklist.usertasks.translators.ICalExportFormat; 33 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 34 import org.openide.cookies.SaveCookie; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.loaders.DataObject; 39 40 45 public class UTSaveCookie implements SaveCookie { 46 private DataObject do_; 47 private UserTaskList utl; 48 49 55 public UTSaveCookie(DataObject do_, UserTaskList utl) { 56 this.do_ = do_; 57 this.utl = utl; 58 } 59 60 public void save() throws IOException { 61 ICalExportFormat io = new ICalExportFormat(); 62 63 FileObject file = do_.getPrimaryFile(); 64 FileLock lock = file.lock(); 65 try { 66 Writer w = new OutputStreamWriter (new BufferedOutputStream ( 67 file.getOutputStream(lock)), "UTF-8"); try { 69 io.writeList(utl, w, false); 70 } catch (ParseException e) { 71 throw new IOException (e.getMessage()); 72 } catch (URISyntaxException e) { 73 throw new IOException (e.getMessage()); 74 } catch (ValidationException e) { 75 throw new IOException (e.getMessage()); 76 } finally { 77 try { 78 w.close(); 79 } catch (IOException e) { 80 UTUtils.LOGGER.log(Level.WARNING, 81 "failed closing file", e); } 83 } 84 } finally { 85 lock.releaseLock(); 86 } 87 88 if (new File ("/bin/chmod").exists()) { try { 92 Runtime.getRuntime().exec( 93 new String [] {"/bin/chmod", "go-rwx", FileUtil.toFile(file).getAbsolutePath()}); 95 } catch (Exception e) { 96 UTUtils.LOGGER.log(Level.INFO, 98 "chmod call failed", e); } 100 } 101 102 do_.setModified(false); 103 } 104 } 105 | Popular Tags |