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.IProject;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.Status;
 import  org.eclipse.core.runtime.jobs.Job;
 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.ide.IDE;
 

Author(s):
Jan Kļæ½hnlein - Initial contribution and API
Sebastian Zarnekow - original coding
Peter Friese
 
 public class LanguageSpecificURIEditorOpener implements IURIEditorOpener {
 
 	private static final Logger logger = Logger.getLogger(LanguageSpecificURIEditorOpener.class);
 
 
 
 
 	@Inject(optional = true)
 	private IWorkbench workbench;
 
 	public void setLocationProvider(ILocationInFileProvider locationProvider) {
 		this. = locationProvider;
 	}
 
 		return ;
 	}
 
 	public IEditorPart open(URI uriboolean select) {
 		return open(urinull, -1, select);
 	}
 
 	public IEditorPart open(final URI urifinal EReference crossReferencefinal int indexInListfinal boolean select) {
 		Iterator<Pair<IStorage, IProject>> storages = .getStorages(uri.trimFragment()).iterator();
 		if (storages != null && storages.hasNext()) {
 			try {
 				IStorage storage = storages.next().getFirst();
 				IEditorInput editorInput = EditorUtils.createEditorInput(storage);
 				IWorkbenchPage activePage = .getActiveWorkbenchWindow().getActivePage();
 				final IEditorPart editor = IDE.openEditor(activePageeditorInputgetEditorId());
 				
 				Job selectAndRevealJob = new Job("Select and reveal referenced object") {
 					protected IStatus run(IProgressMonitor monitor) {
 						selectAndReveal(editoruricrossReferenceindexInListselect);
 						return Status.OK_STATUS;
 					}
 				};
 				selectAndRevealJob.setSystem(true);
 				selectAndRevealJob.setPriority(Job.SHORT);
 				selectAndRevealJob.schedule();
 				
 				return EditorUtils.getXtextEditor(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;
	}

Since:
2.8
	protected String getEditorId() {
	}
	protected void selectAndReveal(IEditorPart openEditorfinal URI urifinal EReference crossReference,
			final int indexInListfinal boolean select) {
		final XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor);
		if (xtextEditor != null) {
			boolean success = false;
			int tries = 0;
			while (!success || tries >= 5) {
				try {
						public void process(XtextResource resourcethrows Exception {
							if (resource != null) {
								EObject object = findEObjectByURI(uriresource);
								if (object != null) {
									final ITextRegion location = (crossReference != null) ? 
											.getSignificantTextRegion(objectcrossReferenceindexInList)
									if (select) {
										.getDisplay().asyncExec(new Runnable() {
											public void run() {
												xtextEditor.selectAndReveal(location.getOffset(), location.getLength());
											}
										});
									}
								}
							}
						}
					});
					success = true;
catch (OperationCanceledException e) {
catch (OperationCanceledError e) {
finally {
					tries++;
				}
			}
		}
	}


Since:
2.2
	protected EObject findEObjectByURI(final URI uriXtextResource resource) {
		if (uri.fragment() != null){
			try {
				EObject result = resource.getEObject(uri.fragment());
				return result;
catch (WrappedException e){
				.error("Error while resolving EObject with URI '" + uri + "'" , e.getCause());
			}
		}
		return null;
	}
New to GrepCode? Check out our FAQ X