KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaTextViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.compare;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.resource.JFaceResources;
22
23 import org.eclipse.jface.text.Document;
24 import org.eclipse.jface.text.source.SourceViewer;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.Viewer;
27
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
29 import org.eclipse.jdt.ui.text.JavaTextTools;
30 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32
33 import org.eclipse.compare.IStreamContentAccessor;
34
35
36 public class JavaTextViewer extends Viewer {
37         
38     private SourceViewer fSourceViewer;
39     private Object JavaDoc fInput;
40     
41     
42     JavaTextViewer(Composite parent) {
43         fSourceViewer= new SourceViewer(parent, null, SWT.LEFT_TO_RIGHT | SWT.H_SCROLL | SWT.V_SCROLL);
44         JavaTextTools tools= JavaCompareUtilities.getJavaTextTools();
45         if (tools != null) {
46             IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
47             fSourceViewer.configure(new JavaSourceViewerConfiguration(tools.getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING));
48         }
49
50         fSourceViewer.setEditable(false);
51         
52         String JavaDoc symbolicFontName= JavaMergeViewer.class.getName();
53         Font font= JFaceResources.getFont(symbolicFontName);
54         if (font != null)
55             fSourceViewer.getTextWidget().setFont(font);
56         
57     }
58         
59     public Control getControl() {
60         return fSourceViewer.getControl();
61     }
62     
63     public void setInput(Object JavaDoc input) {
64         if (input instanceof IStreamContentAccessor) {
65             Document document= new Document(getString(input));
66             JavaCompareUtilities.setupDocument(document);
67             fSourceViewer.setDocument(document);
68         }
69         fInput= input;
70     }
71     
72     public Object JavaDoc getInput() {
73         return fInput;
74     }
75     
76     public ISelection getSelection() {
77         return null;
78     }
79     
80     public void setSelection(ISelection s, boolean reveal) {
81     }
82     
83     public void refresh() {
84     }
85     
86     /**
87      * A helper method to retrieve the contents of the given object
88      * if it implements the IStreamContentAccessor interface.
89      */

90     private static String JavaDoc getString(Object JavaDoc input) {
91         
92         if (input instanceof IStreamContentAccessor) {
93             IStreamContentAccessor sca= (IStreamContentAccessor) input;
94             try {
95                 return JavaCompareUtilities.readString(sca);
96             } catch (CoreException ex) {
97                 JavaPlugin.log(ex);
98             }
99         }
100         return ""; //$NON-NLS-1$
101
}
102 }
103
Popular Tags