Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0c5a7f18ec44947d37702ae43fc64d98a8c737d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Peter Friese <peter.friese@itemis.de> - bug 215314
 *******************************************************************************/
package org.eclipse.pde.internal.ui.editor.contentassist;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.ProposalInfo;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

public class TypeCompletionProposal extends AbstractJavaCompletionProposal implements ICompletionProposalExtension4 {

	private static final class PDETypeProposalInfo extends ProposalInfo {
		private IJavaProject fJavaProject;
		private String fTypeName;

		/**
		 * Creates a new proposal info.
		 *
		 * @param project the java project to reference when resolving types
		 * @param typeName the fully qualified type name
		 */
		public PDETypeProposalInfo(IJavaProject project, String typeName) {
			fJavaProject = project;
			fTypeName = typeName;
		}

		/*
		 * @see org.eclipse.jdt.internal.ui.text.java.ProposalInfo#getJavaElement()
		 */
		@Override
		public IJavaElement getJavaElement() throws JavaModelException {
			return fJavaProject.findType(fTypeName);
		}
	}

	protected String fAdditionalInfo;
	private boolean fProposalInfoComputed;
	private IProject fProject;
	private String fTypeName;

	public TypeCompletionProposal(String replacementString, Image image, String displayString, int startOffset, int length, IProject project, String typeName) {
		this(replacementString, image, displayString, startOffset, length);
		fTypeName = typeName;
		fProject = project;
	}

	public TypeCompletionProposal(String replacementString, Image image, String displayString, int startOffset, int length) {
		Assert.isNotNull(replacementString);
		setReplacementString(replacementString);
		setReplacementOffset(startOffset);
		setReplacementLength(length);
		setDisplayString(displayString);
		setImage(image);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
	 */
	@Override
	public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
		IDocument document = viewer.getDocument();
		if (getReplacementLength() == -1) {
			setReplacementLength(document.getLength());
		}
		try {
			document.replace(getReplacementOffset(), getReplacementLength(), getReplacementString());
		} catch (BadLocationException e) {
			// DEBUG
			// e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
	 */
	@Override
	public Point getSelection(IDocument document) {
		if (getReplacementString().equals("\"\"")) //$NON-NLS-1$
			return new Point(getReplacementOffset() + 1, 0);
		return new Point(getReplacementOffset() + getReplacementString().length(), 0);
	}

	/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension5#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
		if (fAdditionalInfo != null)
			return fAdditionalInfo;
		return super.getAdditionalProposalInfo(monitor);
	}

	public void setAdditionalProposalInfo(String info) {
		fAdditionalInfo = info;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
	 */
	public boolean isAutoInsertable() {
		return true;
	}

	/**
	 * Returns the additional proposal info, or <code>null</code> if none
	 * exists.
	 *
	 * @return the additional proposal info, or <code>null</code> if none
	 *         exists
	 */
	@Override
	protected final ProposalInfo getProposalInfo() {
		if (!fProposalInfoComputed) {
			setProposalInfo(computeProposalInfo());
			fProposalInfoComputed = true;
		}
		return super.getProposalInfo();
	}

	/*
	 * @see org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeProposalInfo()
	 */
	protected ProposalInfo computeProposalInfo() {
		if (fProject != null && fTypeName != null)
			return new PDETypeProposalInfo(JavaCore.create(fProject), fTypeName);
		return null;
	}

}

Back to the top