view src/remoteeditor/editors/RemoteEditor.java @ 34:7d80c9318695

*** empty log message ***
author pin
date Wed, 31 Jan 2007 02:06:52 +0900 (2007-01-30)
parents d01cc686b54f
children 0c5701885b09
line wrap: on
line source
package remoteeditor.editors;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;

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.command.REPCommand;
import remoteeditor.command.REPCommandEvent;
import remoteeditor.command.REPCommandListener;
import remoteeditor.network.REP;
import remoteeditor.network.REPPacketReceive;
import remoteeditor.network.REPPacketSend;
import remoteeditor.network.RSocketEvent;
import remoteeditor.network.RSocketListener;
import sample.merge.Translate;


public class RemoteEditor extends TextEditor implements ITextListener, REPCommandListener{
	
	private ISourceViewer viewer;
	private IDocument document;
	
	REPPacketReceive repreceive;
	REPPacketSend repsend;
	Translate trans = new Translate();
	
	
	int numberOfLinesOld;
	int offset_con;
	private SocketChannel sc;

	public RemoteEditor() {
		super();
		
		int port = 8765;
		String host = "localhost";
		InetSocketAddress addr = new InetSocketAddress(host, port);
		try {
			sc = SocketChannel.open();
			sc.configureBlocking(true);
			sc.connect(addr);
			while(!sc.finishConnect()){
				System.out.println("afro");
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		repreceive = new REPPacketReceive(sc);
		repreceive.addCommandListener(this);
		repsend = new REPPacketSend(sc);
		joinPart();
	}
	
	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();
				
		// �y�[�W�擪����̕�����(���s���܂�)�ɂ����W
		int textOffset = event.getOffset();
		
		System.out.println("replace = " + replacedText);
		System.out.println("input = " + inputText + " : " + inputText.length());
		
		int line = 0;
		int offset = 0;
		int length = 0;
		int cmd = 0;
		int numberOfLinesNew = 0;
		String lineText = null;
		try {
			line = document.getNumberOfLines(0, textOffset); // lineno ���擾���Ă܂��B
			offset = document.getLineOffset(line-1);
			length = document.getLineLength(line-1);
			lineText = document.get(offset, length);
			numberOfLinesNew = document.getNumberOfLines();
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		if(numberOfLinesNew > numberOfLinesOld){
			//insert, delete, replace �� �s���Ŕ��f
			cmd = REP.REP_INSERT_CMD;
		}else if(numberOfLinesNew == numberOfLinesOld){
			cmd = REP.REP_REPLACE_CMD;
		}else {
			cmd = REP.REP_DELETE_CMD;
		}
		//rep.sendCmd(cmd, line, length, lineText);
		numberOfLinesOld = numberOfLinesNew;
	}
	
	void changeText(int kindOfCmd, int lineNo, int LineLength, String text) throws Exception{
		final int offset = document.getLineOffset(lineNo);
		final String changedText = text;
		final int replaceLength = document.getLineLength(lineNo);
		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
			public void run() {
				try {
					document.replace(offset, replaceLength, changedText);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void packetReceived(final RSocketEvent evt) {
		final int lineNo = evt.getLineNo();
		final int Linelength = evt.getLength();
		final String text = evt.getText();
		try {
			changeText(evt.getCmd(), lineNo, Linelength, text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void joinPart(){
		String string = "test.txt";
		repsend.send(new REPCommand(REP.REP_JOIN_CMD,0,0,0,0,string.length(),string));
	}

	public void CommandReceived(REPCommandEvent event) {
		System.out.println("test2");
	}
}