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;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.preference.PreferenceConverter;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IRegion;
29 import org.eclipse.jface.text.ITextHover;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.contentassist.ContentAssistant;
32 import org.eclipse.jface.text.contentassist.IContentAssistant;
33 import org.eclipse.jface.text.source.ISourceViewer;
34 import org.eclipse.jface.text.templates.TemplateContextType;
35 import org.eclipse.jface.text.templates.TemplateVariableResolver;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.ui.texteditor.ITextEditor;
41 public class TemplateEditorSourceViewerConfiguration extends PHPSourceViewerConfiguration {
43 private static class TemplateVariableTextHover implements ITextHover {
45 private TemplateVariableProcessor fProcessor;
50 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
51 fProcessor= processor;
55 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
57 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
59 IDocument doc= textViewer.getDocument();
60 int offset= subject.getOffset();
61 if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
62 String varName= doc.get(offset, subject.getLength());
63 TemplateContextType contextType= fProcessor.getContextType();
64 if (contextType != null) {
65 Iterator iter= contextType.resolvers();
66 while (iter.hasNext()) {
67 TemplateVariableResolver var= (TemplateVariableResolver) iter.next();
68 if (varName.equals(var.getType())) {
69 return var.getDescription();
74 } catch (BadLocationException e) {
80 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
82 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
83 if (textViewer != null) {
84 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
91 private final TemplateVariableProcessor fProcessor;
93 public TemplateEditorSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, ITextEditor editor, TemplateVariableProcessor processor) {
94 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
95 fProcessor= processor;
99 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
101 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
103 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
104 JavaTextTools textTools= PHPeclipsePlugin.getDefault().getJavaTextTools();
105 IColorManager manager= textTools.getColorManager();
108 ContentAssistant assistant= new ContentAssistant();
109 assistant.setContentAssistProcessor(fProcessor, IDocument.DEFAULT_CONTENT_TYPE);
110 // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
111 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_STRING_DQ);
112 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_STRING_SQ);
113 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_SINGLELINE_COMMENT);
114 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_MULTILINE_COMMENT);
115 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_PHPDOC_COMMENT);
117 assistant.enableAutoInsert(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
118 assistant.enableAutoActivation(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
119 assistant.setAutoActivationDelay(store.getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
120 assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
121 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
122 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
124 Color background= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
125 assistant.setContextInformationPopupBackground(background);
126 assistant.setContextSelectorBackground(background);
127 assistant.setProposalSelectorBackground(background);
129 Color foreground= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
130 assistant.setContextInformationPopupForeground(foreground);
131 assistant.setContextSelectorForeground(foreground);
132 assistant.setProposalSelectorForeground(foreground);
137 private Color getColor(IPreferenceStore store, String key, IColorManager manager) {
138 RGB rgb= PreferenceConverter.getColor(store, key);
139 return manager.getColor(rgb);
143 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
146 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
147 return new TemplateVariableTextHover(fProcessor);