package org.cloudcoder.app.client.page;

import org.cloudcoder.app.client.Session;
import org.cloudcoder.app.shared.model.Course;
import org.cloudcoder.app.shared.model.Problem;
import org.cloudcoder.app.shared.util.Publisher;
import org.cloudcoder.app.shared.util.Subscriber;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class CoursesAndProblemsPageUI extends DockLayoutPanel implements Subscriber {
	private CloudCoderPage page;
	
	private Tree tree;
	
	public CoursesAndProblemsPageUI() {
		super(Unit.EM);
		
		setSize("640px", "480px");
		
		tree = new Tree();
		addWest(tree, 18.2);
		
		InlineLabel problemDescriptionLabel = new InlineLabel("");
		addNorth(problemDescriptionLabel, 7.7);
		
		DataGrid<Problem> cellTable = new DataGrid<Problem>();
		add(cellTable);
		cellTable.setSize("100%", "100%");
	}
	
	public void setPage(CloudCoderPage page) {
		this.page = page;
	}

	private static class CourseTreeItem extends TreeItem {
		private Course course;
		
		public CourseTreeItem(Course course) {
			super(course.getName());
			this.course = course;
		}
		
		public Course getCourse() {
			return course;
		}
	}
	
	@Override
	public void eventOccurred(Object key, Publisher publisher, Object hint) {
		if (key == Session.Event.ADDED_OBJECT && hint.getClass() == Course[].class) {
			Window.alert("Loading courses...");
			// Courses loaded
			tree.clear();
			Course[] courseList = (Course[]) hint;
			for (Course course : courseList) {
				tree.addItem(new CourseTreeItem(course));
				GWT.log("Added course " + course.getName());
			}
		}
	}
}

@@ -450,16 +450,64 @@
 Window;%0A
+import com.google.gwt.user.client.ui.Composite;%0A
 import c
@@ -739,31 +739,25 @@
 extends 
-DockLayoutPanel
+Composite
  impleme
@@ -870,29 +870,90 @@
 %7B%0A%09%09
-super(Unit.EM);%0A%09%09%0A%09%09
+DockLayoutPanel dockLayoutPanel = new DockLayoutPanel(Unit.EM);%0A%09%09dockLayoutPanel.
 setS
@@ -1001,16 +1001,32 @@
 ee();%0A%09%09
+dockLayoutPanel.
 addWest(
@@ -1104,16 +1104,32 @@
 (%22%22);%0A%09%09
+dockLayoutPanel.
 addNorth
@@ -1222,16 +1222,32 @@
 m%3E();%0A%09%09
+dockLayoutPanel.
 add(cell
@@ -1291,16 +1291,50 @@
 100%25%22);%0A
+%09%09%0A%09%09initWidget(dockLayoutPanel);%0A
 %09%7D%0A%09%0A%09pu

allow CoursesAndProblemsPageUI to be edited in GWT Designer