Package org.rssowl.contrib.podcast.ui.download

Source Code of org.rssowl.contrib.podcast.ui.download.DownloadLabelProvider

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2006 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   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.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     Christophe Bouhier - podcast plugin                         **
**                                                                          **
**  **********************************************************************  */

package org.rssowl.contrib.podcast.ui.download;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Scrollable;
import org.rssowl.contrib.podcast.core.download.Download;
import org.rssowl.contrib.podcast.core.download.DownloadService;
import org.rssowl.contrib.podcast.core.download.IDownload;
import org.rssowl.contrib.podcast.util.Logger;
import org.rssowl.contrib.podcast.util.Util;
import org.rssowl.ui.internal.OwlUI;

public class DownloadLabelProvider extends OwnerDrawLabelProvider {
  private LocalResourceManager fResources;
 
  private static Logger sLog = new Logger(DownloadLabelProvider.class.getName());

  /* Pre-Cache some Colors being used */
  private Color fGradientFgColor;
  private Color fGradientBgColor;
// private Color fGradientEndColor;
// private Color fGroupFgColor;
// private Color fGroupBgColor;

  /* Pre-Cache some Fonts being used */
  @SuppressWarnings("unused")
  private Font fBoldFont;

  private DownloadTableViewer mViewer;

  /** Creates a new instance of this LabelProvider */
  public DownloadLabelProvider(DownloadTableViewer pViewer) {
    mViewer = pViewer;
    fResources = new LocalResourceManager(JFaceResources.getResources());
    createResources();
  }

  private void createResources() {

    /* Colors */
    // CB TODO, manage generic colors.
    fGradientFgColor = fResources.getDevice().getSystemColor(
        SWT.COLOR_DARK_BLUE);
    fGradientBgColor = fResources.getDevice().getSystemColor(
        SWT.COLOR_WHITE);

// OwlUI.getColor(fResources,
// OwlUI.GROUP_GRADIENT_FG_COLOR);
    fGradientBgColor = OwlUI.getColor(fResources,
        OwlUI.GROUP_GRADIENT_BG_COLOR);
    fBoldFont = OwlUI.getThemeFont(OwlUI.HEADLINES_FONT_ID, SWT.BOLD);
  }

  @Override
  public void dispose() {
    fResources.dispose();
  }

  /**
   * @param gc
   * @param rect
   * @param m_fraction
   * @param background
   */
  protected void drawGradientBar(GC gc, Rectangle rect, float m_fraction,
      Color background, Color foreground) {
    int barWidth = Math.round(rect.width * m_fraction);
    gc.setForeground(background);
    gc.setBackground(foreground);
    gc.fillGradientRectangle(rect.x, rect.y, barWidth, rect.height, false);
    gc.setBackground(background);
    gc.fillRectangle(rect.x + barWidth, rect.y, rect.width - barWidth,
        rect.height);
    drawPercentage(gc, rect, m_fraction, background, foreground);
  }

  protected void drawPercentage(GC gc, Rectangle rect, float m_fraction,
      Color background, Color foreground) {
    String lDraw = new Long(Math.round(m_fraction * 100)).toString() + "%";
    gc.setForeground(foreground);
    gc.drawString(lDraw, rect.x + rect.width / 2, rect.y, true);
  }

  @Override
  protected void erase(Event event, Object element) {
    // Ignore.
  }

  @SuppressWarnings("unused")
  private Color getBackground(Object element, int columnIndex) {
    return null;
  }

  @SuppressWarnings("unused")
  private Image getColumnImage(Object element, int columnIndex) {
    return null;
  }

  /**
   * The element is expected to be of type <code>IDownload</code>.
   *
   * @param element
   * @param columnIndex
   * @return
   */
  private String getColumnText(Object element, int columnIndex) {
    String lDraw = "";
    if (element instanceof IDownload) {
      Download lDownload = (Download) element;
      int row = DownloadService.getInstance().indexOf(lDownload);

      switch (columnIndex) {
      case 0: { // The download index.
        lDraw = new Integer(row).toString();
      }
        break;
      case 1: { // The file name.
        if (lDownload.getAttachment().getFile() != null) {
          lDraw = lDownload.getAttachment().getFile().getName();
        } else {
          lDraw = "Error, File name not set";
        }
      }
        break; // The feed title.
      case 2: {
        lDraw = lDownload.getAttachment().getNews().getFeedReference()
            .resolve().getTitle();
      }
        break;
      case 3: {
        lDraw = "TODO"; // this is the proress bar.
      }
        break;
      case 4: {
        lDraw = Util.formatSpeed(lDownload.getBytesPerSecond())
            + " kB/s";
      }
        break;
      case 5: {
        lDraw = Util.formatTime(lDownload.getTimeElapsed());
      }
        break;
      case 6: {
        String status = DownloadService.STATE_DESCRIPTION[lDownload
            .getState()];
        if (lDownload.getState() == DownloadService.RETRYING
            || lDownload.getState() == DownloadService.ERROR
            || lDownload.getState() == DownloadService.RELEASING) {
          status += lDownload.getMessage();
        }
        lDraw = status;
      }
        break;
      case 7: {
        lDraw = Util.formatSize(lDownload.getCurrent());
      }
      }
    }

    return lDraw;
  }

  @SuppressWarnings("unused")
  private Font getFont(Object element, int columnIndex) {
    return null;
  }

  @SuppressWarnings("unused")
  private Color getForeground(Object element, int columnIndex) {
    return null;
  }

  protected float getFraction(IDownload pDownload) {
    float lCurrent = new Float(pDownload.getCurrent()).floatValue();
    float lLen = new Float(pDownload.getLength()).floatValue();
    float lFraction = lCurrent / lLen;
    return lFraction;
  }

  @Override
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }

  @Override
  protected void measure(Event event, Object element) {
    if (event.index == 3) {
      if (element instanceof IDownload) {
        IDownload lDownload = (IDownload) element;
        int lColumnWidth = mViewer.mTableViewer.getTable().getColumn(
            event.index).getWidth();
        event.width = lColumnWidth;
      }
    }
  }

  @Override
  protected void paint(Event event, Object element) {
    if (event.index == 3) {
      if (element instanceof IDownload) {
        IDownload lDownload = (IDownload) element;
        sLog.info("paint(): " + lDownload.getAttachment().getFileName());
        // This is the total client area.
        Scrollable lScrollable = (Scrollable) event.widget;
        Rectangle lClient = lScrollable.getClientArea();

        Rectangle rect = event.getBounds();
        int lColumnWidth = mViewer.mTableViewer.getTable().getColumn(
            event.index).getWidth();
        rect.width = lColumnWidth;
        GC gc = event.gc;
       
        float lFraction = getFraction(lDownload);
        drawGradientBar(gc, rect, lFraction, fGradientBgColor,
            fGradientFgColor);

      }
    }
  }

  @Override
  public void update(ViewerCell cell) {
    /* Text */
    cell.setText(getColumnText(cell.getElement(), cell.getColumnIndex()));
    Item item = (Item) cell.getItem();

    if( cell.getColumnIndex() == 3){
      cell.setForeground(cell.getViewerRow().getForeground(cell.getColumnIndex()));
//      Event lEvent = new Event();
//      lEvent.setBounds(cell.getBounds());
//      lEvent.display = Display.getDefault();
//      lEvent.widget = cell.getItem();
//      GC lGc = new GC(OwlUI.getPrimaryShell());
//      lEvent.index = cell.getColumnIndex();
//      paint(lEvent, cell.getElement());
    }
  }
}
TOP

Related Classes of org.rssowl.contrib.podcast.ui.download.DownloadLabelProvider

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.