*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / text / XMLConfiguration.java
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.1 2004-09-02 18:28:03 jsurfer 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, String contentType) {
83     if (editor != null) {
84       IDocumentProvider provider = editor.getDocumentProvider();
85       IEditorInput input = editor.getEditorInput();
86       IAnnotationModel model = provider.getAnnotationModel(input);
87       return new XMLTextHover(model);
88     }
89
90     return super.getTextHover(sourceViewer, contentType);
91   }
92
93   /*
94    * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
95    */
96   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
97     if (XMLPartitionScanner.XML_COMMENT.equals(contentType)) {
98       return dcsSimple;
99     }
100
101     if (XMLPartitionScanner.XML_PI.equals(contentType)) {
102       return dcsSimple;
103     }
104
105     if (XMLPartitionScanner.XML_TAG.equals(contentType)) {
106       return dcsTag;
107     }
108
109     if (XMLPartitionScanner.XML_ATTRIBUTE.equals(contentType)) {
110       return dcsAttValue;
111     }
112
113     if (XMLPartitionScanner.XML_CDATA.equals(contentType)) {
114       return dcsSimple;
115     }
116
117     if (contentType.startsWith(XMLPartitionScanner.DTD_INTERNAL)) {
118       return dcsSimple;
119     }
120
121     return dcsDefault;
122   }
123
124   /*
125    * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
126    */
127   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
128     return new String[] { IDocument.DEFAULT_CONTENT_TYPE, XMLPartitionScanner.XML_PI, XMLPartitionScanner.XML_COMMENT,
129         XMLPartitionScanner.XML_DECL, XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_ATTRIBUTE,
130         XMLPartitionScanner.XML_CDATA, XMLPartitionScanner.DTD_INTERNAL, XMLPartitionScanner.DTD_INTERNAL_PI,
131         XMLPartitionScanner.DTD_INTERNAL_COMMENT, XMLPartitionScanner.DTD_INTERNAL_DECL, };
132   }
133
134   /*
135    * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
136    */
137   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
138     PresentationReconciler reconciler = new PresentationReconciler();
139
140     DefaultDamagerRepairer dr;
141
142     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLTextScanner());
143     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
144     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
145
146     dr = new DefaultDamagerRepairer(xmlTextTools.getDTDTextScanner());
147     reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL);
148     reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL);
149
150     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLPIScanner());
151
152     reconciler.setDamager(dr, XMLPartitionScanner.XML_PI);
153     reconciler.setRepairer(dr, XMLPartitionScanner.XML_PI);
154     reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_PI);
155     reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_PI);
156
157     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLCommentScanner());
158
159     reconciler.setDamager(dr, XMLPartitionScanner.XML_COMMENT);
160     reconciler.setRepairer(dr, XMLPartitionScanner.XML_COMMENT);
161     reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_COMMENT);
162     reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_COMMENT);
163
164     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLDeclScanner());
165
166     reconciler.setDamager(dr, XMLPartitionScanner.XML_DECL);
167     reconciler.setRepairer(dr, XMLPartitionScanner.XML_DECL);
168     reconciler.setDamager(dr, XMLPartitionScanner.DTD_INTERNAL_DECL);
169     reconciler.setRepairer(dr, XMLPartitionScanner.DTD_INTERNAL_DECL);
170
171     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLTagScanner());
172
173     reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG);
174     reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG);
175
176     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLAttributeScanner());
177
178     reconciler.setDamager(dr, XMLPartitionScanner.XML_ATTRIBUTE);
179     reconciler.setRepairer(dr, XMLPartitionScanner.XML_ATTRIBUTE);
180
181     dr = new DefaultDamagerRepairer(xmlTextTools.getXMLCDATAScanner());
182
183     reconciler.setDamager(dr, XMLPartitionScanner.XML_CDATA);
184     reconciler.setRepairer(dr, XMLPartitionScanner.XML_CDATA);
185
186     return reconciler;
187   }
188
189   /*
190    * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
191    */
192   public IReconciler getReconciler(ISourceViewer sourceViewer) {
193     if ((editor != null) && editor.isEditable()) {
194       MonoReconciler reconciler = new MonoReconciler(new XMLReconcilingStrategy(editor), false);
195       reconciler.setProgressMonitor(new NullProgressMonitor());
196       reconciler.setDelay(500);
197       return reconciler;
198     }
199
200     return null;
201   }
202
203   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
204     ContentAssistant assistant = new ContentAssistant();
205     assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
206
207     IContentAssistProcessor processor = new BasicCompletionProcessor();
208     assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
209     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_TAG);
210     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_PI);
211     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_COMMENT);
212     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_DECL);
213     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_TAG);
214     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_ATTRIBUTE);
215     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.XML_CDATA);
216     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.DTD_INTERNAL);
217     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.DTD_INTERNAL_PI);
218     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.DTD_INTERNAL_COMMENT);
219     assistant.setContentAssistProcessor(processor, XMLPartitionScanner.DTD_INTERNAL_DECL);
220     assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
221     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
222
223     return assistant;
224   }
225
226 }