/*******************************************************************************
 * Copyright (c) 2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package com.codenvy.ide.editor.codemirror.client;

import com.codenvy.ide.api.text.Region;
import com.codenvy.ide.editor.codemirror.client.jso.CMDocumentOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.CMPositionOverlay;
import com.codenvy.ide.jseditor.client.document.EmbeddedDocument;
import com.codenvy.ide.jseditor.client.events.CursorActivityHandler;
import com.codenvy.ide.jseditor.client.events.HasCursorActivityHandlers;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * CodeMirror implementation of {@link EmbeddedDocument}.
 *
 * @author "Mickaël Leduque"
 */
public class CodeMirrorDocument implements EmbeddedDocument {

    /** The internal document representation for CodeMirror. */
    private final CMDocumentOverlay         documentOverlay;

    private final HasCursorActivityHandlers hasCursorActivityHandlers;

    public CodeMirrorDocument(final CMDocumentOverlay documentOverlay,
                              final HasCursorActivityHandlers hasCursorActivityHandlers) {
        this.documentOverlay = documentOverlay;
        this.hasCursorActivityHandlers = hasCursorActivityHandlers;
    }

    @Override
    public TextPosition getPositionFromIndex(int index) {
        final CMPositionOverlay pos = this.documentOverlay.posFromIndex(index);
        return new TextPosition(pos.getLine(), pos.getCharacter());
    }

    @Override
    public int getIndexFromPosition(TextPosition position) {
        return documentOverlay.indexFromPos(CMPositionOverlay.create(position.getLine(), position.getCharacter()));
    }

    @Override
    public void setCursorPosition(TextPosition position) {
        this.documentOverlay.setCursor(position.getLine(), position.getCharacter());
    }

    @Override
    public TextPosition getCursorPosition() {
        final CMPositionOverlay pos = this.documentOverlay.getCursor();
        return new TextPosition(pos.getLine(), pos.getCharacter());
    }

    @Override
    public int getLineCount() {
        return this.documentOverlay.lineCount();
    }

    @Override
    public HandlerRegistration addCursorHandler(final CursorActivityHandler handler) {
        return this.hasCursorActivityHandlers.addCursorActivityHandler(handler);
    }

    @Override
    public String getContents() {
        return this.documentOverlay.getValue();
    }

    public void replace(final Region region, final String text) {
        final CMPositionOverlay fromPos = this.documentOverlay.posFromIndex(region.getOffset());
        final CMPositionOverlay toPos = this.documentOverlay.posFromIndex(region.getOffset() + region.getLength());

        this.documentOverlay.replaceRange(text, fromPos, toPos);
    }
}

@@ -735,32 +735,162 @@
 ositionOverlay;%0A
+import com.codenvy.ide.jseditor.client.document.DocumentEventBus;%0Aimport com.codenvy.ide.jseditor.client.document.DocumentHandle;%0A
 import com.coden
@@ -939,16 +939,16 @@
 cument;%0A
-
 import c
@@ -1295,16 +1295,32 @@
 Document
+, DocumentHandle
  %7B%0A%0A    
@@ -1441,16 +1441,16 @@
 erlay;%0A%0A
-
     priv
@@ -1513,16 +1513,87 @@
 dlers;%0A%0A
+    private final DocumentEventBus eventBus = new DocumentEventBus();%0A%0A
     publ
@@ -3410,11 +3410,807 @@
 );%0A    %7D
+%0A%0A    public DocumentHandle getDocumentHandle() %7B%0A        return this;%0A    %7D%0A%0A    @Override%0A    public boolean isSameAs(final DocumentHandle document) %7B%0A        return (this.equals(document));%0A    %7D%0A%0A    @Override%0A    public DocumentEventBus getDocEventBus() %7B%0A        return this.eventBus;%0A    %7D%0A%0A    @Override%0A    public EmbeddedDocument getDocument() %7B%0A        return this;%0A    %7D%0A%0A    public int getContentsCharCount() %7B%0A        // same as last offset%0A        final int lastLine = this.documentOverlay.lastLine();%0A        final String lineContent = this.documentOverlay.getLine(lastLine);%0A        final int lineSize = lineContent.length();%0A        // zero based char position on the line%0A        return this.documentOverlay.indexFromPos(CMPositionOverlay.create(lastLine, lineSize - 1));%0A    %7D
 %0A%7D%0A

Add getDocumentHandle method