KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > FileStatusContextViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ltk.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.NullProgressMonitor;
16
17 import org.eclipse.core.resources.IFile;
18
19 import org.eclipse.core.filebuffers.FileBuffers;
20 import org.eclipse.core.filebuffers.ITextFileBuffer;
21 import org.eclipse.core.filebuffers.ITextFileBufferManager;
22 import org.eclipse.core.filebuffers.LocationKind;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26
27 import org.eclipse.jface.text.Document;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.IRegion;
30 import org.eclipse.jface.text.Region;
31 import org.eclipse.jface.text.source.SourceViewer;
32 import org.eclipse.jface.text.source.SourceViewerConfiguration;
33
34 import org.eclipse.ltk.core.refactoring.FileStatusContext;
35 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
36 import org.eclipse.ltk.ui.refactoring.TextStatusContextViewer;
37
38
39 public class FileStatusContextViewer extends TextStatusContextViewer {
40
41     public void createControl(Composite parent) {
42         super.createControl(parent);
43         getSourceViewer().configure(new SourceViewerConfiguration());
44     }
45     
46     public void setInput(RefactoringStatusContext context) {
47         FileStatusContext fc= (FileStatusContext)context;
48         IFile file= fc.getFile();
49         updateTitle(file);
50         IDocument document= getDocument(file);
51         IRegion region= fc.getTextRegion();
52         if (region != null && document.getLength() >= region.getOffset() + region.getLength())
53             setInput(document, region);
54         else {
55             setInput(document, new Region(0, 0));
56         }
57     }
58     
59     protected SourceViewer createSourceViewer(Composite parent) {
60         return new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
61     }
62     
63     private IDocument getDocument(IFile file) {
64         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
65         IPath path= file.getFullPath();
66         try {
67             try {
68                 manager.connect(path, LocationKind.IFILE, new NullProgressMonitor());
69                 ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE);
70                 if (buffer != null) {
71                     return buffer.getDocument();
72                 }
73             } finally {
74                 manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor());
75             }
76         } catch (CoreException e) {
77             RefactoringUIPlugin.log(e);
78         }
79         return new Document(RefactoringUIMessages.FileStatusContextViewer_error_reading_file);
80     }
81 }
82
Popular Tags