<commit_before>package org.pentaho.ui.xul.swt.tags.treeutil;

import java.io.InputStream;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.components.XulTreeCell;
import org.pentaho.ui.xul.containers.XulTree;
import org.pentaho.ui.xul.containers.XulTreeItem;
import org.pentaho.ui.xul.containers.XulTreeRow;
import org.pentaho.ui.xul.swt.tags.SwtTreeItem;
import org.pentaho.ui.xul.util.ColumnType;
import org.pentaho.ui.xul.util.SwtXulUtil;
import org.pentaho.ui.xul.util.XulUtil;

public class XulTableColumnLabelProvider implements ITableLabelProvider {

  private static final String UNCHECKED = "UNCHECKED";
  private static final String CHECKED = "CHECKED";

  private XulTree tree;
  private XulDomContainer domContainer;
  private static Log logger = LogFactory.getLog(XulTableColumnLabelProvider.class);

  private int imageIndex = 0;

  public XulTableColumnLabelProvider(XulTree tree, XulDomContainer aDomContainer) {
    this.tree = tree;
    this.domContainer = aDomContainer;
    if (JFaceResources.getImageRegistry().getDescriptor(CHECKED) == null) {
      JFaceResources.getImageRegistry().put(UNCHECKED,
          makeImage(((TableViewer) tree.getManagedObject()).getControl().getShell(), false));
      JFaceResources.getImageRegistry().put(CHECKED,
          makeImage(((TableViewer) tree.getManagedObject()).getControl().getShell(), true));
    }
  }

  public String getColumnText(Object obj, int i) {

    final int colIdx = i;
    XulTreeCell cell = ((XulTreeItem) obj).getRow().getCell(colIdx);
    if (cell == null) {
      return "";
    }

    switch (tree.getColumns().getColumn(colIdx).getColumnType()) {
    case CHECKBOX:
      return cell.getLabel() != null ? cell.getLabel() : cell.getLabel();
    case COMBOBOX:
    case EDITABLECOMBOBOX:
      Vector vec = (Vector) cell.getValue();
      if(vec != null  && vec.size() > cell.getSelectedIndex()){
        return "" + vec.get(cell.getSelectedIndex());
      } else {
        return "";
      }
    case TEXT:
      return cell.getLabel() != null ? cell.getLabel() : "";
    default:
      return cell.getLabel() != null ? cell.getLabel() : "";
    }

  }

  public void addListener(ILabelProviderListener ilabelproviderlistener) {
  }

  public void dispose() {
  }

  public boolean isLabelProperty(Object obj, String s) {
    return false;
  }

  public void removeListener(ILabelProviderListener ilabelproviderlistener) {
  }

  public Image getColumnImage(Object row, int col) {
    if (tree.getColumns().getColumn(col).getColumnType() == ColumnType.CHECKBOX) {
      if (isSelected(row, col)) {
        return JFaceResources.getImageRegistry().getDescriptor(CHECKED)
            .createImage();
      } else {
        return JFaceResources.getImageRegistry().getDescriptor(UNCHECKED)
            .createImage();
      }
    }
    
    if (tree.getColumns().getColumn(col).getImagebinding() != null){
      String src = ((SwtTreeItem)row).getImage();
      Display display = ((TableViewer) tree.getManagedObject()).getTable().getDisplay();
      return SwtXulUtil.getCachedImage(src, domContainer, display);
    }
    return null;
  }

  private Image makeImage(Shell shell, boolean type) {
    Shell placeholder = new Shell(shell, SWT.NO_TRIM);
    Button btn = new Button(placeholder, SWT.CHECK);<commit_msg>[PDI-3576] reusing checkbox image instead of creating a new one every time.<commit_after>package org.pentaho.ui.xul.swt.tags.treeutil;

import java.io.InputStream;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.ui.xul.XulDomContainer;
import org.pentaho.ui.xul.components.XulTreeCell;
import org.pentaho.ui.xul.containers.XulTree;
import org.pentaho.ui.xul.containers.XulTreeItem;
import org.pentaho.ui.xul.containers.XulTreeRow;
import org.pentaho.ui.xul.swt.tags.SwtTreeItem;
import org.pentaho.ui.xul.util.ColumnType;
import org.pentaho.ui.xul.util.SwtXulUtil;
import org.pentaho.ui.xul.util.XulUtil;

public class XulTableColumnLabelProvider implements ITableLabelProvider {

  private static final String UNCHECKED = "UNCHECKED";
  private static final String CHECKED = "CHECKED";

  private XulTree tree;
  private XulDomContainer domContainer;
  private static Log logger = LogFactory.getLog(XulTableColumnLabelProvider.class);

  private int imageIndex = 0;

  public XulTableColumnLabelProvider(XulTree tree, XulDomContainer aDomContainer) {
    this.tree = tree;
    this.domContainer = aDomContainer;
    if (JFaceResources.getImageRegistry().getDescriptor(CHECKED) == null) {
      JFaceResources.getImageRegistry().put(UNCHECKED,
          makeImage(((TableViewer) tree.getManagedObject()).getControl().getShell(), false));
      JFaceResources.getImageRegistry().put(CHECKED,
          makeImage(((TableViewer) tree.getManagedObject()).getControl().getShell(), true));
    }
  }

  public String getColumnText(Object obj, int i) {

    final int colIdx = i;
    XulTreeCell cell = ((XulTreeItem) obj).getRow().getCell(colIdx);
    if (cell == null) {
      return "";
    }

    switch (tree.getColumns().getColumn(colIdx).getColumnType()) {
    case CHECKBOX:
      return cell.getLabel() != null ? cell.getLabel() : cell.getLabel();
    case COMBOBOX:
    case EDITABLECOMBOBOX:
      Vector vec = (Vector) cell.getValue();
      if(vec != null  && vec.size() > cell.getSelectedIndex()){
        return "" + vec.get(cell.getSelectedIndex());
      } else {
        return "";
      }
    case TEXT:
      return cell.getLabel() != null ? cell.getLabel() : "";
    default:
      return cell.getLabel() != null ? cell.getLabel() : "";
    }

  }

  public void addListener(ILabelProviderListener ilabelproviderlistener) {
  }

  public void dispose() {
  }

  public boolean isLabelProperty(Object obj, String s) {
    return false;
  }

  public void removeListener(ILabelProviderListener ilabelproviderlistener) {
  }

  public Image getColumnImage(Object row, int col) {
    if (tree.getColumns().getColumn(col).getColumnType() == ColumnType.CHECKBOX) {
      if (isSelected(row, col)) {
        return JFaceResources.getImageRegistry().get(CHECKED);
      } else {
        return JFaceResources.getImageRegistry().get(UNCHECKED);
      }
    }
    
    if (tree.getColumns().getColumn(col).getImagebinding() != null){
      String src = ((SwtTreeItem)row).getImage();
      Display display = ((TableViewer) tree.getManagedObject()).getTable().getDisplay();
      return SwtXulUtil.getCachedImage(src, domContainer, display);
    }
    return null;
  }

  private Image makeImage(Shell shell, boolean type) {
    Shell placeholder = new Shell(shell, SWT.NO_TRIM);
    Button btn = new Button(placeholder, SWT.CHECK);
{'fraction_non_alphanumeric': 0.12239650295705837, 'fraction_numerical': 0.0007714065312419646, 'mean_word_length': 4.031694695989651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987416', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2411, 'n_words': 460}