1 12 package org.eclipse.ant.internal.ui.editor.templates; 13 14 import org.eclipse.ant.internal.ui.AntUIPlugin; 15 import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences; 16 import org.eclipse.ant.internal.ui.editor.formatter.XmlFormatter; 17 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant; 18 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.text.Document; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.source.SourceViewer; 23 import org.eclipse.jface.text.source.SourceViewerConfiguration; 24 import org.eclipse.jface.text.templates.Template; 25 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.graphics.Font; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage; 31 32 35 public class AntTemplatePreferencePage extends TemplatePreferencePage { 36 37 private FormattingPreferences fFormattingPreferences= new FormattingPreferences(); 38 39 public AntTemplatePreferencePage() { 40 setPreferenceStore(AntUIPlugin.getDefault().getPreferenceStore()); 41 setTemplateStore(AntTemplateAccess.getDefault().getTemplateStore()); 42 setContextTypeRegistry(AntTemplateAccess.getDefault().getContextTypeRegistry()); 43 } 44 45 48 public boolean performOk() { 49 boolean ok = super.performOk(); 50 AntUIPlugin.getDefault().savePluginPreferences(); 51 return ok; 52 } 53 54 59 protected SourceViewer createViewer(Composite parent) { 60 SourceViewer viewer = new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 61 62 SourceViewerConfiguration configuration = new AntTemplateViewerConfiguration(); 63 IDocument document = new Document(); 64 new AntDocumentSetupParticipant().setup(document); 65 viewer.configure(configuration); 66 viewer.setDocument(document); 67 viewer.setEditable(false); 68 Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT); 69 viewer.getTextWidget().setFont(font); 70 71 return viewer; 72 } 73 74 77 protected String getFormatterPreferenceKey() { 78 return AntEditorPreferenceConstants.TEMPLATES_USE_CODEFORMATTER; 79 } 80 81 84 protected void updateViewerInput() { 85 IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection(); 86 SourceViewer viewer= getViewer(); 87 88 if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) { 89 TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement(); 90 Template template= data.getTemplate(); 91 if (AntUIPlugin.getDefault().getPreferenceStore().getBoolean(getFormatterPreferenceKey())) { 92 String formatted= XmlFormatter.format(template.getPattern(), fFormattingPreferences); 93 viewer.getDocument().set(formatted); 94 } else { 95 viewer.getDocument().set(template.getPattern()); 96 } 97 } else { 98 viewer.getDocument().set(""); } 100 } 101 104 protected boolean isShowFormatterSetting() { 105 return false; 106 } 107 } | Popular Tags |