Package tool.navigator.cdf

Source Code of tool.navigator.cdf.InterfaceContentProvider

package tool.navigator.cdf;

import java.util.HashMap;
import java.util.Map;

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.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.progress.UIJob;

import tool.ToolPlugin;
import tool.model.IClassComponent;
import tool.model.ToolInterface;

public class InterfaceContentProvider implements ITreeContentProvider,
          IResourceChangeListener, IResourceDeltaVisitor{

  private static final Object[] NO_CHILDREN = new Object[0];
  private static final Object CDF_EXT = "cdf";
  private StructuredViewer viewer;
  private final Map<IFile, ToolInterface> cachedModelMap = new HashMap<IFile, ToolInterface>();
 
  public InterfaceContentProvider() {
    ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
  }

  @Override
  public void dispose() {
    cachedModelMap.clear();
    ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
  }

  @Override
  public void inputChanged(Viewer aViewer, Object oldInput, Object newInput) {
    if (oldInput != null && !oldInput.equals(newInput))
      cachedModelMap.clear();
    viewer = (StructuredViewer) aViewer;
  }

  @Override
  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 (CDF_EXT.equals(file.getFileExtension())) {
        updateModel(file);
        new UIJob("Update Interface Component Model in InterfaceViewer") {  //$NON-NLS-1$
          public IStatus runInUIThread(IProgressMonitor monitor) {
            if (viewer != null && !viewer.getControl().isDisposed())
              viewer.refresh(file);
            return Status.OK_STATUS;           
          }
        }.schedule();
      }
      return false;
    }
    return false;
  }

  @Override
  public void resourceChanged(IResourceChangeEvent event) {

    IResourceDelta delta = event.getDelta();
    try {
      delta.accept(this);
    } catch (CoreException e) {
      ToolPlugin.log(IStatus.ERROR,"Error interface resource change.", e);
    }
  }

  @Override
  public Object[] getChildren(Object parentElement) {
    Object[] children = null;
    if (parentElement instanceof IClassComponent) {
      children = NO_CHILDREN;
    } else if(parentElement instanceof IFile) {
      /* possible model file */
      IFile modelFile = (IFile) parentElement;
      if(CDF_EXT.equals(modelFile.getFileExtension())) {   
        ToolInterface model = getToolInterface(modelFile);
        children = model.getComponents() ;
      }
    } else if (parentElement instanceof ToolInterface){
      ToolInterface model = (ToolInterface)parentElement;
      children = model.getComponents();
    }
    return children != null ? children : NO_CHILDREN;
  }

  private ToolInterface getToolInterface(IFile modelFile) {
    ToolInterface model = (ToolInterface)cachedModelMap.get(modelFile);
    if (model == null){
      model = updateModel(modelFile);
    }
    return model;
  }

  private ToolInterface updateModel(IFile modelFile) {

    if(CDF_EXT.equals(modelFile.getFileExtension()) ) {
      if (modelFile.exists()) {
        ToolInterface model = ToolInterface.fetch(modelFile.getProject(), modelFile);
        if (model != null) {
          cachedModelMap.put(modelFile, model);
          return model;
        } else {
          cachedModelMap.remove(modelFile);
        }
      }
    }
    return null;
  }

  @Override
  public Object[] getElements(Object inputElement) {
    return getChildren(inputElement);
  }

  @Override
  public Object getParent(Object element) {
    if (element instanceof IClassComponent) {
      IClassComponent data = (IClassComponent) element;
      return data.getClassFile();
    }
    return null;
  }

  @Override
  public boolean hasChildren(Object element) {   
    if (element instanceof IClassComponent) {
      return false;   
    } else if(element instanceof IFile) {
      return CDF_EXT.equals(((IFile) element).getFileExtension());
    }
    return false;
  }

}
TOP

Related Classes of tool.navigator.cdf.InterfaceContentProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.