[jbosstools-commits] JBoss Tools SVN: r39811 - in trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common: util and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Mar 23 20:06:05 EDT 2012


Author: scabanovich
Date: 2012-03-23 20:05:39 -0400 (Fri, 23 Mar 2012)
New Revision: 39811

Added:
   trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/DirtyEditorTracker.java
Modified:
   trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/CommonPlugin.java
   trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseUIUtil.java
Log:
JBIDE-11385
https://issues.jboss.org/browse/JBIDE-11385
Method getDirtyFiles() implemented through workbench/page/editor listener that collects all dirty editors.

Modified: trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/CommonPlugin.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/CommonPlugin.java	2012-03-23 23:01:40 UTC (rev 39810)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/CommonPlugin.java	2012-03-24 00:05:39 UTC (rev 39811)
@@ -18,11 +18,14 @@
 
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Platform;
-import org.jboss.tools.common.log.BasePlugin;
+import org.eclipse.swt.widgets.Display;
+import org.jboss.tools.common.log.BaseUIPlugin;
 import org.jboss.tools.common.log.IPluginLog;
+import org.jboss.tools.common.util.DirtyEditorTracker;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 
-public class CommonPlugin extends BasePlugin {
+public class CommonPlugin extends BaseUIPlugin {
 
 	public static final String PLUGIN_ID = "org.jboss.tools.common"; //$NON-NLS-1$
 	protected static CommonPlugin instance;
@@ -37,6 +40,14 @@
 	    return instance;
 	}
 
+    public void start(BundleContext context) throws Exception {
+        super.start(context);
+        Display.getDefault().asyncExec(new Runnable() {
+        	public void run() {
+        		DirtyEditorTracker.getInstance();
+        	}
+        });
+	}
 	/**
 	 * Gets message from plugin.properties
 	 * @param key

Added: trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/DirtyEditorTracker.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/DirtyEditorTracker.java	                        (rev 0)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/DirtyEditorTracker.java	2012-03-24 00:05:39 UTC (rev 39811)
@@ -0,0 +1,171 @@
+package org.jboss.tools.common.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IPageListener;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IPropertyListener;
+import org.eclipse.ui.IWindowListener;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.jboss.tools.common.CommonPlugin;
+
+public class DirtyEditorTracker implements IWindowListener, IPageListener, IPartListener, IPropertyListener {
+	private static DirtyEditorTracker INSTANCE;
+
+	private Set<IFile> dirtyFiles = new HashSet<IFile>();
+
+	private DirtyEditorTracker() {
+		init();
+	}
+
+	public static DirtyEditorTracker getInstance() {
+		if(INSTANCE == null) {
+			INSTANCE = new DirtyEditorTracker();
+		}
+		return INSTANCE;
+	}
+
+	private void init() {
+		IWorkbench workbench = CommonPlugin.getDefault().getWorkbench();
+		if(workbench != null) {
+			IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
+			for (IWorkbenchWindow window: windows) {
+				if(window.getShell() != null) {
+					IWorkbenchPage[] pages = window.getPages();
+					for (IWorkbenchPage page: pages) {
+						IEditorReference[] rs = page.getEditorReferences();
+						for (IEditorReference r: rs) {
+							IEditorPart part = r.getEditor(false);
+							if(part != null) {
+								update(part);
+								part.addPropertyListener(this);
+							}							
+						}
+						page.addPartListener(this);
+					}
+					window.addPageListener(this);
+				}
+			}
+			CommonPlugin.getDefault().getWorkbench().addWindowListener(this);
+		}
+	}
+
+	public Set<IFile> getDirtyFiles() {
+		Set<IFile> result = new HashSet<IFile>();
+		synchronized(this) {
+			result.addAll(dirtyFiles);
+		}
+		return result;
+	}
+
+	public synchronized boolean isDirty(IFile file) {
+		return dirtyFiles.contains(file);
+	}
+
+	@Override
+	public void partActivated(IWorkbenchPart part) {
+	}
+
+	@Override
+	public void partBroughtToTop(IWorkbenchPart part) {
+	}
+
+	@Override
+	public void partClosed(IWorkbenchPart part) {
+		if (part instanceof IEditorPart) {
+			editorClosed((IEditorPart)part);
+		}
+	}
+
+	@Override
+	public void partDeactivated(IWorkbenchPart part) {
+	}
+
+	@Override
+	public void partOpened(IWorkbenchPart part) {
+		if (part instanceof IEditorPart) {
+			editorOpened((IEditorPart)part);
+		}
+	}
+
+	@Override
+	public void pageActivated(IWorkbenchPage page) {
+	}
+
+	@Override
+	public void pageClosed(IWorkbenchPage page) {
+		page.removePartListener(this);
+	}
+
+	@Override
+	public void pageOpened(IWorkbenchPage page) {
+		page.addPartListener(this);
+	}
+
+	@Override
+	public void windowActivated(IWorkbenchWindow window) {
+	}
+
+	@Override
+	public void windowDeactivated(IWorkbenchWindow window) {
+	}
+
+	@Override
+	public void windowClosed(IWorkbenchWindow window) {
+		window.removePageListener(this);
+	}
+
+	@Override
+	public void windowOpened(IWorkbenchWindow window) {
+		window.addPageListener(this);
+	}
+
+	public void editorOpened(IEditorPart part) {
+		IEditorInput input = part.getEditorInput();
+		if(input instanceof IFileEditorInput) {
+			part.addPropertyListener(this);
+		}
+	}
+
+	public void editorClosed(IEditorPart part) {
+		part.removePropertyListener(this);
+	}
+
+	@Override
+	public void propertyChanged(Object source, int propId) {
+		if(propId == IEditorPart.PROP_DIRTY && source instanceof IEditorPart) {
+			IEditorPart part = (IEditorPart)source;
+			update(part);
+		}
+		
+	}
+
+	private void update(IEditorPart part) {
+		IEditorInput input = part.getEditorInput();
+		if(input instanceof IFileEditorInput) {
+			IFile f = ((IFileEditorInput)input).getFile();
+			update(f, part.isDirty());
+		}
+	}
+
+	private synchronized void update(IFile file, boolean isDirty) {
+		if(isDirty) {
+			if(!dirtyFiles.contains(file)) {
+				dirtyFiles.add(file);
+			}
+		} else {
+			if(dirtyFiles.contains(file)) {
+				dirtyFiles.remove(file);
+			}
+		}
+	}
+}


Property changes on: trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/DirtyEditorTracker.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseUIUtil.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseUIUtil.java	2012-03-23 23:01:40 UTC (rev 39810)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseUIUtil.java	2012-03-24 00:05:39 UTC (rev 39811)
@@ -102,38 +102,6 @@
 	 * @return
 	 */
 	public static Set<IFile> getDirtyFiles() {
-		final Set<IFile> dirtyFiles = new HashSet<IFile>();
-		IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
-		for (IWorkbenchWindow window : windows) {
-			final IWorkbenchPage page = window.getActivePage();
-			if (page != null) {
-				// If this method is invoked in non-UI thread then some editors may throw Invalid Thread Access exception.
-				// We use SafeRunnable as a workaround to avoid crashing. 
-				// See https://issues.jboss.org/browse/JBIDE-11385
-				SafeRunnable sr = new SafeRunnable() {
-					@Override
-					public void run() throws Exception {
-						IEditorPart[] editors = page.getDirtyEditors();
-						for (IEditorPart editor : editors) {
-							IEditorInput input = editor.getEditorInput();
-							if(input instanceof IFileEditorInput) {
-								IFile file = ((IFileEditorInput)input).getFile();
-								dirtyFiles.add(file);
-							}
-						}
-					}
-
-					/* (non-Javadoc)
-					 * @see org.eclipse.jface.util.SafeRunnable#handleException(java.lang.Throwable)
-					 */
-					@Override
-					public void handleException(Throwable e) {
-						CommonPlugin.getDefault().logError(e);
-					}
-				};
-				SafeRunnable.run(sr);
-			}
-		}
-		return dirtyFiles;
+		return DirtyEditorTracker.getInstance().getDirtyFiles();
 	}
 }
\ No newline at end of file



More information about the jbosstools-commits mailing list