|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.internal.ide; |
| 13 |
|
| 14 |
import java.util.Collection; |
| 15 |
import java.util.HashSet; |
| 16 |
import java.util.Iterator; |
| 17 |
|
| 18 |
import org.eclipse.core.resources.IContainer; |
| 19 |
import org.eclipse.core.resources.IFile; |
| 20 |
import org.eclipse.core.resources.IResource; |
| 21 |
import org.eclipse.core.resources.IResourceChangeEvent; |
| 22 |
import org.eclipse.core.resources.IResourceChangeListener; |
| 23 |
import org.eclipse.core.resources.IResourceDelta; |
| 24 |
import org.eclipse.core.resources.IResourceDeltaVisitor; |
| 25 |
import org.eclipse.core.runtime.CoreException; |
| 26 |
import org.eclipse.ui.IEditorInput; |
| 27 |
import org.eclipse.ui.IEditorReference; |
| 28 |
import org.eclipse.ui.IFileEditorInput; |
| 29 |
import org.eclipse.ui.IWorkbenchPage; |
| 30 |
import org.eclipse.ui.IWorkbenchWindow; |
| 31 |
import org.eclipse.ui.PartInitException; |
| 32 |
import org.eclipse.ui.PlatformUI; |
| 33 |
import org.eclipse.ui.statushandlers.StatusManager; |
| 34 |
|
| 35 |
/** |
| 36 |
* IDEEditorUpdater is a class that updates editor references if there has been |
| 37 |
* a change to invalidate them in the model such as a deletion of thier input. |
| 38 |
* |
| 39 |
* @since 3.3 |
| 40 |
* |
| 41 |
*/ |
| 42 |
public class IDEEditorUpdater implements IResourceChangeListener { |
| 43 |
|
| 44 |
private Collection editorsToClose = new HashSet(); |
| 45 |
private Collection resources = new HashSet(); |
| 46 |
|
| 47 |
/* |
| 48 |
* (non-Javadoc) |
| 49 |
* |
| 50 |
* @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent) |
| 51 |
*/ |
| 52 |
public void resourceChanged(IResourceChangeEvent event) { |
| 53 |
if (event.getType() != IResourceChangeEvent.POST_CHANGE) |
| 54 |
return; |
| 55 |
|
| 56 |
addDeletedResources(event.getDelta(), resources); |
| 57 |
|
| 58 |
if (resources.isEmpty()) |
| 59 |
return; |
| 60 |
|
| 61 |
IWorkbenchWindow[] windows = PlatformUI.getWorkbench() |
| 62 |
.getWorkbenchWindows(); |
| 63 |
for (int i = 0; i < windows.length; i++) { |
| 64 |
IWorkbenchPage[] pages = windows[i].getPages(); |
| 65 |
for (int j = 0; j < pages.length; j++) { |
| 66 |
closeEditorReferences(resources, pages[i]); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
resources.clear(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the deleted resources for the delta. |
| 75 |
* |
| 76 |
* @param delta |
| 77 |
* the delta to check |
| 78 |
* @param resources |
| 79 |
* the Collection to add the resources to. |
| 80 |
*/ |
| 81 |
private void addDeletedResources(IResourceDelta delta, |
| 82 |
final Collection resources) { |
| 83 |
IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() { |
| 84 |
/* |
| 85 |
* (non-Javadoc) |
| 86 |
* |
| 87 |
* @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta) |
| 88 |
*/ |
| 89 |
public boolean visit(IResourceDelta delta) { |
| 90 |
switch (delta.getKind()) { |
| 91 |
case IResourceDelta.ADDED: |
| 92 |
return false; |
| 93 |
case IResourceDelta.REMOVED: |
| 94 |
IResource resource = delta.getResource(); |
| 95 |
resources.add(resource); |
| 96 |
return false; |
| 97 |
case IResourceDelta.CHANGED: |
| 98 |
// handle changed resource |
| 99 |
break; |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
}; |
| 104 |
try { |
| 105 |
delta.accept(visitor); |
| 106 |
} catch (CoreException e) { |
| 107 |
StatusManager.getManager().addLoggedStatus(e.getStatus()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Close the editor references for resources on page. |
| 113 |
* |
| 114 |
* @param resources |
| 115 |
* @param i |
| 116 |
* @param pages |
| 117 |
*/ |
| 118 |
private void closeEditorReferences(Collection resources, IWorkbenchPage page) { |
| 119 |
|
| 120 |
IEditorReference[] references = page.getEditorReferences(); |
| 121 |
Iterator resourceIterator = resources.iterator(); |
| 122 |
while (resourceIterator.hasNext()) { |
| 123 |
IResource resource = (IResource) resourceIterator.next(); |
| 124 |
|
| 125 |
IContainer container = null; |
| 126 |
if (resource instanceof IContainer) |
| 127 |
container = (IContainer) resource; |
| 128 |
for (int l = 0; l < references.length; l++) { |
| 129 |
IEditorReference reference = references[l]; |
| 130 |
{// Is this not created yet? |
| 131 |
if (reference.getPart(false) == null) { |
| 132 |
IEditorInput input; |
| 133 |
try { |
| 134 |
input = reference.getEditorInput(); |
| 135 |
if (input instanceof IFileEditorInput) { |
| 136 |
IFile file = ((IFileEditorInput) input) |
| 137 |
.getFile(); |
| 138 |
if (container == null) { |
| 139 |
if (file.equals(resource)) |
| 140 |
editorsToClose.add(reference); |
| 141 |
} else if (container.getFile(file |
| 142 |
.getProjectRelativePath()) != null) |
| 143 |
editorsToClose.add(reference); |
| 144 |
} |
| 145 |
} catch (PartInitException e) { |
| 146 |
StatusManager.getManager().addLoggedStatus( |
| 147 |
e.getStatus()); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
if (editorsToClose.isEmpty()) |
| 155 |
return; |
| 156 |
IEditorReference[] closedReferences = new IEditorReference[editorsToClose |
| 157 |
.size()]; |
| 158 |
editorsToClose.toArray(closedReferences); |
| 159 |
page.closeEditors(closedReferences, false); |
| 160 |
editorsToClose.clear(); |
| 161 |
|
| 162 |
} |
| 163 |
} |