1 11 12 package org.eclipse.jdt.internal.ui.preferences.formatter; 13 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 19 import org.eclipse.swt.widgets.Composite; 20 21 import org.eclipse.jface.text.Region; 22 import org.eclipse.jface.text.formatter.FormattingContextProperties; 23 import org.eclipse.jface.text.formatter.IContentFormatter; 24 import org.eclipse.jface.text.formatter.IContentFormatterExtension; 25 import org.eclipse.jface.text.formatter.IFormattingContext; 26 27 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 28 import org.eclipse.jdt.internal.ui.JavaPlugin; 29 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext; 30 31 32 public class CompilationUnitPreview extends JavaPreview { 33 34 private String fPreviewText; 35 36 40 public CompilationUnitPreview(Map workingValues, Composite parent) { 41 42 super(workingValues, parent); 43 } 44 45 protected void doFormatPreview() { 46 if (fPreviewText == null) { 47 fPreviewDocument.set(""); return; 49 } 50 fPreviewDocument.set(fPreviewText); 51 52 fSourceViewer.setRedraw(false); 53 final IFormattingContext context = new CommentFormattingContext(); 54 try { 55 final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer); 56 if (formatter instanceof IContentFormatterExtension) { 57 final IContentFormatterExtension extension = (IContentFormatterExtension) formatter; 58 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues); 59 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); 60 extension.format(fPreviewDocument, context); 61 } else 62 formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength())); 63 } catch (Exception e) { 64 final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, 65 FormatterMessages.JavaPreview_formatter_exception, e); 66 JavaPlugin.log(status); 67 } finally { 68 context.dispose(); 69 fSourceViewer.setRedraw(true); 70 } 71 } 72 73 public void setPreviewText(String previewText) { 74 fPreviewText= previewText; 76 update(); 77 } 78 } 79 | Popular Tags |