1 16 17 package org.eclipse.ant.internal.ui.editor; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration; 24 import org.eclipse.ant.internal.ui.ColorManager; 25 import org.eclipse.ant.internal.ui.editor.formatter.XmlDocumentFormattingStrategy; 26 import org.eclipse.ant.internal.ui.editor.formatter.XmlElementFormattingStrategy; 27 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant; 28 import org.eclipse.ant.internal.ui.editor.text.AntEditorPartitionScanner; 29 import org.eclipse.ant.internal.ui.editor.text.AntInformationProvider; 30 import org.eclipse.ant.internal.ui.editor.text.NotifyingReconciler; 31 import org.eclipse.ant.internal.ui.editor.text.XMLAnnotationHover; 32 import org.eclipse.ant.internal.ui.editor.text.XMLReconcilingStrategy; 33 import org.eclipse.ant.internal.ui.editor.text.XMLTextHover; 34 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 35 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 36 import org.eclipse.jface.preference.PreferenceConverter; 37 import org.eclipse.jface.text.DefaultInformationControl; 38 import org.eclipse.jface.text.IAutoEditStrategy; 39 import org.eclipse.jface.text.IDocument; 40 import org.eclipse.jface.text.IInformationControl; 41 import org.eclipse.jface.text.IInformationControlCreator; 42 import org.eclipse.jface.text.ITextHover; 43 import org.eclipse.jface.text.contentassist.ContentAssistant; 44 import org.eclipse.jface.text.contentassist.IContentAssistant; 45 import org.eclipse.jface.text.formatter.IContentFormatter; 46 import org.eclipse.jface.text.formatter.MultiPassContentFormatter; 47 import org.eclipse.jface.text.information.IInformationPresenter; 48 import org.eclipse.jface.text.information.IInformationProvider; 49 import org.eclipse.jface.text.information.InformationPresenter; 50 import org.eclipse.jface.text.reconciler.IReconciler; 51 import org.eclipse.jface.text.source.IAnnotationHover; 52 import org.eclipse.jface.text.source.ISourceViewer; 53 import org.eclipse.jface.util.PropertyChangeEvent; 54 import org.eclipse.swt.SWT; 55 import org.eclipse.swt.graphics.Color; 56 import org.eclipse.swt.graphics.RGB; 57 import org.eclipse.swt.widgets.Shell; 58 59 62 public class AntEditorSourceViewerConfiguration extends AntSourceViewerConfiguration { 63 64 private AntEditor fEditor; 65 66 private XMLTextHover fTextHover; 67 68 private ContentAssistant fContentAssistant; 69 70 private AntAutoEditStrategy[] fAutoEditorStategies; 71 72 75 public AntEditorSourceViewerConfiguration(AntEditor editor) { 76 super(); 77 fEditor= editor; 78 } 79 80 83 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { 84 fContentAssistant= new ContentAssistant(); 85 AntEditorCompletionProcessor processor = new AntEditorCompletionProcessor(fEditor.getAntModel()); 86 fContentAssistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE); 87 fContentAssistant.setContentAssistProcessor(processor, AntEditorPartitionScanner.XML_TAG); 88 fContentAssistant.setDocumentPartitioning(AntDocumentSetupParticipant.ANT_PARTITIONING); 89 90 String triggers= fPreferenceStore.getString(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS); 91 if (triggers != null) { 92 processor.setCompletionProposalAutoActivationCharacters(triggers.toCharArray()); 93 } 94 95 fContentAssistant.enableAutoInsert(fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT)); 96 fContentAssistant.enableAutoActivation(fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION)); 97 fContentAssistant.setAutoActivationDelay(fPreferenceStore.getInt(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY)); 98 fContentAssistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY); 99 fContentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE); 100 fContentAssistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); 101 102 ColorManager manager= ColorManager.getDefault(); 103 Color background= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager); 104 fContentAssistant.setContextInformationPopupBackground(background); 105 fContentAssistant.setContextSelectorBackground(background); 106 fContentAssistant.setProposalSelectorBackground(background); 107 108 Color foreground= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager); 109 fContentAssistant.setContextInformationPopupForeground(foreground); 110 fContentAssistant.setContextSelectorForeground(foreground); 111 fContentAssistant.setProposalSelectorForeground(foreground); 112 113 IInformationControlCreator creator = getInformationControlCreator(sourceViewer); 114 fContentAssistant.setInformationControlCreator(creator); 115 116 fContentAssistant.setRepeatedInvocationMode(true); 117 fContentAssistant.setStatusLineVisible(true); 118 fContentAssistant.setShowEmptyList(true); 119 fContentAssistant.addCompletionListener(processor); 120 return fContentAssistant; 121 } 122 123 126 public IReconciler getReconciler(ISourceViewer sourceViewer) { 127 NotifyingReconciler reconciler= new NotifyingReconciler(new XMLReconcilingStrategy(fEditor)); 128 reconciler.setDelay(XMLReconcilingStrategy.DELAY); 129 reconciler.addReconcilingParticipant(fEditor); 130 return reconciler; 131 } 132 133 136 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { 137 return new XMLAnnotationHover(); 138 } 139 140 143 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) { 144 return new IInformationControlCreator() { 145 public IInformationControl createInformationControl(Shell parent) { 146 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true)); 147 } 148 }; 149 } 150 151 154 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { 155 if (fTextHover == null) { 156 fTextHover= new XMLTextHover(fEditor); 157 } 158 return fTextHover; 159 } 160 161 private Color getColor(String key, ColorManager manager) { 162 RGB rgb= PreferenceConverter.getColor(fPreferenceStore, key); 163 return manager.getColor(rgb); 164 } 165 166 protected void changeConfiguration(PropertyChangeEvent event) { 167 String p= event.getProperty(); 168 169 ColorManager manager= ColorManager.getDefault(); 170 if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION.equals(p)) { 171 boolean enabled= fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION); 172 fContentAssistant.enableAutoActivation(enabled); 173 } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY.equals(p) && fContentAssistant != null) { 174 int delay= fPreferenceStore.getInt(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY); 175 fContentAssistant.setAutoActivationDelay(delay); 176 } else if (AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND.equals(p) && fContentAssistant != null) { 177 Color c= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager); 178 fContentAssistant.setProposalSelectorForeground(c); 179 } else if (AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND.equals(p) && fContentAssistant != null) { 180 Color c= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager); 181 fContentAssistant.setProposalSelectorBackground(c); 182 } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT.equals(p) && fContentAssistant != null) { 183 boolean enabled= fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT); 184 fContentAssistant.enableAutoInsert(enabled); 185 } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS.equals(p)) { 186 changeContentAssistProcessor(); 187 } 188 } 189 190 private void changeContentAssistProcessor() { 191 String triggers= fPreferenceStore.getString(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS); 192 if (triggers != null) { 193 AntEditorCompletionProcessor cp= (AntEditorCompletionProcessor)fContentAssistant.getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE); 194 if (cp != null) { 195 cp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray()); 196 } 197 } 198 } 199 202 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) { 203 204 MultiPassContentFormatter formatter = new MultiPassContentFormatter( 205 getConfiguredDocumentPartitioning(sourceViewer), 206 IDocument.DEFAULT_CONTENT_TYPE); 207 208 209 formatter.setMasterStrategy(new XmlDocumentFormattingStrategy()); 210 211 formatter.setSlaveStrategy(new XmlElementFormattingStrategy(), AntEditorPartitionScanner.XML_TAG); 212 213 215 return formatter; 216 } 217 218 221 public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) { 222 InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator()); 223 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); 224 IInformationProvider provider= new AntInformationProvider(new XMLTextHover(fEditor)); 225 presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE); 226 presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_CDATA); 227 presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_COMMENT); 228 presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_DTD); 229 presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_TAG); 230 presenter.setSizeConstraints(60, 10, true, true); 231 return presenter; 232 } 233 234 242 public static IInformationControlCreator getInformationPresenterControlCreator() { 243 return new IInformationControlCreator() { 244 public IInformationControl createInformationControl(Shell parent) { 245 int shellStyle= SWT.RESIZE; 246 int style= SWT.V_SCROLL | SWT.H_SCROLL; 247 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false)); 248 } 249 }; 250 } 251 252 255 public String [] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) { 256 List list= new ArrayList (); 257 258 260 int tabWidth= getTabWidth(sourceViewer); 261 boolean useSpaces= fEditor.isTabsToSpacesConversionEnabled(); 262 263 for (int i= 0; i <= tabWidth; i++) { 264 StringBuffer prefix= new StringBuffer (); 265 if (useSpaces) { 266 for (int j= 0; j + i < tabWidth; j++) { 267 prefix.append(' '); 268 } 269 270 if (i != 0) { 271 prefix.append('\t'); 272 } 273 } else { 274 for (int j= 0; j < i; j++) { 275 prefix.append(' '); 276 } 277 if (i != tabWidth) { 278 prefix.append('\t'); 279 } 280 } 281 282 list.add(prefix.toString()); 283 } 284 285 list.add(""); 287 return (String []) list.toArray(new String [list.size()]); 288 } 289 290 293 public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) { 294 if (AntEditorPartitionScanner.XML_COMMENT.equals(contentType)) { 295 return super.getAutoEditStrategies(sourceViewer, contentType); 296 } 297 if (fAutoEditorStategies == null) { 298 fAutoEditorStategies= new AntAutoEditStrategy[] {new AntAutoEditStrategy(fEditor.getAntModel())}; 299 } 300 return fAutoEditorStategies; 301 } 302 303 306 protected Map getHyperlinkDetectorTargets(ISourceViewer sourceViewer) { 307 Map targets = super.getHyperlinkDetectorTargets(sourceViewer); 308 targets.put("org.eclipse.ant.ui.buildFiles", fEditor); return targets; 310 } 311 } | Popular Tags |