浏览代码

Initialization does not work if an editor is already open :-/ Lazy initialization added.

paphko@gmail.com 14 年之前
父节点
当前提交
64f950a390

+ 34 - 2
de.itemis.xtext.utils/plugins/de.itemis.xtext.utils.jface/src/de/itemis/xtext/utils/jface/viewers/util/ActiveEditorTracker.java

@@ -57,7 +57,7 @@ public class ActiveEditorTracker implements IPageListener, IPartListener,
 	 * @return The last active editor in the current active workbench page.
 	 */
 	public static IEditorPart getLastActiveEditor() {
-		return INSTANCE.getEditorById(INSTANCE.lastActiveEditorId);
+		return INSTANCE.getLastActiveEditorInternal();
 	}
 
 	/**
@@ -134,6 +134,7 @@ public class ActiveEditorTracker implements IPageListener, IPartListener,
 
 	public void pageOpened(IWorkbenchPage page) {
 		// do nothing
+		System.out.println("PAGE OPENED: " + page);
 	}
 
 	public void partActivated(IWorkbenchPart part) {
@@ -172,6 +173,30 @@ public class ActiveEditorTracker implements IPageListener, IPartListener,
 		}
 	}
 
+	private IEditorPart getLastActiveEditorInternal() {
+		if (activePage == null) {
+			initialize(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+			if (activePage == null)
+				return null;
+		}
+		boolean updated = false;
+		if (lastActiveEditorId == null) {
+			final IEditorPart editor = activePage.getActiveEditor();
+			if (editor != null) {
+				setActiveEditor(editor);
+			}
+			updated = true;
+		}
+		IEditorPart editor = getEditorById(lastActiveEditorId);
+		if (editor == null && !updated) {
+			editor = activePage.getActiveEditor();
+			if (editor != null) {
+				setActiveEditor(editor);
+			}
+		}
+		return editor;
+	}
+
 	private IEditorPart getEditorById(String editorId) {
 		if (activePage == null || editorId == null)
 			return null;
@@ -240,7 +265,14 @@ public class ActiveEditorTracker implements IPageListener, IPartListener,
 			 */
 		}
 		this.workbenchWindow = window;
+		if (window == null)
+			return;
 		this.activePage = window.getActivePage();
+		final IEditorPart editor = this.activePage.getActiveEditor();
+		if (editor != null) {
+			lastActiveEditorId = checkEditorAndGetId(editor);
+			activeEditors.put(lastActiveEditorId, editor);
+		}
 		window.addPageListener(this);
 		window.getPartService().addPartListener(this);
 	}
@@ -256,4 +288,4 @@ public class ActiveEditorTracker implements IPageListener, IPartListener,
 	public void windowOpened(IWorkbenchWindow window) {
 		// not of interest
 	}
-}
+}