KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > JavaStatusContextViewer


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.refactoring;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.core.resources.IFile;
16
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.ISourceRange;
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.resource.JFaceResources;
29
30 import org.eclipse.jface.text.Document;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.jface.text.Region;
34 import org.eclipse.jface.text.source.SourceViewer;
35
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.part.FileEditorInput;
38 import org.eclipse.ui.texteditor.IDocumentProvider;
39
40 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
41 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
42 import org.eclipse.jdt.internal.corext.util.Messages;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.javaeditor.InternalClassFileEditorInput;
46 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
47
48 import org.eclipse.jdt.ui.PreferenceConstants;
49 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
50 import org.eclipse.jdt.ui.text.JavaTextTools;
51
52 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
53 import org.eclipse.ltk.ui.refactoring.TextStatusContextViewer;
54
55
56 public class JavaStatusContextViewer extends TextStatusContextViewer {
57
58     /* (non-Javadoc)
59      * @see org.eclipse.jdt.internal.ui.refactoring.IStatusContextViewer#createControl(org.eclipse.swt.widgets.Composite)
60      */

61     public void createControl(Composite parent) {
62         super.createControl(parent);
63         final SourceViewer viewer= getSourceViewer();
64         viewer.unconfigure();
65         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
66         viewer.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
67         viewer.getControl().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
68     }
69     
70     protected SourceViewer createSourceViewer(Composite parent) {
71         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
72         return new JavaSourceViewer(parent, null, null, false, SWT.LEFT_TO_RIGHT | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION, store);
73     }
74
75     private IPackageFragmentRoot getPackageFragmentRoot(IClassFile file) {
76
77         IJavaElement element= file.getParent();
78         while (element != null && element.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT)
79             element= element.getParent();
80
81         return (IPackageFragmentRoot) element;
82     }
83
84     public void setInput(RefactoringStatusContext context) {
85         if (context instanceof JavaStatusContext) {
86             JavaStatusContext jsc= (JavaStatusContext)context;
87             IDocument document= null;
88             if (jsc.isBinary()) {
89                 IClassFile file= jsc.getClassFile();
90                 IEditorInput editorInput= new InternalClassFileEditorInput(file);
91                 document= getDocument(JavaPlugin.getDefault().getClassFileDocumentProvider(), editorInput);
92                 if (document.getLength() == 0)
93                     document= new Document(Messages.format(RefactoringMessages.JavaStatusContextViewer_no_source_found0, getPackageFragmentRoot(file).getElementName()));
94                 updateTitle(file);
95             } else {
96                 ICompilationUnit cunit= jsc.getCompilationUnit();
97                 if (cunit.isWorkingCopy()) {
98                     try {
99                         document= newJavaDocument(cunit.getSource());
100                     } catch (JavaModelException e) {
101                         // document is null which is a valid input.
102
}
103                 } else {
104                     IEditorInput editorInput= new FileEditorInput((IFile)cunit.getResource());
105                     document= getDocument(JavaPlugin.getDefault().getCompilationUnitDocumentProvider(), editorInput);
106                 }
107                 if (document == null)
108                     document= new Document(RefactoringMessages.JavaStatusContextViewer_no_source_available);
109                 updateTitle(cunit);
110             }
111             setInput(document, createRegion(jsc.getSourceRange()));
112         } else if (context instanceof JavaStringStatusContext) {
113             updateTitle(null);
114             JavaStringStatusContext sc= (JavaStringStatusContext)context;
115             setInput(newJavaDocument(sc.getSource()), createRegion(sc.getSourceRange()));
116         }
117     }
118     
119     private IDocument newJavaDocument(String JavaDoc source) {
120         IDocument result= new Document(source);
121         JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
122         textTools.setupJavaDocumentPartitioner(result);
123         return result;
124     }
125     
126     private static IRegion createRegion(ISourceRange range) {
127         return new Region(range.getOffset(), range.getLength());
128     }
129     
130     private IDocument getDocument(IDocumentProvider provider, IEditorInput input) {
131         if (input == null)
132             return null;
133         IDocument result= null;
134         try {
135             provider.connect(input);
136             result= provider.getDocument(input);
137         } catch (CoreException e) {
138         } finally {
139             provider.disconnect(input);
140         }
141         return result;
142     }
143 }
144
Popular Tags