package org.fedorahosted.flies.webtrans.editor.table;

import org.fedorahosted.flies.gwt.model.TransUnit;


import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.weborient.codemirror.client.HighlightingLabel;
import com.weborient.codemirror.client.ParserSyntax;

public class SourcePanel extends Composite implements HasValue<TransUnit> {

	private final FlowPanel panel;
	private final Label sourceLabel;
	private final TextArea textarea;
	private TransUnit value;
	
	public SourcePanel(TransUnit value) {
		this.value = value;
		panel = new FlowPanel();
		panel.setSize("100%", "100%");
		initWidget(panel);
		setStylePrimaryName("TableEditorSource");

		sourceLabel = new HighlightingLabel(value.getSource(), ParserSyntax.MIXED);
		sourceLabel.setStylePrimaryName("TableEditorContent");
		sourceLabel.setTitle("Source Comment: " + value.getSourceComment());
		
		panel.add(sourceLabel);
		textarea = new TextArea();
		textarea.getSelectedText();
		sinkEvents(Event.MOUSEEVENTS);
		refresh();
	}
	
	public void refresh() {
	}
	
	@Override
	public TransUnit getValue() {
		return value;
	}
	@Override
	public void setValue(TransUnit value) {
		setValue(value, true);
	}
	
	@Override
	public void setValue(TransUnit value, boolean fireEvents) {
		if(this.value != value) {
			this.value = value;
			if(fireEvents) {
				ValueChangeEvent.fire(this, value);
			}
			refresh();
		}
	}
	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<TransUnit> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}
	
	public void onBrowserEvent(Event event) {
		if (event.getTypeInt() == Event.ONMOUSEDOWN) {
			//DOM.setCapture(sourceLabel.getElement());
		}
		if (event.getTypeInt() == Event.ONMOUSEUP) {
			//Element elem = DOM.getCaptureElement();
			//DOM.releaseCapture(this.getElement());
			//String str = elem.getString();
			//String str = getSelection();
		}
	
		super.onBrowserEvent(event);		
	}

	  public static native String getSelection() /*-{
	    var txt = '';
	    if ($wnd.getSelection) {
	      txt = $wnd.getSelection();
	    } else if ($wnd.document.getSelection) {
	      txt = $wnd.document.getSelection();
	    } else if ($wnd.document.selection) {
	      txt = $wnd.document.selection.createRange().text;
	    } else {
	      return;
	    }
	    return txt;
	  }-*/;

}

@@ -1566,41 +1566,8 @@
 ();%0A
-%09%09sinkEvents(Event.MOUSEEVENTS);%0A
 %09%09re
@@ -2133,786 +2133,6 @@
 %0A%09%7D%0A
-%09%0A%09public void onBrowserEvent(Event event) %7B%0A%09%09if (event.getTypeInt() == Event.ONMOUSEDOWN) %7B%0A%09%09%09//DOM.setCapture(sourceLabel.getElement());%0A%09%09%7D%0A%09%09if (event.getTypeInt() == Event.ONMOUSEUP) %7B%0A%09%09%09//Element elem = DOM.getCaptureElement();%0A%09%09%09//DOM.releaseCapture(this.getElement());%0A%09%09%09//String str = elem.getString();%0A%09%09%09//String str = getSelection();%0A%09%09%7D%0A%09%0A%09%09super.onBrowserEvent(event);%09%09%0A%09%7D%0A%0A%09  public static native String getSelection() /*-%7B%0A%09    var txt = '';%0A%09    if ($wnd.getSelection) %7B%0A%09      txt = $wnd.getSelection();%0A%09    %7D else if ($wnd.document.getSelection) %7B%0A%09      txt = $wnd.document.getSelection();%0A%09    %7D else if ($wnd.document.selection) %7B%0A%09      txt = $wnd.document.selection.createRange().text;%0A%09    %7D else %7B%0A%09      return;%0A%09    %7D%0A%09    return txt;%0A%09  %7D-*/;%0A%0A
 %7D%0A

Remove obsolete code in Source Panel