001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.graphics.Color;
016: import org.eclipse.swt.graphics.RGB;
017: import org.eclipse.swt.widgets.Shell;
018:
019: import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
020: import org.eclipse.jface.preference.IPreferenceStore;
021: import org.eclipse.jface.preference.PreferenceConverter;
022:
023: import org.eclipse.jface.text.BadLocationException;
024: import org.eclipse.jface.text.DefaultInformationControl;
025: import org.eclipse.jface.text.IDocument;
026: import org.eclipse.jface.text.IInformationControl;
027: import org.eclipse.jface.text.IInformationControlCreator;
028: import org.eclipse.jface.text.IRegion;
029: import org.eclipse.jface.text.ITextHover;
030: import org.eclipse.jface.text.ITextViewer;
031: import org.eclipse.jface.text.contentassist.ContentAssistant;
032: import org.eclipse.jface.text.contentassist.IContentAssistant;
033: import org.eclipse.jface.text.source.ISourceViewer;
034: import org.eclipse.jface.text.templates.TemplateContextType;
035: import org.eclipse.jface.text.templates.TemplateVariableResolver;
036:
037: import org.eclipse.ui.texteditor.ITextEditor;
038:
039: import org.eclipse.jdt.ui.PreferenceConstants;
040: import org.eclipse.jdt.ui.text.IColorManager;
041: import org.eclipse.jdt.ui.text.IJavaPartitions;
042: import org.eclipse.jdt.ui.text.JavaTextTools;
043:
044: import org.eclipse.jdt.internal.ui.JavaPlugin;
045: import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
046: import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
047: import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
048:
049: public class CodeTemplateSourceViewerConfiguration extends
050: SimpleJavaSourceViewerConfiguration {
051:
052: private static class TemplateVariableTextHover implements
053: ITextHover {
054:
055: private TemplateVariableProcessor fProcessor;
056:
057: /**
058: * @param processor the template variable processor
059: */
060: public TemplateVariableTextHover(
061: TemplateVariableProcessor processor) {
062: fProcessor = processor;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
067: */
068: public String getHoverInfo(ITextViewer textViewer,
069: IRegion subject) {
070: try {
071: IDocument doc = textViewer.getDocument();
072: int offset = subject.getOffset();
073: if (offset >= 2 && "${".equals(doc.get(offset - 2, 2))) { //$NON-NLS-1$
074: String varName = doc.get(offset, subject
075: .getLength());
076: TemplateContextType contextType = fProcessor
077: .getContextType();
078: if (contextType != null) {
079: Iterator iter = contextType.resolvers();
080: while (iter.hasNext()) {
081: TemplateVariableResolver var = (TemplateVariableResolver) iter
082: .next();
083: if (varName.equals(var.getType())) {
084: return var.getDescription();
085: }
086: }
087: }
088: }
089: } catch (BadLocationException e) {
090: }
091: return null;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
096: */
097: public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
098: if (textViewer != null) {
099: return JavaWordFinder.findWord(
100: textViewer.getDocument(), offset);
101: }
102: return null;
103: }
104:
105: }
106:
107: private final TemplateVariableProcessor fProcessor;
108:
109: public CodeTemplateSourceViewerConfiguration(
110: IColorManager colorManager, IPreferenceStore store,
111: ITextEditor editor, TemplateVariableProcessor processor) {
112: super (colorManager, store, editor,
113: IJavaPartitions.JAVA_PARTITIONING, false);
114: fProcessor = processor;
115: }
116:
117: /*
118: * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
119: */
120: public IContentAssistant getContentAssistant(
121: ISourceViewer sourceViewer) {
122:
123: IPreferenceStore store = JavaPlugin.getDefault()
124: .getPreferenceStore();
125: JavaTextTools textTools = JavaPlugin.getDefault()
126: .getJavaTextTools();
127: IColorManager manager = textTools.getColorManager();
128:
129: ContentAssistant assistant = new ContentAssistant();
130: assistant.setContentAssistProcessor(fProcessor,
131: IDocument.DEFAULT_CONTENT_TYPE);
132: // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
133: assistant.setContentAssistProcessor(fProcessor,
134: IJavaPartitions.JAVA_STRING);
135: assistant.setContentAssistProcessor(fProcessor,
136: IJavaPartitions.JAVA_CHARACTER);
137: assistant.setContentAssistProcessor(fProcessor,
138: IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
139: assistant.setContentAssistProcessor(fProcessor,
140: IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
141: assistant.setContentAssistProcessor(fProcessor,
142: IJavaPartitions.JAVA_DOC);
143:
144: assistant.enableAutoInsert(store
145: .getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
146: assistant
147: .enableAutoActivation(store
148: .getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
149: assistant
150: .setAutoActivationDelay(store
151: .getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
152: assistant
153: .setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
154: assistant
155: .setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
156: assistant
157: .setInformationControlCreator(new IInformationControlCreator() {
158: public IInformationControl createInformationControl(
159: Shell parent) {
160: return new DefaultInformationControl(parent,
161: SWT.NONE, new HTMLTextPresenter(true));
162: }
163: });
164:
165: Color background = getColor(store,
166: PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND,
167: manager);
168: assistant.setContextInformationPopupBackground(background);
169: assistant.setContextSelectorBackground(background);
170: assistant.setProposalSelectorBackground(background);
171:
172: Color foreground = getColor(store,
173: PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND,
174: manager);
175: assistant.setContextInformationPopupForeground(foreground);
176: assistant.setContextSelectorForeground(foreground);
177: assistant.setProposalSelectorForeground(foreground);
178:
179: return assistant;
180: }
181:
182: private Color getColor(IPreferenceStore store, String key,
183: IColorManager manager) {
184: RGB rgb = PreferenceConverter.getColor(store, key);
185: return manager.getColor(rgb);
186: }
187:
188: /*
189: * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
190: * @since 2.1
191: */
192: public ITextHover getTextHover(ISourceViewer sourceViewer,
193: String contentType, int stateMask) {
194: return new TemplateVariableTextHover(fProcessor);
195: }
196:
197: }
|