# HG changeset patch # User one # Date 1230702075 -32400 # Node ID c921022bf494b348770867059e8712250a538975 # Parent e22430832991a8ad6e1ba04855a6c0a8b6b55ac7 (no commit message) diff -r e22430832991 -r c921022bf494 bin/remoteeditor/Activator.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/MyDialog.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/editors/RemoteEditor$1.class Binary file bin/remoteeditor/editors/RemoteEditor$1.class has changed diff -r e22430832991 -r c921022bf494 bin/remoteeditor/editors/RemoteEditor.class Binary file bin/remoteeditor/editors/RemoteEditor.class has changed diff -r e22430832991 -r c921022bf494 bin/remoteeditor/network/REP.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/network/RSocketEvent.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/network/RSocketListener.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/ui/CommitDialog$TextBox.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/ui/CommitDialog.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/ui/RepInputDialog.class diff -r e22430832991 -r c921022bf494 bin/remoteeditor/ui/TestDialog.class diff -r e22430832991 -r c921022bf494 src/remoteeditor/editors/REPText.java --- a/src/remoteeditor/editors/REPText.java Mon Nov 10 01:14:22 2008 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package remoteeditor.editors; - -import org.eclipse.jface.text.IDocument; -import org.eclipse.swt.widgets.Display; - -public class REPText { - - public REPText(Display display, IDocument document){ - - } - - public void insert(int line, String text){ - - } - - public void delete(int line, String text){ - - } - -} diff -r e22430832991 -r c921022bf494 src/remoteeditor/editors/RemoteEditor.java --- a/src/remoteeditor/editors/RemoteEditor.java Mon Nov 10 01:14:22 2008 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,322 +0,0 @@ -package remoteeditor.editors; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.SocketChannel; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.DocumentEvent; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IDocumentListener; -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; - - -public class RemoteEditor extends TextEditor implements REPCommandListener, IDocumentListener{ - - private ISourceViewer viewer; - private IDocument document; - - private REPPacketReceive repreceive; - private REPPacketSend repsend; - - private int numberOfLinesOld; - private SocketChannel sc; - private int myeid = 0; - private int myseq = 0; - private int mysid = 0; - private String filename = "afro"; - private String BR = System.getProperty("line.separator"); - public boolean others = false; - - public RemoteEditor() { - super(); - } - - public void createPartControl(Composite parent) { - super.createPartControl(parent); - viewer = getSourceViewer(); - document = viewer.getDocument(); - filename = this.getEditorInput().getName(); - numberOfLinesOld = document.getNumberOfLines(); - - document.addDocumentListener(this); - - int port = 8766; - 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); - - repreceive.addCommandListener(this); - if(document.getLength() < 1){ - repJoin(); - }else { - repPut(); - } - } - - public void dispose() { - super.dispose(); - } - - public void repJoin(){ - repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, "")); - } - - public void repPut(){ - if(this.getEditorInput() != null) filename = this.getEditorInput().getName(); - repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename)); - } - - - public void repSelect(){ - repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, "")); - } - - public void CommandReceived(REPCommandEvent event) { - REPCommand receivedCommand = event.getCommand(); - if(receivedCommand.eid == myeid){ - System.out.println("my REP Command."); - }else{ - - } - manage(receivedCommand); - } - - private void manage(REPCommand command) { - switch(command.cmd){ - case REP.SMCMD_JOIN_ACK: - mysid = command.sid; - myeid = command.eid; - sendSync(); - break; - - case REP.SMCMD_PUT_ACK: - mysid = command.sid; - myeid = command.eid; - break; - - case REP.SMCMD_SELECT_ACK: - mysid = command.sid; - break; - - case REP.SMCMD_SYNC: - receiveSync(command); - break; - - case REP.REPCMD_INSERT: - try { - if(command.eid == myeid) break; //mergerを導入する時に消す - insertText(command.lineno, command.len, command.string); - } catch (Exception e) { - e.printStackTrace(); - } - repsend.send(command); - break; - - case REP.REPCMD_DELETE: - try { - if(command.eid == myeid) break;//mergerを導入する時に消す - deleteText(command.lineno, command.len, command.string); - } catch (BadLocationException e1) { - e1.printStackTrace(); - } - repsend.send(command); - break; - - } - } - - private void sendSync() { - REPCommand command = new REPCommand(REP.SMCMD_SYNC, mysid, myeid, this.myseq++, 0, 0, ""); - repsend.send(command); - } - - private void receiveSync(REPCommand command) { - if(this.myeid != 1) return; - int lines = document.getNumberOfLines(); - for(int i = 0; i < lines; i++){ - - String text = null; - try { - text = document.get(document.getLineOffset(i), document.getLineLength(i)); - } catch (BadLocationException e) { - e.printStackTrace(); - } - REPCommand deleteCommand = new REPCommand(REP.REPCMD_DELETE, mysid, myeid, myseq++, i, text.length(), text); - repsend.send(deleteCommand); - REPCommand insertCommand = new REPCommand(REP.REPCMD_INSERT, mysid, myeid, myseq++, i, text.length(), text); - repsend.send(insertCommand); - - } - } - - private void deleteText(final int lineNo, final int len, final String text) throws BadLocationException { - if(document.getNumberOfLines() < lineNo+1){ - increaseLine(); - deleteText(lineNo, len, text); - } - final int offset = document.getLineOffset(lineNo); - - final int replaceLength = document.getLineLength(lineNo); - - viewer.getTextWidget().getDisplay().syncExec(new Runnable() { - public void run() { - try { - others = true; - document.replace(offset, replaceLength, ""); - others = false; - } catch (BadLocationException e) { - e.printStackTrace(); - } - } - }); - } - - private void insertText(final int lineNo, final int len, final String text) throws BadLocationException { - if(document.getNumberOfLines() < lineNo+1){ - increaseLine(); - insertText(lineNo, len, text); - } - final int offset = document.getLineOffset(lineNo); - final String changedText = text + BR; - - viewer.getTextWidget().getDisplay().syncExec(new Runnable() { - public void run() { - try { - others = true; - document.replace(offset, 0, changedText); - others = false; - } catch (BadLocationException e) { - e.printStackTrace(); - - } - } - }); - } - - private void increaseLine(){ - viewer.getTextWidget().getDisplay().syncExec(new Runnable() { - public void run() { - try { - others = true; - document.replace(document.getLength(), 0, BR); - others = false; - } catch (BadLocationException e) { - e.printStackTrace(); - - } - } - }); - } - - public void documentAboutToBeChanged(DocumentEvent event) { - if(others == true) return; - - IDocument bDoc = event.getDocument(); - int lineno = 0; - String replacedText = null; - - try { - lineno = bDoc.getLineOfOffset(event.getOffset()); - replacedText = bDoc.get(event.getOffset(), event.getLength()); - } catch (BadLocationException e) { - e.printStackTrace(); - } - - String text = ""; - try{ - String str = new String(getLineText(lineno, bDoc)); - String[] splited = str.split(BR); - if(splited.length > 0){ - text = splited[0]; - }else{ - text = ""; - } - }catch(Exception e){ - e.printStackTrace(); - } - sendDelete(lineno, text); - - if(replacedText.contains(BR)){ - String text2 = getLineText(lineno+1, bDoc).split(BR)[0]; - sendDelete(lineno, text2); - } - - - } - - public void documentChanged(DocumentEvent event) { - if(others == true) return; - - IDocument doc = event.getDocument(); - int numberOfLines = doc.getNumberOfLines(); - int lineno = 0; - - try { - lineno = doc.getLineOfOffset(event.getOffset()); - } catch (BadLocationException e) { - e.printStackTrace(); - } - - try{ - String text = getLineText(lineno, doc).split(BR)[0]; - sendInsert(lineno, text); - - if(numberOfLines > numberOfLinesOld){ - String text2; - String str = new String(getLineText(lineno+1, doc)); - String[] split = str.split(BR); - if(split.length > 0){ - text2 = getLineText(lineno+1, doc).split(BR)[0]; - }else{ - text2 = ""; - } - sendInsert(lineno+1, text2); - } - }catch (Exception e){ - e.printStackTrace(); - } - numberOfLinesOld = numberOfLines; - } - - private void sendInsert(int lineno, String text) { - REPCommand command = new REPCommand(REP.REPCMD_INSERT, mysid, myeid, this.myseq++, lineno, text.length(), text); - repsend.send(command); - } - - private void sendDelete(int lineno, String text) { - REPCommand command = new REPCommand(REP.REPCMD_DELETE, mysid, myeid, this.myseq++, lineno, text.length(), text); - repsend.send(command); - //System.out.println("RemoteEditor.sendDelete() : command = " + command.toString()); - } - - private String getLineText(int lineno, IDocument document2) { - String text = null; - try{ - int lineOffset = document2.getLineOffset(lineno); - int length = document2.getLineLength(lineno); - text = document2.get(lineOffset, length); - }catch(BadLocationException e){ } - return text; - } -}