package org.cloudcoder.app.client.view;

import java.util.Arrays;
import java.util.List;

import org.cloudcoder.app.client.model.Session;
import org.cloudcoder.app.client.page.SessionObserver;
import org.cloudcoder.app.shared.model.TestResult;
import org.cloudcoder.app.shared.util.Publisher;
import org.cloudcoder.app.shared.util.Subscriber;
import org.cloudcoder.app.shared.util.SubscriptionRegistrar;

import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;

public class TestResultListView extends Composite implements SessionObserver, Subscriber {
	private CellTable<TestResult> cellTable;
	
	public TestResultListView() {
		cellTable = new CellTable<TestResult>();
		cellTable.setSize("100%", "100%");
		
		cellTable.addColumn(new OutcomeColumn(), "Outcome");
		cellTable.addColumn(new MessageColumn(), "Message");
		cellTable.addColumn(new OutputColumn(), "Output");
		cellTable.addColumn(new ErrorOutputColumn(), "Error output");
		
		initWidget(cellTable);
	}
	
	private static class OutcomeColumn extends TextColumn<TestResult> {
		@Override
		public String getValue(TestResult object) {
			return object.getOutcome().toString().toLowerCase();
		}
	}
	
	private static class MessageColumn extends TextColumn<TestResult> {
		@Override
		public String getValue(TestResult object) {
			return object.getMessage();
		}
	}
	
	private static class OutputColumn extends TextColumn<TestResult> {
		@Override
		public String getValue(TestResult object) {
			return object.getStdout();
		}
	}
	
	private static class ErrorOutputColumn extends TextColumn<TestResult> {
		@Override
		public String getValue(TestResult object) {
			return object.getStderr();
		}
	}
	
	public void activate(final Session session, final SubscriptionRegistrar subscriptionRegistrar) {
		session.subscribe(Session.Event.ADDED_OBJECT, this, subscriptionRegistrar);
		
		TestResult[] testResultList = session.get(TestResult[].class);
		if (testResultList != null) {
			displayTestResults(testResultList);
		}
	}

	@Override
	public void eventOccurred(Object key, Publisher publisher, Object hint) {
		if (key == Session.Event.ADDED_OBJECT && hint instanceof TestResult[]) {
			displayTestResults((TestResult[]) hint);
		}
	}
	
	private void displayTestResults(TestResult[] testResultList) {
//		cellTable.setRowCount(testResultList.length);
		cellTable.setRowData(Arrays.asList(testResultList));
	}
}

@@ -1,8 +1,878 @@
+// CloudCoder - a web-based pedagogical programming environment%0A// Copyright (C) 2011-2012, Jaime Spacco %3Cjspacco@knox.edu%3E%0A// Copyright (C) 2011-2012, David H. Hovemeyer %3Cdavid.hovemeyer@gmail.com%3E%0A//%0A// This program is free software: you can redistribute it and/or modify%0A// it under the terms of the GNU Affero General Public License as published by%0A// the Free Software Foundation, either version 3 of the License, or%0A// (at your option) any later version.%0A//%0A// This program is distributed in the hope that it will be useful,%0A// but WITHOUT ANY WARRANTY; without even the implied warranty of%0A// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A// GNU Affero General Public License for more details.%0A//%0A// You should have received a copy of the GNU Affero General Public License%0A// along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A%0A
 package 
@@ -932,31 +932,8 @@
 ays;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -1288,25 +1288,24 @@
 .client.
-CellTable
+DataGrid
 ;%0Aimport
@@ -1402,16 +1402,99 @@
 osite;%0A%0A
+/**%0A * View displaying the list of TestResults.%0A * %0A * @author David Hovemeyer%0A */%0A
 public c
@@ -1585,25 +1585,24 @@
 private 
-CellTable
+DataGrid
 %3CTestRes
@@ -1672,17 +1672,16 @@
 new 
-CellTable
+DataGrid
 %3CTes
@@ -1692,16 +1692,166 @@
 ult%3E();%0A
+%09%09%0A%09%09// Odd bug: when width is set to 100%25, the scroll bar extends%0A%09%09// beyond the width of the parent.  So, make it a bit less than%0A%09%09// 100%25 width.%0A
 %09%09cellTa
@@ -1863,19 +1863,18 @@
 etSize(%22
-100
+98
 %25%22, %22100

use a DataGrid for TestResultListView, to allow it to scroll properly