package tool.navigator.pex;
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.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.texteditor.IDocumentProvider;
import tool.ToolPlugin;
import tool.editors.text.ClassDocumentProvider;
import tool.model.IClassComponent;
import tool.model.IProjectComponent;
import tool.model.ToolCType;
import tool.model.ToolClass;
import tool.model.ToolComponent;
import tool.model.ToolConstant;
import tool.model.ToolInterface;
import tool.model.ToolPlan;
import tool.model.ToolType;
/**
* Provides the properties contained in a *.properties file as children of that
* file in a Common Navigator.
* @since 3.2
*/
public class ContentProvider implements ITreeContentProvider,
IResourceChangeListener, IResourceDeltaVisitor {
protected final static String PROJECT_CONSTANTS = "__project_constants";
protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(
PROJECT_CONSTANTS);
private static final Object[] NO_CHILDREN = new Object[0];
private static final Object PLAN_EXT = "pex"; //$NON-NLS-1$
private StructuredViewer viewer;
protected IDocumentProvider fDocumentProvider;
/**
* Create the PropertiesContentProvider instance.
*
* Adds the content provider as a resource change listener to track changes on disk.
*
*/
public ContentProvider() {
ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
fDocumentProvider = new ClassDocumentProvider();
}
/**
* Return the model elements for a Plan IFile or
* NO_CHILDREN for otherwise.
*/
public Object[] getChildren(Object parentElement) {
Object[] children = null;
if (parentElement instanceof ToolConstant) {
return NO_CHILDREN;
} else if(parentElement instanceof IFile) {
/* possible model file */
IFile modelFile = (IFile) parentElement;
if(PLAN_EXT.equals(modelFile.getFileExtension())) {
ToolPlan tpm = ToolPlan.getInstance(modelFile);
IProjectComponent[] tpcs = tpm.getComponents();
return tpcs;
}
} else if (parentElement instanceof ToolClass){
ToolComponent cls = (ToolComponent)parentElement;
return cls.getComponents();
} else if (parentElement instanceof ToolInterface){
ToolComponent cls = (ToolComponent)parentElement;
return cls.getComponents();
} else if (parentElement instanceof ToolCType){
ToolComponent cls = (ToolComponent)parentElement;
return cls.getComponents();
} else if (parentElement instanceof IClassComponent){
return NO_CHILDREN;
}
return children != null ? children : NO_CHILDREN;
}
public Object getParent(Object element) {
if (element instanceof ToolConstant) {
ToolConstant data = (ToolConstant) element;
return data.getParent();
}
return null;
}
public boolean hasChildren(Object element) {
if (element instanceof ToolConstant) {
return false;
}else if (element instanceof IClassComponent) {
return false;
} else if(element instanceof IFile) {
return PLAN_EXT.equals(((IFile) element).getFileExtension());
} else if (element instanceof ToolType){
return true;
}
return false;
}
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
public void dispose() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
}
public void inputChanged(Viewer aViewer, Object oldInput, Object newInput) {
if (oldInput != null && !oldInput.equals(newInput))
viewer = (StructuredViewer) aViewer;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
*/
public void resourceChanged(IResourceChangeEvent event) {
IResourceDelta delta = event.getDelta();
try {
delta.accept(this);
} catch (CoreException e) {
ToolPlugin.log(IStatus.ERROR,"Error in resourceChange.", e);
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
*/
public boolean visit(IResourceDelta delta) {
IResource source = delta.getResource();
switch (source.getType()) {
case IResource.ROOT:
case IResource.PROJECT:
case IResource.FOLDER:
return true;
case IResource.FILE:
final IFile file = (IFile) source;
if (PLAN_EXT.equals(file.getFileExtension())) {
ToolPlan.getInstance(file);
new UIJob("Update Plan Model in ToolViewer") {
public IStatus runInUIThread(IProgressMonitor monitor) {
if (viewer != null && !viewer.getControl().isDisposed())
viewer.refresh(file);
return Status.OK_STATUS;
}
}.schedule();
}
return false;
}
return false;
}
}