package com.github.fengtan.solrgui;

import java.util.Objects;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.solr.common.SolrDocument;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

public class SolrGUILabelProvider extends LabelProvider implements ITableLabelProvider {

	// Names of images used to represent checkboxes
	public static final String CHECKED_IMAGE 	= "checked";
	public static final String UNCHECKED_IMAGE  = "unchecked";

	// For the checkbox images
	private static ImageRegistry imageRegistry = new ImageRegistry(); // TODO

	/**
	 * Note: An image registry owns all of the image objects registered with it,
	 * and automatically disposes of them the SWT Display is disposed.
	 */ 
	static {
		String iconPath = "icons/"; 
		imageRegistry.put(CHECKED_IMAGE, ImageDescriptor.createFromFile(SolrGUI.class, iconPath + CHECKED_IMAGE + ".gif"));
		imageRegistry.put(UNCHECKED_IMAGE, ImageDescriptor.createFromFile(SolrGUI.class, iconPath + UNCHECKED_IMAGE + ".gif"));	
	}
	

	/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
	 */
	public String getColumnText(Object element, int columnIndex) {
		String result = "";
		SolrDocument document = (SolrDocument) element;
		/* TODO drop
		switch (columnIndex) {
			case 0:
				result = document.isModified() ? "X" : "";
				break;
			case 1 :
				result = document.getDescription();
				break;
			case 2 :
				result = document.getOwner();
				break;
			case 3 :
				result = document.getPercentComplete() + "";
				break;
			default :
				break; 	
		}
		*/
		result = Objects.toString(document.getFieldValue("item_id"), "");
		return result;
	}

	/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
	 */
	public Image getColumnImage(Object element, int columnIndex) {
		return null;
	}

}
@@ -51,62 +51,68 @@
 til.
-Object
+Array
 s;%0A
-%0A
 import 
-org.apache.commons.lang3.ObjectUtils;
+java.util.Collection;%0Aimport java.util.List;%0A
 %0Aimp
@@ -1369,30 +1369,8 @@
 ) %7B%0A
-%09%09String result = %22%22;%0A
 %09%09So
@@ -1419,23 +1419,8 @@
 nt;%0A
-%09%09/* TODO drop%0A
 %09%09sw
@@ -1450,21 +1450,41 @@
 %09case 0:
-%0A%09%09%09%09
+ // Modified%0A%09%09%09%09// TODO 
 result =
@@ -1526,48 +1526,122 @@
 %09%09%09%09
-b
 re
-ak;%0A%09%09%09case 1 :%0A%09%09%09%09result = 
+turn %22%22;%0A%09%09%09default:%0A%09%09%09%09// TODO Objects.toString(myvalue, %22%22) ? null safe%0A%09%09%09%09String field = getFields(
 document
 .get
@@ -1640,286 +1640,335 @@
 ment
+)
 .get
-Description(
+(columnIndex-1
 );%0A%09%09%09%09
-b
 re
-ak;%0A%09%09%09case 2 :%0A%09%09%09%09result = document.getOwner();%0A%09%09%09%09break;%0A%09%09%09case 3 :%0A%09%09%09%09result = document.getPercentComplete() + %22%22;%0A%09%09%09%09break;%0A%09%09%09default :%0A%09%09%09%09break; %09%0A%09%09%7D%0A%09%09*/%0A%09%09result = Objects.toString(document.getFieldValue(%22item_id%22), %22%22);%0A%09%09return result;
+turn document.getFieldValue(field).toString();%0A%09%09%7D%0A%09%7D%0A%09%0A%09// TODO re-use server.getFields() + SolrGUI.getColumnNames() ?%0A%09public List%3CString%3E getFields(SolrDocument document) %7B%0A%09%09Collection%3CString%3E fields = document.getFieldNames();%0A%09%09return Arrays.asList(fields.toArray(new String%5Bfields.size()%5D));%09
 %0A%09%7D%0A

Use Solr data to fill up table rows.