Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Christoph Kulla All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Christoph Kulla - Initial API and implementation /
 
 package org.eclipse.xtext.ui.editor.hover.html;
 
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
 
 import  org.eclipse.jface.viewers.ILabelProvider;
 import  org.eclipse.swt.browser.LocationAdapter;
 import  org.eclipse.swt.browser.LocationEvent;
 import  org.eclipse.swt.browser.LocationListener;
 import  org.eclipse.swt.widgets.Display;
 
 
 // Clone of org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks
 // Removed all java specific parts
 

Author(s):
Christoph Kulla - Initial contribution and API
 
 public class XtextElementLinks {

A handler is asked to handle links to targets.

See also:
XtextElementLinks.createLocationListener(XtextElementLinks.ILinkHandler)
 
 	public interface ILinkHandler {

Handle normal kind of link to given target.

Parameters:
uri the target to show
 
Handle link to given target to open in Xtextdoc view.

Parameters:
uri the target to show
 
Handle link to given target to open its declaration

Parameters:
uri the target to show
 
Handle link to given link to open in external browser

Parameters:
url the url to show
display the current display
Returns:
true if the handler could open the link false if the browser should follow the link
 
 		boolean handleExternalLink(URL url, Display display);

Informs the handler that the text of the browser was set.
 
 		void handleTextSet();
 	}	
 	
 	ILabelProvider labelProvider;
 	
 	private static final Logger log = Logger.getLogger(XtextElementLinks.class);
 	
 	// should be all lower case (Internet Explorer issue)
 	public static final String OPEN_LINK_SCHEME"eclipse-xtext-open"//$NON-NLS-1$
 	public static final String XTEXTDOC_SCHEME"eclipse-xtext-doc"//$NON-NLS-1$
 	public static final String XTEXTDOC_VIEW_SCHEME"eclipse-xtext-doc-view"//$NON-NLS-1$
 	
 	public LocationListener createLocationListener(final ILinkHandler handler) {
 		return new XtextLinkAdapter(handler);
 	}
 
 	public String createLink (EObject o) {
 		return createLink (o);
 	}
 
 	public String createLink (String schemeEObject o) {
		return createLink (schemeo.getText (o));
	}
	public String createLink (String schemeEObject oString elementName) {
		try {
			return createLink (createURI (schemeo), elementName);
catch (URISyntaxException e) {
			.warn("Could not create Link for "+elementNamee);
		return elementName;
	}	
	public String createLink (String uriString elementName) {
		return "<a class='header' href='" + uri + ("'>" + elementName + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
	public String createURI(String schemeEObject othrows URISyntaxException {
		return new URI (schemecreateURI(o).toString(), null).toASCIIString();
	}
		return org.eclipse.emf.common.util.URI.createURI(uri.getSchemeSpecificPart());
	}
	}

Since:
2.3
	protected class XtextLinkAdapter extends LocationAdapter {
		protected ILinkHandler handler;
		protected XtextLinkAdapter(ILinkHandler handler) {
			this. = handler;
		}
		public void changing(LocationEvent event) {
			String locevent.location;
			URI uri = initURI(event);
			if(uri == null)
				return;
			String schemeuri.getScheme();
				org.eclipse.emf.common.util.URI linkTargetparseURI(uri);
				if (linkTarget == null)
					return;
else if (..equals(scheme)) {
				org.eclipse.emf.common.util.URI linkTargetparseURI(uri);
				if (linkTarget == null)
					return;
else if (..equals(scheme)) {
				org.eclipse.emf.common.util.URI linkTargetparseURI(uri);
				if (linkTarget == null)
					return;
else {
				try {
					if (.handleExternalLink(new URL(loc), event.display))
						return;
					event.doit= true;
catch (MalformedURLException e) {
					.warn("Could not handle location"+loce);
				}
			}
		}
		protected URI initURI(LocationEvent event){
			String locevent.location;
			if ("about:blank".equals(loc)) { //$NON-NLS-1$
				/*
				 * Using the Browser.setText API triggers a location change to "about:blank".
				 * remove this code once https://bugs.eclipse.org/bugs/show_bug.cgi?id=130314 is fixed
				 */
				//input set with setText
				return null;
			}
			event.doit= false;
			if (loc.startsWith("about:")) { //$NON-NLS-1$
				// Relative links should be handled via head > base tag.
				// If no base is available, links just won't work.
				return null;
			}
			URI uri;
			try {
				urinew URI(loc);
catch (URISyntaxException e) {
				// try it with a file (workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=237903 ):
				File filenew File(loc);
				if (! file.exists()) {
					.warn("Could not handle location"+loce);
					return null;
				}
				urifile.toURI();
			}
			return uri;
		}
	}
New to GrepCode? Check out our FAQ X