1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.logging.Level ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import net.fortuna.ical4j.data.ParserException; 29 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 30 import org.netbeans.modules.tasklist.usertasks.translators.ICalImportFormat; 31 import org.netbeans.modules.tasklist.usertasks.util.AWTThreadAnnotation; 32 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 33 import org.openide.DialogDisplayer; 34 import org.openide.NotifyDescriptor; 35 import org.openide.NotifyDescriptor.Message; 36 import org.openide.cookies.OpenCookie; 37 import org.openide.cookies.SaveCookie; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.loaders.DataObjectExistsException; 41 import org.openide.loaders.MultiDataObject; 42 import org.openide.nodes.CookieSet; 43 import org.openide.nodes.Node; 44 import org.openide.util.NbBundle; 45 46 53 public class TaskListDataObject extends MultiDataObject implements OpenCookie, 54 ChangeListener { 55 private static final long serialVersionUID = 1; 56 57 private UserTaskList utl; 58 59 66 public TaskListDataObject(FileObject pf, TaskListLoader loader) 67 throws DataObjectExistsException { 68 super(pf, loader); 69 CookieSet cookies = getCookieSet(); 70 cookies.add(this); } 72 73 76 public void release() { 77 if (utl != null) { 78 utl.removeChangeListener(this); 79 utl = null; 80 } 81 } 82 83 88 @AWTThreadAnnotation 89 public UserTaskList getUserTaskList() throws IOException { 90 if (utl == null) { 91 utl = readDocument(getPrimaryFile()); 92 utl.addChangeListener(this); 93 } 94 return utl; 95 } 96 97 @Override 98 public void setModified(boolean modif) { 99 firePropertyChange(TaskListDataObject.PROP_COOKIE, null, null); 100 super.setModified(modif); 101 } 102 103 protected Node createNodeDelegate() { 104 return new TaskListDataNode(this); 105 } 106 107 public void open() { 109 SwingUtilities.invokeLater(new Runnable () { 110 public void run() { 111 open_(); 112 } 113 }); 114 } 115 116 119 private void open_() { 120 UserTaskView view = UserTaskViewRegistry.getInstance(). 121 findView(getPrimaryEntry().getFile()); 122 if (view == null) { 123 FileObject fo = getPrimaryEntry().getFile(); 124 view = new UserTaskView(fo, false); 125 view.showInMode(); 126 } else { 127 view.showInMode(); 129 } 130 } 131 132 @Override 133 public <T extends Node.Cookie> T getCookie(Class <T> c) { 134 if (c == SaveCookie.class && isModified()) 135 return (T) new UTSaveCookie(this, utl); 136 else 137 return super.getCookie(c); 138 } 139 140 145 @AWTThreadAnnotation 146 private static UserTaskList readDocument(FileObject fo) throws IOException { 147 if (!fo.isValid()) 148 throw new IOException ( 149 NbBundle.getMessage(UserTaskList.class, 150 "FileNotValid", FileUtil.getFileDisplayName(fo))); InputStream is = fo.getInputStream(); 152 UserTaskList ret = null; 153 try { 154 long m = System.currentTimeMillis(); 155 ICalImportFormat io = new ICalImportFormat(); 156 157 ret = new UserTaskList(); 158 try { 159 io.read(ret, is); 160 } catch (ParserException e) { 161 DialogDisplayer.getDefault().notify(new Message(e.getMessage(), 163 NotifyDescriptor.ERROR_MESSAGE)); 164 UTUtils.LOGGER.log(Level.SEVERE, "", e); } catch (IOException e) { 166 DialogDisplayer.getDefault().notify(new Message(e.getMessage(), 168 NotifyDescriptor.ERROR_MESSAGE)); 169 UTUtils.LOGGER.log(Level.SEVERE, "", e); } 171 172 UTUtils.LOGGER.fine("File " + fo + " read in " + (System.currentTimeMillis() - m) + "ms"); } finally { 175 try { 176 is.close(); 177 } catch (IOException e) { 178 UTUtils.LOGGER.log(Level.WARNING, 179 "closing file failed", e); } 181 } 182 return ret; 183 } 184 185 public void stateChanged(ChangeEvent e) { 186 setModified(true); 187 } 188 } 189 | Popular Tags |