/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* 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 | |
* | |
* Contributors: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.mm.navigator; | |
import org.eclipse.core.resources.IFile; | |
import org.eclipse.core.resources.IResource; | |
import org.eclipse.core.resources.IResourceChangeEvent; | |
import org.eclipse.core.resources.IResourceChangeListener; | |
import org.eclipse.core.resources.IResourceDelta; | |
import org.eclipse.core.resources.IResourceDeltaVisitor; | |
import org.eclipse.core.resources.ResourcesPlugin; | |
import org.eclipse.core.runtime.CoreException; | |
import org.eclipse.emf.common.notify.Notification; | |
import org.eclipse.emf.common.notify.impl.NotificationImpl; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.resource.ResourceSet; | |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | |
import org.eclipse.emf.edit.provider.ViewerNotification; | |
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; | |
public class ProjectAdapterFactoryContentProvider extends | |
AdapterFactoryContentProvider implements IResourceChangeListener, | |
IResourceDeltaVisitor { | |
private static ResourceSet resourceSet = new ResourceSetImpl();; | |
public ProjectAdapterFactoryContentProvider() { | |
super(ProjectAdapterFactoryProvider.getAdapterFactory()); | |
ResourcesPlugin.getWorkspace().addResourceChangeListener(this, | |
IResourceChangeEvent.POST_CHANGE); | |
} | |
public boolean hasChildren(Object object) { | |
if (object instanceof IFile) | |
return true; | |
return super.hasChildren(object); | |
} | |
public Object[] getChildren(Object object) { | |
if (object instanceof IFile) { | |
String path = ((IFile) object).getFullPath().toString(); | |
URI uri = URI.createPlatformResourceURI(path, true); | |
object = resourceSet.getResource(uri, false); | |
} | |
return super.getChildren(object); | |
} | |
public Object[] getElements(Object object) { | |
if (object instanceof IFile) { | |
String path = ((IFile) object).getFullPath().toString(); | |
URI uri = URI.createPlatformResourceURI(path, false); | |
object = resourceSet.getResource(uri, true); | |
} | |
return super.getElements(object); | |
} | |
public Object getParent(Object object) { | |
if (object instanceof IFile) | |
return ((IResource) object).getParent(); | |
return super.getParent(object); | |
} | |
@Override | |
public void resourceChanged(IResourceChangeEvent event) { | |
IResourceDelta delta = event.getDelta(); | |
try { | |
delta.accept(this); | |
} catch (CoreException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public boolean visit(IResourceDelta delta) throws CoreException { | |
try { | |
IResource changedResource = delta.getResource(); | |
if ((changedResource.getType() == IResource.FILE) | |
&& ("block".equals(changedResource.getFileExtension()))) { | |
String path = ((IFile) changedResource).getFullPath() | |
.toString(); | |
URI uri = URI.createPlatformResourceURI(path, true); | |
Resource res = resourceSet.getResource(uri, true); | |
res.unload(); | |
res.load(resourceSet.getLoadOptions()); | |
notifyChanged(new ViewerNotification(new NotificationImpl( | |
Notification.SET, "", "Refresh"))); | |
} | |
return true; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
@Override | |
public void dispose() { | |
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); | |
super.dispose(); | |
} | |
} |