Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. 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 /
  
  package org.eclipse.xtext.ui.editor;
  
 
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.ui.IEditorInput;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.PartInitException;
 import  org.eclipse.ui.PlatformUI;
 import  org.eclipse.ui.ide.IDE;
 

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class GlobalURIEditorOpener implements IURIEditorOpener {
 
 	private static final Logger logger = Logger.getLogger(GlobalURIEditorOpener.class);
 	
 	
 	
 	@Inject(optional=true
 	/* @Nullable */
 	private IWorkbench workbench;

Since:
2.2
 
 	public IEditorPart open(URI uriObject contextboolean select) {
 		return open(uriselect);
 	}
 	
 	public IEditorPart open(URI uriboolean select) {
 		if(resourceServiceProvider instanceof IResourceUIServiceProvider) {
 			return ((IResourceUIServiceProviderresourceServiceProvider).getURIEditorOpener().open(uriselect);
 		}
 		return openDefaultEditor(urinull, -1, select);
 	}
 
 	public IEditorPart open(URI referenceOwnerURIEReference referenceint indexInListboolean select) {
 		if(resourceServiceProvider instanceof IResourceUIServiceProvider) {
 			return ((IResourceUIServiceProviderresourceServiceProvider).getURIEditorOpener().open(referenceOwnerURIreferenceindexInListselect);
 		}
 		return openDefaultEditor(referenceOwnerURIreferenceindexInListselect);
 	}
 	
 	protected IEditorPart openDefaultEditor(URI uriEReference crossReferenceint indexInListboolean select) {
 		Iterator<Pair<IStorage, IProject>> storages = .getStorages(uri.trimFragment()).iterator();
 		if (storages != null && storages.hasNext()) {
 			try {
 				IStorage storage = storages.next().getFirst();
 				IEditorPart editor = null;
 				if (storage instanceof IFile) {
 					editor = openDefaultEditor((IFile) storage);
 				} else {
 					editor = openDefaultEditor(storageuri);
 				}
 				selectAndReveal(editoruricrossReferenceindexInListselect);
 				return editor;
 			} catch (WrappedException e) {
 				.error("Error while opening editor part for EMF URI '" + uri + "'"e.getCause());
 			} catch (PartInitException partInitException) {
 				.error("Error while opening editor part for EMF URI '" + uri + "'"partInitException);
 			}
 		}
		return null;
	}
	protected void selectAndReveal(IEditorPart openEditorfinal URI urifinal EReference crossReference,
			final int indexInListfinal boolean select) {
		final XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor);
		if (xtextEditor != null) {
			if (uri.fragment() != null) {
					public void process(XtextResource resourcethrows Exception {
						if (resource != null) {
							EObject object = resource.getEObject(uri.fragment());
							ITextRegion location = (crossReference != null) ? locationProvider.getSignificantTextRegion(object,
									crossReferenceindexInList) : locationProvider.getSignificantTextRegion(object);
							if (select) {
								xtextEditor.selectAndReveal(location.getOffset(), location.getLength());
else {
								xtextEditor.reveal(location.getOffset(), location.getLength());								
							}
						}
					}
				});
			}
		}
	}
	protected IEditorPart openDefaultEditor(IFile filethrows PartInitException {
		IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
		return IDE.openEditor(pagefile);
	}
	protected IEditorPart openDefaultEditor(IStorage storageURI urithrows PartInitException {
		IEditorInput editorInput = EditorUtils.createEditorInput(storage); 
		IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
		return IDE.openEditor(pageeditorInput, PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(
				uri.lastSegment()).getId());
	}
	protected IWorkbench getWorkbench() {
		if (==null)
			throw new IllegalStateException("No workbench");
		return ;
	}
New to GrepCode? Check out our FAQ X