1 11 12 package org.eclipse.ant.internal.ui.editor; 13 14 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant; 15 import org.eclipse.ant.internal.ui.editor.text.AntEditorPartitionScanner; 16 import org.eclipse.ant.internal.ui.editor.text.AntEditorProcInstrScanner; 17 import org.eclipse.ant.internal.ui.editor.text.AntEditorTagScanner; 18 import org.eclipse.ant.internal.ui.editor.text.IAntEditorColorConstants; 19 import org.eclipse.ant.internal.ui.editor.text.MultilineDamagerRepairer; 20 import org.eclipse.ant.internal.ui.model.AntUIPlugin; 21 import org.eclipse.ant.internal.ui.model.ColorManager; 22 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 23 import org.eclipse.jface.preference.IPreferenceStore; 24 import org.eclipse.jface.resource.StringConverter; 25 import org.eclipse.jface.text.IDocument; 26 import org.eclipse.jface.text.TextAttribute; 27 import org.eclipse.jface.text.presentation.IPresentationReconciler; 28 import org.eclipse.jface.text.presentation.PresentationReconciler; 29 import org.eclipse.jface.text.source.ISourceViewer; 30 import org.eclipse.jface.text.source.SourceViewerConfiguration; 31 import org.eclipse.jface.util.PropertyChangeEvent; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.graphics.RGB; 34 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; 35 36 public abstract class AbstractAntSourceViewerConfiguration extends SourceViewerConfiguration { 37 38 private AntEditorTagScanner tagScanner; 39 private AntEditorProcInstrScanner instructionScanner; 40 private MultilineDamagerRepairer damageRepairer; 41 private TextAttribute xmlCommentAttribute; 42 43 private AntEditorProcInstrScanner getDefaultScanner() { 44 if (instructionScanner == null) { 45 instructionScanner = new AntEditorProcInstrScanner(); 46 } 47 return instructionScanner; 48 } 49 50 private AntEditorTagScanner getTagScanner() { 51 if (tagScanner == null) { 52 tagScanner = new AntEditorTagScanner(); 53 } 54 return tagScanner; 55 } 56 57 60 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { 61 PresentationReconciler reconciler = new PresentationReconciler(); 62 reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); 63 64 MultilineDamagerRepairer dr = new MultilineDamagerRepairer(getDefaultScanner(), null); 65 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); 66 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); 67 68 dr = new MultilineDamagerRepairer(getTagScanner(), null); 69 reconciler.setDamager(dr, AntEditorPartitionScanner.XML_TAG); 70 reconciler.setRepairer(dr, AntEditorPartitionScanner.XML_TAG); 71 72 IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore(); 73 int style= SWT.NORMAL; 74 if (store.getBoolean(IAntEditorColorConstants.XML_COMMENT_COLOR + AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) { 75 style |= SWT.BOLD; 76 } 77 if (store.getBoolean(IAntEditorColorConstants.XML_COMMENT_COLOR + AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) { 78 style |= SWT.ITALIC; 79 } 80 81 xmlCommentAttribute= new TextAttribute(AntUIPlugin.getPreferenceColor(IAntEditorColorConstants.XML_COMMENT_COLOR), null, style); 82 damageRepairer= new MultilineDamagerRepairer(null, xmlCommentAttribute); 83 reconciler.setDamager(damageRepairer, AntEditorPartitionScanner.XML_COMMENT); 84 reconciler.setRepairer(damageRepairer, AntEditorPartitionScanner.XML_COMMENT); 85 86 return reconciler; 87 } 88 89 93 public void adaptToPreferenceChange(PropertyChangeEvent event) { 94 if (tagScanner == null) { 95 return; } 97 tagScanner.adaptToPreferenceChange(event); 98 instructionScanner.adaptToPreferenceChange(event); 99 String property= event.getProperty(); 100 if (property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR)) { 101 if (property.endsWith(AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) { 102 adaptToStyleChange(event, SWT.BOLD); 103 } else if (property.endsWith(AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) { 104 adaptToStyleChange(event, SWT.ITALIC); 105 } else { 106 adaptToColorChange(event); 107 } 108 damageRepairer.setDefaultTextAttribute(xmlCommentAttribute); 109 } 110 } 111 112 private void adaptToStyleChange(PropertyChangeEvent event, int styleAttribute) { 113 boolean eventValue= false; 114 Object value= event.getNewValue(); 115 if (value instanceof Boolean ) { 116 eventValue= ((Boolean ) value).booleanValue(); 117 } else if (IPreferenceStore.TRUE.equals(value)) { 118 eventValue= true; 119 } 120 121 boolean activeValue= (xmlCommentAttribute.getStyle() & styleAttribute) == styleAttribute; 122 if (activeValue != eventValue) { 123 xmlCommentAttribute= new TextAttribute(xmlCommentAttribute.getForeground(), xmlCommentAttribute.getBackground(), eventValue ? xmlCommentAttribute.getStyle() | styleAttribute : xmlCommentAttribute.getStyle() & ~styleAttribute); 124 } 125 } 126 127 130 private void adaptToColorChange(PropertyChangeEvent event) { 131 RGB rgb= null; 132 133 Object value= event.getNewValue(); 134 if (value instanceof RGB) { 135 rgb= (RGB) value; 136 } else if (value instanceof String ) { 137 rgb= StringConverter.asRGB((String ) value); 138 } 139 140 if (rgb != null) { 141 xmlCommentAttribute= new TextAttribute(ColorManager.getDefault().getColor(rgb), xmlCommentAttribute.getBackground(), xmlCommentAttribute.getStyle()); 142 } 143 } 144 145 148 public String [] getConfiguredContentTypes(ISourceViewer sourceViewer) { 149 return new String [] { 150 IDocument.DEFAULT_CONTENT_TYPE, 151 AntEditorPartitionScanner.XML_COMMENT, 152 AntEditorPartitionScanner.XML_TAG, 153 AntEditorPartitionScanner.XML_CDATA}; 154 } 155 156 159 public int getTabWidth(ISourceViewer sourceViewer) { 160 return AntUIPlugin.getDefault().getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); 161 } 162 163 public boolean affectsTextPresentation(PropertyChangeEvent event) { 164 String property= event.getProperty(); 165 return property.startsWith(IAntEditorColorConstants.TEXT_COLOR) || 166 property.startsWith(IAntEditorColorConstants.PROCESSING_INSTRUCTIONS_COLOR) || 167 property.startsWith(IAntEditorColorConstants.STRING_COLOR) || 168 property.startsWith(IAntEditorColorConstants.TAG_COLOR) || 169 property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR); 170 } 171 172 175 public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) { 176 return AntDocumentSetupParticipant.ANT_PARTITIONING; 177 } 178 } | Popular Tags |