1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 13 import java.util.Map ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.custom.StyledText; 17 import org.eclipse.swt.events.DisposeEvent; 18 import org.eclipse.swt.events.DisposeListener; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.graphics.RGB; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.jface.preference.PreferenceConverter; 26 import org.eclipse.jface.preference.PreferenceStore; 27 import org.eclipse.jface.resource.JFaceResources; 28 import org.eclipse.jface.util.IPropertyChangeListener; 29 import org.eclipse.jface.util.PropertyChangeEvent; 30 31 import org.eclipse.jface.text.Document; 32 import org.eclipse.jface.text.MarginPainter; 33 import org.eclipse.jface.text.WhitespaceCharacterPainter; 34 import org.eclipse.jface.text.source.SourceViewer; 35 36 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; 37 import org.eclipse.ui.texteditor.ChainedPreferenceStore; 38 39 import org.eclipse.jdt.core.JavaCore; 40 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 41 42 import org.eclipse.jdt.ui.PreferenceConstants; 43 import org.eclipse.jdt.ui.text.IJavaPartitions; 44 import org.eclipse.jdt.ui.text.JavaTextTools; 45 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; 48 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration; 49 50 51 public abstract class JavaPreview { 52 53 54 private final class JavaSourcePreviewerUpdater { 55 56 final IPropertyChangeListener fontListener= new IPropertyChangeListener() { 57 public void propertyChange(PropertyChangeEvent event) { 58 if (event.getProperty().equals(PreferenceConstants.EDITOR_TEXT_FONT)) { 59 final Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT); 60 fSourceViewer.getTextWidget().setFont(font); 61 if (fMarginPainter != null) { 62 fMarginPainter.initialize(); 63 } 64 } 65 } 66 }; 67 68 final IPropertyChangeListener propertyListener= new IPropertyChangeListener() { 69 public void propertyChange(PropertyChangeEvent event) { 70 if (fViewerConfiguration.affectsTextPresentation(event)) { 71 fViewerConfiguration.handlePropertyChangeEvent(event); 72 fSourceViewer.invalidateTextPresentation(); 73 } 74 } 75 }; 76 77 78 public JavaSourcePreviewerUpdater() { 79 80 JFaceResources.getFontRegistry().addListener(fontListener); 81 fPreferenceStore.addPropertyChangeListener(propertyListener); 82 83 fSourceViewer.getTextWidget().addDisposeListener(new DisposeListener() { 84 public void widgetDisposed(DisposeEvent e) { 85 JFaceResources.getFontRegistry().removeListener(fontListener); 86 fPreferenceStore.removePropertyChangeListener(propertyListener); 87 } 88 }); 89 } 90 } 91 92 protected final SimpleJavaSourceViewerConfiguration fViewerConfiguration; 93 protected final Document fPreviewDocument; 94 protected final SourceViewer fSourceViewer; 95 protected final IPreferenceStore fPreferenceStore; 96 97 protected final MarginPainter fMarginPainter; 98 99 protected Map fWorkingValues; 100 101 private int fTabSize= 0; 102 private WhitespaceCharacterPainter fWhitespaceCharacterPainter; 103 104 109 public JavaPreview(Map workingValues, Composite parent) { 110 JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools(); 111 fPreviewDocument= new Document(); 112 fWorkingValues= workingValues; 113 tools.setupJavaDocumentPartitioner( fPreviewDocument, IJavaPartitions.JAVA_PARTITIONING); 114 115 PreferenceStore prioritizedSettings= new PreferenceStore(); 116 prioritizedSettings.setValue(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); 117 prioritizedSettings.setValue(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); 118 prioritizedSettings.setValue(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); 119 prioritizedSettings.setValue(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR); 120 121 IPreferenceStore[] chain= { prioritizedSettings, JavaPlugin.getDefault().getCombinedPreferenceStore() }; 122 fPreferenceStore= new ChainedPreferenceStore(chain); 123 fSourceViewer= new JavaSourceViewer(parent, null, null, false, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER, fPreferenceStore); 124 fViewerConfiguration= new SimpleJavaSourceViewerConfiguration(tools.getColorManager(), fPreferenceStore, null, IJavaPartitions.JAVA_PARTITIONING, true); 125 fSourceViewer.configure(fViewerConfiguration); 126 fSourceViewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT)); 127 128 fMarginPainter= new MarginPainter(fSourceViewer); 129 final RGB rgb= PreferenceConverter.getColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR); 130 fMarginPainter.setMarginRulerColor(tools.getColorManager().getColor(rgb)); 131 fSourceViewer.addPainter(fMarginPainter); 132 133 new JavaSourcePreviewerUpdater(); 134 fSourceViewer.setDocument(fPreviewDocument); 135 } 136 137 public Control getControl() { 138 return fSourceViewer.getControl(); 139 } 140 141 142 public void update() { 143 if (fWorkingValues == null) { 144 fPreviewDocument.set(""); return; 146 } 147 148 final String value= (String )fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT); 150 final int lineWidth= getPositiveIntValue(value, 0); 151 fMarginPainter.setMarginRulerColumn(lineWidth); 152 153 final int tabSize= getPositiveIntValue((String ) fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE), 0); 155 if (tabSize != fTabSize) fSourceViewer.getTextWidget().setTabs(tabSize); 156 fTabSize= tabSize; 157 158 final StyledText widget= (StyledText)fSourceViewer.getControl(); 159 final int height= widget.getClientArea().height; 160 final int top0= widget.getTopPixel(); 161 162 final int totalPixels0= getHeightOfAllLines(widget); 163 final int topPixelRange0= totalPixels0 > height ? totalPixels0 - height : 0; 164 165 widget.setRedraw(false); 166 doFormatPreview(); 167 fSourceViewer.setSelection(null); 168 169 final int totalPixels1= getHeightOfAllLines(widget); 170 final int topPixelRange1= totalPixels1 > height ? totalPixels1 - height : 0; 171 172 final int top1= topPixelRange0 > 0 ? (int)(topPixelRange1 * top0 / (double)topPixelRange0) : 0; 173 widget.setTopPixel(top1); 174 widget.setRedraw(true); 175 } 176 177 private int getHeightOfAllLines(StyledText styledText) { 178 int height= 0; 179 int lineCount= styledText.getLineCount(); 180 for (int i= 0; i < lineCount; i++) 181 height= height + styledText.getLineHeight(styledText.getOffsetAtLine(i)); 182 return height; 183 } 184 185 protected abstract void doFormatPreview(); 186 187 188 private static int getPositiveIntValue(String string, int defaultValue) { 189 try { 190 int i= Integer.parseInt(string); 191 if (i >= 0) { 192 return i; 193 } 194 } catch (NumberFormatException e) { 195 } 196 return defaultValue; 197 } 198 199 200 201 public Map getWorkingValues() { 202 return fWorkingValues; 203 } 204 205 206 public void setWorkingValues(Map workingValues) { 207 fWorkingValues= workingValues; 208 } 209 210 public void showInvisibleCharacters(boolean enable) { 211 if (enable) { 212 if (fWhitespaceCharacterPainter == null) { 213 fWhitespaceCharacterPainter= new WhitespaceCharacterPainter(fSourceViewer); 214 fSourceViewer.addPainter(fWhitespaceCharacterPainter); 215 } 216 } else { 217 fSourceViewer.removePainter(fWhitespaceCharacterPainter); 218 fWhitespaceCharacterPainter= null; 219 } 220 update(); 221 } 222 } 223 | Popular Tags |