package org.omo.core;

import java.util.Collection;
import java.util.concurrent.ConcurrentSkipListMap;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TableModelView<K, V> extends AbstractTableModel implements View<K, V> {

	private final Log log = LogFactory.getLog(getClass());
	
	private Metadata<K, V> metadata;
	private final ConcurrentSkipListMap<K, V> map = new ConcurrentSkipListMap<K, V>();
	
	public void setMetadata(Metadata<K, V> metadata) {
		this.metadata = metadata;
	}

	// TableModel
	
	// Listener
	
	@Override
	public void update(Collection<V> updates) {
		//log.debug("UPDATE("+updates+")");
		if (updates != null)
			for (V insertion : updates) {
				K key = metadata.getKey(insertion);
				map.put(key, insertion);
				int index = map.headMap(key).size();
				fireTableRowsInserted(index, index);
			}
	}

	protected String columnNames[] = new String[]{"id", "version"};

	@Override
	public String getColumnName(int columnIndex) {
		return metadata.getAttributeNames().get(columnIndex);
	}
	
	@Override
	public int getColumnCount() {
		return metadata.getAttributeNames().size();
	}

	@Override
	public int getRowCount() {
		return map.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		V value = (V)map.values().toArray()[rowIndex]; // yikes !!
		return metadata.getAttributeValue(value, columnIndex);
	}

}

@@ -229,16 +229,87 @@
 ctory;%0A%0A
+import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;%0A%0A
 public c
@@ -443,16 +443,75 @@
 ss());%0A%09
+private final CountDownLatch latch = new CountDownLatch(1);
 %0A%09privat
@@ -704,18 +704,202 @@
 adata;%0A%09
-%7D%0A
+%09latch.countDown();%0A%09%7D%0A%0A%09public Metadata%3CK, V%3E getMetadata() %7B%0A%09%09try %7B%0A%09%09latch.await();%0A%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D%0A%09%09return metadata;%0A%09%7D%0A%09
 %0A%09// Tab
@@ -1017,16 +1017,59 @@
 s+%22)%22);%0A
+%09%09Metadata%3CK, V%3E metadata = getMetadata();%0A
 %09%09if (up
@@ -1406,32 +1406,37 @@
  %7B%0A%09%09return 
-m
+getM
 etadata
+()
 .getAttribut
@@ -1514,32 +1514,37 @@
  %7B%0A%09%09return 
-m
+getM
 etadata
+()
 .getAttribut
@@ -1768,24 +1768,29 @@
 %09return 
-m
+getM
 etadata
+()
 .getAttr

fix concurrency bug - updates were arriving before registration was setting up metadata - added a latch