KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > console > FileLink


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.debug.ui.console;
12
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.content.IContentDescription;
17 import org.eclipse.core.runtime.content.IContentType;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.ui.IEditorDescriptor;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorRegistry;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.part.FileEditorInput;
31 import org.eclipse.ui.texteditor.IDocumentProvider;
32 import org.eclipse.ui.texteditor.ITextEditor;
33
34 /**
35  * A hyperlink that opens a file in a text editor and selects a range of text.
36  * <p>
37  * This class is not intended to be subclassed; clients may instantiate this
38  * class.
39  * </p>
40  * @since 2.1
41  */

42 public class FileLink implements IConsoleHyperlink {
43
44     private IFile fFile;
45     private int fFileOffset;
46     private int fFileLength;
47     private int fFileLineNumber;
48     private String JavaDoc fEditorId;
49     
50     /**
51      * Constructs a hyperlink to the specified file.
52      *
53      * @param file the file to open when activated
54      * @param editorId the identifier of the editor to open the file in, or
55      * <code>null</code> if the default editor should be used
56      * @param fileOffset the offset in the file to select when activated, or -1
57      * @param fileLength the length of text to select in the file when activated
58      * or -1
59      * @param fileLineNumber the line number to select in the file when
60      * activated, or -1
61      */

62     public FileLink(IFile file, String JavaDoc editorId, int fileOffset, int fileLength, int fileLineNumber) {
63         fFile = file;
64         fFileOffset = fileOffset;
65         fFileLength = fileLength;
66         fFileLineNumber = fileLineNumber;
67         fEditorId = editorId;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
72      */

73     public void linkActivated() {
74         IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow();
75         if (window != null) {
76             IWorkbenchPage page = window.getActivePage();
77             if (page != null) {
78                 try {
79                     IEditorPart editorPart = page.openEditor(new FileEditorInput(fFile), getEditorId() , false);
80                     if (fFileLineNumber > 0) {
81                         ITextEditor textEditor = null;
82                         if (editorPart instanceof ITextEditor) {
83                             textEditor = (ITextEditor) editorPart;
84                         } else {
85                             textEditor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
86                         }
87                         if (textEditor != null) {
88                             IEditorInput input = editorPart.getEditorInput();
89                             if (fFileOffset < 0) {
90                                 IDocumentProvider provider = textEditor.getDocumentProvider();
91                                 try {
92                                     provider.connect(input);
93                                 } catch (CoreException e) {
94                                     // unable to link
95
DebugUIPlugin.log(e);
96                                     return;
97                                 }
98                                 IDocument document = provider.getDocument(input);
99                                 try {
100                                     IRegion region= document.getLineInformation(fFileLineNumber - 1);
101                                     fFileOffset = region.getOffset();
102                                     fFileLength = region.getLength();
103                                 } catch (BadLocationException e) {
104                                     // unable to link
105
DebugUIPlugin.log(e);
106                                 }
107                                 provider.disconnect(input);
108                             }
109                             if (fFileOffset >= 0 && fFileLength >=0) {
110                                 textEditor.selectAndReveal(fFileOffset, fFileLength);
111                             }
112                         }
113                     }
114                 } catch (PartInitException e) {
115                     DebugUIPlugin.log(e);
116                 }
117             }
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkEntered()
123      */

124     public void linkEntered() {
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkExited()
129      */

130     public void linkExited() {
131     }
132     
133     private String JavaDoc getEditorId() {
134         if (fEditorId == null) {
135             IWorkbench workbench= DebugUIPlugin.getDefault().getWorkbench();
136             // If there is a registered editor for the file use it.
137
IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(fFile.getName(), getFileContentType());
138             if (desc == null) {
139                 //default editor
140
desc= workbench.getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
141             }
142             fEditorId= desc.getId();
143         }
144         return fEditorId;
145     }
146
147     private IContentType getFileContentType() {
148         try {
149             IContentDescription description= fFile.getContentDescription();
150             if (description != null) {
151                 return description.getContentType();
152             }
153         } catch (CoreException e) {
154         }
155         return null;
156     }
157 }
158
Popular Tags