Source Code Cross Referenced for SearchScopeNodeSelection.java in  » IDE-Netbeans » utilities » org » netbeans » modules » search » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » utilities » org.netbeans.modules.search 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.modules.search;
043:
044:        import java.beans.PropertyChangeEvent;
045:        import java.beans.PropertyChangeListener;
046:        import java.util.ArrayList;
047:        import java.util.Collection;
048:        import java.util.Collections;
049:        import java.util.HashMap;
050:        import java.util.Iterator;
051:        import java.util.List;
052:        import java.util.Map;
053:        import org.openide.loaders.DataFolder;
054:        import org.openide.loaders.DataObject;
055:        import org.openide.nodes.Node;
056:        import org.openide.util.Lookup;
057:        import org.openide.util.LookupEvent;
058:        import org.openide.util.LookupListener;
059:        import org.openide.util.NbBundle;
060:        import org.openide.util.WeakListeners;
061:        import org.openide.windows.TopComponent;
062:        import org.openidex.search.FileObjectFilter;
063:        import org.openidex.search.SearchInfo;
064:        import org.openidex.search.SearchInfoFactory;
065:        import static org.openide.windows.TopComponent.Registry.PROP_ACTIVATED_NODES;
066:
067:        /**
068:         * Defines search scope across selected nodes.
069:         *
070:         * @author  Marian Petras
071:         */
072:        final class SearchScopeNodeSelection extends AbstractSearchScope
073:                implements  PropertyChangeListener {
074:
075:            private PropertyChangeListener currentNodesWeakListener;
076:
077:            public SearchScopeNodeSelection() {
078:                super ();
079:            }
080:
081:            @Override
082:            public String getTypeId() {
083:                return "node selection"; //NOI18N
084:            }
085:
086:            public String getDisplayName() {
087:                return NbBundle.getMessage(getClass(),
088:                        "SearchScopeNameSelectedNodes"); //NOI18N
089:            }
090:
091:            @Override
092:            protected String getAdditionalInfo() {
093:                final Node[] nodes = getNodes();
094:
095:                if ((nodes == null) || (nodes.length == 0)) {
096:                    return null;
097:                }
098:
099:                return (nodes.length == 1) ? nodes[0].getDisplayName()
100:                        : NbBundle.getMessage(getClass(),
101:                                "SearchScopeSelectionAddInfo", //NOI18N
102:                                nodes.length);
103:            }
104:
105:            @Override
106:            protected boolean checkIsApplicable() {
107:                return checkIsApplicable(getNodes());
108:            }
109:
110:            private Node[] getNodes() {
111:                return TopComponent.getRegistry().getActivatedNodes();
112:            }
113:
114:            /**
115:             * Decides whether searching should be enabled with respect to a set
116:             * of selected nodes.
117:             * Searching is enabled if searching instructions
118:             * (<code>SearchInfo</code> object) are available for all selected nodes
119:             * and at least one registered search type is able to search all the
120:             * selected nodes.
121:             *
122:             * @param  nodes  selected nodes
123:             * @return  <code>true</code> if searching the selected nodes should be
124:             *          enabled; <code>false</code> otherwise
125:             * @see  SearchInfo
126:             * @see  SearchType
127:             */
128:            private static boolean checkIsApplicable(Node[] nodes) {
129:                if (nodes == null || nodes.length == 0) {
130:                    return false;
131:                }
132:
133:                for (Node node : nodes) {
134:                    if (!canSearch(node)) {
135:                        return false;
136:                    }
137:                }
138:                return true;
139:            }
140:
141:            /**
142:             */
143:            private static boolean canSearch(Node node) {
144:                Lookup nodeLookup = node.getLookup();
145:
146:                /* 1st try - is the SearchInfo object in the node's lookup? */
147:                SearchInfo searchInfo = nodeLookup.lookup(SearchInfo.class);
148:                if (searchInfo != null) {
149:                    return searchInfo.canSearch();
150:                }
151:
152:                final DataObject dataObj = nodeLookup.lookup(DataObject.class);
153:                return (dataObj != null) && dataObj.isValid();
154:            }
155:
156:            protected void startListening() {
157:
158:                /* thread: <any> */
159:
160:                TopComponent.Registry tcRegistry = TopComponent.getRegistry();
161:                currentNodesWeakListener = WeakListeners.propertyChange(this ,
162:                        tcRegistry);
163:                tcRegistry.addPropertyChangeListener(currentNodesWeakListener);
164:            }
165:
166:            protected void stopListening() {
167:
168:                /* thread: <any> */
169:
170:                TopComponent.getRegistry().removePropertyChangeListener(
171:                        currentNodesWeakListener);
172:                currentNodesWeakListener = null;
173:            }
174:
175:            public synchronized void propertyChange(PropertyChangeEvent e) {
176:                if (PROP_ACTIVATED_NODES.equals(e.getPropertyName())) {
177:                    updateIsApplicable();
178:                }
179:            }
180:
181:            public SearchInfo getSearchInfo() {
182:                return getSearchInfo(TopComponent.getRegistry()
183:                        .getActivatedNodes());
184:            }
185:
186:            private SearchInfo getSearchInfo(Node[] nodes) {
187:                if ((nodes == null) || (nodes.length == 0)) {
188:                    return createEmptySearchInfo();
189:                }
190:
191:                nodes = normalizeNodes(nodes);
192:                if (nodes.length == 1) {
193:                    SearchInfo searchInfo = getSearchInfo(nodes[0]);
194:                    return (searchInfo != null) ? searchInfo
195:                            : createEmptySearchInfo();
196:                }
197:
198:                List<SearchInfo> searchInfos = new ArrayList<SearchInfo>(
199:                        nodes.length);
200:                for (Node node : nodes) {
201:                    SearchInfo searchInfo = getSearchInfo(node);
202:                    if (searchInfo != null) {
203:                        searchInfos.add(searchInfo);
204:                    }
205:                }
206:
207:                if (searchInfos.isEmpty()) {
208:                    return createEmptySearchInfo();
209:                }
210:                int searchInfoCount = searchInfos.size();
211:                if (searchInfoCount == 1) {
212:                    return searchInfos.get(0);
213:                } else {
214:                    return SearchInfoFactory
215:                            .createCompoundSearchInfo(searchInfos
216:                                    .toArray(new SearchInfo[searchInfoCount]));
217:                }
218:            }
219:
220:            /**
221:             */
222:            private static SearchInfo getSearchInfo(Node node) {
223:                /* 1st try - is the SearchInfo object in the node's lookup? */
224:                SearchInfo info = node.getLookup().lookup(SearchInfo.class);
225:                if (info != null) {
226:                    return info;
227:                }
228:
229:                /* 2nd try - does the node represent a DataObject.Container? */
230:                final Lookup nodeLookup = node.getLookup();
231:                DataFolder dataFolder = nodeLookup.lookup(DataFolder.class);
232:                if (dataFolder != null) {
233:                    return SearchInfoFactory
234:                            .createSearchInfo(
235:                                    dataFolder.getPrimaryFile(),
236:                                    true, //recursive
237:                                    new FileObjectFilter[] { SearchInfoFactory.VISIBILITY_FILTER });
238:                } else {
239:                    DataObject dataObj = nodeLookup.lookup(DataObject.class);
240:                    if (dataObj != null) {
241:                        return new DataObjectSearchInfo(dataObj);
242:                    }
243:                }
244:
245:                return null;
246:            }
247:
248:            private static final class DataObjectSearchInfo implements 
249:                    SearchInfo {
250:
251:                private final DataObject dataObj;
252:
253:                DataObjectSearchInfo(final DataObject dataObj) {
254:                    this .dataObj = dataObj;
255:                }
256:
257:                public boolean canSearch() {
258:                    return dataObj.isValid();
259:                }
260:
261:                public Iterator<DataObject> objectsToSearch() {
262:                    return Collections.<DataObject> singleton(dataObj)
263:                            .iterator();
264:                }
265:
266:            }
267:
268:            /**
269:             * Computes a subset of nodes (search origins) covering all specified nodes.
270:             * <p>
271:             * Search is performed on trees whose roots are the specified nodes.
272:             * If node A is a member of the tree determined by node B, then the A's tree
273:             * is a subtree of the B's tree. It means that it is redundant to extra
274:             * search the A's tree. This method computes a minimum set of nodes whose
275:             * trees cover all nodes' subtrees but does not contain any node not covered
276:             * by the original set of nodes.
277:             *
278:             * @param  nodes  roots of search trees
279:             * @return  subset of the original set of nodes
280:             *          (may be the same object as the parameter)
281:             */
282:            private static Node[] normalizeNodes(Node[] nodes) {
283:
284:                /* No need to normalize: */
285:                if (nodes.length < 2) {
286:                    return nodes;
287:                }
288:
289:                /*
290:                 * In the algorithm, we use two sets of nodes: "good nodes" and "bad
291:                 * nodes". "Good nodes" are nodes not known to be covered by any 
292:                 * search root. "Bad nodes" are nodes known to be covered by at least
293:                 * one of the search roots.
294:                 *
295:                 * Since all the search roots are covered by themselves, they are all
296:                 * put to "bad nodes" initially. To recognize whether a search root
297:                 * is covered only by itself or whether it is covered by any other
298:                 * search root, the former group of nodes has mapped value FALSE
299:                 * and the later group of nodes has mapped value TRUE.
300:                 *
301:                 * Initially, all search roots have mapped value FALSE (not known to be
302:                 * covered by any other search root) and as the procedure runs, some of
303:                 * them may be remapped to value TRUE (known to be covered by at least
304:                 * one other search root).
305:                 *
306:                 * The algorithm checks all search roots one by one. The ckeck starts
307:                 * at a search root to be tested and continues up to its parents until
308:                 * one of the following:
309:                 *  a) the root of the whole tree of nodes is reached
310:                 *     - i.e. the node being checked is not covered by any other
311:                 *       search root
312:                 *     - mark all the nodes in the path from the node being checked
313:                 *       to the root as "good nodes", except the search root being
314:                 *       checked
315:                 *     - put the search root being checked into the resulting set
316:                 *       of nodes
317:                 *  b) a "good node" is reached
318:                 *     - i.e. neither the good node nor any of the nodes on the path
319:                 *       are covered by any other search root
320:                 *     - mark all the nodes in the path from the node being checked
321:                 *       to the root as "good nodes", except the search root being
322:                 *       checked
323:                 *     - put the search root being checked into the resulting set
324:                 *       of nodes
325:                 *  c) a "bad node" is reached (it may be either another search root
326:                 *     or another "bad node")
327:                 *     - i.e. we know that the reached node is covered by another search
328:                 *       root or the reached node is another search root - in both cases
329:                 *       the search root being checked is covered by another search root
330:                 *     - mark all the nodes in the path from the node being checked
331:                 *       to the root as "bad nodes"; the search root being checked
332:                 *       will be remapped to value TRUE
333:                 */
334:
335:                Map<Node, Boolean> badNodes = new HashMap<Node, Boolean>(
336:                        2 * nodes.length, 0.75f);
337:                Map<Node, Boolean> goodNodes = new HashMap<Node, Boolean>(
338:                        2 * nodes.length, 0.75f);
339:                List<Node> path = new ArrayList<Node>(10);
340:                List<Node> result = new ArrayList<Node>(nodes.length);
341:
342:                /* Put all search roots into "bad nodes": */
343:                for (Node node : nodes) {
344:                    badNodes.put(node, Boolean.FALSE);
345:                }
346:
347:                main_cycle: for (Node node : nodes) {
348:                    path.clear();
349:                    boolean isBad = false;
350:                    for (Node n = node.getParentNode(); n != null; n = n
351:                            .getParentNode()) {
352:                        if (badNodes.containsKey(n)) {
353:                            isBad = true;
354:                            break;
355:                        }
356:                        if (goodNodes.containsKey(n)) {
357:                            break;
358:                        }
359:                        path.add(n);
360:                    }
361:                    if (isBad) {
362:                        badNodes.put(node, Boolean.TRUE);
363:                        for (Node n : path) {
364:                            badNodes.put(n, Boolean.TRUE);
365:                        }
366:                    } else {
367:                        for (Node n : path) {
368:                            goodNodes.put(n, Boolean.TRUE);
369:                        }
370:                        result.add(node);
371:                    }
372:                }
373:                return result.toArray(new Node[result.size()]);
374:            }
375:
376:            @Override
377:            protected SearchScope getContextSensitiveInstance(Lookup context) {
378:                return new LookupSensitive(this , context);
379:            }
380:
381:            /**
382:             * Lookup-sensitive variant of {@code SearchScopeNodeSelection}.
383:             */
384:            private static final class LookupSensitive extends
385:                    AbstractSearchScope implements  LookupListener {
386:
387:                private static final Node[] emptyNodesArray = new Node[0];
388:                private final SearchScopeNodeSelection delegate;
389:                private final Lookup.Result<Node> lookupResult;
390:                private LookupListener lookupListener;
391:
392:                LookupSensitive(SearchScopeNodeSelection delegate, Lookup lookup) {
393:                    this .delegate = delegate;
394:
395:                    lookupResult = lookup.lookupResult(Node.class);
396:                }
397:
398:                private Node[] nodes() {
399:                    Collection<? extends Node> nodesColl = lookupResult
400:                            .allInstances();
401:                    return nodesColl.isEmpty() ? emptyNodesArray : nodesColl
402:                            .toArray(emptyNodesArray);
403:                }
404:
405:                protected void startListening() {
406:
407:                    /* thread: <any> */
408:
409:                    lookupListener = WeakListeners.create(LookupListener.class,
410:                            this , lookupResult);
411:                    lookupResult.addLookupListener(lookupListener);
412:                }
413:
414:                protected void stopListening() {
415:
416:                    /* thread: <any> */
417:
418:                    if (lookupListener != null) {
419:                        lookupResult.removeLookupListener(lookupListener);
420:                    }
421:                }
422:
423:                public void resultChanged(LookupEvent ev) {
424:                    updateIsApplicable();
425:                }
426:
427:                protected boolean checkIsApplicable() {
428:                    return SearchScopeNodeSelection.checkIsApplicable(nodes());
429:                }
430:
431:                protected SearchInfo getSearchInfo() {
432:                    return delegate.getSearchInfo(nodes());
433:                }
434:
435:                @Override
436:                public String getTypeId() {
437:                    return delegate.getTypeId();
438:                }
439:
440:                protected String getDisplayName() {
441:                    return delegate.getDisplayName();
442:                }
443:
444:                @Override
445:                protected String getAdditionalInfo() {
446:                    return delegate.getAdditionalInfo();
447:                }
448:
449:            }
450:
451:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.