View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.eclipse.jface.text.IDocument;
28  import org.eclipse.jface.text.IRegion;
29  import org.eclipse.jface.text.ITextViewer;
30  import org.eclipse.jface.text.contentassist.CompletionProposal;
31  import org.eclipse.jface.text.contentassist.ICompletionProposal;
32  import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
33  import org.eclipse.jface.text.contentassist.IContextInformation;
34  import org.eclipse.jface.text.contentassist.IContextInformationValidator;
35  
36  /**
37  * ...
38  * @author zhanghao
39  *
40  */
41  public class LLRPContentAssistant implements IContentAssistProcessor {	
42  	
43  	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
44  			int documentOffset) {
45  		ICompletionProposal[] proposals = null;
46  		try {
47  			IDocument document = viewer.getDocument();
48  			IRegion range = document.getLineInformationOfOffset(documentOffset);
49  			int start = range.getOffset();
50  			String prefix = document.get(start, documentOffset - start);
51  			
52  			ConfigurationModel model = new ConfigurationModel(document.get());
53  			List completions = model.getCompletions(prefix);
54  
55  			proposals = new CompletionProposal[completions.size()];
56  			int i = 0;
57  			for (Iterator iter = completions.iterator(); iter.hasNext();) {
58  				String completion = (String) iter.next();
59  				proposals[i++] = new CompletionProposal(completion, start,
60  						documentOffset - start, completion.length());
61  			}
62  		} catch (Exception e) {
63  			e.printStackTrace();
64  		}
65  
66  		return proposals;
67  	}
68  
69  	public IContextInformation[] computeContextInformation(ITextViewer viewer,
70  			int documentOffset) {
71  		return null;
72  	}
73  
74  	public char[] getCompletionProposalAutoActivationCharacters() {
75  		return new char[] { ':' };
76  	}
77  
78  	public char[] getContextInformationAutoActivationCharacters() {
79  		return null;
80  	}
81  
82  	public String getErrorMessage() {
83  		return "No completions available.";
84  	}
85  
86  	public IContextInformationValidator getContextInformationValidator() {
87  		return null;
88  	}
89  }