refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 import net.sourceforge.phpeclipse.ui.WebUI;
24
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;
40
41 public class TemplateEditorSourceViewerConfiguration extends
42                 PHPSourceViewerConfiguration {
43
44         private static class TemplateVariableTextHover implements ITextHover {
45
46                 private TemplateVariableProcessor fProcessor;
47
48                 /**
49                  * @param type
50                  */
51                 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
52                         fProcessor = processor;
53                 }
54
55                 /*
56                  * (non-Javadoc)
57                  * 
58                  * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
59                  *      org.eclipse.jface.text.IRegion)
60                  */
61                 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
62                         try {
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
68                                                         .getContextType();
69                                         if (contextType != null) {
70                                                 Iterator iter = contextType.resolvers();
71                                                 while (iter.hasNext()) {
72                                                         TemplateVariableResolver var = (TemplateVariableResolver) iter
73                                                                         .next();
74                                                         if (varName.equals(var.getType())) {
75                                                                 return var.getDescription();
76                                                         }
77                                                 }
78                                         }
79                                 }
80                         } catch (BadLocationException e) {
81                         }
82                         return null;
83                 }
84
85                 /*
86                  * (non-Javadoc)
87                  * 
88                  * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
89                  *      int)
90                  */
91                 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
92                         if (textViewer != null) {
93                                 return JavaWordFinder
94                                                 .findWord(textViewer.getDocument(), offset);
95                         }
96                         return null;
97                 }
98
99         }
100
101         private final TemplateVariableProcessor fProcessor;
102
103         public TemplateEditorSourceViewerConfiguration(IColorManager colorManager,
104                         IPreferenceStore store, ITextEditor editor,
105                         TemplateVariableProcessor processor) {
106                 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
107                 fProcessor = processor;
108         }
109
110         /*
111          * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
112          */
113         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
114
115                 IPreferenceStore store = WebUI.getDefault()
116                                 .getPreferenceStore();
117                 JavaTextTools textTools = WebUI.getDefault()
118                                 .getJavaTextTools();
119                 IColorManager manager = textTools.getColorManager();
120
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);
138
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));
145                 assistant
146                                 .setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
147                 assistant
148                                 .setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
149                 assistant
150                                 .setInformationControlCreator(getInformationControlCreator(sourceViewer));
151
152                 Color background = getColor(store,
153                                 PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
154                 assistant.setContextInformationPopupBackground(background);
155                 assistant.setContextSelectorBackground(background);
156                 assistant.setProposalSelectorBackground(background);
157
158                 Color foreground = getColor(store,
159                                 PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
160                 assistant.setContextInformationPopupForeground(foreground);
161                 assistant.setContextSelectorForeground(foreground);
162                 assistant.setProposalSelectorForeground(foreground);
163
164                 return assistant;
165         }
166
167         private Color getColor(IPreferenceStore store, String key,
168                         IColorManager manager) {
169                 RGB rgb = PreferenceConverter.getColor(store, key);
170                 return manager.getColor(rgb);
171         }
172
173         /*
174          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
175          * @since 2.1
176          */
177         public ITextHover getTextHover(ISourceViewer sourceViewer,
178                         String contentType, int stateMask) {
179                 return new TemplateVariableTextHover(fProcessor);
180         }
181
182 }