view test/editortest/UserSimulator.java @ 419:7ff127c8ad64

(no commit message)
author one
date Tue, 20 Jan 2009 18:39:02 +0900
parents
children 5c95a9020e31
line wrap: on
line source

package test.editortest;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;

import rep.REP;
import rep.REPCommand;

public class UserSimulator extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton startButton;
	private LinkedList<SimpleEditorForREPEditor> editorList;
	private LinkedList<REPCommand> userInputList;
	private JButton initButton;
	
	public UserSimulator(String title){
		super(title);
		
		editorList = new LinkedList<SimpleEditorForREPEditor>();
		
		setButton();
		pack();
	}

	private void setUserInput() {
		userInputList = new LinkedList<REPCommand>();
		userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 0, "AAAAA"));
		userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 1, "BBBBB"));
		userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 2, "CCCCC"));
	}
	
	private void init(){
		setUserInput();
		editorList.get(0).repPut();
		for(int i = 1; i < editorList.size(); i++){
			editorList.get(i).repJoin();
		}
		initButton.setEnabled(false);
	}

	private void setButton() {
		setLayout(new FlowLayout());

		initButton = new JButton("init");
		startButton = new JButton("start");
		add(initButton);
		add(startButton);
		
		initButton.addActionListener(this);
		startButton.addActionListener(this);
	}

	public void add(SimpleEditorForREPEditor editor) {
		editorList.add(editor);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == startButton){
			userStart();
		}else if(e.getSource() == initButton){
			init();
		}
	}

	private void userStart() {
		UserThread user = new UserThread();
		user.start();
	}
	
	class UserThread extends Thread{
		public void run(){
//			for(REPCommand command : userInputList){
			for(int i = 0; i < 5; i++){
				for(SimpleEditorForREPEditor editor : editorList){
					REPCommand command = new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, i, editor.getTitle() + ":" + i);
					((REPTextWithJTextArea) editor.getREPEditor().getREPText()).userInsert(command);
				}
			}
		}
	}

}