Package tool.navigator.cdf

Source Code of tool.navigator.cdf.CdfContentProvider

package tool.navigator.cdf;

import java.io.IOException;

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.core.runtime.content.IContentDescriber;
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.ToolClass;
import tool.model.ToolComponent;
import tool.model.ToolConstant;
import tool.model.ToolInterface;


public class CdfContentProvider implements ITreeContentProvider,
IResourceChangeListener, IResourceDeltaVisitor{

  private static final Object[] NO_CHILDREN = new Object[0];

  private static final Object CDF_EXT = "cdf"; //$NON-NLS-1$


  private StructuredViewer viewer;
 
  private InterfaceContentDescriber intDesc = new InterfaceContentDescriber();
  private ServiceObjectContentDescriber soDesc = new ServiceObjectContentDescriber();
  private ClassContentDescriber classDesc = new ClassContentDescriber();
  private WindowClassContentDescriber mappedDesc = new WindowClassContentDescriber();

  /**
   * Create the PropertiesContentProvider instance.
   *
   * Adds the content provider as a resource change listener to track changes on disk.
   *
   */
  public CdfContentProvider() {
    ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
  }

  /**
   * Return the model elements for a *.properties IFile or
   * NO_CHILDREN for otherwise.
   */
  public Object[] getChildren(Object parentElement) { 
    Object[] children = null;
    if (parentElement instanceof ToolConstant) {
      children = NO_CHILDREN;
    } else 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())) {
        try {
          ToolComponent model;
          if (intDesc.describe(modelFile.getContents(), null)==IContentDescriber.VALID){
            model = ToolInterface.fetch(modelFile.getProject(), modelFile);
            children = model.getComponents() ;
          }else if ((classDesc.describe(modelFile.getContents(), null)==IContentDescriber.VALID)||
              (mappedDesc.describe(modelFile.getContents(), null)==IContentDescriber.VALID)){
            model = ToolClass.fetch(modelFile);
            children = model.getComponents() ;
          }
        } catch (IOException e) {
          ToolPlugin.showError("Error in CDF conten provider", e);
        } catch (CoreException e) {
          ToolPlugin.showError("Error in CDF conten provider", e);
        }
       
      }
    } else if (parentElement instanceof ToolClass){
      ToolComponent model = (ToolComponent)parentElement;
      children = model.getComponents();
    }
    return children != null ? children : NO_CHILDREN;
 

  /**
   * Load the model from the given file, if possible. 
   * @param modelFile The IFile which contains the persisted model
   */
  private synchronized ToolComponent updateClassModel(IFile modelFile) {

      if (modelFile.exists()) {
        ToolComponent model = ToolClass.fetch(modelFile);
        return model;
      }
    return null;
  }
  private synchronized ToolInterface updateInterfaceModel(IFile file) {
    if (file.exists()) {
      ToolInterface model = ToolInterface.fetch(file.getProject(), file);
      return model;
    }
    return null;
  }

  public Object getParent(Object element) {
    if (element instanceof IClassComponent) {
      IClassComponent data = (IClassComponent) element;
      return data.getClassFile();
    }
    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 CDF_EXT.equals(((IFile) element).getFileExtension());
    }
    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) {
    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 Class resource change.", 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 (CDF_EXT.equals(file.getFileExtension())) {
        try {
          if (intDesc.describe(file.getContents(), null)==IContentDescriber.VALID){
            updateInterfaceModel(file);
          }else if ((classDesc.describe(file.getContents(), null)==IContentDescriber.VALID)||
              (mappedDesc.describe(file.getContents(), null)==IContentDescriber.VALID)){
            updateClassModel(file);
          }
        } catch (IOException e) {
          ToolPlugin.showError("Error in CDF conten provider", e);
        } catch (CoreException e) {
          ToolPlugin.showError("Error in CDF conten provider", e);
        }
        new UIJob("Update Cdf Component Model in ClassViewer") {  //$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;
  }

}
TOP

Related Classes of tool.navigator.cdf.CdfContentProvider

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.