14a17c96ab91bdca1f839cba5e8dfa2ecaad4808
[phpeclipse.git] /
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU 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  *     Igor Malinin - initial contribution
10  *
11  * $Id: XMLConfiguration.java,v 1.2 2006-10-21 23:14:13 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import net.sourceforge.phpeclipse.ui.templates.template.BasicCompletionProcessor;
17 import net.sourceforge.phpeclipse.ui.text.TextDoubleClickStrategy;
18 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
19
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITextDoubleClickStrategy;
23 import org.eclipse.jface.text.ITextHover;
24 import org.eclipse.jface.text.contentassist.ContentAssistant;
25 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
26 import org.eclipse.jface.text.contentassist.IContentAssistant;
27 import org.eclipse.jface.text.presentation.IPresentationReconciler;
28 import org.eclipse.jface.text.presentation.PresentationReconciler;
29 import org.eclipse.jface.text.reconciler.IReconciler;
30 import org.eclipse.jface.text.reconciler.MonoReconciler;
31 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
32 import org.eclipse.jface.text.source.IAnnotationHover;
33 import org.eclipse.jface.text.source.IAnnotationModel;
34 import org.eclipse.jface.text.source.ISourceViewer;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
37 import org.eclipse.ui.texteditor.IDocumentProvider;
38 import org.eclipse.ui.texteditor.ITextEditor;
39
40 /**
41  * XML editor configuration.
42  * 
43  * @author Igor Malinin
44  */
45 public class XMLConfiguration extends TextSourceViewerConfiguration {
46         protected XMLTextTools xmlTextTools;
47
48         private ITextDoubleClickStrategy dcsDefault;
49
50         private ITextDoubleClickStrategy dcsSimple;
51
52         private ITextDoubleClickStrategy dcsTag;
53
54         private ITextDoubleClickStrategy dcsAttValue;
55
56         /** The associated editor. */
57         private ITextEditor editor;
58
59         public XMLConfiguration(XMLTextTools tools) {
60                 this(tools, null);
61         }
62
63         public XMLConfiguration(XMLTextTools tools, ITextEditor editor) {
64                 xmlTextTools = tools;
65                 this.editor = editor;
66                 dcsDefault = new TextDoubleClickStrategy();
67                 dcsSimple = new SimpleDoubleClickStrategy();
68                 dcsTag = new TagDoubleClickStrategy();
69                 dcsAttValue = new AttValueDoubleClickStrategy();
70         }
71
72         /*
73          * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
74          */
75         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
76                 return new XMLAnnotationHover();
77         }
78
79         /*
80          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
81          */
82         public ITextHover getTextHover(ISourceViewer sourceViewer,
83                         String contentType) {
84                 if (editor != null) {
85                         IDocumentProvider provider = editor.getDocumentProvider();
86                         IEditorInput input = editor.getEditorInput();
87                         IAnnotationModel model = provider.getAnnotationModel(input);
88                         return new XMLTextHover(model);
89                 }
90
91                 return super.getTextHover(sourceViewer, contentType);
92         }
93
94         /*
95          * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer,
96          *      String)
97          */
98         public ITextDoubleClickStrategy getDoubleClickStrategy(
99                         ISourceViewer sourceViewer, String contentType) {
100                 if (XMLPartitionScanner.XML_COMMENT.equals(contentType)) {
101                         return dcsSimple;
102                 }
103
104                 if (XMLPartitionScanner.XML_PI.equals(contentType)) {
105                         return dcsSimple;
106                 }
107
108                 if (XMLPartitionScanner.XML_TAG.equals(contentType)) {
109                         return dcsTag;
110                 }
111
112                 if (XMLPartitionScanner.XML_ATTRIBUTE.equals(contentType)) {
113                         return dcsAttValue;
114                 }
115
116                 if (XMLPartitionScanner.XML_CDATA.equals(contentType)) {
117                         return dcsSimple;
118                 }
119
120                 if (contentType.startsWith(XMLPartitionScanner.DTD_INTERNAL)) {
121                         return dcsSimple;
122                 }
123
124                 return dcsDefault;
125         }
126
127         /*
128          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
129          */
130         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
131                 return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
132                                 XMLPartitionScanner.XML_PI, XMLPartitionScanner.XML_COMMENT,
133                                 XMLPartitionScanner.XML_DECL, XMLPartitionScanner.XML_TAG,
134                                 XMLPartitionScanner.XML_ATTRIBUTE,
135                                 XMLPartitionScanner.XML_CDATA,
136                                 XMLPartitionScanner.DTD_INTERNAL,
137                                 XMLPartitionScanner.DTD_INTERNAL_PI,
138                                 XMLPartitionScanner.DTD_INTERNAL_COMMENT,
139                                 XMLPartitionScanner.DTD_INTERNAL_DECL, };
140         }
141
142         /*
143          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
144          */
145         public IPresentationReconciler getPresentationReconciler(
146                         ISourceViewer sourceViewer) {
147                 PresentationReconciler reconciler = new PresentationReconciler();
148
149                 DefaultDamagerRepairer dr;
150
151                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLTextScanner());
152                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
153                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
154
155                 dr = new DefaultDamagerRepairer(xmlTextTools.getDTDTextScanner());
156                 reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL);
157                 reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL);
158
159                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLPIScanner());
160
161                 reconciler.setDamager(dr, XMLPartitionScanner.XML_PI);
162                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_PI);
163                 reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_PI);
164                 reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_PI);
165
166                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLCommentScanner());
167
168                 reconciler.setDamager(dr, XMLPartitionScanner.XML_COMMENT);
169                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_COMMENT);
170                 reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_COMMENT);
171                 reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_COMMENT);
172
173                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLDeclScanner());
174
175                 reconciler.setDamager(dr, XMLPartitionScanner.XML_DECL);
176                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_DECL);
177                 reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_DECL);
178                 reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_DECL);
179
180                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLTagScanner());
181
182                 reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG);
183                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG);
184
185                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLAttributeScanner());
186
187                 reconciler.setDamager(dr, XMLPartitionScanner.XML_ATTRIBUTE);
188                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_ATTRIBUTE);
189
190                 dr = new DefaultDamagerRepairer(xmlTextTools.getXMLCDATAScanner());
191
192                 reconciler.setDamager(dr, XMLPartitionScanner.XML_CDATA);
193                 reconciler.setRepairer(dr, XMLPartitionScanner.XML_CDATA);
194
195                 return reconciler;
196         }
197
198         /*
199          * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
200          */
201         public IReconciler getReconciler(ISourceViewer sourceViewer) {
202                 if ((editor != null) && editor.isEditable()) {
203                         MonoReconciler reconciler = new MonoReconciler(
204                                         new XMLReconcilingStrategy(editor), false);
205                         reconciler.setProgressMonitor(new NullProgressMonitor());
206                         reconciler.setDelay(500);
207                         return reconciler;
208                 }
209
210                 return null;
211         }
212
213         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
214                 ContentAssistant assistant = new ContentAssistant();
215                 assistant
216                                 .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
217
218                 IContentAssistProcessor processor = new BasicCompletionProcessor();
219                 assistant.setContentAssistProcessor(processor,
220                                 IDocument.DEFAULT_CONTENT_TYPE);
221                 assistant.setContentAssistProcessor(processor,
222                                 XMLPartitionScanner.XML_TAG);
223                 assistant.setContentAssistProcessor(processor,
224                                 XMLPartitionScanner.XML_PI);
225                 assistant.setContentAssistProcessor(processor,
226                                 XMLPartitionScanner.XML_COMMENT);
227                 assistant.setContentAssistProcessor(processor,
228                                 XMLPartitionScanner.XML_DECL);
229                 assistant.setContentAssistProcessor(processor,
230                                 XMLPartitionScanner.XML_TAG);
231                 assistant.setContentAssistProcessor(processor,
232                                 XMLPartitionScanner.XML_ATTRIBUTE);
233                 assistant.setContentAssistProcessor(processor,
234                                 XMLPartitionScanner.XML_CDATA);
235                 assistant.setContentAssistProcessor(processor,
236                                 XMLPartitionScanner.DTD_INTERNAL);
237                 assistant.setContentAssistProcessor(processor,
238                                 XMLPartitionScanner.DTD_INTERNAL_PI);
239                 assistant.setContentAssistProcessor(processor,
240                                 XMLPartitionScanner.DTD_INTERNAL_COMMENT);
241                 assistant.setContentAssistProcessor(processor,
242                                 XMLPartitionScanner.DTD_INTERNAL_DECL);
243                 assistant
244                                 .setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
245                 assistant
246                                 .setInformationControlCreator(getInformationControlCreator(sourceViewer));
247
248                 return assistant;
249         }
250
251 }