view test/editortest/REPSimpleEditor.java @ 414:784a4d67e6a5

(no commit message)
author one
date Tue, 09 Dec 2008 15:44:28 +0900
parents
children 648c676bf9df
line wrap: on
line source

package test.editortest;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import rep.REP;
import rep.REPCommand;
import test.Text;

public class REPSimpleEditor extends JFrame implements DocumentListener, ActionListener, LogTarget{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String BR = System.getProperty("line.separator");
	private Document document;
	private JTextArea textArea;
	private int seq;
	private int eid;
	private int sid;
	private JButton putButton;
	private JButton joinButton;
	private REPText repText;
	private TestEditor2 testEditor;
	private JSplitPane splitPane;
	private JScrollPane scrollPane1;
	private JTextArea console;
	private JScrollPane scrollPane2;

	public REPSimpleEditor(String title){
		super(title);
		setSize(new Dimension(640, 480));
		this.setLayout(new BorderLayout());
		
		setToolBar();
		setEditor();
		setConsole();
		setSplitPane();
	}
	  
	public REPSimpleEditor(){
		this("Sample Editor");
	}
	
	private void setToolBar() {
		JToolBar toolbar = new JToolBar();
		putButton = new JButton("put");
		joinButton = new JButton("join");
		
		putButton.addActionListener(this);
		joinButton.addActionListener(this);
		
		toolbar.add(putButton);
		toolbar.add(joinButton);
		add(toolbar, BorderLayout.NORTH);
	}

	private void setEditor(){
		textArea = new JTextArea();
		textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize()));
		document = textArea.getDocument();
		document.addDocumentListener(this);
		
		scrollPane1 = new JScrollPane(textArea);
		
		repText = new REPTextImpl(textArea);
	}
	
	private void setConsole(){
		console = new JTextArea();
		console.setFont(new Font("Monaco", Font.PLAIN, console.getFont().getSize()));
		console.setEditable(false);
		scrollPane2 = new JScrollPane(console);
	}
	
	private void setSplitPane(){
		splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.add(scrollPane1, JSplitPane.TOP);
		splitPane.add(scrollPane2, JSplitPane.BOTTOM);
		splitPane.setDividerLocation(300);
		add(splitPane, BorderLayout.CENTER);
	}

	private REPCommand createREPCommand(int offset, int length) {
		REPCommand command = null;
		try {
			int lineno = textArea.getLineOfOffset(offset);
			int lineStart = textArea.getLineStartOffset(lineno);
			int lineEnd = textArea.getLineEndOffset(lineno);
			String text = textArea.getText(lineStart, lineEnd-lineStart);
			command = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, seq++, lineno, text);
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		Logger.printT(command);
		return command;
	}

	public void changedUpdate(DocumentEvent e) {
		Logger.print(e);
	}

	public void insertUpdate(DocumentEvent e) {
		int offset = e.getOffset();
		int length = e.getLength();
		createREPCommand(offset, length);
	}
	
	public void removeUpdate(DocumentEvent e) {
		Logger.print(e);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == putButton){
			repPut();
		}else if(e.getSource() == joinButton){
			repJoin();
		}
	}

	private void repJoin() {
		testEditor = new TestEditor2("TestEditor", "localhost", 8766, false);
		testEditor.setREPText(repText);
		testEditor.setLogTarget(this);
		testEditor.start();
	}

	private void repPut() {
		setMasterText();
		testEditor = new TestEditor2("TestEditor", "localhost", 8766, true);
		testEditor.setText(repText.list());
		testEditor.setREPText(repText);
		testEditor.setLogTarget(this);
		testEditor.start();
	}

	private void setMasterText() {
		textArea.append("AAAAA" + BR);
		textArea.append("BBBBB" + BR);
		textArea.append("CCCCC" + BR);
		textArea.append("DDDDD" + BR);
	}

	public void printLog(Object obj) {
		String log = obj.toString();
		console.append(log + BR);
	}

	public REPText getREPText() {
		return repText;
	}

	public void setText(Text text) {
		for(String str : text){
			textArea.append(str + BR);
		}
	}

}