01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
11:
12: import org.eclipse.core.resources.IFile;
13: import org.eclipse.core.resources.IProject;
14: import org.eclipse.core.resources.IResource;
15: import org.eclipse.core.resources.IWorkspace;
16: import org.eclipse.jdt.ui.IPackagesViewPart;
17: import org.eclipse.jdt.ui.JavaUI;
18: import org.eclipse.jface.text.IRegion;
19: import org.eclipse.pde.internal.ui.PDEPlugin;
20: import org.eclipse.swt.widgets.Display;
21: import org.eclipse.ui.PartInitException;
22: import org.eclipse.ui.ide.IDE;
23:
24: public class ResourceHyperlink extends AbstractHyperlink {
25:
26: private IResource fResource;
27:
28: public ResourceHyperlink(IRegion region, String element,
29: IResource res) {
30: super (region, element);
31: fResource = res;
32: }
33:
34: public void open() {
35: if (fResource == null)
36: return;
37:
38: IResource resource = processAbsolutePathes();
39: if (resource == null) {
40: if (fElement.indexOf("$nl$/") == 0) //$NON-NLS-1$
41: fElement = fElement.substring(5);
42: resource = fResource.getProject().findMember(fElement);
43: }
44: try {
45: if (resource instanceof IFile) {
46: IDE.openEditor(PDEPlugin.getActivePage(),
47: (IFile) resource, true);
48: } else if (resource != null) {
49: IPackagesViewPart part = (IPackagesViewPart) PDEPlugin
50: .getActivePage().showView(JavaUI.ID_PACKAGES);
51: part.selectAndReveal(resource);
52: } else {
53: Display.getDefault().beep();
54: }
55: } catch (PartInitException e) {
56: PDEPlugin.logException(e);
57: }
58: }
59:
60: /**
61: * @return
62: */
63: private IResource processAbsolutePathes() {
64: // Check to see if we got an absolute path
65: if (fElement.startsWith("/") == false) { //$NON-NLS-1$
66: // Not an absolute path
67: return null;
68: }
69: // Absolute path
70: // Format: /<project-name>/<path-to-file>
71: // Remove the '/'
72: String path = fElement.substring(1);
73: // Parse the project name from the path
74: int index = path.indexOf('/');
75: String projectName = path.substring(0, index);
76: // Ensure we have a valid index
77: if ((index + 1) >= path.length()) {
78: return null;
79: }
80: // Parse the file name from the path (skip the '/')
81: String fileName = path.substring(index + 1);
82: // Get the workspace
83: IWorkspace workspace = fResource.getWorkspace();
84: // Find the project
85: IProject project = workspace.getRoot().getProject(projectName);
86: // Ensure the project was found
87: if (project == null) {
88: return null;
89: }
90: // Find the file
91: return project.findMember(fileName);
92: }
93:
94: }
|