Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 240441 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/sandbox/ui/hyperlinks/JavaResourceHyperlink.java (+126 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2007 Mylyn project committers 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
9
package org.eclipse.mylyn.internal.sandbox.ui.hyperlinks;
10
11
import org.eclipse.core.runtime.CoreException;
12
import org.eclipse.core.runtime.IProgressMonitor;
13
import org.eclipse.core.runtime.IStatus;
14
import org.eclipse.core.runtime.Status;
15
import org.eclipse.core.runtime.jobs.Job;
16
import org.eclipse.debug.ui.IDebugModelPresentation;
17
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
18
import org.eclipse.jdt.internal.debug.ui.actions.OpenTypeAction;
19
import org.eclipse.jface.dialogs.MessageDialog;
20
import org.eclipse.jface.text.IRegion;
21
import org.eclipse.jface.text.hyperlink.IHyperlink;
22
import org.eclipse.ui.IEditorInput;
23
import org.eclipse.ui.PlatformUI;
24
import org.eclipse.ui.progress.UIJob;
25
26
/**
27
 * Truncated class of JavaStackTraceFileHyperlink, open a hyperlink using OpenTypeAction
28
 * 
29
 * @author Rob Elves
30
 * @author Jingwe Ou
31
 */
32
public class JavaResourceHyperlink implements IHyperlink {
33
34
	private final IRegion region;
35
36
	private final String typeName;
37
38
	public JavaResourceHyperlink(IRegion region, String typeName) {
39
		this.region = region;
40
		this.typeName = typeName;
41
	}
42
43
	public IRegion getHyperlinkRegion() {
44
		return region;
45
	}
46
47
	public String getHyperlinkText() {
48
		return "Open " + typeName;
49
	}
50
51
	public String getTypeLabel() {
52
		return null;
53
	}
54
55
	public void open() {
56
		startSourceSearch(typeName);
57
	}
58
59
	/**
60
	 * Starts a search for the type with the given name. Reports back to 'searchCompleted(...)'.
61
	 * 
62
	 * @param typeName
63
	 *            the type to search for
64
	 */
65
	protected void startSourceSearch(final String typeName) {
66
		Job search = new Job("Searching...") {
67
			@Override
68
			protected IStatus run(IProgressMonitor monitor) {
69
				try {
70
					// search for the type in the workspace
71
					Object result = OpenTypeAction.findTypeInWorkspace(typeName);
72
					searchCompleted(result, typeName, null);
73
				} catch (CoreException e) {
74
					searchCompleted(null, typeName, e.getStatus());
75
				}
76
				return Status.OK_STATUS;
77
			}
78
79
		};
80
		search.schedule();
81
	}
82
83
	protected void searchCompleted(final Object source, final String typeName, final IStatus status) {
84
		UIJob job = new UIJob("link search complete") { //$NON-NLS-1$
85
			@Override
86
			public IStatus runInUIThread(IProgressMonitor monitor) {
87
				if (source == null) {
88
					// did not find source
89
					MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
90
							"Open Type", "Type could not be located.");
91
				} else {
92
					processSearchResult(source, typeName);
93
				}
94
				return Status.OK_STATUS;
95
			}
96
		};
97
		job.setSystem(true);
98
		job.schedule();
99
	}
100
101
	/**
102
	 * The search succeeded with the given result
103
	 * 
104
	 * @param source
105
	 *            resolved source object for the search
106
	 * @param typeName
107
	 *            type name searched for
108
	 */
109
	protected void processSearchResult(Object source, String typeName) {
110
		IDebugModelPresentation presentation = JDIDebugUIPlugin.getDefault().getModelPresentation();
111
		IEditorInput editorInput = presentation.getEditorInput(source);
112
		if (editorInput != null) {
113
			String editorId = presentation.getEditorId(editorInput, source);
114
			if (editorId != null) {
115
				try {
116
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(editorInput,
117
							editorId);
118
119
				} catch (CoreException e) {
120
					MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
121
							"Open Type", "Failed to open type.");
122
				}
123
			}
124
		}
125
	}
126
}
(-)src/org/eclipse/mylyn/internal/sandbox/ui/hyperlinks/JavaResourceHyperlinkExtension.java (+41 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2007 Mylyn project committers 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
9
package org.eclipse.mylyn.internal.sandbox.ui.hyperlinks;
10
11
import org.eclipse.core.runtime.CoreException;
12
import org.eclipse.jdt.internal.debug.ui.actions.OpenTypeAction;
13
import org.eclipse.jface.text.IRegion;
14
import org.eclipse.jface.text.hyperlink.IHyperlink;
15
16
/**
17
 * @author Jingwen Ou
18
 */
19
public class JavaResourceHyperlinkExtension extends AbstractResourceHyperlinkExtension {
20
21
	private static final String JAVA_PREFIX = "java\\sclass\\s";
22
23
	@Override
24
	protected String getResourceExpressions() {
25
		return JAVA_PREFIX + DEFAULT_QUALIFIED_NAME;
26
	}
27
28
	@Override
29
	protected boolean isResourceExists(String resourceName) {
30
		try {
31
			return OpenTypeAction.findTypeInWorkspace(resourceName) != null;
32
		} catch (CoreException e) {
33
			return false;
34
		}
35
	}
36
37
	@Override
38
	protected IHyperlink createHyperlinkInstance(IRegion region, String resourceName) {
39
		return new JavaResourceHyperlink(region, resourceName);
40
	}
41
}
(-)src/org/eclipse/mylyn/internal/sandbox/ui/hyperlinks/AbstractResourceHyperlinkExtension.java (+130 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2007 Mylyn project committers 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
9
package org.eclipse.mylyn.internal.sandbox.ui.hyperlinks;
10
11
import java.util.ArrayList;
12
import java.util.regex.Matcher;
13
import java.util.regex.Pattern;
14
15
import org.eclipse.jface.text.IRegion;
16
import org.eclipse.jface.text.Region;
17
import org.eclipse.jface.text.hyperlink.IHyperlink;
18
19
/**
20
 * An IResourceHyperlinkExtension implementation with various helper methods. Subclasses may choose to extend it instead
21
 * of IResourceHyperlinkExtension.
22
 * 
23
 * It matches text that is in the form of <prefix> <qualified name>.
24
 * 
25
 * @author Jingwen Ou
26
 */
27
public abstract class AbstractResourceHyperlinkExtension implements IResourceHyperlinkExtension {
28
29
	/**
30
	 * A regular expression matching class name, e.g. org.eclipse.mylyn.ITask or foo. Subclasses may use it as a default
31
	 * qualified name.
32
	 */
33
	protected static final String DEFAULT_QUALIFIED_NAME = "((\\w(\\w|\\.)*\\w)|\\w)";
34
35
	/**
36
	 * Extracts a IHyperlink from current region offset.
37
	 */
38
	private IHyperlink extractHyperlink(int regionOffset, Matcher m) {
39
		int start = m.start();
40
41
		int end = m.end();
42
43
		if (end == -1) {
44
			end = m.group().length();
45
		}
46
47
		try {
48
			start += regionOffset;
49
			end += regionOffset;
50
			String resourceName = extractResourceName(m.group());
51
52
			IRegion region = new Region(start, end - start);
53
			return createHyperlinkInstance(region, resourceName);
54
55
		} catch (NumberFormatException e) {
56
			return null;
57
		}
58
	}
59
60
	/**
61
	 * Extracts the resource name from the matched text. Subclasses may choose to override it for customized name
62
	 * extracting mechanism. The default implementation is to extract the last block of the matched text, e.g. the
63
	 * "foo.bar" from "java class foo.bar".
64
	 * 
65
	 * @param matchedText
66
	 *            the matched text using the regular expression defined in getResourceExpressions
67
	 */
68
	protected String extractResourceName(String matchedText) {
69
		return matchedText.substring(matchedText.lastIndexOf(" ") + 1);
70
	}
71
72
	public IHyperlink[] findHyperlink(String text, int lineOffset, int regionOffset) {
73
		ArrayList<IHyperlink> hyperlinksFound = new ArrayList<IHyperlink>();
74
75
		Matcher m = getMatcherFor(text);
76
		while (m.find()) {
77
			if (lineOffset >= m.start() && lineOffset < m.end()) {
78
				// ignore when resource does not exist
79
				if (!isResourceExists(extractResourceName(m.group()))) {
80
					continue;
81
				}
82
83
				IHyperlink link = extractHyperlink(regionOffset, m);
84
				if (link != null) {
85
					hyperlinksFound.add(link);
86
				}
87
			}
88
		}
89
90
		if (hyperlinksFound.size() > 0) {
91
			return hyperlinksFound.toArray(new IHyperlink[1]);
92
		}
93
		return null;
94
	}
95
96
	/**
97
	 * Creates correspondent IHyperlink instance for this resource. Subclasses should override.
98
	 * 
99
	 * @param region
100
	 *            the region of the hyperlink
101
	 * @param resourceName
102
	 *            the found resource name
103
	 * 
104
	 * @return the correspondent IHyperlink instance for this resouce
105
	 */
106
	protected abstract IHyperlink createHyperlinkInstance(IRegion region, String resourceName);
107
108
	/**
109
	 * Gets the Matcher for the to-be-matched text. Default flag is Pattern.CASE_INSENSITIVE.
110
	 */
111
	private Matcher getMatcherFor(String text) {
112
		return Pattern.compile(getResourceExpressions(), Pattern.CASE_INSENSITIVE).matcher(text);
113
	}
114
115
	/**
116
	 * Gets the resource's regular expressions. Subclasses should override.
117
	 * 
118
	 * @return the regular expression of matching current resource. For example, matching "java class org.foo.bar".
119
	 */
120
	protected abstract String getResourceExpressions();
121
122
	/**
123
	 * Indicate whether the current resource exists. Subclasses should override.
124
	 * 
125
	 * @param resourceName
126
	 *            the name of the resource to be tested
127
	 * @return if the resource exists
128
	 */
129
	protected abstract boolean isResourceExists(String resourceName);
130
}

Return to bug 240441