KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > search > ReturningAttributesContentAssistProcessor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets.search;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.eclipse.jface.contentassist.IContentAssistSubjectControl;
32 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.ITextViewer;
35 import org.eclipse.jface.text.contentassist.CompletionProposal;
36 import org.eclipse.jface.text.contentassist.ICompletionProposal;
37 import org.eclipse.jface.text.contentassist.IContextInformation;
38 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
39
40
41 /**
42  * The ReturningAttributesContentAssistProcessor provides proposals for the
43  * {@link ReturningAttributesWidget}. It splits the comma separted text input
44  * into separate regions.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class ReturningAttributesContentAssistProcessor implements ISubjectControlContentAssistProcessor
50 {
51
52     /** The auto activation characters */
53     private char[] autoActivationCharacters;
54
55     /** The possible attribute types */
56     private String JavaDoc[] possibleAttributeTypes;
57
58
59     /**
60      * Creates a new instance of ReturningAttributesContentAssistProcessor.
61      *
62      * @param possibleAttributeNames
63      */

64     public ReturningAttributesContentAssistProcessor( String JavaDoc[] possibleAttributeNames )
65     {
66         super();
67         setPossibleAttributeTypes( possibleAttributeTypes );
68     }
69
70
71     /**
72      * {@inheritDoc}
73      */

74     public char[] getCompletionProposalAutoActivationCharacters()
75     {
76         return autoActivationCharacters;
77     }
78
79
80     /**
81      * Sets the possible attribute types.
82      *
83      * @param possibleAttributeTypes the possible strings
84      */

85     public void setPossibleAttributeTypes( String JavaDoc[] possibleAttributeTypes )
86     {
87         if ( possibleAttributeTypes == null )
88         {
89             possibleAttributeTypes = new String JavaDoc[0];
90         }
91
92         // set possible strings
93
Arrays.sort( possibleAttributeTypes );
94         this.possibleAttributeTypes = possibleAttributeTypes;
95
96         // set auto activation characters
97
Set JavaDoc<Character JavaDoc> characterSet = new HashSet JavaDoc<Character JavaDoc>();
98         for ( int i = 0; i < possibleAttributeTypes.length; i++ )
99         {
100             String JavaDoc string = possibleAttributeTypes[i];
101             for ( int k = 0; k < string.length(); k++ )
102             {
103                 char ch = string.charAt( k );
104                 characterSet.add( Character.toLowerCase( ch ) );
105                 characterSet.add( Character.toUpperCase( ch ) );
106             }
107         }
108         autoActivationCharacters = new char[characterSet.size()];
109         int i = 0;
110         for ( Iterator JavaDoc<Character JavaDoc> it = characterSet.iterator(); it.hasNext(); )
111         {
112             Character JavaDoc ch = it.next();
113             autoActivationCharacters[i] = ch.charValue();
114             i++;
115         }
116     }
117
118
119     /**
120      * {@inheritDoc}
121      *
122      * This implementation always returns null.
123      */

124     public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int offset )
125     {
126         return null;
127     }
128
129
130     /**
131      * {@inheritDoc}
132      */

133     public ICompletionProposal[] computeCompletionProposals( IContentAssistSubjectControl contentAssistSubjectControl,
134         int documentOffset )
135     {
136         IDocument document = contentAssistSubjectControl.getDocument();
137         String JavaDoc text = document.get();
138        
139         // search start of current attribute type
140
int start = 0;
141         for ( int i = documentOffset - 1; i >= 0; i-- )
142         {
143             char c = text.charAt( i );
144             if ( c == ',' || Character.isWhitespace( c ) )
145             {
146                 start = i + 1;
147                 break;
148             }
149         }
150         String JavaDoc attribute = text.substring( start, documentOffset );
151
152         // create proposal list
153
List JavaDoc<ICompletionProposal> proposalList = new ArrayList JavaDoc<ICompletionProposal>();
154         for ( int k = 0; k < possibleAttributeTypes.length; k++ )
155         {
156             if ( possibleAttributeTypes[k].toUpperCase().startsWith( attribute.toUpperCase() ) )
157             {
158                 ICompletionProposal proposal = new CompletionProposal( possibleAttributeTypes[k] + ", ", start,
159                     documentOffset - start, possibleAttributeTypes[k].length() + 2, null, possibleAttributeTypes[k],
160                     null, null );
161                 proposalList.add( proposal );
162             }
163         }
164         return proposalList.toArray( new ICompletionProposal[proposalList.size()] );
165     }
166
167
168     /**
169      * {@inheritDoc}
170      *
171      * This implementation always returns null.
172      */

173     public char[] getContextInformationAutoActivationCharacters()
174     {
175         return null;
176     }
177
178
179     /**
180      * {@inheritDoc}
181      *
182      * This implementation always returns null.
183      */

184     public IContextInformation[] computeContextInformation( ITextViewer viewer, int offset )
185     {
186         return null;
187     }
188
189
190     /**
191      * {@inheritDoc}
192      *
193      * This implementation always returns null.
194      */

195     public IContextInformation[] computeContextInformation( IContentAssistSubjectControl contentAssistSubjectControl,
196         int documentOffset )
197     {
198         return null;
199     }
200
201
202     /**
203      * {@inheritDoc}
204      *
205      * This implementation always returns null.
206      */

207     public String JavaDoc getErrorMessage()
208     {
209         return null;
210     }
211
212
213     /**
214      * {@inheritDoc}
215      *
216      * This implementation always returns null.
217      */

218     public IContextInformationValidator getContextInformationValidator()
219     {
220         return null;
221     }
222
223 }
224
Popular Tags