1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration; 18 import org.eclipse.jdt.ui.text.IJavaPartitions; 19 import org.eclipse.jface.preference.IPreferenceStore; 20 import org.eclipse.jface.preference.PreferenceConverter; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.jface.text.IRegion; 25 import org.eclipse.jface.text.ITypedRegion; 26 import org.eclipse.jface.text.contentassist.ContentAssistant; 27 import org.eclipse.jface.text.contentassist.IContentAssistant; 28 import org.eclipse.jface.text.source.IVerticalRuler; 29 import org.eclipse.jface.text.source.SourceViewer; 30 import org.eclipse.jface.text.source.SourceViewerConfiguration; 31 import org.eclipse.jface.util.IPropertyChangeListener; 32 import org.eclipse.jface.util.PropertyChangeEvent; 33 import org.eclipse.swt.custom.BidiSegmentEvent; 34 import org.eclipse.swt.custom.BidiSegmentListener; 35 import org.eclipse.swt.custom.StyledText; 36 import org.eclipse.swt.graphics.Color; 37 import org.eclipse.swt.graphics.Font; 38 import org.eclipse.swt.graphics.FontData; 39 import org.eclipse.swt.graphics.Point; 40 import org.eclipse.swt.graphics.RGB; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Display; 43 import org.eclipse.ui.texteditor.AbstractTextEditor; 44 45 50 public class JDISourceViewer extends SourceViewer implements IPropertyChangeListener { 51 52 private Font fFont; 53 private Color fBackgroundColor; 54 private Color fForegroundColor; 55 private IPreferenceStore fStore; 56 private DisplayViewerConfiguration fConfiguration; 57 58 public JDISourceViewer(Composite parent, IVerticalRuler ruler, int styles) { 59 super(parent, ruler, styles); 60 StyledText text= this.getTextWidget(); 61 text.addBidiSegmentListener(new BidiSegmentListener() { 62 public void lineGetSegments(BidiSegmentEvent event) { 63 try { 64 event.segments= getBidiLineSegments(event.lineOffset); 65 } catch (BadLocationException x) { 66 } 68 } 69 }); 70 } 71 72 75 private void updateViewerFont() { 76 IPreferenceStore store= getPreferenceStore(); 77 if (store != null) { 78 FontData data= null; 79 if (store.contains(JFaceResources.TEXT_FONT) && !store.isDefault(JFaceResources.TEXT_FONT)) { 80 data= PreferenceConverter.getFontData(store, JFaceResources.TEXT_FONT); 81 } else { 82 data= PreferenceConverter.getDefaultFontData(store, JFaceResources.TEXT_FONT); 83 } 84 if (data != null) { 85 Font font= new Font(getTextWidget().getDisplay(), data); 86 applyFont(font); 87 if (getFont() != null) { 88 getFont().dispose(); 89 } 90 setFont(font); 91 return; 92 } 93 } 94 applyFont(JFaceResources.getTextFont()); 96 } 97 98 103 private void setFont(Font font) { 104 fFont= font; 105 } 106 107 112 private Font getFont() { 113 return fFont; 114 } 115 116 121 private void applyFont(Font font) { 122 IDocument doc= getDocument(); 123 if (doc != null && doc.getLength() > 0) { 124 Point selection= getSelectedRange(); 125 int topIndex= getTopIndex(); 126 127 StyledText styledText= getTextWidget(); 128 styledText.setRedraw(false); 129 130 styledText.setFont(font); 131 setSelectedRange(selection.x , selection.y); 132 setTopIndex(topIndex); 133 134 styledText.setRedraw(true); 135 } else { 136 getTextWidget().setFont(font); 137 } 138 } 139 140 143 public void updateViewerColors() { 144 IPreferenceStore store= getPreferenceStore(); 145 if (store != null) { 146 StyledText styledText= getTextWidget(); 147 Color color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) 148 ? null 149 : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay()); 150 styledText.setForeground(color); 151 if (getForegroundColor() != null) { 152 getForegroundColor().dispose(); 153 } 154 setForegroundColor(color); 155 156 color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) 157 ? null 158 : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay()); 159 styledText.setBackground(color); 160 if (getBackgroundColor() != null) { 161 getBackgroundColor().dispose(); 162 } 163 setBackgroundColor(color); 164 } 165 } 166 167 171 private Color createColor(IPreferenceStore store, String key, Display display) { 172 RGB rgb= null; 173 if (store.contains(key)) { 174 if (store.isDefault(key)) { 175 rgb= PreferenceConverter.getDefaultColor(store, key); 176 } else { 177 rgb= PreferenceConverter.getColor(store, key); 178 } 179 if (rgb != null) { 180 return new Color(display, rgb); 181 } 182 } 183 return null; 184 } 185 186 191 protected Color getBackgroundColor() { 192 return fBackgroundColor; 193 } 194 195 200 protected void setBackgroundColor(Color backgroundColor) { 201 fBackgroundColor = backgroundColor; 202 } 203 204 209 protected Color getForegroundColor() { 210 return fForegroundColor; 211 } 212 213 218 protected void setForegroundColor(Color foregroundColor) { 219 fForegroundColor = foregroundColor; 220 } 221 222 225 public void propertyChange(PropertyChangeEvent event) { 226 IContentAssistant assistant= getContentAssistant(); 227 if (assistant instanceof ContentAssistant) { 228 JDIContentAssistPreference.changeConfiguration((ContentAssistant) assistant, event); 229 } 230 String property= event.getProperty(); 231 232 if (JFaceResources.TEXT_FONT.equals(property)) { 233 updateViewerFont(); 234 } 235 if (AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(property) || 236 AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)) { 237 updateViewerColors(); 238 } 239 if (fConfiguration != null) { 240 if (fConfiguration.affectsTextPresentation(event)) { 241 fConfiguration.handlePropertyChangeEvent(event); 242 invalidateTextPresentation(); 243 } 244 } 245 } 246 247 252 public IContentAssistant getContentAssistant() { 253 return fContentAssistant; 254 } 255 256 265 protected int[] getBidiLineSegments(int lineOffset) throws BadLocationException { 266 IDocument document= getDocument(); 267 if (document == null) { 268 return null; 269 } 270 IRegion line= document.getLineInformationOfOffset(lineOffset); 271 ITypedRegion[] linePartitioning= document.computePartitioning(lineOffset, line.getLength()); 272 273 List segmentation= new ArrayList (); 274 for (int i= 0; i < linePartitioning.length; i++) { 275 if (IJavaPartitions.JAVA_STRING.equals(linePartitioning[i].getType())) 276 segmentation.add(linePartitioning[i]); 277 } 278 279 280 if (segmentation.size() == 0) 281 return null; 282 283 int size= segmentation.size(); 284 int[] segments= new int[size * 2 + 1]; 285 286 int j= 0; 287 for (int i= 0; i < size; i++) { 288 ITypedRegion segment= (ITypedRegion) segmentation.get(i); 289 290 if (i == 0) 291 segments[j++]= 0; 292 293 int offset= segment.getOffset() - lineOffset; 294 if (offset > segments[j - 1]) 295 segments[j++]= offset; 296 297 if (offset + segment.getLength() >= line.getLength()) 298 break; 299 300 segments[j++]= offset + segment.getLength(); 301 } 302 303 if (j < segments.length) { 304 int[] result= new int[j]; 305 System.arraycopy(segments, 0, result, 0, j); 306 segments= result; 307 } 308 309 return segments; 310 } 311 312 315 public void dispose() { 316 if (getFont() != null) { 317 getFont().dispose(); 318 setFont(null); 319 } 320 if (getBackgroundColor() != null) { 321 getBackgroundColor().dispose(); 322 setBackgroundColor(null); 323 } 324 if (getForegroundColor() != null) { 325 getForegroundColor().dispose(); 326 setForegroundColor(null); 327 } 328 if (fStore != null) { 329 fStore.removePropertyChangeListener(this); 330 fStore = null; 331 } 332 } 333 334 337 public void configure(SourceViewerConfiguration configuration) { 338 super.configure(configuration); 339 if (fStore != null) { 340 fStore.removePropertyChangeListener(this); 341 fStore = null; 342 } 343 if (configuration instanceof DisplayViewerConfiguration) { 344 fConfiguration = (DisplayViewerConfiguration) configuration; 345 fStore = fConfiguration.getTextPreferenceStore(); 346 fStore.addPropertyChangeListener(this); 347 } 348 updateViewerFont(); 349 updateViewerColors(); 350 } 351 352 356 private IPreferenceStore getPreferenceStore() { 357 return fStore; 358 } 359 360 } 361 | Popular Tags |