Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 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.findrefs;
  
 import static com.google.common.collect.Maps.*;
 
 import java.util.Map;
 
 import  org.eclipse.core.runtime.Assert;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.ui.IEditorInput;
 import  org.eclipse.ui.IEditorReference;
 import  org.eclipse.ui.IPageListener;
 import  org.eclipse.ui.IPartListener2;
 import  org.eclipse.ui.IStorageEditorInput;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.IWorkbenchPart;
 import  org.eclipse.ui.IWorkbenchPartReference;
 import  org.eclipse.ui.IWorkbenchWindow;
 
Tracks open document to allow easy read access to their already loaded resources. As the tracking is separated from the access, this avoids deadlocks resulting from the lock order inversion of the UI lock and the document lock.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class OpenDocumentTracker {
 
 	protected class PartListener implements IPartListener2 {
 		public void partOpened(IWorkbenchPartReference partRef) {
 			Pair<URIIXtextDocumententry = getEntry(partRef);
 			if (entry != null) {
 				synchronized () {
 					.put(partRefentry.getFirst());
 				}
 			}
 		}
 
 		public void partInputChanged(IWorkbenchPartReference partRef) {
 			Pair<URIIXtextDocumententry = getEntry(partRef);
 			if (entry != null) {
 				synchronized () {
 					URI oldURI = .remove(partRef);
 				}
 			}
 		}
 
 		public void partClosed(IWorkbenchPartReference partRef) {
 			Pair<URIIXtextDocumententry = getEntry(partRef);
 			if (entry != null) {
 				synchronized () {
 					.remove(partRef);
 				}
 			}
 		}
 
 		public void partVisible(IWorkbenchPartReference partRef) {
 		}
 
 		public void partHidden(IWorkbenchPartReference partRef) {
 		}
 
 		public void partDeactivated(IWorkbenchPartReference partRef) {
 		}
 
 		public void partBroughtToTop(IWorkbenchPartReference partRef) {
 		}
 
		public void partActivated(IWorkbenchPartReference partRef) {
		}
	}
	protected class PageListener implements IPageListener {
		public void pageOpened(IWorkbenchPage page) {
			page.addPartListener();
		}
		public void pageClosed(IWorkbenchPage page) {
			page.removePartListener();
		}
		public void pageActivated(IWorkbenchPage page) {
		}
	}
	private static final Logger LOG = Logger.getLogger(OpenDocumentTracker.class);
	private Map<IWorkbenchPartReference, URIpart2resourceURI = newHashMap();
	private IPageListener pageListener;
	private IPartListener2 partListener;
	@Inject(optional=true)
	protected void initialize(final IWorkbench workbench) {
		Assert.isNotNull(Display.getCurrent());
		for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
			window.addPageListener();
			for (IWorkbenchPage page : window.getPages()) {
				page.addPartListener();
				for (IEditorReference editorRef : page.getEditorReferences()) {
					Pair<URIIXtextDocumententry = getEntry(editorRef);
					if (entry != null) {
					}
				}
			}
		}
	}
	protected Pair<URIIXtextDocumentgetEntry(IWorkbenchPartReference partRef) {
		IWorkbenchPart part = partRef.getPart(false);
		if (part instanceof XtextEditor) {
			XtextEditor xtextEditor = (XtextEditorpart;
			URI resourceURI = getResourceURI(xtextEditor);
			IXtextDocument document = xtextEditor.getDocument();
			if (resourceURI != null && document != null)
				return Tuples.create(resourceURIdocument);
		}
		return null;
	}
	protected URI getResourceURI(XtextEditor editor) {
		IEditorInput editorInput = editor.getEditorInput();
		if (editorInput instanceof IStorageEditorInput) {
			try {
				return .getUri(((IStorageEditorInput) editorInput).getStorage());
catch (CoreException e) {
				.error("Error getting URI for storage"e);
			}
		}
		return null;
	}
	public IXtextDocument getOpenDocument(URI resourceURI) {
		synchronized () {
			return .get(resourceURI);
		}
	}
New to GrepCode? Check out our FAQ X