001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.core.resources.IFile;
015:
016: import org.eclipse.jdt.core.IClassFile;
017: import org.eclipse.jdt.core.ICompilationUnit;
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.IPackageFragmentRoot;
020: import org.eclipse.jdt.core.ISourceRange;
021: import org.eclipse.jdt.core.JavaModelException;
022:
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.widgets.Composite;
025:
026: import org.eclipse.jface.preference.IPreferenceStore;
027: import org.eclipse.jface.resource.JFaceResources;
028:
029: import org.eclipse.jface.text.Document;
030: import org.eclipse.jface.text.IDocument;
031: import org.eclipse.jface.text.IRegion;
032: import org.eclipse.jface.text.Region;
033: import org.eclipse.jface.text.source.SourceViewer;
034:
035: import org.eclipse.ui.IEditorInput;
036: import org.eclipse.ui.part.FileEditorInput;
037: import org.eclipse.ui.texteditor.IDocumentProvider;
038:
039: import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
040: import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
041: import org.eclipse.jdt.internal.corext.util.Messages;
042:
043: import org.eclipse.jdt.internal.ui.JavaPlugin;
044: import org.eclipse.jdt.internal.ui.javaeditor.InternalClassFileEditorInput;
045: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
046:
047: import org.eclipse.jdt.ui.PreferenceConstants;
048: import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
049: import org.eclipse.jdt.ui.text.JavaTextTools;
050:
051: import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
052: import org.eclipse.ltk.ui.refactoring.TextStatusContextViewer;
053:
054: public class JavaStatusContextViewer extends TextStatusContextViewer {
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.jdt.internal.ui.refactoring.IStatusContextViewer#createControl(org.eclipse.swt.widgets.Composite)
058: */
059: public void createControl(Composite parent) {
060: super .createControl(parent);
061: final SourceViewer viewer = getSourceViewer();
062: viewer.unconfigure();
063: IPreferenceStore store = JavaPlugin.getDefault()
064: .getCombinedPreferenceStore();
065: viewer.configure(new JavaSourceViewerConfiguration(JavaPlugin
066: .getDefault().getJavaTextTools().getColorManager(),
067: store, null, null));
068: viewer.getControl().setFont(
069: JFaceResources
070: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
071: }
072:
073: protected SourceViewer createSourceViewer(Composite parent) {
074: IPreferenceStore store = JavaPlugin.getDefault()
075: .getCombinedPreferenceStore();
076: return new JavaSourceViewer(parent, null, null, false,
077: SWT.LEFT_TO_RIGHT | SWT.V_SCROLL | SWT.H_SCROLL
078: | SWT.MULTI | SWT.FULL_SELECTION, store);
079: }
080:
081: private IPackageFragmentRoot getPackageFragmentRoot(IClassFile file) {
082:
083: IJavaElement element = file.getParent();
084: while (element != null
085: && element.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT)
086: element = element.getParent();
087:
088: return (IPackageFragmentRoot) element;
089: }
090:
091: public void setInput(RefactoringStatusContext context) {
092: if (context instanceof JavaStatusContext) {
093: JavaStatusContext jsc = (JavaStatusContext) context;
094: IDocument document = null;
095: if (jsc.isBinary()) {
096: IClassFile file = jsc.getClassFile();
097: IEditorInput editorInput = new InternalClassFileEditorInput(
098: file);
099: document = getDocument(JavaPlugin.getDefault()
100: .getClassFileDocumentProvider(), editorInput);
101: if (document.getLength() == 0)
102: document = new Document(
103: Messages
104: .format(
105: RefactoringMessages.JavaStatusContextViewer_no_source_found0,
106: getPackageFragmentRoot(file)
107: .getElementName()));
108: updateTitle(file);
109: } else {
110: ICompilationUnit cunit = jsc.getCompilationUnit();
111: if (cunit.isWorkingCopy()) {
112: try {
113: document = newJavaDocument(cunit.getSource());
114: } catch (JavaModelException e) {
115: // document is null which is a valid input.
116: }
117: } else {
118: IEditorInput editorInput = new FileEditorInput(
119: (IFile) cunit.getResource());
120: document = getDocument(JavaPlugin.getDefault()
121: .getCompilationUnitDocumentProvider(),
122: editorInput);
123: }
124: if (document == null)
125: document = new Document(
126: RefactoringMessages.JavaStatusContextViewer_no_source_available);
127: updateTitle(cunit);
128: }
129: setInput(document, createRegion(jsc.getSourceRange()));
130: } else if (context instanceof JavaStringStatusContext) {
131: updateTitle(null);
132: JavaStringStatusContext sc = (JavaStringStatusContext) context;
133: setInput(newJavaDocument(sc.getSource()), createRegion(sc
134: .getSourceRange()));
135: }
136: }
137:
138: private IDocument newJavaDocument(String source) {
139: IDocument result = new Document(source);
140: JavaTextTools textTools = JavaPlugin.getDefault()
141: .getJavaTextTools();
142: textTools.setupJavaDocumentPartitioner(result);
143: return result;
144: }
145:
146: private static IRegion createRegion(ISourceRange range) {
147: return new Region(range.getOffset(), range.getLength());
148: }
149:
150: private IDocument getDocument(IDocumentProvider provider,
151: IEditorInput input) {
152: if (input == null)
153: return null;
154: IDocument result = null;
155: try {
156: provider.connect(input);
157: result = provider.getDocument(input);
158: } catch (CoreException e) {
159: } finally {
160: provider.disconnect(input);
161: }
162: return result;
163: }
164: }
|