changeset 199:61db2f70ceda

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 03 Sep 2010 09:38:45 +0900
parents 1f63cec3755c (current diff) ca9f72b8b4ab (diff)
children afa6f235d763
files
diffstat 3 files changed, 68 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/remoteeditor/editors/REPEditor.java	Fri Sep 03 09:38:17 2010 +0900
+++ b/src/remoteeditor/editors/REPEditor.java	Fri Sep 03 09:38:45 2010 +0900
@@ -27,7 +27,7 @@
 	private int eid;
 	private int sid;
 	private REPText repText;
-	private boolean hasInputLock;
+	private boolean hasInputLock = true;
 	private boolean master;
 	private boolean syncEnable = true;
 	
@@ -99,7 +99,7 @@
 	/*
 	 * Editor main loop with input lock
 	 */
-	private void mainloop() throws IOException {
+	void mainloop() throws IOException {
 		
 		channel.configureBlocking(false);
 		selector = REPSelector.create();
@@ -113,7 +113,8 @@
 				runners.clear();
 			}
 			
-			if (inputLock) {
+//			if(inputLock){
+			if (repText.isMerging()) {
 				// No user input during merge mode (optional)
 				if (selector.select(0)>0) {
 					handle(channel.read());
@@ -133,7 +134,7 @@
 		}
 	}
 
-	private void handle(REPCommand command) {
+	public void handle(REPCommand command) {
 		Logger.print(command);
 		if(command == null) return;
 		switch(command.cmd){
@@ -170,11 +171,13 @@
 		case SMCMD_START_MERGE :
 			// lock user input during merge (optional)
 			inputLock = hasInputLock;
+			repText.startMerge(this);
 			command.cmd = REP.SMCMD_START_MERGE_ACK;
 			send(command);
 			break;
 		case SMCMD_END_MERGE :
 			inputLock = false;
+			repText.endMerge();
 			break;
 		case SMCMD_SYNC:
 			// start contents sync with newly joined editor
--- a/src/remoteeditor/editors/REPText.java	Fri Sep 03 09:38:17 2010 +0900
+++ b/src/remoteeditor/editors/REPText.java	Fri Sep 03 09:38:45 2010 +0900
@@ -2,6 +2,8 @@
 
 import java.util.List;
 
+import org.eclipse.swt.widgets.Display;
+
 public interface REPText {
 	
 	public void insert(int lineno, String text);
@@ -16,4 +18,12 @@
 
 	public String get(int i);
 
+	public void startMerge(REPEditor editor);
+
+	public void endMerge();
+
+	public boolean isMerging();
+
+	public Display getDisplay();
+
 }
--- a/src/remoteeditor/editors/REPTextImpl2.java	Fri Sep 03 09:38:17 2010 +0900
+++ b/src/remoteeditor/editors/REPTextImpl2.java	Fri Sep 03 09:38:45 2010 +0900
@@ -1,5 +1,6 @@
 package remoteeditor.editors;
 
+import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -16,6 +17,7 @@
 	private String BR = System.getProperty("line.separator");
 	private List<REPTextListener> textListenerList;
 	private boolean myInput = false;
+	private boolean inMerging = false;
 
 	public REPTextImpl2(IDocument document, Display display){
 		this.document = document; 
@@ -61,6 +63,24 @@
 	}
 
 	private void edit(final int offset, final int length, final String text) {
+		if(inMerging){
+			edit2(offset, length, text);
+		}else{
+			edit1(offset, length, text);
+		}
+	}
+
+	private void edit2(int offset, int length, String text) {
+		try {
+			myInput = true;
+			document.replace(offset, length, text);
+			myInput = false;
+		} catch (BadLocationException e) {
+			e.printStackTrace();
+		}
+	}
+
+	private void edit1(final int offset, final int length, final String text) {
 		display.syncExec(new Runnable(){
 			public void run(){
 				try {
@@ -136,7 +156,6 @@
 		} catch (BadLocationException e) {
 			e.printStackTrace();
 		}
-		
 	}
 
 	public synchronized void addTextListener(REPTextListener listener) {
@@ -152,4 +171,35 @@
 		return document.getNumberOfLines();
 	}
 
+	public void doMerge(REPEditor editor) {
+		while(inMerging){
+			try {
+				editor.mainloop();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+
+	public void endMerge() {
+		inMerging = false;
+	}
+
+	public void startMerge(final REPEditor editor) {
+		inMerging = true;
+		display.syncExec(new Runnable(){
+			public void run(){
+				doMerge(editor);
+			}
+		});
+	}
+
+	public boolean isMerging() {
+		return inMerging;
+	}
+
+	public Display getDisplay() {
+		return display;
+	}
+
 }