KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FileEvent


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.openide.filesystems;
21
22 import java.util.Date JavaDoc;
23 import java.util.EventObject JavaDoc;
24
25 /** Event for listening on filesystem changes.
26 * <P>
27 * By calling {@link #getFile} the original file where the action occurred
28 * can be obtained.
29 *
30 * @author Jaroslav Tulach, Petr Hamernik
31 */

32 public class FileEvent extends EventObject JavaDoc {
33     /** generated Serialized Version UID */
34     private static final long serialVersionUID = 1028087432345400108L;
35
36     /** Original file object where the action took place. */
37     private FileObject file;
38
39     /** time when this event has been fired */
40     private long time;
41
42     /** is expected? */
43     private boolean expected;
44
45     /***/
46     private EventControl.AtomicActionLink atomActionID;
47
48     /** Creates new <code>FileEvent</code>. The <code>FileObject</code> where the action occurred
49     * is assumed to be the same as the source object.
50     * @param src source file which sent this event
51     */

52     public FileEvent(FileObject src) {
53         this(src, src);
54     }
55
56     /** Creates new <code>FileEvent</code>, specifying the action object.
57     * <p>
58     * Note that the two arguments of this method need not be identical
59     * in cases where it is reasonable that a different file object from
60     * the one affected would be listened to by other components. E.g.,
61     * in the case of a file creation event, the event source (which
62     * listeners are attached to) would be the containing folder, while
63     * the action object would be the newly created file object.
64     * @param src source file which sent this event
65     * @param file <code>FileObject</code> where the action occurred */

66     public FileEvent(FileObject src, FileObject file) {
67         super(src);
68         this.file = file;
69         this.time = System.currentTimeMillis();
70     }
71
72     /** Creates new <code>FileEvent</code>. The <code>FileObject</code> where the action occurred
73     * is assumed to be the same as the source object. Important if FileEvent is created according to
74     * existing FileEvent but with another source and file but with the same time.
75     */

76     FileEvent(FileObject src, FileObject file, long time) {
77         this(src, file);
78         this.time = time;
79     }
80
81     /** Creates new <code>FileEvent</code>, specifying the action object.
82     * <p>
83     * Note that the two arguments of this method need not be identical
84     * in cases where it is reasonable that a different file object from
85     * the one affected would be listened to by other components. E.g.,
86     * in the case of a file creation event, the event source (which
87     * listeners are attached to) would be the containing folder, while
88     * the action object would be the newly created file object.
89     * @param src source file which sent this event
90     * @param file <code>FileObject</code> where the action occurred
91     * @param expected sets flag whether the value was expected*/

92     public FileEvent(FileObject src, FileObject file, boolean expected) {
93         this(src, file);
94         this.expected = expected;
95     }
96
97     /** @return the original file where action occurred
98     */

99     public final FileObject getFile() {
100         return file;
101     }
102
103     /** The time when this event has been created.
104     * @return the milliseconds
105     */

106     public final long getTime() {
107         return time;
108     }
109
110     /** Getter to test whether the change has been expected or not.
111     */

112     public final boolean isExpected() {
113         return expected;
114     }
115
116     @Override JavaDoc
117     public String JavaDoc toString() {
118         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
119         b.append(getClass().getName().replaceFirst(".+\\.", ""));
120         b.append('[');
121         FileObject src = (FileObject) getSource();
122         if (src != file) {
123             b.append("src=");
124             b.append(FileUtil.getFileDisplayName(src));
125             b.append(',');
126         }
127         b.append("file=");
128         b.append(FileUtil.getFileDisplayName(file));
129         b.append(",time=");
130         b.append(new Date JavaDoc(time));
131         b.append(",expected=");
132         b.append(expected);
133         insertIntoToString(b);
134         b.append(']');
135         return b.toString();
136     }
137     void insertIntoToString(StringBuilder JavaDoc b) {}
138
139     /** */
140     void setAtomicActionLink(EventControl.AtomicActionLink atomActionID) {
141         this.atomActionID = atomActionID;
142     }
143
144     /** Tests if FileEvent was fired from atomic action.
145      * @param run is tested atomic action.
146      * @return true if fired from run.
147      * @since 1.35
148      */

149     public boolean firedFrom(FileSystem.AtomicAction run) {
150         EventControl.AtomicActionLink currentPropID = this.atomActionID;
151
152         if (run == null) {
153             return false;
154         }
155
156         while (currentPropID != null) {
157             if (run.equals(currentPropID.getAtomicAction())) {
158                 return true;
159             }
160
161             currentPropID = currentPropID.getPreviousLink();
162         }
163
164         return false;
165     }
166 }
167
Popular Tags