1 11 12 package org.eclipse.jdt.internal.ui.preferences.formatter; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.swt.widgets.Composite; 23 24 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 25 26 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 27 import org.eclipse.jdt.internal.ui.JavaPlugin; 28 29 30 31 public class SnippetPreview extends JavaPreview { 32 33 public final static class PreviewSnippet { 34 35 public String header; 36 public final String source; 37 public final int kind; 38 39 public PreviewSnippet(int kind, String source) { 40 this.kind= kind; 41 this.source= source; 42 } 43 } 44 45 private ArrayList fSnippets; 46 47 public SnippetPreview(Map workingValues, Composite parent) { 48 super(workingValues, parent); 49 fSnippets= new ArrayList (); 50 } 51 52 protected void doFormatPreview() { 53 if (fSnippets.isEmpty()) { 54 fPreviewDocument.set(""); return; 56 } 57 58 final String delimiter= "\n"; 61 final StringBuffer buffer= new StringBuffer (); 62 for (final Iterator iter= fSnippets.iterator(); iter.hasNext();) { 63 final PreviewSnippet snippet= (PreviewSnippet) iter.next(); 64 String formattedSource; 65 try { 66 formattedSource= CodeFormatterUtil.format(snippet.kind, snippet.source, 0, null, delimiter, fWorkingValues); 67 } catch (Exception e) { 68 final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, 69 FormatterMessages.JavaPreview_formatter_exception, e); 70 JavaPlugin.log(status); 71 continue; 72 } 73 buffer.append(delimiter); 74 buffer.append(formattedSource); 75 buffer.append(delimiter); 76 buffer.append(delimiter); 77 } 78 fPreviewDocument.set(buffer.toString()); 79 } 80 81 82 83 public void add(PreviewSnippet snippet) { 84 fSnippets.add(snippet); 85 } 86 87 public void remove(PreviewSnippet snippet) { 88 fSnippets.remove(snippet); 89 } 90 91 public void addAll(Collection snippets) { 92 fSnippets.addAll(snippets); 93 } 94 95 public void clear() { 96 fSnippets.clear(); 97 } 98 99 } 100 | Popular Tags |