KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > TaskListDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
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 /**
47  * Represents a tasklist object in the Repository.
48  *
49  * @author Tor Norbye
50  * @author Trond Norbye
51  * @author tl
52  */

53 public class TaskListDataObject extends MultiDataObject implements OpenCookie,
54 ChangeListener JavaDoc {
55     private static final long serialVersionUID = 1;
56
57     private UserTaskList utl;
58     
59     /**
60      * Constructor.
61      *
62      * @param pf .ics file
63      * @param loader TaskListLoader
64      * @throws org.openide.loaders.DataObjectExistsException
65      */

66     public TaskListDataObject(FileObject pf, TaskListLoader loader)
67             throws DataObjectExistsException {
68     super(pf, loader);
69         CookieSet cookies = getCookieSet();
70     cookies.add(this); // OpenCookie
71
}
72
73     /**
74      * Releases internal reference to the UserTaskList object.
75      */

76     public void release() {
77         if (utl != null) {
78             utl.removeChangeListener(this);
79             utl = null;
80         }
81     }
82     
83     /**
84      * Returns the task list contained in the file.
85      *
86      * @return task list
87      */

88     @AWTThreadAnnotation
89     public UserTaskList getUserTaskList() throws IOException JavaDoc {
90         if (utl == null) {
91             utl = readDocument(getPrimaryFile());
92             utl.addChangeListener(this);
93         }
94         return utl;
95     }
96     
97     @Override JavaDoc
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     // Implements OpenCookie
108
public void open() {
109         SwingUtilities.invokeLater(new Runnable JavaDoc() {
110             public void run() {
111                 open_();
112             }
113         });
114     }
115     
116     /**
117      * Opens the TC in the Swing thread
118      */

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             // This view already exists, show it...
128
view.showInMode();
129         }
130     }
131
132     @Override JavaDoc
133     public <T extends Node.Cookie> T getCookie(Class JavaDoc<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     /**
141      * Reads an ics file. Shows error messages if it cannot be read.
142      *
143      * @param fo an .ics file
144      */

145     @AWTThreadAnnotation
146     private static UserTaskList readDocument(FileObject fo) throws IOException JavaDoc {
147         if (!fo.isValid())
148             throw new IOException JavaDoc(
149                 NbBundle.getMessage(UserTaskList.class,
150                     "FileNotValid", FileUtil.getFileDisplayName(fo))); // NOI18N
151
InputStream JavaDoc 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                 // NOTE the exception text should be localized!
162
DialogDisplayer.getDefault().notify(new Message(e.getMessage(),
163                         NotifyDescriptor.ERROR_MESSAGE));
164                 UTUtils.LOGGER.log(Level.SEVERE, "", e); // NOI18N
165
} catch (IOException JavaDoc e) {
166                 // NOTE the exception text should be localized!
167
DialogDisplayer.getDefault().notify(new Message(e.getMessage(),
168                         NotifyDescriptor.ERROR_MESSAGE));
169                 UTUtils.LOGGER.log(Level.SEVERE, "", e); // NOI18N
170
}
171
172             UTUtils.LOGGER.fine("File " + fo + " read in " + // NOI18N
173
(System.currentTimeMillis() - m) + "ms"); // NOI18N
174
} finally {
175             try {
176                 is.close();
177             } catch (IOException JavaDoc e) {
178                 UTUtils.LOGGER.log(Level.WARNING,
179                         "closing file failed", e); // NOI18N
180
}
181         }
182         return ret;
183     }
184
185     public void stateChanged(ChangeEvent JavaDoc e) {
186         setModified(true);
187     }
188 }
189
Popular Tags