KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > types > DataObjectType


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.search.types;
21
22
23 import org.openide.filesystems.FileObject;
24 import org.openide.loaders.DataObject;
25 import org.openide.loaders.DataObjectNotFoundException;
26 import org.openide.nodes.Node;
27 import org.openidex.search.SearchType;
28
29 import org.openide.ErrorManager;
30 import org.openide.util.Lookup;
31 import org.openidex.search.SearchInfo;
32
33
34 /**
35  * Type used by SearchType which can be part of <code>DataObjectSearchGroup</code>.
36  *
37  * @author Petr Kuzel
38  * @see org.openidex.search.DataObjectSearchGroup
39  */

40 public abstract class DataObjectType extends SearchType {
41
42     private static final long serialVersionUID = 1L;
43     //private static final long serialVersionUID = 4048358224805855612L;
44

45     /***** LIVE UPDATE: ****** /
46     / ** Property change listener. * /
47     private transient PropertyChangeListener propListener;
48     / *************************/

49     
50     
51     /** Prepares search object for search. Listens on the underlying
52      * object and fires SearchType.PROP_OBJECT_CHANGED property change
53      * in cases object has changed. */

54     protected void prepareSearchObject(Object JavaDoc searchObject) {
55         
56         /***** LIVE UPDATE: ****** /
57         DataObject dataObject = extractDataObject(searchObject);
58
59         if (dataObject == null) {
60             return;
61         }
62         dataObject.addPropertyChangeListener(
63             WeakListeners.propertyChange(getDataObjectListener(), dataObject)
64         );
65         / *************************/

66     }
67
68     /***** LIVE UPDATE: ****** /
69     / ** Gets property change listener which listens on changes on searched data object. * /
70     private synchronized PropertyChangeListener getDataObjectListener() {
71         if (propListener == null) {
72             propListener = new PropertyChangeListener() {
73                 public void propertyChange(PropertyChangeEvent evt) {
74                     if (DataObject.PROP_COOKIE.equals(evt.getPropertyName())) {
75                         firePropertyChange(PROP_OBJECT_CHANGED, null, evt.getSource());
76                     }
77                 }
78             };
79         }
80         
81         return propListener;
82     }
83     / *************************/

84     
85     /**
86      */

87     public boolean testObject(Object JavaDoc object) {
88         DataObject dataObject = extractDataObject(object);
89         return dataObject != null && testDataObject(dataObject);
90     }
91
92     /** Gets data object from search object. */
93     private static DataObject extractDataObject(Object JavaDoc object) {
94         DataObject dataObject = null;
95         
96         if (object instanceof DataObject) {
97             dataObject = (DataObject) object;
98         } else if (object instanceof FileObject) {
99             try {
100                 dataObject = DataObject.find((FileObject) object);
101             } catch (DataObjectNotFoundException dnfe) {
102                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, dnfe);
103             }
104         }
105
106         return dataObject;
107     }
108
109     /** Creates search types classes. */
110     protected Class JavaDoc[] createSearchTypeClasses() {
111         return new Class JavaDoc[] {DataObject.class};
112     }
113     
114     /**
115      * Checks whether a data object matches the criteria defined in this search
116      * type.
117      *
118      * @param dataObject object to be tested
119      * @return <code>true</code> if the object matches the criteria,
120      * <code>false</code> it it does not
121      */

122     protected abstract boolean testDataObject(DataObject dataObject);
123
124     /**
125      * {@inheritDoc}
126      *
127      * @return <code>true</code> if any of the specified nodes is a folder,
128      * <code>false</code> otherwise
129      */

130     public boolean enabled(Node[] nodes) {
131         
132         if (nodes == null || nodes.length == 0) {
133             return false;
134         }
135
136         // NodeCookie test
137
for (int i = 0; i < nodes.length; i++ ) {
138             Lookup nodeLookup = nodes[i].getLookup();
139
140             /*
141              * All DataObject containers are searchable.
142              * Even if they have SearchInfo and their canSearch() returns false.
143              */

144             if (nodeLookup.lookup(DataObject.Container.class) != null) {
145                 return true;
146             }
147
148             /*
149              * We must have checked that canSearch() returns true.
150              *
151              * This method must ensure that if it returns true,
152              * all the passed nodes are searchable. Only nodes whose
153              * SearchInfo.canSearch() return true are guaranteed to be
154              * searchable.
155              */

156             Object JavaDoc searchInfo = nodeLookup.lookup(SearchInfo.class);
157             if (searchInfo != null && ((SearchInfo) searchInfo).canSearch()) {
158                 return true;
159             }
160         }
161
162         return false;
163     }
164
165 }
166
Popular Tags