package com.github.fengtan.solrgui;

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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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 SolrGUIColumnList columns = new SolrGUIColumnList();
	private Table table;
	private Shell shell;
	
	public SolrGUIDisplay(SolrDocumentList docs) {
	    this.docs = docs;
	    
	    for (SolrDocument document:docs) {
	    	for (String title:document.keySet()) {
	    		if (!columns.contains(title)) {
	    			columns.add(title);
	    		}
	    	}
	    }
		
	    Display display = new Display();
	    shell = new Shell(display);
	    	    
	    updateTable();
	    updateMenus();
                
	    shell.pack();
	    shell.open();
	    while (!shell.isDisposed()) {
	      if (!display.readAndDispatch())
	        display.sleep();
	    }
	    display.dispose();
	}
		
	public void updateTable() { // TODO might be worth to move 'new Table()' in the constructor
	    table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); // TODO
	    table.setLinesVisible(true);
	    table.setHeaderVisible(true);

		for (String title:columns.getItemsDisplayed()) {
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		    tableColumn.setText(title);
		}

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

	    for (int i = 0; i < table.getColumnCount(); i++) {
	      table.getColumn(i).pack();
	    }
	    
	    table.setSize(table.computeSize(SWT.DEFAULT, 200)); // TODO
	}
	
	private void updateMenus() {
        Menu menuBar = new Menu(shell, SWT.BAR);
        
        // File menu.
        MenuItem fileMenuItem = new MenuItem(menuBar, SWT.CASCADE);
        fileMenuItem.setText("&File");
        
        Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
        fileMenuItem.setMenu(fileMenu);

        MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
        exitItem.setText("&Exit");
        shell.setMenuBar(menuBar);

        exitItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.getDisplay().dispose();
                System.exit(0);
            }
        });
        
        // Columns menu.
        MenuItem columnsMenuItem = new MenuItem(menuBar, SWT.CASCADE);
        columnsMenuItem.setText("&Columns");

        Menu columnsMenu = new Menu(shell, SWT.DROP_DOWN);
        columnsMenuItem.setMenu(columnsMenu);

        for (String title:columns) {
            MenuItem columnItem = new MenuItem(columnsMenu, SWT.CHECK);
            columnItem.setText(title);
            columnItem.setSelection(true);
            shell.setMenuBar(menuBar);            
            columnItem.addSelectionListener(new ColumnAdapter(table, title, columns));
        }
        
	}
	
}

@@ -263,24 +263,66 @@
 ctionEvent;%0A
+import org.eclipse.swt.widgets.Composite;%0A
 import org.e
@@ -349,16 +349,16 @@
 isplay;%0A
-
 import o
@@ -465,16 +465,98 @@
 .Shell;%0A
+import org.eclipse.swt.widgets.TabFolder;%0Aimport org.eclipse.swt.widgets.TabItem;%0A
 import o
@@ -671,16 +671,53 @@
 leItem;%0A
+import org.eclipse.swt.widgets.Text;%0A
 %0A%0Apublic
@@ -1224,27 +1224,619 @@
    %0A
-%09    updateTable();
+%0A                %0A%09    %0A%09    %0A%09    %0A%09    %0A%09    %0A%0A%09    shell.setText(%22Solr GUI%22);%0A%09    shell.setSize(450, 250);%0A%0A%09    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);%0A%0A%09    for (int loopIndex = 0; loopIndex %3C 10; loopIndex++) %7B%0A%09      TabItem tabItem = new TabItem(tabFolder, SWT.NULL);%0A%09      tabItem.setText(%22Tab %22 + loopIndex);%0A%0A%09      Text text = new Text(tabFolder, SWT.BORDER);%0A%09      text.setText(%22This is page %22 + loopIndex);%0A%09    %7D%0A%09    tabFolder.setSize(1200, 300); // TODO set max size of  window%0A%09    %0A%09    updateTable(tabFolder);%0A%09    %0A%09    tabFolder.getItem(0).setControl(table);%0A%09    
 %0A%09  
@@ -1848,36 +1848,37 @@
 ateMenus();%0A
+%09
     
+%0A%09
     
-    
+%0A%09
     %0A%09    sh
@@ -2070,16 +2070,32 @@
 teTable(
+Composite parent
 ) %7B // T
@@ -2177,21 +2177,22 @@
 w Table(
-shell
+parent
 , SWT.MU

Add tabs.