view src/remoteeditor/editors/RemoteEditor.java @ 38:99dc7f13ed80

*** empty log message ***
author pin
date Fri, 06 Apr 2007 02:36:23 +0900
parents 7f346cf2a07b
children 188f09aff2be
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.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
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 remoteeditor.ui.REPSelectWindow;
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;
	private int myeid = 0;
	private int myseq = 0;
	private int mysid = 0;
	private String filename = "afro";
	
	public RemoteEditor() {
		super();
		//filename = this.getEditorInput().getName();

	}
	
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		viewer = getSourceViewer();
		viewer.addTextListener(this);
		document = viewer.getDocument();
		filename = this.getEditorInput().getName();
		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);
		repsend = new REPPacketSend(sc);
		

		IWorkbench workbench = PlatformUI.getWorkbench();
		Display display = workbench.getDisplay();
		REPSelectWindow selectwindow = new REPSelectWindow(display);
		selectwindow.initWindow();
		selectwindow.setName(this.getEditorInput().getName());
		selectwindow.setChannel(sc);
		selectwindow.open();
		
		//String sessionlist = selectwindow.getSessionList();
		//String sessionID = selectwindow.getSessionName();
		
		//joinPart();
		REPCommand temp = repreceive.unpack();
		myeid = temp.eid; mysid = temp.sid;
		repreceive.addCommandListener(this);
		repsend.send(new REPCommand(REP.REPCMD_READ, mysid, myeid, myseq, 0, 0, ""));
	}
	
	public void dispose() {
		//rep.dispose();
		super.dispose();
	}
	public void textChanged(TextEvent event) {
		String replacedText = event.getReplacedText();
		String inputText = event.getText();
				
		// ページ先頭からの文字数(改行を含む)による座標
		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 を取得してます。
			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 を 行数で判断
			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);
		repsend.send(new REPCommand(cmd, mysid, myeid, myseq, line, lineText.length(), lineText));
		numberOfLinesOld = numberOfLinesNew;
	}
	
	public 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, 0, ""));
	}
	
	public void putPart(){
		if(this.getEditorInput() != null) filename = this.getEditorInput().getName();
		repsend.send(new REPCommand(REP.REP_PUT_CMD, mysid, myeid, myseq, 0, filename.length(), filename));
	}
	
	
	public void selectPart(){
		repsend.send(new REPCommand(REP.REP_SELECT_CMD, mysid, myeid, myseq, 0, 0, ""));
	}

	public void CommandReceived(REPCommandEvent event) {
		REPCommand receivedCommand = event.getCommand();
		//System.out.println("received command : " + receivedCommand.toString());
		commandManager(receivedCommand);
	}

	private void commandManager(REPCommand command) {
		switch(command.cmd){
		case REP.REP_JOIN_ACK_CMD:
			myeid = command.eid;
			putPart();
			break;
		case REP.REP_PUT_ACK_CMD:
			mysid = command.sid;
			selectPart();
			break;
		case REP.REP_SELECT_ACK_CMD:
			
			break;
		case REP.REP_INSERT_CMD:
			try {
					changeText(command.cmd, command.lineno, command.len, command.string);
				} catch (Exception e) {
					e.printStackTrace();
				}
			break;
		case REP.REPCMD_READ:
			try {
					receiveReadCMD();
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			break;
		}
	}

	private void receiveReadCMD() throws BadLocationException {
		for(int i = 0; i < document.getNumberOfLines(); i++){
			int offset = document.getLineOffset(i);
			int length = document.getLineLength(i);
			String text = document.get(offset, length);
			repsend.send(new REPCommand(REP.REP_INSERT_CMD, mysid, myeid, myseq, i, text.length(), text));
		}
	}
}