1 /*******************************************************************************
2  * Copyright (c) 2016, 2017 Red Hat Inc. and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * - Mickael Istria, Sopot Cela (Red Hat Inc.)
13  *******************************************************************************/
14 package org.eclipse.ui.genericeditor.examples.dotproject;
15 
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectNatureDescriptor;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.CompletionProposal;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
26 
27 public class NaturesAndProjectsContentAssistProcessor implements IContentAssistProcessor {
28 
NaturesAndProjectsContentAssistProcessor()29 	public NaturesAndProjectsContentAssistProcessor() {
30 		// TODO Auto-generated constructor stub
31 	}
32 
33 	@Override
computeCompletionProposals(ITextViewer viewer, int offset)34 	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
35 		String text = viewer.getDocument().get();
36 		String natureTag= "<nature>";
37 		String projectReferenceTag="<project>";
38 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
39 		int natureTagLength = natureTag.length();
40 		if (text.length() >= natureTagLength && offset >= natureTagLength && text.substring(offset - natureTagLength, offset).equals(natureTag)) {
41 			IProjectNatureDescriptor[] natureDescriptors= workspace.getNatureDescriptors();
42 			ICompletionProposal[] proposals = new ICompletionProposal[natureDescriptors.length];
43 			for (int i= 0; i < natureDescriptors.length; i++) {
44 				IProjectNatureDescriptor descriptor= natureDescriptors[i];
45 				proposals[i] = new CompletionProposal(descriptor.getNatureId(), offset, 0, descriptor.getNatureId().length());
46 			}
47 			return proposals;
48 		}
49 		int projectReferenceTagLength = projectReferenceTag.length();
50 		if (text.length() >= projectReferenceTagLength && offset >= projectReferenceTagLength && text.substring(offset - projectReferenceTagLength, offset).equals(projectReferenceTag)) {
51 			IProject[] projects= workspace.getRoot().getProjects();
52 			//TODO - filter out the project this file is in
53 			ICompletionProposal[] proposals = new ICompletionProposal[projects.length];
54 			for (int i= 0; i < projects.length; i++) {
55 				proposals[i]=new CompletionProposal(projects[i].getName(), offset, 0, projects[i].getName().length());
56 			}
57 			return proposals;
58 		}
59 		return new ICompletionProposal[0];
60 	}
61 
62 	@Override
computeContextInformation(ITextViewer viewer, int offset)63 	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
64 		return null;
65 	}
66 
67 	@Override
getCompletionProposalAutoActivationCharacters()68 	public char[] getCompletionProposalAutoActivationCharacters() {
69 		return null;
70 	}
71 
72 	@Override
getContextInformationAutoActivationCharacters()73 	public char[] getContextInformationAutoActivationCharacters() {
74 		return null;
75 	}
76 
77 	@Override
getErrorMessage()78 	public String getErrorMessage() {
79 		return null;
80 	}
81 
82 	@Override
getContextInformationValidator()83 	public IContextInformationValidator getContextInformationValidator() {
84 		return null;
85 	}
86 
87 }