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

(no commit message)
author one
date Tue, 20 Jan 2009 18:39:02 +0900
parents d1bfcff0cdd2
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 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;

public class SimpleEditorForREPEditor extends JFrame implements ActionListener, LogTarget{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton putButton;
	private JButton joinButton;
	private JTextField lineField;
	private JTextField textField;
	private JTextArea textArea;
	private JScrollPane scrollPane1;
	private JTextArea console;
	private JScrollPane scrollPane2;
	private JSplitPane splitPane;
	private String BR = System.getProperty("line.separator");
	private JButton deleteButton;
	private JButton insertButton;
	private REPEditor repEditor;
	
	public SimpleEditorForREPEditor(String title){
		super(title);
		setSize(new Dimension(640, 480));
		setLayout(new BorderLayout());
		
		setToolBar();
		setEditor();
		setConsole();
		setSplitPane();
	}
	
	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();
		
		deleteButton = new JButton("delete");
		insertButton = new JButton("insert");
		
		toolbar.add(putButton);
		toolbar.add(joinButton);
		toolbar.addSeparator();
		toolbar.add(label1);
		toolbar.add(lineField);
		toolbar.add(label2);
		toolbar.add(textField);
		toolbar.addSeparator();
		toolbar.add(deleteButton);
		toolbar.add(insertButton);
		
		add(toolbar, BorderLayout.NORTH);
	}
	
	private void setEditor(){
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize()));
		
		scrollPane1 = new JScrollPane(textArea);
	}
	
	private void setConsole(){
		console = new JTextArea();
		console.setFont(new Font("Monaco", Font.PLAIN, console.getFont().getSize()-2));
		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 actionPerformed(ActionEvent e) {
		if(e.getSource() == putButton){
			repPut();
		}else if(e.getSource() == joinButton){
			repJoin();
		}
	}

	public void repPut() {
		repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), true);
		repEditor.start();
		repEditor.setLogTarget(this);
		putButton.setEnabled(false);
		joinButton.setEnabled(false);
	}

	public void repJoin() {
		repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), false);
		repEditor.start();
		repEditor.setLogTarget(this);
		putButton.setEnabled(false);
		joinButton.setEnabled(false);
	}

	public void printLog(String msg) {
		console.append(msg + BR);
	}
	
	public static void main(String[] args){
		SimpleEditorForREPEditor editor = new SimpleEditorForREPEditor("Simple Editor");
		editor.setVisible(true);
	}

	public REPEditor getREPEditor() {
		return repEditor;
	}

}