1 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 40 public abstract class DataObjectType extends SearchType { 41 42 private static final long serialVersionUID = 1L; 43 45 49 50 51 54 protected void prepareSearchObject(Object searchObject) { 55 56 66 } 67 68 84 85 87 public boolean testObject(Object object) { 88 DataObject dataObject = extractDataObject(object); 89 return dataObject != null && testDataObject(dataObject); 90 } 91 92 93 private static DataObject extractDataObject(Object 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 110 protected Class [] createSearchTypeClasses() { 111 return new Class [] {DataObject.class}; 112 } 113 114 122 protected abstract boolean testDataObject(DataObject dataObject); 123 124 130 public boolean enabled(Node[] nodes) { 131 132 if (nodes == null || nodes.length == 0) { 133 return false; 134 } 135 136 for (int i = 0; i < nodes.length; i++ ) { 138 Lookup nodeLookup = nodes[i].getLookup(); 139 140 144 if (nodeLookup.lookup(DataObject.Container.class) != null) { 145 return true; 146 } 147 148 156 Object 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 |