KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.CoreException;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.Viewer;
24
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.source.SourceViewer;
27
28 import org.eclipse.compare.IStreamContentAccessor;
29
30 import org.eclipse.jdt.ui.text.JavaTextTools;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
34 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileSourceViewerConfiguration;
35
36 /**
37  * Properties file viewer.
38  *
39  * @since 3.1
40  */

41 public class PropertiesFileViewer extends Viewer {
42         
43     private SourceViewer fSourceViewer;
44     private Object JavaDoc fInput;
45     
46     
47     PropertiesFileViewer(Composite parent) {
48         fSourceViewer= new SourceViewer(parent, null, SWT.LEFT_TO_RIGHT | SWT.H_SCROLL | SWT.V_SCROLL);
49         JavaTextTools tools= JavaCompareUtilities.getJavaTextTools();
50         if (tools != null) {
51             IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
52             fSourceViewer.configure(new PropertiesFileSourceViewerConfiguration(tools.getColorManager(), store, null, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING));
53         }
54
55         fSourceViewer.setEditable(false);
56
57         String JavaDoc symbolicFontName= PropertiesFileMergeViewer.class.getName();
58         Font font= JFaceResources.getFont(symbolicFontName);
59         if (font != null)
60             fSourceViewer.getTextWidget().setFont(font);
61     }
62         
63     public Control getControl() {
64         return fSourceViewer.getControl();
65     }
66     
67     public void setInput(Object JavaDoc input) {
68         if (input instanceof IStreamContentAccessor) {
69             Document document= new Document(getString(input));
70             JavaCompareUtilities.setupPropertiesFileDocument(document);
71             fSourceViewer.setDocument(document);
72         }
73         fInput= input;
74     }
75     
76     public Object JavaDoc getInput() {
77         return fInput;
78     }
79     
80     public ISelection getSelection() {
81         return null;
82     }
83     
84     public void setSelection(ISelection s, boolean reveal) {
85     }
86     
87     public void refresh() {
88     }
89     
90     /**
91      * A helper method to retrieve the contents of the given object
92      * if it implements the IStreamContentAccessor interface.
93      */

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