Source code: com/port80/eclipse/xml/editors/XMLEditorConfiguration.java
1 package com.port80.eclipse.xml.editors;
2
3 import org.eclipse.jdt.internal.ui.text.HTMLTextPresenter;
4 import org.eclipse.jface.preference.IPreferenceStore;
5 import org.eclipse.jface.text.DefaultInformationControl;
6 import org.eclipse.jface.text.IAutoIndentStrategy;
7 import org.eclipse.jface.text.IInformationControl;
8 import org.eclipse.jface.text.IInformationControlCreator;
9 import org.eclipse.jface.text.ITextDoubleClickStrategy;
10 import org.eclipse.jface.text.TextAttribute;
11 import org.eclipse.jface.text.formatter.ContentFormatter;
12 import org.eclipse.jface.text.formatter.IContentFormatter;
13 import org.eclipse.jface.text.presentation.IPresentationReconciler;
14 import org.eclipse.jface.text.presentation.PresentationReconciler;
15 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
16 import org.eclipse.jface.text.rules.DefaultPartitioner;
17 import org.eclipse.jface.text.rules.Token;
18 import org.eclipse.jface.text.source.IAnnotationHover;
19 import org.eclipse.jface.text.source.ISourceViewer;
20 import org.eclipse.jface.text.source.SourceViewer;
21 import org.eclipse.jface.text.source.SourceViewerConfiguration;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Shell;
25
26 import com.port80.eclipse.editors.EditorsPlugin;
27 import com.port80.eclipse.editors.IConstants;
28 import com.port80.eclipse.editors.IEditorConfiguration;
29 import com.port80.eclipse.editors.ThemeManager;
30 import com.port80.eclipse.editors.util.MarkerAnnotationHover;
31 import com.port80.eclipse.editors.util.NonRuleBasedDamagerRepairer;
32
33 public class XMLEditorConfiguration extends SourceViewerConfiguration implements IEditorConfiguration {
34
35 ////////////////////////////////////////////////////////////////////////
36
37 public final static String[] PARITION_TYPES =
38 new String[] {
39 DEFAULT_PARTITION,
40 COMMENT_PARTITION,
41 TAG_PARTITION,
42 CDATA_PARTITION,
43 PROC_INST_PARTITION };
44
45 ////////////////////////////////////////////////////////////////////////
46
47 private XMLEditor fEditor;
48 private IPreferenceStore fPreferences;
49 private XMLDoubleClickStrategy fDoubleClickStrategy;
50 private XMLAutoIndentStrategy fAutoIndentStrategy;
51 private ContentFormatter fFormatter;
52 private XMLFormattingStrategy fFormattingStrategy;
53 private XMLTagScanner tagScanner;
54 private XMLScanner scanner;
55
56 ////////////////////////////////////////////////////////////////////////
57
58 public XMLEditorConfiguration(XMLEditor editor, IPreferenceStore prefs) {
59 fEditor = editor;
60 fPreferences = prefs;
61 }
62
63 ////////////////////////////////////////////////////////////////////////
64
65 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
66 return PARITION_TYPES;
67 }
68
69 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
70 if (fDoubleClickStrategy == null)
71 fDoubleClickStrategy = new XMLDoubleClickStrategy();
72 return fDoubleClickStrategy;
73 }
74
75 /**
76 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoIndentStrategy(ISourceViewer, String)
77 */
78 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
79 if (fAutoIndentStrategy == null)
80 fAutoIndentStrategy = new XMLAutoIndentStrategy(this, sourceViewer);
81 return fAutoIndentStrategy;
82 }
83
84 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
85 PresentationReconciler reconciler = new PresentationReconciler();
86 //
87 DefaultDamagerRepairer dr;
88 dr = new DefaultDamagerRepairer(getXMLScanner());
89 reconciler.setDamager(dr, DEFAULT_PARTITION);
90 reconciler.setRepairer(dr, DEFAULT_PARTITION);
91 //
92 dr = new DefaultDamagerRepairer(getXMLTagScanner());
93 reconciler.setDamager(dr, TAG_PARTITION);
94 reconciler.setRepairer(dr, TAG_PARTITION);
95 //
96 NonRuleBasedDamagerRepairer ndr;
97 ndr =
98 new NonRuleBasedDamagerRepairer(
99 new TextAttribute(EditorsPlugin.getColor("XMLProcessInstruction")));
100 reconciler.setDamager(ndr, PROC_INST_PARTITION);
101 reconciler.setRepairer(ndr, PROC_INST_PARTITION);
102 //
103 ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(EditorsPlugin.getColor("XMLCData")));
104 reconciler.setDamager(ndr, CDATA_PARTITION);
105 reconciler.setRepairer(ndr, CDATA_PARTITION);
106 //
107 ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(EditorsPlugin.getColor("XMLComment")));
108 reconciler.setDamager(ndr, COMMENT_PARTITION);
109 reconciler.setRepairer(ndr, COMMENT_PARTITION);
110 //
111 return reconciler;
112 }
113
114 /*
115 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
116 */
117 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
118 if (fFormatter == null) {
119 fFormatter = new ContentFormatter();
120 fFormattingStrategy = new XMLFormattingStrategy(this, (SourceViewer) sourceViewer);
121 fFormatter.setFormattingStrategy(fFormattingStrategy, DEFAULT_PARTITION);
122 fFormatter.enablePartitionAwareFormatting(false);
123 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
124 }
125 return fFormatter;
126 }
127
128 /**
129 * Returns the names of the document position categories used by the document
130 * partitioners created by this object to manage their partition information.
131 * If the partitioners don't use document position categories, the returned
132 * result is <code>null</code>.
133 *
134 * @return the partition managing position categories or <code>null</code>
135 * if there is none
136 */
137 public String[] getPartitionManagingPositionCategories() {
138 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
139 }
140
141 ////////////////////////////////////////////////////////////////////////
142
143 public IPreferenceStore getPreferences() {
144 return fPreferences;
145 }
146
147 public int getLineWidth(ISourceViewer viewer) {
148 return fPreferences.getInt(IConstants.PREF_XML_LINEWIDTH);
149 }
150
151 public int getTabWidth(ISourceViewer viewer) {
152 return fPreferences.getInt(IConstants.PREF_XML_TABWIDTH);
153 }
154
155 public String getTab(ISourceViewer viewer) {
156 if (fPreferences.getBoolean(IConstants.PREF_XML_INDENT_USE_TAB)) {
157 return "\t";
158 } else {
159 String ret = "";
160 int tabwidth = getTabWidth(viewer);
161 for (int i = 0; i < tabwidth; ++i)
162 ret += " ";
163 return ret;
164 }
165 }
166
167 ////////////////////////////////////////////////////////////////////////
168
169 public XMLEditor getEditor() {
170 return fEditor;
171 }
172
173 public void handlePreferenceStoreChanged(PropertyChangeEvent event) {
174 if (fAutoIndentStrategy != null)
175 fAutoIndentStrategy.initPreferences();
176 if (fFormattingStrategy != null)
177 fFormattingStrategy.initPreferences();
178 }
179
180 ////////////////////////////////////////////////////////////////////////
181
182 protected XMLScanner getXMLScanner() {
183 if (scanner == null) {
184 ThemeManager manager = EditorsPlugin.getThemeManager();
185 scanner = new XMLScanner(manager);
186 scanner.setDefaultReturnToken(new Token(new TextAttribute(manager.getColor("XMLText"))));
187 }
188 return scanner;
189 }
190
191 protected XMLTagScanner getXMLTagScanner() {
192 if (tagScanner == null) {
193 ThemeManager manager = EditorsPlugin.getThemeManager();
194 tagScanner = new XMLTagScanner(manager);
195 }
196 return tagScanner;
197 }
198
199 ////////////////////////////////////////////////////////////////////////
200
201 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
202 return new MarkerAnnotationHover();
203 }
204
205 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
206 return new IInformationControlCreator() {
207 public IInformationControl createInformationControl(Shell parent) {
208 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
209 }
210 };
211 }
212
213 ////////////////////////////////////////////////////////////////////////
214
215 }