view src/remoteeditor/editors/RemoteEditor.java @ 16:b8f407692ecf

*** empty log message ***
author pin
date Tue, 24 Oct 2006 18:03:13 +0900
parents 535f124c2fc3
children b409b85ab73f
line wrap: on
line source

package remoteeditor.editors;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.editors.text.TextEditor;

import remoteeditor.network.REP;
import remoteeditor.network.RSocketEvent;
import remoteeditor.network.RSocketListener;


public class RemoteEditor extends TextEditor implements ITextListener, RSocketListener{
	
	private ISourceViewer viewer;
	private IDocument document;
	
	REP rep;

	private int textOffset;

	public RemoteEditor() throws Exception {
		super();
		
		rep = new REP();
		rep.addSocketListener(this);
	}
	
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		viewer = getSourceViewer();
		viewer.addTextListener(this);
		document = viewer.getDocument();
		
	}
	
	public void dispose() {
		rep.dispose();
		super.dispose();
	}
	public void textChanged(TextEvent event) {
		String replacedText = event.getReplacedText();
		String inputText = event.getText();
		
		// ページ先頭からの文字数(改行を含む)による座標
		textOffset = event.getOffset();
		
		System.out.println("replace = " + replacedText);
		System.out.println("input = " + inputText);
		
		int line = 0;
		int offset;
		int length = 0;
		String lineText = null;
		try {
			line = document.getNumberOfLines(0, textOffset); // lineno を取得してます。
			offset = document.getLineOffset(line-1);
			length = document.getLineLength(line-1);
			lineText = document.get(offset, length);
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		
		try {
			rep.insert(line, length, lineText);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}


	public void packetReceived(RSocketEvent evt) {
		 	final int offset = evt.getLineNo();
			final int length = evt.getLength();
			final String text = evt.getText();
			System.out.println(offset + " : " + length);
			if(evt.getCmd() == REP.REP_INSERT_CMD){
			viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
				public void run() {
					try {
						document.replace(offset, 0, text);
					} catch (BadLocationException e) {
						e.printStackTrace();
					}
				}
		    });
			}
	}


}