KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > contentassist > TypeContentProposalProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.pde.internal.ui.editor.contentassist;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.ListIterator JavaDoc;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.jface.fieldassist.IContentProposal;
22 import org.eclipse.jface.fieldassist.IContentProposalProvider;
23 import org.eclipse.swt.graphics.Image;
24
25 /**
26  * TypeContentProposalProvider
27  *
28  */

29 public class TypeContentProposalProvider extends TypePackageCompletionProcessor
30         implements IContentProposalProvider {
31
32     public static final char F_DOT = '.';
33     
34     private IProject fProject;
35     
36     private int fTypeScope;
37     
38     private ArrayList JavaDoc fInitialContentProposals;
39
40     private String JavaDoc fInitialContent;
41     
42     private Comparator JavaDoc fComparator;
43     
44     /**
45      *
46      */

47     public TypeContentProposalProvider(IProject project, int scope) {
48         fProject = project;
49         fTypeScope = scope;
50         fComparator = new TypeComparator();
51         
52         reset();
53     }
54
55     /**
56      * TypeComparator
57      *
58      */

59     private static class TypeComparator implements Comparator JavaDoc {
60         
61         /**
62          *
63          */

64         public TypeComparator() {
65             // NO-OP
66
}
67         
68         /* (non-Javadoc)
69          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
70          */

71         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
72             String JavaDoc proposalSortKey1 = ((IContentProposal)arg0).getLabel();
73             String JavaDoc proposalSortKey2 = ((IContentProposal)arg1).getLabel();
74             return proposalSortKey1.compareToIgnoreCase(proposalSortKey2);
75         }
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.fieldassist.IContentProposalProvider#getProposals(java.lang.String, int)
80      */

81     public IContentProposal[] getProposals(String JavaDoc contents, int position) {
82         // Generate a list of proposals based on the current contents
83
ArrayList JavaDoc currentContentProposals = null;
84         // Determine method to obtain proposals based on current field contents
85
if (position == 0) {
86             // If the document offset is at the 0 position (i.e. no input entered),
87
// do not perform content assist. The operation is too expensive
88
// because all classes and interfaces (depending on the specified scope)
89
// will need to be resolved as proposals
90
currentContentProposals = null;
91         } else if (
92             (fInitialContentProposals == null) ||
93             (contents.length() < fInitialContent.length()) ||
94             (endsWithDot(contents))) {
95             // Generate new proposals if the content assist session was just
96
// started
97
// Or generate new proposals if the current contents of the field
98
// is less than the initial contents of the field used to
99
// generate the original proposals; thus, widening the search
100
// scope. This can occur when the user types backspace
101
// Or generate new proposals if the current contents ends with a
102
// dot
103
currentContentProposals = generateContentProposals(contents);
104         } else {
105             // Filter existing proposals from a prevous search; thus, narrowing
106
// the search scope. This can occur when the user types additional
107
// characters in the field causing new characters to be appended to
108
// the initial field contents
109
currentContentProposals = filterContentProposals(contents);
110         }
111         
112         return convertResultsToSortedProposals(currentContentProposals);
113     }
114
115     /**
116      *
117      */

118     public void reset() {
119         fInitialContentProposals = null;
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.pde.internal.ui.editor.contentassist.TypePackageCompletionProcessor#addProposalToCollection(java.util.Collection, int, int, java.lang.String, java.lang.String, org.eclipse.swt.graphics.Image)
124      */

125     protected void addProposalToCollection(Collection JavaDoc collection, int startOffset,
126             int length, String JavaDoc label, String JavaDoc content, Image image) {
127         // Create content proposals for field assist
128
// start offset and length not required
129
IContentProposal proposal =
130             new TypeContentProposal(label, content, null, image);
131         // Add the proposal to the list of proposals
132
collection.add(proposal);
133     }
134
135     /**
136      * @param string
137      * @return
138      */

139     private boolean endsWithDot(String JavaDoc string) {
140         int index = string.lastIndexOf(F_DOT);
141         if ((index + 1) == string.length()) {
142             return true;
143         }
144         return false;
145     }
146     
147     /**
148      * @param currentContent
149      * @return
150      */

151     private ArrayList JavaDoc generateContentProposals(String JavaDoc currentContent) {
152         fInitialContentProposals = new ArrayList JavaDoc();
153         // Store the initial field contents to determine if we need to
154
// widen the scope later
155
fInitialContent = currentContent;
156         generateTypePackageProposals(currentContent, fProject,
157                 fInitialContentProposals, 0, fTypeScope, true);
158         return fInitialContentProposals;
159     }
160
161     /**
162      * @param list
163      * @return
164      */

165     private IContentProposal[] convertResultsToSortedProposals(ArrayList JavaDoc list) {
166         IContentProposal[] proposals = null;
167         if ((list != null) &&
168                 (list.size() != 0)) {
169             // Convert the results array list into an array of completion
170
// proposals
171
proposals = (IContentProposal[]) list.toArray(new IContentProposal[list.size()]);
172             // Sort the proposals alphabetically
173
Arrays.sort(proposals, fComparator);
174         } else {
175             proposals = new IContentProposal[0];
176         }
177         return proposals;
178     }
179
180     /**
181      * @param currentContent
182      * @return
183      */

184     private ArrayList JavaDoc filterContentProposals(String JavaDoc currentContent) {
185         String JavaDoc lowerCaseCurrentContent = currentContent.toLowerCase();
186         ListIterator JavaDoc iterator = fInitialContentProposals.listIterator();
187         // Maintain a list of filtered search results
188
ArrayList JavaDoc filteredContentProposals = new ArrayList JavaDoc();
189         // Iterate over the initial search results
190
while (iterator.hasNext()) {
191             Object JavaDoc object = iterator.next();
192             IContentProposal proposal = (IContentProposal)object;
193             String JavaDoc compareString = null;
194             if (lowerCaseCurrentContent.indexOf(F_DOT) == -1) {
195                 // Use only the type name
196
compareString = proposal.getLabel().toLowerCase();
197             } else {
198                 // Use the fully qualified type name
199
compareString = proposal.getContent().toLowerCase();
200             }
201             // Filter out any proposal not matching the current contents
202
// except for the edge case where the proposal is identical to the
203
// current contents
204
if (compareString.startsWith(lowerCaseCurrentContent, 0)) {
205                 filteredContentProposals.add(proposal);
206             }
207         }
208         return filteredContentProposals;
209     }
210     
211 }
212
Popular Tags