1 19 20 package org.netbeans.modules.search.project; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectUtils; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.api.project.ui.OpenProjects; 34 import org.netbeans.modules.search.SearchPanel; 35 import org.netbeans.modules.search.SearchPerformer; 36 import org.openide.filesystems.FileObject; 37 import org.openide.nodes.AbstractNode; 38 import org.openide.nodes.Children; 39 40 import org.openide.nodes.Node; 41 import org.openide.util.Mutex; 42 import org.openide.util.actions.CallableSystemAction; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 import org.openide.util.SharedClassObject; 46 import org.openide.util.lookup.Lookups; 47 import org.openidex.search.FileObjectFilter; 48 import org.openidex.search.SearchInfo; 49 import org.openidex.search.SearchInfoFactory; 50 51 52 77 public final class ProjectsSearchAction extends CallableSystemAction 78 implements PropertyChangeListener { 79 80 static final long serialVersionUID = 4554342565076372610L; 81 82 88 private static final String VAR_FIRST_ISENABLED 89 = "first call of isEnabled()"; 93 private static final String VAR_TOOLBAR_COMP_REF 94 = "toolbar presenter ref"; 100 private static final String VAR_LISTENING 101 = "listening"; 103 105 protected void initialize() { 106 super.initialize(); 107 putProperty(VAR_FIRST_ISENABLED, Boolean.TRUE); 108 } 109 110 112 public Component getToolbarPresenter() { 113 synchronized (getLock()) { 114 Component presenter = getStoredToolbarPresenter(); 115 if (putProperty(VAR_LISTENING, Boolean.TRUE) == null) { 116 OpenProjects.getDefault().addPropertyChangeListener(this); 117 putProperty(VAR_FIRST_ISENABLED, null); 118 updateState(); 119 } 120 return presenter; 121 } 122 } 123 124 133 private Component getStoredToolbarPresenter() { 134 Object refObj = getProperty(VAR_TOOLBAR_COMP_REF); 135 if (refObj != null) { 136 Reference ref = (Reference ) refObj; 137 Object presenterObj = ref.get(); 138 if (presenterObj != null) { 139 return (Component ) presenterObj; 140 } 141 } 142 143 Component presenter = super.getToolbarPresenter(); 144 putProperty(VAR_TOOLBAR_COMP_REF, new WeakReference <Component >(presenter)); 145 return presenter; 146 } 147 148 157 private boolean checkToolbarPresenterExists() { 158 Object refObj = getProperty(VAR_TOOLBAR_COMP_REF); 159 if (refObj == null) { 160 return false; 161 } 162 return ((Reference ) refObj).get() != null; 163 } 164 165 169 public void propertyChange(PropertyChangeEvent e) { 170 synchronized (getLock()) { 171 172 179 if (getProperty(VAR_LISTENING) == null) { 180 return; 181 } 182 183 if (checkToolbarPresenterExists()) { 184 updateState(); 185 } else { 186 OpenProjects.getDefault().removePropertyChangeListener(this); 187 putProperty(VAR_LISTENING, null); 188 putProperty(VAR_TOOLBAR_COMP_REF, null); 189 } 190 } 191 192 } 193 194 196 public boolean isEnabled() { 197 synchronized (getLock()) { 198 if (getProperty(VAR_LISTENING) != null) { 199 return super.isEnabled(); 200 } else if (getProperty(VAR_FIRST_ISENABLED) == null) { 201 return OpenProjects.getDefault().getOpenProjects().length != 0; 202 } else { 203 204 205 putProperty(VAR_FIRST_ISENABLED, null); 206 return false; 207 } 208 } 209 } 210 211 213 private synchronized void updateState() { 214 215 221 222 final boolean enabled 223 = OpenProjects.getDefault().getOpenProjects().length != 0; 224 Mutex.EVENT.writeAccess(new Runnable () { 225 public void run() { 226 setEnabled(enabled); 227 } 228 }); 229 } 230 231 protected String iconResource() { 232 return "org/openide/resources/actions/find.gif"; } 234 235 public String getName() { 236 return NbBundle.getMessage(ProjectsSearchAction.class, 237 "LBL_SearchProjects"); } 239 240 public HelpCtx getHelpCtx() { 241 return new HelpCtx(ProjectsSearchAction.class); 242 } 243 244 245 Node[] getNodesToSearch() { 246 Project[] openProjects = OpenProjects.getDefault().getOpenProjects(); 247 248 if (openProjects.length == 0) { 249 250 258 return new Node[0]; 259 } 260 261 List <SearchInfo> searchInfos = new ArrayList <SearchInfo>(); 262 List <FileObject> rootFolders = new ArrayList <FileObject>(); 263 for (int i = 0; i < openProjects.length; i++) { 264 SearchInfo prjSearchInfo = openProjects[i].getLookup().lookup(SearchInfo.class); 265 if (prjSearchInfo != null) { 266 searchInfos.add(prjSearchInfo); 267 continue; 268 } 269 270 Sources sources = ProjectUtils.getSources(openProjects[i]); 271 SourceGroup[] sourceGroups 272 = sources.getSourceGroups(Sources.TYPE_GENERIC); 273 for (int j = 0; j < sourceGroups.length; j++) { 274 rootFolders.add(sourceGroups[j].getRootFolder()); 275 } 276 } 277 278 int nodesCount = 0; 279 if (!searchInfos.isEmpty()) { 280 nodesCount += searchInfos.size(); 281 } 282 if (!rootFolders.isEmpty()) { 283 nodesCount++; 284 } 285 286 if (nodesCount == 0) { 287 return new Node[0]; 288 } 289 290 Node[] searchNodes = new Node[nodesCount]; 291 int nodeIndex = 0; 292 if (!searchInfos.isEmpty()) { 293 for (SearchInfo searchInfo : searchInfos) { 294 searchNodes[nodeIndex++] = new AbstractNode( 295 Children.LEAF, 296 Lookups.singleton(searchInfo)); 297 } 298 } 299 if (!rootFolders.isEmpty()) { 300 SearchInfo searchInfo = SearchInfoFactory.createSearchInfo( 301 rootFolders.toArray(new FileObject[rootFolders.size()]), 302 true, new FileObjectFilter[] {SearchInfoFactory.VISIBILITY_FILTER, 304 SearchInfoFactory.SHARABILITY_FILTER}); 305 searchNodes[nodeIndex++] = new AbstractNode( 306 Children.LEAF, 307 Lookups.singleton(searchInfo)); 308 } 309 310 searchNodes[0].setValue( 311 SearchPanel.PROP_DIALOG_TITLE, 312 NbBundle.getMessage(ProjectsSearchAction.class, 313 "LBL_Title_SearchProjects")); return searchNodes; 315 } 316 317 318 public void performAction() { 319 SearchPerformer performer 320 = ((SearchPerformer) SharedClassObject.findObject( 321 SearchPerformer.class, true)); 322 performer.performAction(getNodesToSearch()); 323 } 324 325 327 protected boolean asynchronous() { 328 return false; 329 } 330 331 } 332 | Popular Tags |