f16ecab1672fbbc2d0d6bc7f7a48ed1082faf7dd
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / TemplateEditorSourceViewerConfiguration.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
13 import java.util.Iterator;
14
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
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;
39
40 public class TemplateEditorSourceViewerConfiguration extends
41                 PHPSourceViewerConfiguration {
42
43         private static class TemplateVariableTextHover implements ITextHover {
44
45                 private TemplateVariableProcessor fProcessor;
46
47                 /**
48                  * @param type
49                  */
50                 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
51                         fProcessor = processor;
52                 }
53
54                 /*
55                  * (non-Javadoc)
56                  * 
57                  * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
58                  *      org.eclipse.jface.text.IRegion)
59                  */
60                 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
61                         try {
62                                 IDocument doc = textViewer.getDocument();
63                                 int offset = subject.getOffset();
64                                 if (offset >= 2 && "${".equals(doc.get(offset - 2, 2))) { //$NON-NLS-1$
65                                         String varName = doc.get(offset, subject.getLength());
66                                         TemplateContextType contextType = fProcessor
67                                                         .getContextType();
68                                         if (contextType != null) {
69                                                 Iterator iter = contextType.resolvers();
70                                                 while (iter.hasNext()) {
71                                                         TemplateVariableResolver var = (TemplateVariableResolver) iter
72                                                                         .next();
73                                                         if (varName.equals(var.getType())) {
74                                                                 return var.getDescription();
75                                                         }
76                                                 }
77                                         }
78                                 }
79                         } catch (BadLocationException e) {
80                         }
81                         return null;
82                 }
83
84                 /*
85                  * (non-Javadoc)
86                  * 
87                  * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
88                  *      int)
89                  */
90                 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
91                         if (textViewer != null) {
92                                 return JavaWordFinder
93                                                 .findWord(textViewer.getDocument(), offset);
94                         }
95                         return null;
96                 }
97
98         }
99
100         private final TemplateVariableProcessor fProcessor;
101
102         public TemplateEditorSourceViewerConfiguration(IColorManager colorManager,
103                         IPreferenceStore store, ITextEditor editor,
104                         TemplateVariableProcessor processor) {
105                 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
106                 fProcessor = processor;
107         }
108
109         /*
110          * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
111          */
112         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
113
114                 IPreferenceStore store = PHPeclipsePlugin.getDefault()
115                                 .getPreferenceStore();
116                 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
117                                 .getJavaTextTools();
118                 IColorManager manager = textTools.getColorManager();
119
120                 ContentAssistant assistant = new ContentAssistant();
121                 assistant.setContentAssistProcessor(fProcessor,
122                                 IDocument.DEFAULT_CONTENT_TYPE);
123                 // Register the same processor for strings and single line comments to
124                 // get code completion at the start of those partitions.
125                 assistant.setContentAssistProcessor(fProcessor,
126                                 IPHPPartitions.PHP_STRING_DQ);
127                 assistant.setContentAssistProcessor(fProcessor,
128                                 IPHPPartitions.PHP_STRING_SQ);
129                 assistant.setContentAssistProcessor(fProcessor,
130                                 IPHPPartitions.PHP_STRING_HEREDOC);
131                 assistant.setContentAssistProcessor(fProcessor,
132                                 IPHPPartitions.PHP_SINGLELINE_COMMENT);
133                 assistant.setContentAssistProcessor(fProcessor,
134                                 IPHPPartitions.PHP_MULTILINE_COMMENT);
135                 assistant.setContentAssistProcessor(fProcessor,
136                                 IPHPPartitions.PHP_PHPDOC_COMMENT);
137
138                 assistant.enableAutoInsert(store
139                                 .getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
140                 assistant.enableAutoActivation(store
141                                 .getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
142                 assistant.setAutoActivationDelay(store
143                                 .getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
144                 assistant
145                                 .setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
146                 assistant
147                                 .setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
148                 assistant
149                                 .setInformationControlCreator(getInformationControlCreator(sourceViewer));
150
151                 Color background = getColor(store,
152                                 PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
153                 assistant.setContextInformationPopupBackground(background);
154                 assistant.setContextSelectorBackground(background);
155                 assistant.setProposalSelectorBackground(background);
156
157                 Color foreground = getColor(store,
158                                 PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
159                 assistant.setContextInformationPopupForeground(foreground);
160                 assistant.setContextSelectorForeground(foreground);
161                 assistant.setProposalSelectorForeground(foreground);
162
163                 return assistant;
164         }
165
166         private Color getColor(IPreferenceStore store, String key,
167                         IColorManager manager) {
168                 RGB rgb = PreferenceConverter.getColor(store, key);
169                 return manager.getColor(rgb);
170         }
171
172         /*
173          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
174          * @since 2.1
175          */
176         public ITextHover getTextHover(ISourceViewer sourceViewer,
177                         String contentType, int stateMask) {
178                 return new TemplateVariableTextHover(fProcessor);
179         }
180
181 }