Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: c713a224d0355c02d7633a7f8ff2cd570418822f (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
 * Copyright (c) 2001, 2010 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.ui.internal.contentassist;



import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.ui.internal.IReleasable;
import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
import org.eclipse.wst.xml.core.text.IXMLPartitions;


/**
 * ContentAssistProcessor to handle special cases in content assist where the
 * partitioner cannot determine a partition type at the current cursor
 * position (usually at EOF).
 * 
 * @author pavery
 * @deprecated This class is no longer used locally and will be removed in the future
 */
public class NoRegionContentAssistProcessor implements IContentAssistProcessor, IReleasable {

	private static final boolean DEBUG = false;
	protected char completionProposalAutoActivationCharacters[] = null;
	protected char contextInformationAutoActivationCharacters[] = null;

	private final ICompletionProposal[] EMPTY_PROPOSAL_SET = new ICompletionProposal[0];
	protected String fErrorMessage = null;
	private HashMap fNameToProcessorMap = null;
	private HashMap fPartitionToProcessorMap = null;

	public NoRegionContentAssistProcessor() {
		super();
	}

	protected void addPartitionProcessor(String key, IContentAssistProcessor processor) {
		addProcessor(getPartitionToProcessorMap(), key, processor);
	}

	protected void addNameProcessor(String key, IContentAssistProcessor processor) {
		addProcessor(getNameToProcessorMap(), key, processor);
	}

	protected IContentAssistProcessor getPartitionProcessor(String key) {
		return (IContentAssistProcessor) getPartitionToProcessorMap().get(key);
	}

	/**
	 * Ensures release if it's a duplicate partition type.
	 * 
	 * @param map
	 * @param key
	 * @param processor
	 */
	private void addProcessor(HashMap map, String key, IContentAssistProcessor processor) {
		Object o = map.remove(key);
		if (o != null) {
			if (o instanceof IReleasable) {
				((IReleasable) o).release();
			}
		}
		map.put(key, processor);
	}

	/**
	 * Figures out what the correct ICompletionProposalProcessor is and
	 * computesCompletionProposals on that.
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
	 *      int)
	 */
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
		IContentAssistProcessor p = null;
		ICompletionProposal[] results = EMPTY_PROPOSAL_SET;

		p = guessContentAssistProcessor(viewer, documentOffset);
		if (p != null) {
			results = p.computeCompletionProposals(viewer, documentOffset);
		}

		return (results != null) ? results : EMPTY_PROPOSAL_SET;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer,
	 *      int)
	 */
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
		// get context info from processor that we end up using...
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
	 */
	public char[] getCompletionProposalAutoActivationCharacters() {
		return completionProposalAutoActivationCharacters;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
	 */
	public char[] getContextInformationAutoActivationCharacters() {
		return contextInformationAutoActivationCharacters;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
	 */
	public IContextInformationValidator getContextInformationValidator() {
		// return the validator for the content assist processor that we
		// used...
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
	 */
	public String getErrorMessage() {
		return fErrorMessage;
	}
	
	private HashMap getNameToProcessorMap() {
		if (fNameToProcessorMap == null) {
			fNameToProcessorMap = new HashMap();
			initNameToProcessorMap();
		}
		return fNameToProcessorMap;
	}
	
	private HashMap getPartitionToProcessorMap() {
		if (fPartitionToProcessorMap == null) {
			fPartitionToProcessorMap = new HashMap();
			initPartitionToProcessorMap();
		}
		return fPartitionToProcessorMap;
	}

	/**
	 * Gives you the document partition type (String) for the given
	 * StructuredTextViewer and documentPosition.
	 * 
	 * @param viewer
	 * @param documentPosition
	 * @return String
	 */
	protected String getPartitionType(StructuredTextViewer viewer, int documentPosition) {
		IDocument document = viewer.getDocument();
		String partitionType = null;
		ITypedRegion partition = null;
		try {
			partition = document.getPartition(documentPosition);
			partitionType = partition.getType();
		}
		catch (BadLocationException e) {
			partitionType = null;
		}
		return partitionType;
	}

	/**
	 * Guesses a ContentAssistProcessor based on the TextViewer and
	 * documentOffset.
	 * 
	 * @param viewer
	 * @param documentOffset
	 */
	protected IContentAssistProcessor guessContentAssistProcessor(ITextViewer viewer, int documentOffset) {
		// mapping logic here...
		// look @ previous region
		// look @ previous doc partition type
		// look @ page language
		IContentAssistProcessor p = null;
		IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentOffset);
		if (sdRegion != null) {
			String currentRegionType = sdRegion.getType();
			// System.out.println("current region type is >> " +
			// currentRegionType);
			if (currentRegionType == DOMRegionContext.UNDEFINED) {
				IStructuredDocumentRegion sdPrev = sdRegion.getPrevious();
				if (sdPrev != null) {
					if (DEBUG) {
						String prevRegionType = sdPrev.getType();
						System.out.println("previous region type is >> " + prevRegionType); //$NON-NLS-1$
					}
				}
			}
		}
		// working w/ viewer & document partition
		if ((p == null) && (viewer.getDocument().getLength() > 0)) {
			String prevPartitionType = getPartitionType((StructuredTextViewer) viewer, documentOffset - 1);
			// System.out.println("previous partition type is > " +
			// prevPartitionType);
			p = (IContentAssistProcessor) getPartitionToProcessorMap().get(prevPartitionType);
		}
		return p;
	}

	/**
	 * Inits map for extra ContentAssistProcessors (useBean, get/setProperty)
	 */
	protected void initNameToProcessorMap() {
		// nothing to do in this case
	}

	/**
	 * Adds all relevent ContentAssistProcessors to the partition to processor
	 * map (just XML here)
	 */
	protected void initPartitionToProcessorMap() {
		XMLContentAssistProcessor xmlProcessor = new XMLContentAssistProcessor();
		addProcessor(getPartitionToProcessorMap(), IXMLPartitions.XML_DEFAULT, xmlProcessor);
	}

	public void release() {
		releasePartitionToProcessorMap();
		releaseNameToProcessorMap();
	}

	protected void releaseMap(HashMap map) {
		if ((map != null) && !map.isEmpty()) {
			Iterator it = map.keySet().iterator();
			Object key = null;
			while (it.hasNext()) {
				key = it.next();
				if (map.get(key) instanceof IReleasable) {
					((IReleasable) map.get(key)).release();
				}
			}
			map.clear();
			map = null;
		}
	}

	protected void releaseNameToProcessorMap() {
		releaseMap(fNameToProcessorMap);
	}

	protected void releasePartitionToProcessorMap() {
		releaseMap(fPartitionToProcessorMap);
	}
}

Back to the top