Package es.java.otro.view

Source Code of es.java.otro.view.RssTreeContentProvider

/**
*
*/
package es.java.otro.view;

import java.util.Observable;
import java.util.Observer;

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.Viewer;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.progress.UIJob;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;

import es.java.otro.Activator;
import es.java.otro.model.Feed;
import es.java.otro.model.Root;
import es.java.otro.service.FeedService;
import es.java.otro.util.RssUtil;

/**
* @author jaime
*
*/

public class RssTreeContentProvider implements ITreeContentProvider, Observer {

  private Viewer viewer;

  private FeedService feedService;

  public FeedService getFeedService() {
    if (feedService == null) {
          ServiceTracker tracker = new ServiceTracker(Activator.getDefault().getBundle().getBundleContext(), FeedService.class.getName(),null);
          tracker.open();
          feedService = (FeedService) tracker.getService();
    }
    return feedService;
  }

  public void setFeedService(FeedService feedService) {
    this.feedService = feedService;
  }

  ComponentContext context;

  public void activate(ComponentContext context) {}

  public RssTreeContentProvider() {
   
  }

  public void loadFeeds() {
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.
   * Object)
   */
  @Override
  public Object[] getChildren(Object parentElement) {
    if (parentElement instanceof Root) {
      Root root = (Root) parentElement;
      return root.getFeeds().toArray();
    } else if (parentElement instanceof Feed) {
      Feed feed = (Feed) parentElement;
      return feed.getEntries().toArray();
    } else {
      return new Object[0];
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object
   * )
   */
  @Override
  public Object getParent(Object element) {
    return null;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.
   * Object)
   */
  @Override
  public boolean hasChildren(Object element) {
    if (element instanceof Root) {
      Root root = (Root) element;
      return !root.getFeeds().isEmpty();
    } else if (element instanceof Feed) {
      Feed feed = (Feed) element;
      return !feed.getEntries().isEmpty();
    } else
      return false;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
   * .lang.Object)
   */
  @Override
  public Object[] getElements(Object inputElement) {
    return getChildren(inputElement);
  }

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.viewers.IContentProvider#dispose()
   */
  @Override
  public void dispose() {
    // TODO Auto-generated method stub

  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
   * .viewers.Viewer, java.lang.Object, java.lang.Object)
   */
  @Override
  public void inputChanged(final Viewer viewer, Object oldInput, Object newInput) {
   
    if (this.viewer == null) {
      this.viewer = viewer;
      Root.getInstance().setFeeds(getFeedService().getFeeds());
      Root.getInstance().refresh();
    } else {
      this.viewer = viewer;
    }
   
    if (oldInput != null) {
      ((Observable) oldInput).deleteObserver(this);
    }
    if (newInput != null) {
      ((Observable) newInput).addObserver(this);
    }
    this.viewer.refresh();
  }

  @Override
  public void update(Observable arg0, Object arg1) {
    if (arg0 != null && arg0 instanceof Root) {
      getFeedService().persistRoot((Root) arg0);
    }
    viewer.refresh();
  }

 
}
TOP

Related Classes of es.java.otro.view.RssTreeContentProvider

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.