1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import java.util.Iterator;
15 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
16 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
17 import net.sourceforge.phpdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
18 import net.sourceforge.phpdt.ui.PreferenceConstants;
19 import net.sourceforge.phpdt.ui.text.IColorManager;
20 import net.sourceforge.phpdt.ui.text.JavaTextTools;
21 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
22 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.ui.WebUI;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferenceConverter;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.IRegion;
30 import org.eclipse.jface.text.ITextHover;
31 import org.eclipse.jface.text.ITextViewer;
32 import org.eclipse.jface.text.contentassist.ContentAssistant;
33 import org.eclipse.jface.text.contentassist.IContentAssistant;
34 import org.eclipse.jface.text.source.ISourceViewer;
35 import org.eclipse.jface.text.templates.TemplateContextType;
36 import org.eclipse.jface.text.templates.TemplateVariableResolver;
37 import org.eclipse.swt.graphics.Color;
38 import org.eclipse.swt.graphics.RGB;
39 import org.eclipse.ui.texteditor.ITextEditor;
41 public class TemplateEditorSourceViewerConfiguration extends
42 PHPSourceViewerConfiguration {
44 private static class TemplateVariableTextHover implements ITextHover {
46 private TemplateVariableProcessor fProcessor;
51 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
52 fProcessor = processor;
58 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
59 * org.eclipse.jface.text.IRegion)
61 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
63 IDocument doc = textViewer.getDocument();
64 int offset = subject.getOffset();
65 if (offset >= 2 && "${".equals(doc.get(offset - 2, 2))) { //$NON-NLS-1$
66 String varName = doc.get(offset, subject.getLength());
67 TemplateContextType contextType = fProcessor
69 if (contextType != null) {
70 Iterator iter = contextType.resolvers();
71 while (iter.hasNext()) {
72 TemplateVariableResolver var = (TemplateVariableResolver) iter
74 if (varName.equals(var.getType())) {
75 return var.getDescription();
80 } catch (BadLocationException e) {
88 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
91 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
92 if (textViewer != null) {
94 .findWord(textViewer.getDocument(), offset);
101 private final TemplateVariableProcessor fProcessor;
103 public TemplateEditorSourceViewerConfiguration(IColorManager colorManager,
104 IPreferenceStore store, ITextEditor editor,
105 TemplateVariableProcessor processor) {
106 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
107 fProcessor = processor;
111 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
113 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
115 IPreferenceStore store = WebUI.getDefault()
116 .getPreferenceStore();
117 JavaTextTools textTools = WebUI.getDefault()
119 IColorManager manager = textTools.getColorManager();
121 ContentAssistant assistant = new ContentAssistant();
122 assistant.setContentAssistProcessor(fProcessor,
123 IDocument.DEFAULT_CONTENT_TYPE);
124 // Register the same processor for strings and single line comments to
125 // get code completion at the start of those partitions.
126 assistant.setContentAssistProcessor(fProcessor,
127 IPHPPartitions.PHP_STRING_DQ);
128 assistant.setContentAssistProcessor(fProcessor,
129 IPHPPartitions.PHP_STRING_SQ);
130 assistant.setContentAssistProcessor(fProcessor,
131 IPHPPartitions.PHP_STRING_HEREDOC);
132 assistant.setContentAssistProcessor(fProcessor,
133 IPHPPartitions.PHP_SINGLELINE_COMMENT);
134 assistant.setContentAssistProcessor(fProcessor,
135 IPHPPartitions.PHP_MULTILINE_COMMENT);
136 assistant.setContentAssistProcessor(fProcessor,
137 IPHPPartitions.PHP_PHPDOC_COMMENT);
139 assistant.enableAutoInsert(store
140 .getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
141 assistant.enableAutoActivation(store
142 .getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
143 assistant.setAutoActivationDelay(store
144 .getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
146 .setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
148 .setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
150 .setInformationControlCreator(getInformationControlCreator(sourceViewer));
152 Color background = getColor(store,
153 PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
154 assistant.setContextInformationPopupBackground(background);
155 assistant.setContextSelectorBackground(background);
156 assistant.setProposalSelectorBackground(background);
158 Color foreground = getColor(store,
159 PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
160 assistant.setContextInformationPopupForeground(foreground);
161 assistant.setContextSelectorForeground(foreground);
162 assistant.setProposalSelectorForeground(foreground);
167 private Color getColor(IPreferenceStore store, String key,
168 IColorManager manager) {
169 RGB rgb = PreferenceConverter.getColor(store, key);
170 return manager.getColor(rgb);
174 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
177 public ITextHover getTextHover(ISourceViewer sourceViewer,
178 String contentType, int stateMask) {
179 return new TemplateVariableTextHover(fProcessor);