package name.shamansir.sametimed.wave.chat;

import java.util.List;

import name.shamansir.sametimed.wave.chat.cursor.LinesExtractionCursor;
import name.shamansir.sametimed.wave.model.base.atom.ChatLine;

import org.waveprotocol.wave.examples.fedone.waveclient.common.ClientUtils;
import org.waveprotocol.wave.examples.fedone.waveclient.common.ClientWaveView;
import org.waveprotocol.wave.examples.fedone.waveclient.console.ScrollableWaveView.RenderMode;
import org.waveprotocol.wave.model.document.operation.BufferedDocOp;
import org.waveprotocol.wave.model.document.operation.impl.InitializationCursorAdapter;


/**
 * 
 * @author shamansir <shaman.sir@gmail.com>
 * 
 * Chat, gives the model of Chat in the List<ChatLine> form,
 * using the ClientWaveView as source
 * 
 * @see ClientWaveView
 * @see ChatLine
 * 
 */

// based on ScrollableWaveView
public class Chat {
	
	protected static final String DOCUMENT_ID = "main";	
	
	public static final String AUTHOR_ATTR_NAME = "by";
	public static final String LINE_TAG_NAME = "line";
	
	private ClientWaveView sourceWave;
	private RenderMode outputMode = RenderMode.NORMAL;
	
	public Chat(ClientWaveView sourceWave) {
		this.sourceWave = sourceWave;
	}
	
	public List<ChatLine> getChatLines() {		
	    BufferedDocOp document = ClientUtils.getConversationRoot(
				sourceWave).getDocuments().get(DOCUMENT_ID);
	    LinesExtractionCursor linesCursor = new LinesExtractionCursor(outputMode);
	    document.apply(new InitializationCursorAdapter(linesCursor));
	    
		return linesCursor.getExtractedLines();
	}

	public void setOutputMode(RenderMode outputMode) {
		this.outputMode  = outputMode;		
	}

}

@@ -34,24 +34,52 @@
 wave.chat;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1387,16 +1387,52 @@
 );%0A%09    
+%0A%09    if (document != null) %7B%0A%09    %09
 LinesExt
@@ -1503,16 +1503,17 @@
 );%0A%09    
+%09
 document
@@ -1571,18 +1571,16 @@
 );%0A%09    
-%0A%09
 %09return 
@@ -1612,16 +1612,80 @@
 ines();%0A
+%09    %7D else %7B%0A%09    %09return new ArrayList%3CChatLine%3E();%0A%09    %7D%0A%09%09%0A
 %09%7D%0A%0A%09pub

fix for issue 3 (chat not updated when opened new wave)