KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > OccurrencesFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ant.internal.ui.editor.text.XMLTextHover;
18 import org.eclipse.ant.internal.ui.model.AntElementNode;
19 import org.eclipse.ant.internal.ui.model.AntModel;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.Position;
24
25 public class OccurrencesFinder {
26     
27     private AntModel fAntModel;
28     private AntEditor fEditor;
29     private int fOffset;
30     private IDocument fDocument;
31     
32     public OccurrencesFinder(AntEditor editor, AntModel model, IDocument document, int offset) {
33         fAntModel= model;
34         fEditor= editor;
35         fDocument= document;
36         fOffset= offset;
37     }
38     
39     public List JavaDoc perform() {
40         if (fOffset == 0 || fAntModel == null) {
41             return null;
42         }
43         
44         AntElementNode container= fAntModel.getNode(fOffset, false);
45         if (container == null) {
46             return null;
47         }
48         IRegion region= XMLTextHover.getRegion(fEditor.getViewer(), fOffset);
49         if (region != null) {
50             if (!container.isRegionPotentialReference(region)) {
51                 return null;
52             }
53         }
54         AntElementNode node;
55         if (container.isFromDeclaration(region)) {
56             node= container;
57         } else {
58             Object JavaDoc potentialNode= fEditor.findTarget(region);
59             if (!(potentialNode instanceof AntElementNode)) {
60                 return null;
61             }
62             node= (AntElementNode) potentialNode;
63         }
64         String JavaDoc occurrencesIdentifier= node.getOccurrencesIdentifier();
65         if (occurrencesIdentifier == null) {
66             return null;
67         }
68         List JavaDoc nodes= new ArrayList JavaDoc(1);
69         nodes.add(fAntModel.getProjectNode());
70         List JavaDoc usages= new ArrayList JavaDoc();
71         usages.add(node);
72         scanNodesForOccurrences(nodes, usages, occurrencesIdentifier);
73         String JavaDoc identifier;
74         try {
75             identifier= fDocument.get(region.getOffset(), region.getLength());
76         } catch (BadLocationException e) {
77             return null;
78         }
79         int length= identifier.length();
80         if (length == 0) {
81             return null;
82         }
83         List JavaDoc positions= new ArrayList JavaDoc(usages.size());
84         for (Iterator JavaDoc each= usages.iterator(); each.hasNext();) {
85             AntElementNode currentNode= (AntElementNode)each.next();
86             List JavaDoc offsets= currentNode.computeIdentifierOffsets(identifier);
87             if (offsets != null) {
88                 for (int i = 0; i < offsets.size(); i++) {
89                     positions.add(new Position(((Integer JavaDoc)offsets.get(i)).intValue(), length));
90                 }
91             }
92         }
93     
94         return positions;
95     }
96     
97     private void scanNodesForOccurrences(List JavaDoc nodes, List JavaDoc usages, String JavaDoc identifier) {
98         Iterator JavaDoc iter= nodes.iterator();
99         while (iter.hasNext()) {
100             AntElementNode node = (AntElementNode) iter.next();
101             if (!usages.contains(node) && node.containsOccurrence(identifier)) {
102                 usages.add(node);
103             }
104             if (node.hasChildren()) {
105                 scanNodesForOccurrences(node.getChildNodes(), usages, identifier);
106             }
107         }
108     }
109 }
Popular Tags