KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.openide.filesystems;
20
21 import java.util.List JavaDoc;
22 import org.openide.util.Exceptions;
23
24
25 /**
26  * Support class for impl. of FileChangeListener
27  * @author rm111737
28  */

29 class FCLSupport {
30     enum Op {DATA_CREATED, FOLDER_CREATED, FILE_CHANGED, FILE_DELETED, FILE_RENAMED, ATTR_CHANGED}
31
32     /** listeners */
33     ListenerList<FileChangeListener> listeners;
34
35     /* Add new listener to this object.
36     * @param l the listener
37     */

38     synchronized final void addFileChangeListener(FileChangeListener fcl) {
39         if (listeners == null) {
40             listeners = new ListenerList<FileChangeListener>();
41         }
42
43         listeners.add(fcl);
44     }
45
46     /* Remove listener from this object.
47     * @param l the listener
48     */

49     synchronized final void removeFileChangeListener(FileChangeListener fcl) {
50         if (listeners != null) {
51             listeners.remove(fcl);
52         }
53     }
54
55     final void dispatchEvent(FileEvent fe, Op operation) {
56         List JavaDoc<FileChangeListener> fcls;
57
58         synchronized (this) {
59             if (listeners == null) {
60                 return;
61             }
62
63             fcls = listeners.getAllListeners();
64         }
65
66         for (FileChangeListener l : fcls) {
67             dispatchEvent(l, fe, operation);
68         }
69     }
70
71     final static void dispatchEvent(FileChangeListener fcl, FileEvent fe, Op operation) {
72         try {
73             switch (operation) {
74                 case DATA_CREATED:
75                     fcl.fileDataCreated(fe);
76                     break;
77                 case FOLDER_CREATED:
78                     fcl.fileFolderCreated(fe);
79                     break;
80                 case FILE_CHANGED:
81                     fcl.fileChanged(fe);
82                     break;
83                 case FILE_DELETED:
84                     fcl.fileDeleted(fe);
85                     break;
86                 case FILE_RENAMED:
87                     fcl.fileRenamed((FileRenameEvent) fe);
88                     break;
89                 case ATTR_CHANGED:
90                     fcl.fileAttributeChanged((FileAttributeEvent) fe);
91                     break;
92                 default:
93                     throw new AssertionError JavaDoc(operation);
94             }
95         } catch (RuntimeException JavaDoc x) {
96             Exceptions.printStackTrace(x);
97         }
98     }
99
100     /** @return true if there is a listener
101     */

102     synchronized final boolean hasListeners() {
103         return listeners != null && listeners.hasListeners();
104     }
105 }
106
Popular Tags