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

(-)plugin.xml (-3 / +36 lines)
Lines 2-8 Link Here
2
<?eclipse version="3.0"?>
2
<?eclipse version="3.0"?>
3
<plugin>
3
<plugin>
4
    <extension-point id="taskEditorExtensions" name="Task Editor Extension" schema="schema/taskEditorExtensions.exsd"/>
4
    <extension-point id="taskEditorExtensions" name="Task Editor Extension" schema="schema/taskEditorExtensions.exsd"/>
5
5
    <extension-point id="resourceHyperlinkExtensions" name="Resource Hyperlink Extension" schema="schema/resourceHyperlinkExtensions.exsd"/>
6
    
6
	<extension point="org.eclipse.ui.views">
7
	<extension point="org.eclipse.ui.views">
7
     	<category name="Experimental" id="org.eclipse.mylyn.sandbox"/>
8
     	<category name="Experimental" id="org.eclipse.mylyn.sandbox"/>
8
    </extension>
9
    </extension>
Lines 404-411 Link Here
404
      </propertyTester>
405
      </propertyTester>
405
   </extension>
406
   </extension>
406
   
407
   
407
   
408
    <extension
408
   
409
         point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectors">
410
      <hyperlinkDetector
411
            class="org.eclipse.mylyn.internal.sandbox.ui.hyperlinks.ResourceHyperlinkDetector"
412
            id="org.eclipse.mylyn.sandbox.ui.hyperlinkDetector"
413
            name="Resource Hyperlink Detector"
414
            targetId="org.eclipse.ui.DefaultTextEditor">
415
      </hyperlinkDetector>
416
   </extension>
417
418
    <extension
419
          point="org.eclipse.mylyn.sandbox.ui.resourceHyperlinkExtensions">
420
       <resourceHyperlinkExtension
421
             class="org.eclipse.mylyn.internal.sandbox.ui.hyperlinks.JavaResourceHyperlinkExtension"
422
             fileType="java"
423
             generatedPrefix="java class">
424
       </resourceHyperlinkExtension>
425
      
426
    <!--  
427
       <resourceHyperlinkExtension
428
             class="org.eclipse.mylyn.internal.sandbox.ui.hyperlinks.CppResourceHyperlinkExtension"
429
             
430
             fileType="cpp"
431
             generatedPrefix="cpp class">
432
433
       </resourceHyperlinkExtension>
434
       <resourceHyperlinkExtension
435
             class="org.eclipse.mylyn.internal.sandbox.ui.hyperlinks.UnknownResourceHyperlinkExtension"
436
             fileType="unknown"
437
             generatedPrefix="file">
438
       </resourceHyperlinkExtension>
439
     -->
440
    </extension>
441
409
   <!--
442
   <!--
410
   <extension point="org.eclipse.mylyn.tasks.ui.presentations">
443
   <extension point="org.eclipse.mylyn.tasks.ui.presentations">
411
      <presentation 
444
      <presentation 
(-)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 getReourceExpressions() {
25
		return JAVA_PREFIX + DEFAULT_QUALIFIED_NAME;
26
	}
27
28
	@Override
29
	public boolean resourceExists(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 getHyperlink(IRegion region, String resourceName) {
39
		return new JavaResourceHyperlink(region, resourceName);
40
	}
41
}
(-)src/org/eclipse/mylyn/internal/sandbox/ui/hyperlinks/AbstractResourceHyperlinkExtension.java (+107 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
 * IResourceHyperlinkExtension with various helper methods
21
 * 
22
 * @author Jingwen Ou
23
 */
24
public abstract class AbstractResourceHyperlinkExtension implements IResourceHyperlinkExtension {
25
26
	protected static final String DEFAULT_QUALIFIED_NAME = "((\\w(\\w|\\.)*\\w)|\\w)";
27
28
	/**
29
	 * detailed work done to extract a hyperlink
30
	 */
31
	protected IHyperlink extractHyperlink(int regionOffset, Matcher m) {
32
		int start = -1;
33
34
		start = m.start();
35
36
		int end = m.end();
37
38
		if (end == -1) {
39
			end = m.group().length();
40
		}
41
42
		try {
43
			start += regionOffset;
44
			end += regionOffset;
45
			String resourceName = extractResourceName(m.group());
46
47
			IRegion region = new Region(start, end - start);
48
			return getHyperlink(region, resourceName);
49
50
		} catch (NumberFormatException e) {
51
			return null;
52
		}
53
	}
54
55
	/**
56
	 * return correspondent IHyperlink, subclasses should implement
57
	 */
58
	protected abstract IHyperlink getHyperlink(IRegion region, String resourceName);
59
60
	/**
61
	 * extract the resource name for the matched text, can be overridden by subclasses
62
	 */
63
	protected String extractResourceName(String matchedText) {
64
		return matchedText.substring(matchedText.lastIndexOf(" ") + 1);
65
	}
66
67
	public IHyperlink[] findHyperlink(String text, int lineOffset, int regionOffset) {
68
		ArrayList<IHyperlink> hyperlinksFound = new ArrayList<IHyperlink>();
69
70
		Matcher m = getMatcherFor(text);
71
		while (m.find()) {
72
			if (lineOffset >= m.start() && lineOffset < m.end()) {
73
				// ignore when resource does not exist
74
				if (!resourceExists(extractResourceName(m.group()))) {
75
					continue;
76
				}
77
78
				IHyperlink link = extractHyperlink(regionOffset, m);
79
				if (link != null) {
80
					hyperlinksFound.add(link);
81
				}
82
			}
83
		}
84
85
		if (hyperlinksFound.size() > 0) {
86
			return hyperlinksFound.toArray(new IHyperlink[1]);
87
		}
88
		return null;
89
	}
90
91
	/**
92
	 * get the Matcher for the to-be-matched text, can be overridden by subclasses
93
	 */
94
	protected Matcher getMatcherFor(String text) {
95
		return Pattern.compile(getReourceExpressions(), Pattern.CASE_INSENSITIVE).matcher(text);
96
	}
97
98
	/**
99
	 * get the resource's regular expressions, subclasses should implement
100
	 */
101
	protected abstract String getReourceExpressions();
102
103
	/**
104
	 * whether the resource exists for a certain resource name, subclasses should implement
105
	 */
106
	public abstract boolean resourceExists(String resourceName);
107
}
(-)src/org/eclipse/mylyn/internal/sandbox/ui/hyperlinks/ResourceHyperlinkDetector.java (+68 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.Arrays;
13
import java.util.List;
14
15
import org.eclipse.jface.text.BadLocationException;
16
import org.eclipse.jface.text.IDocument;
17
import org.eclipse.jface.text.IRegion;
18
import org.eclipse.jface.text.ITextViewer;
19
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
20
import org.eclipse.jface.text.hyperlink.IHyperlink;
21
22
/**
23
 * Resource hyperlink detector, detecting format like < prefix > < qualified name >, examples are
24
 * 
25
 * See java class foo.bar.Baz, it could be related; See cpp class Foo, it could be related; See file foo.txt, I left
26
 * some notes there; See task 123, I put a comment there
27
 * 
28
 * @author Jingwen Ou
29
 */
30
public class ResourceHyperlinkDetector extends AbstractHyperlinkDetector {
31
32
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
33
		try {
34
			IDocument document = textViewer.getDocument();
35
			if (document == null) {
36
				return null;
37
			}
38
			int offset = region.getOffset();
39
40
			IRegion lineInfo = document.getLineInformationOfOffset(offset);
41
			String line = document.get(lineInfo.getOffset(), lineInfo.getLength());
42
			if (line.length() == 0) {
43
				return null;
44
			}
45
46
			List<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
47
			detectHyperlinks(line, region.getOffset() - lineInfo.getOffset(), lineInfo.getOffset(), hyperlinks);
48
49
			if (hyperlinks.isEmpty()) {
50
				return null;
51
			}
52
			return hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
53
54
		} catch (BadLocationException e) {
55
			return null;
56
		}
57
	}
58
59
	private void detectHyperlinks(String line, int lineOffset, int regionOffset, List<IHyperlink> hyperlinks) {
60
		for (IResourceHyperlinkExtension resourceHyperlinkExtension : ResourceHyperlinkExtensions.getResourceHyperlinkExtensions()) {
61
			IHyperlink[] links = resourceHyperlinkExtension.findHyperlink(line, lineOffset, regionOffset);
62
			if (links == null) {
63
				continue;
64
			}
65
			hyperlinks.addAll(Arrays.asList(links));
66
		}
67
	}
68
}

Return to bug 240441