view src/remoteeditor/editors/REPTextImpl2.java @ 213:bea1625524fe default tip

when you put and join to SessionManager, this plugin open a window for inputting address and port of SessionManager
author kazz
date Mon, 20 Dec 2010 14:14:41 +0900
parents ca9f72b8b4ab
children
line wrap: on
line source

package remoteeditor.editors;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

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.swt.widgets.Display;

public class REPTextImpl2 implements REPText, IDocumentListener {
	
	private IDocument document;
	private Display display;
	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; 
		this.display = display;
		textListenerList = new LinkedList<REPTextListener>();
		document.addDocumentListener(this);
	}

	public String delete(int lineno) {
		int d = lineno - document.getNumberOfLines();
		for(int i = 0; i <= d; i++){
			increaseLine();
		}
		
		String deleted = getLineText(lineno);
		try {
			final int length = document.getLineLength(lineno);
			final int offset = document.getLineOffset(lineno);
			edit(offset, length, "");
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return deleted;
	}

	public void insert(int lineno, String text) {
		int d = lineno - document.getNumberOfLines();
		for(int i = 0; i <= d; i++){
			increaseLine();
		}
		
		try {
			final int offset = document.getLineOffset(lineno);
			final String changedText = text + BR;
			edit(offset, 0, changedText);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	private void increaseLine() {
		edit(document.getLength(), 0, BR);
	}

	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 {
					myInput = true;
					document.replace(offset, length, text);
					myInput = false;
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public List<String> list() {
		LinkedList<String> list = new LinkedList<String>();
		int lines = document.getNumberOfLines();
		for(int i = 0; i < lines; i++){
			String text = getLineText(i);
			list.add(text);
		}
		return list;
	}
	
	private String getLineText(int lineno){
		int offset = 0;
		int length = 0;
		String text = null;
		try {
			offset = document.getLineOffset(lineno);
			length = document.getLineLength(lineno);
			String tmp = document.get(offset, length);
			text = tmp.replace(BR, "");
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return text;
	}

	public void documentAboutToBeChanged(DocumentEvent event) {
		if(myInput) return;
		int offset = event.getOffset();
		int length = event.getLength();
		
		try {
			int startLine = document.getLineOfOffset(offset);
			int endLine = document.getLineOfOffset(offset + length);
			for(int i = endLine; i >= startLine; i--){
				String text = getLineText(i);
				for(REPTextListener listener : textListenerList){
					listener.textDeleted(new REPTextEvent(i, text));
				}
			}
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
	}

	public void documentChanged(DocumentEvent event) {
		if(myInput) return;
		int offset = event.getOffset();
		int length = event.getText().length();
		
		try {
			int startLine = document.getLineOfOffset(offset);
			int endLine = document.getLineOfOffset(offset + length);
			for(int i = startLine; i <= endLine; i++){
				String text = getLineText(i);
				for(REPTextListener listener : textListenerList){
					listener.textInserted(new REPTextEvent(i, text));
				}
			}
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	public synchronized void addTextListener(REPTextListener listener) {
		Logger.print(listener);
		textListenerList.add(listener);
	}

	public String get(int lineno) {
		return getLineText(lineno);
	}

	public int size() {
		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;
	}

}