KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > ExternalHyperlink


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ant.internal.ui;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.filesystem.EFS;
16 import org.eclipse.core.filesystem.IFileStore;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.console.IHyperlink;
26 import org.eclipse.ui.ide.FileStoreEditorInput;
27 import org.eclipse.ui.texteditor.IDocumentProvider;
28 import org.eclipse.ui.texteditor.ITextEditor;
29
30 public class ExternalHyperlink implements IHyperlink {
31
32     private File JavaDoc fFile;
33     private int fLineNumber;
34
35     public ExternalHyperlink(File JavaDoc file, int lineNumber) {
36         super();
37         fFile = file;
38         fLineNumber= lineNumber;
39     }
40
41     public void linkEntered() {
42     }
43
44     public void linkExited() {
45     }
46
47     public void linkActivated() {
48         IEditorInput input;
49         IFileStore fileStore;
50         try {
51             fileStore= EFS.getStore(fFile.toURI());
52             input = new FileStoreEditorInput(fileStore);
53         } catch (CoreException e) {
54             // unable to link
55
AntUIPlugin.log(e);
56             return;
57         }
58         
59         IWorkbenchPage activePage = AntUIPlugin.getActiveWorkbenchWindow().getActivePage();
60         try {
61             IEditorPart editorPart= activePage.openEditor(input, "org.eclipse.ant.ui.internal.editor.AntEditor", true); //$NON-NLS-1$
62
if (fLineNumber > 0 && editorPart instanceof ITextEditor) {
63                 ITextEditor textEditor = (ITextEditor)editorPart;
64                 
65                     IDocumentProvider provider = textEditor.getDocumentProvider();
66                     try {
67                         provider.connect(input);
68                     } catch (CoreException e) {
69                         // unable to link
70
AntUIPlugin.log(e);
71                         return;
72                     }
73                     IDocument document = provider.getDocument(input);
74                     try {
75                         IRegion lineRegion= document.getLineInformation(fLineNumber);
76                         textEditor.selectAndReveal(lineRegion.getOffset(), lineRegion.getLength());
77                     } catch (BadLocationException e) {
78                         // unable to link
79
AntUIPlugin.log(e);
80                     }
81                     provider.disconnect(input);
82                 }
83             
84         } catch (PartInitException e) {
85         }
86     }
87 }
Popular Tags