1 11 12 package org.eclipse.jdt.internal.ui.preferences.cleanup; 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.core.JavaCore; 28 29 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 30 import org.eclipse.jdt.internal.ui.JavaPlugin; 31 import org.eclipse.jdt.internal.ui.fix.ICleanUp; 32 import org.eclipse.jdt.internal.ui.fix.MultiFixMessages; 33 import org.eclipse.jdt.internal.ui.preferences.formatter.JavaPreview; 34 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext; 35 36 37 public class CleanUpPreview extends JavaPreview { 38 39 private ICleanUp[] fPreviewCleanUps; 40 private boolean fFormat; 41 public CleanUpPreview(Composite parent, ICleanUp[] cleanUps) { 42 super(JavaCore.getDefaultOptions(), parent); 43 fPreviewCleanUps= cleanUps; 44 fFormat= false; 45 } 46 47 public void setCleanUps(ICleanUp[] fCleanUps) { 48 fPreviewCleanUps= fCleanUps; 49 } 50 51 public void setFormat(boolean enable) { 52 fFormat= enable; 53 } 54 55 58 protected void doFormatPreview() { 59 60 StringBuffer buf= new StringBuffer (); 61 for (int i= 0; i < fPreviewCleanUps.length; i++) { 62 buf.append(fPreviewCleanUps[i].getPreview()); 63 buf.append("\n"); } 65 format(buf.toString()); 66 } 67 68 private void format(String text) { 69 if (text == null) { 70 fPreviewDocument.set(""); return; 72 } 73 fPreviewDocument.set(text); 74 75 if (!fFormat) 76 return; 77 78 fSourceViewer.setRedraw(false); 79 final IFormattingContext context = new CommentFormattingContext(); 80 try { 81 final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer); 82 if (formatter instanceof IContentFormatterExtension) { 83 final IContentFormatterExtension extension = (IContentFormatterExtension) formatter; 84 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions()); 85 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); 86 extension.format(fPreviewDocument, context); 87 } else 88 formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength())); 89 } catch (Exception e) { 90 final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, 91 MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e); 92 JavaPlugin.log(status); 93 } finally { 94 context.dispose(); 95 fSourceViewer.setRedraw(true); 96 } 97 } 98 99 public void setWorkingValues(Map workingValues) { 100 } 102 103 } 104 | Popular Tags |