package com.github.fengtan.solrgui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;


public class SolrGUIDisplay {
	
	private SolrDocumentList docs;
	private List<String> titles = new ArrayList<String>(); // TODO hashed ? use of indexOf
	private Table table;
	
	private Listener sortListener = new Listener() {
        public void handleEvent(Event e) {
        	/*
            TableColumn column = (TableColumn) e.widget;
            /*
            if (column == intColumn) Arrays.sort(rows, BY_VAL);
            if (column == strColumn) Arrays.sort(rows, BY_STR);
            if (column == dateColumn) Arrays.sort(rows, BY_DATE);
            */
            //table.setSortColumn(column);
            updateDocuments();

        }
    };
	
	public SolrGUIDisplay(SolrDocumentList docs) {
	    this.docs = docs;
	    
	    for (SolrDocument document:docs) {
	    	for (String title:document.keySet()) {
	    		if (!titles.contains(title)) {
	    			titles.add(title);
	    		}
	    	}
	    }
		
	    Display display = new Display();
	    Shell shell = new Shell(display);
	    table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); // TODO
	    table.setLinesVisible(true);
	    table.setHeaderVisible(true);
	    	    
	    updateTitles();
	    updateDocuments();
                
	    table.setSize(table.computeSize(SWT.DEFAULT, 200)); // TODO
	    shell.pack();
	    shell.open();
	    while (!shell.isDisposed()) {
	      if (!display.readAndDispatch())
	        display.sleep();
	    }
	    display.dispose();
	}
	
	private void updateTitles() {
		for (String title:titles) {
			TableColumn column = new TableColumn(table, SWT.NONE);
	      	column.setText(title);
	      	column.addListener(SWT.Selection, sortListener);
		}
	}
	
	private void updateDocuments() {
		table.removeAll();

		for(SolrDocument doc:docs) {
	    	TableItem item = new TableItem(table, SWT.NONE);
	    	for (Map.Entry<String, Object> field:doc.entrySet()) {
	    		item.setText(titles.indexOf(field.getKey()), field.getValue().toString());	
	    	}
		}

	    for (int i = 0; i < titles.size(); i++) {
	      table.getColumn(i).pack();
	    }
	}
	
}

@@ -224,16 +224,110 @@
 wt.SWT;%0A
+import org.eclipse.swt.events.SelectionAdapter;%0Aimport org.eclipse.swt.events.SelectionEvent;%0A
 import o
@@ -389,21 +389,20 @@
 widgets.
-Event
+Menu
 ;%0Aimport
@@ -426,24 +426,24 @@
 widgets.
-Listener
+MenuItem
 ;%0Aimport
@@ -774,18 +774,16 @@
  table;%0A
-%09%0A
 %09private
@@ -787,475 +787,19 @@
 ate 
-Listener sortListener = new Listener() %7B%0A        public void handleEvent(Event e) %7B%0A        %09/*%0A            TableColumn column = (TableColumn) e.widget;%0A            /*%0A            if (column == intColumn) Arrays.sort(rows, BY_VAL);%0A            if (column == strColumn) Arrays.sort(rows, BY_STR);%0A            if (column == dateColumn) Arrays.sort(rows, BY_DATE);%0A            */%0A            //table.setSortColumn(column);%0A            updateDocuments();%0A%0A        %7D%0A    %7D
+Shell shell
 ;%0A%09%0A
@@ -1095,22 +1095,16 @@
 );%0A%09    
-Shell 
 shell = 
@@ -1132,293 +1132,64 @@
     
-table = new Table(shell, SWT.MULTI %7C SWT.BORDER %7C SWT.FULL_SELECTION); // TODO%0A%09    table.setLinesVisible(true);%0A%09    table.setHeaderVisible(true);%0A%09    %09    %0A%09    updateTitles();%0A%09    updateDocuments();%0A                %0A%09    table.setSize(table.computeSize(SWT.DEFAULT, 200)); // TODO
+%09    %0A%09    setTable();%0A%09    setMenus();%0A                
 %0A%09  
@@ -1354,24 +1354,25 @@
 pose();%0A%09%7D%0A%09
+%09
 %0A%09private vo
@@ -1378,24 +1378,179 @@
 oid 
-updateTitles() %7B
+setTable() %7B%0A%09    table = new Table(shell, SWT.MULTI %7C SWT.BORDER %7C SWT.FULL_SELECTION); // TODO%0A%09    table.setLinesVisible(true);%0A%09    table.setHeaderVisible(true);%0A%09    
 %0A%09%09f
@@ -1670,127 +1670,10 @@
 );%0A%09
-      %09column.addListener(SWT.Selection, sortListener);%0A%09%09%7D%0A%09%7D%0A%09%0A%09private void updateDocuments() %7B%0A%09%09table.removeAll();
+%09%7D
 %0A%0A%09%09
@@ -2001,15 +2001,770 @@
 %0A%09    %7D%0A
+%09    %0A%09    table.setSize(table.computeSize(SWT.DEFAULT, 200)); // TODO%0A%09%7D%0A%09%0A%09private void setMenus() %7B%0A        Menu menuBar = new Menu(shell, SWT.BAR);%0A        MenuItem cascadeFileMenu = new MenuItem(menuBar, SWT.CASCADE);%0A        cascadeFileMenu.setText(%22&File%22);%0A        %0A        Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);%0A        cascadeFileMenu.setMenu(fileMenu);%0A%0A        MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);%0A        exitItem.setText(%22&Exit%22);%0A        shell.setMenuBar(menuBar);%0A%0A        exitItem.addSelectionListener(new SelectionAdapter() %7B%0A            @Override%0A            public void widgetSelected(SelectionEvent e) %7B%0A                shell.getDisplay().dispose();%0A                System.exit(0);%0A            %7D%0A        %7D);%0A
 %09%7D%0A%09%0A%7D%0A

Add menus.