view test/editortest/REPSimpleEditor.java @ 416:b7f42fc75a36

(no commit message)
author one
date Wed, 31 Dec 2008 14:47:39 +0900
parents 648c676bf9df
children
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 java.io.IOException;
import java.net.InetSocketAddress;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
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 rep.REPCommandPacker;
import rep.channel.REPSocketChannel;
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;
	private JTextField lineField;
	private JTextField textField;
	private REPSocketChannel<REPCommand> channel;

	public REPSimpleEditor(String title){
		super(title);
		setSize(new Dimension(640, 480));
		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);
		
		JLabel label1 = new JLabel("line");
		JLabel label2 = new JLabel("text");
		lineField = new JTextField();
		textField = new JTextField();
		
		textField.addActionListener(this);
		
		toolbar.add(putButton);
		toolbar.add(joinButton);
		toolbar.addSeparator();
		toolbar.add(label1);
		toolbar.add(lineField);
		toolbar.add(label2);
		toolbar.add(textField);
		
		add(toolbar, BorderLayout.NORTH);
	}

	private void setEditor(){
		textArea = new JTextArea();
		textArea.setEditable(false);
		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);
	}

	public void setButtonEnabled(boolean b) {
		putButton.setEnabled(b);
		joinButton.setEnabled(b);
	}

	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();
		}
		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();
		}else if(e.getSource() == textField){
			testEditor.addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 0, textField.getText()));
			lineField.setText("");
			textField.setText("");
		}
	}

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

	private void repPut() {
		setMasterText();
		connectToSessionManager();
//		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);
	}

	private void connectToSessionManager() {
		InetSocketAddress semaIP = new InetSocketAddress("localhost", 8766);
		try {
			channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
		} catch (IOException e) {
			e.printStackTrace(); return;
		}
		try {
			while (!channel.connect(semaIP)){
				Logger.print("SeMa not listen to socket yet, wait");
			}
		} catch (IOException e) {
			e.printStackTrace(); return;
		}
		
	}

	public void printLog(String msg) {
		console.append(msg + BR);
	}

	public REPText getREPText() {
		return repText;
	}

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

	public void setTestEditor(TestEditor2 testEditor2) {
		testEditor = testEditor2;
	}

}