KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JDISourceViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
46  * A source viewer configured to display Java source. This
47  * viewer obeys the font and color preferences specified in
48  * the Java UI plugin.
49  */

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                     // ignore
67
}
68             }
69         });
70     }
71     
72     /**
73      * Updates the viewer's font to match the preferences.
74      */

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         // if all the preferences failed
95
applyFont(JFaceResources.getTextFont());
96     }
97     
98     /**
99      * Sets the current font.
100      *
101      * @param font the new font
102      */

103     private void setFont(Font font) {
104         fFont= font;
105     }
106     
107     /**
108      * Returns the current font.
109      *
110      * @return the current font
111      */

112     private Font getFont() {
113         return fFont;
114     }
115     
116     /**
117      * Sets the font for the given viewer sustaining selection and scroll position.
118      *
119      * @param font the font
120      */

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     /**
141      * Updates the given viewer's colors to match the preferences.
142      */

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     /**
168      * Creates a color from the information stored in the given preference store.
169      * Returns <code>null</code> if there is no such information available.
170      */

171     private Color createColor(IPreferenceStore store, String JavaDoc 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     /**
187      * Returns the current background color.
188      *
189      * @return the current background color
190      */

191     protected Color getBackgroundColor() {
192         return fBackgroundColor;
193     }
194
195     /**
196      * Sets the current background color.
197      *
198      * @param backgroundColor the new background color
199      */

200     protected void setBackgroundColor(Color backgroundColor) {
201         fBackgroundColor = backgroundColor;
202     }
203
204     /**
205      * Returns the current foreground color.
206      *
207      * @return the current foreground color
208      */

209     protected Color getForegroundColor() {
210         return fForegroundColor;
211     }
212
213     /**
214      * Sets the current foreground color.
215      *
216      * @param foregroundColor the new foreground color
217      */

218     protected void setForegroundColor(Color foregroundColor) {
219         fForegroundColor = foregroundColor;
220     }
221     
222     /**
223      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
224      */

225     public void propertyChange(PropertyChangeEvent event) {
226         IContentAssistant assistant= getContentAssistant();
227         if (assistant instanceof ContentAssistant) {
228             JDIContentAssistPreference.changeConfiguration((ContentAssistant) assistant, event);
229         }
230         String JavaDoc 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     /**
248      * Returns the current content assistant.
249      *
250      * @return the current content assistant
251      */

252     public IContentAssistant getContentAssistant() {
253         return fContentAssistant;
254     }
255     
256     /**
257      * Returns a segmentation of the line of the given document appropriate for bidi rendering.
258      * The default implementation returns only the string literals of a Java code line as segments.
259      *
260      * @param document the document
261      * @param lineOffset the offset of the line
262      * @return the line's bidi segmentation
263      * @throws BadLocationException in case lineOffset is not valid in document
264      */

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 JavaDoc segmentation= new ArrayList JavaDoc();
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     /**
313      * Disposes the system resources currently in use by this viewer.
314      */

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     /* (non-Javadoc)
335      * @see org.eclipse.jface.text.source.SourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
336      */

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     /**
353      * Returns the preference store used to configure this source viewer or
354      * <code>null</code> if none;
355      */

356     private IPreferenceStore getPreferenceStore() {
357         return fStore;
358     }
359     
360 }
361
Popular Tags