Skip to main content
summaryrefslogtreecommitdiffstats
blob: fb1223dee593e366026670c5dc5dac358fad78fe (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2003, 2004 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
 *******************************************************************************/
/*
 * Created on Sep 4, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.jst.common.internal.annotations.ui;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
import org.eclipse.jst.common.internal.annotations.registry.AttributeValueProposalHelper;
import org.eclipse.swt.graphics.Image;


/**
 * @author kelleyp
 * 
 * A completion proposal especially for Annotation tag completions. This problem this class was
 * created to solve was the problem of attaching help text to a proposal. The mechanism inside of
 * JavaCompletionProposal was useless to us, since it was tied to the idea that the proposal would
 * be for an actual java element, that has javadoc attached to it, etc... So here we subclass
 * JavaCompletionProposal and override <code>getAdditionalProposalInfo</code> for a more suitable
 * way of associating help text with a proposal.
 */
public class AnnotationTagProposal extends AbstractAnnotationTagProposal implements IJavaCompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 {
	private static final char QUOTE = '"';
	private boolean ensureBeginQuote = false;
	private boolean ensureEndQuote = false;
	private String localString;
	//String used to validate the prefix.
	private String fValidationString;

	/**
	 * Localized help text.
	 */
	private String locText;

	/**
	 * 
	 * @param replacementString
	 * @param replacementOffset
	 * @param replacementLength
	 * @param image
	 * @param displayString
	 * @param relevance
	 */
	public AnnotationTagProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, int relevance) {
		Assert.isNotNull(replacementString);
		Assert.isTrue(replacementOffset >= 0);
		Assert.isTrue(replacementLength >= 0);

		setReplacementString(replacementString);
		setReplacementOffset(replacementOffset);
		setReplacementLength(replacementLength);
		setImage(image);
		setDisplayString(displayString == null ? replacementString : displayString);
		setRelevance(relevance);
		setCursorPosition(replacementString.length());
		setSortString(displayString == null ? replacementString : displayString);
		this.localString = replacementString;
	}

	/**
	 * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#JavaCompletionProposal(java.lang.String, int, int,
	 *      org.eclipse.swt.graphics.Image, java.lang.String, int,
	 *      org.eclipse.jface.text.ITextViewer)
	 * @param replacementString
	 * @param replacementOffset
	 * @param replacementLength
	 * @param image
	 * @param displayString
	 * @param relevance
	 */

	public AnnotationTagProposal(AttributeValueProposalHelper proposalHelper) {
		this(proposalHelper.getReplacementString(), proposalHelper.getValueOffset(), proposalHelper.getReplacementLength(), null, proposalHelper.getValueDisplayString(), 90);
		if (proposalHelper instanceof UIAttributeValueProposalHelper)
			setImage(((UIAttributeValueProposalHelper) proposalHelper).getImage());
		setEnsureBeginQuote(proposalHelper.ensureBeginQuote());
		setEnsureEndQuote(proposalHelper.ensureEndQuote());
	}

	public AnnotationTagProposal(UIAttributeValueProposalHelper proposalHelper) {
		this(proposalHelper.getReplacementString(), proposalHelper.getValueOffset(), proposalHelper.getReplacementLength(), proposalHelper.getImage(), proposalHelper.getValueDisplayString(), 90);
		setEnsureBeginQuote(proposalHelper.ensureBeginQuote());
		setEnsureEndQuote(proposalHelper.ensureEndQuote());
	}

	/**
	 * Our override that uses <code>textHolder</code> to provide the help text.
	 */
	@Override
	public String getAdditionalProposalInfo() {
		return locText;
	}

	/**
	 * Sets the holder of the help text that can be displayed with this proposal.
	 * 
	 * @param hld
	 *            an LocalizedTextContainer
	 */
	public void setHelpText(String s) {
		locText = s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal#apply(org.eclipse.jface.text.IDocument,
	 *      char, int)
	 */
	@Override
	public void apply(IDocument document, char trigger, int offset) {
		ensureQuotedIfNecessary(document, offset);
		super.apply(document, trigger, offset);
	}

	/**
	 * @param document
	 * @param offset
	 */
	private void ensureQuotedIfNecessary(IDocument document, int offset) {
		if (ensureBeginQuote || ensureEndQuote) {
			try {
				char begin = document.getChar(getReplacementOffset() - 1);
				char end = document.getChar(offset);
				if (ensureBeginQuote && ensureEndQuote && begin != QUOTE && end != QUOTE) {
					StringBuffer b = new StringBuffer();
					b.append(QUOTE).append(localString).append(QUOTE);
					localString = b.toString();
				} else if (ensureBeginQuote && begin != QUOTE)
					localString = QUOTE + localString;
				else if (ensureEndQuote && end != QUOTE)
					localString = localString + QUOTE;
				setReplacementString(localString);
				setCursorPosition(localString.length());
			} catch (BadLocationException e) {
				// Do nothing
			}
		}
	}

	public void setEnsureQuoted(boolean ensureQuoted) {
		setEnsureBeginQuote(ensureQuoted);
		setEnsureEndQuote(ensureQuoted);
	}

	//public void setPartialValueString(String partialValueString) {
	//	this.partialValueString = partialValueString;
	//}
	public void setEnsureBeginQuote(boolean ensureBeginQuote) {
		this.ensureBeginQuote = ensureBeginQuote;
	}

	public void setEnsureEndQuote(boolean ensureEndQuote) {
		this.ensureEndQuote = ensureEndQuote;
	}
	
	@Override
	protected boolean isValidPrefix(String prefix) {
		if (getReplacementString().startsWith(getDisplayString())) {
			return super.isValidPrefix(prefix);
		}
		return super.isPrefix(trim(prefix), getValidationString());
	}
	
	private String getValidationString() {
		if (fValidationString == null) {
			fValidationString = trim(getReplacementString());
		}
		return fValidationString;
	}

	
	/*
	 * It is possible that the replacement string is complex and larger than the prefix.
	 * In this case we want to trim the prefix to the last whitespace character.  This
	 * will provide us with a prefix to use when matching the display string.
	 */
	private String trim(String aString) {
		if (aString == null || aString.length() == 0)
			return aString;
		StringBuffer buffer = new StringBuffer();
		char[] chars = aString.toCharArray();
		for (int i = chars.length - 1; i > -1; i--) {
			if (Character.isWhitespace(chars[i])) {
				break;
			}
			buffer.append(chars[i]);
		}
		buffer = buffer.reverse();
		return buffer.toString();
	}
}

Back to the top