view src/main/java/christie/textEditor/TextFrame.java @ 267:1ac366f96815

remake for CommandPattern
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Wed, 29 Jan 2020 18:50:14 +0900
parents 824d75bafe67
children 1f42a0903440
line wrap: on
line source

package christie.textEditor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.StyleContext;

@org.msgpack.annotation.Message
class TextFrame extends JInternalFrame {
    private JTextArea textArea;

    private static String DEFAULT_CHARACTER_CODE = "Shift_JIS";

    public int loc = 0;

    public boolean send = false;

    private String inserted_string;

    private int sendLoc = 0;

    private int endLoc = 0;

    private boolean canWrite = true;

    private boolean dFrag = false;
//    文字列削除でtrue,されなければfalse

    StyleContext sc = new StyleContext();

    public TextFrame() {
        this("新規テキスト");
    }

    public TextFrame(String title) {
        //JInternalFrameのコンストラクタの呼び出しを実行
        super(title, true, true, true, true);
        //サイズの指定
        this.setSize(800, 600);
        // JTextArea(テキスト入力のコンポーネントを追加する。)
        textArea = new JTextArea();

        textArea.getDocument().addDocumentListener(new MyDocumentListener());

        //textArea.getDocument().putProperty("name", "Text Area");
        this.add(textArea);
    }


    public TextFrame(File file) throws IOException {
        this(file.getName());
        this.openFile(file);
    }

    void openFile(File file) throws IOException {
        FileInputStream fiStream = null;
        ByteArrayOutputStream baoStream = null;
        try {
            fiStream = new FileInputStream(file);
            System.out.print(fiStream);
            baoStream = new ByteArrayOutputStream();
            System.out.print(baoStream);
            //読み込みデータ格納用配列
            byte[] byteData = new byte[1];
            int ret = fiStream.read(byteData);
            //ファイルの最後まで繰り返す。
            while (ret != -1) {
                baoStream.write(byteData);
                ret = fiStream.read(byteData);
            }
            //バイト配列を文字列に変換、重い。
            String text = new String(baoStream.toByteArray(), DEFAULT_CHARACTER_CODE);
            //テキストGUIに読み込んだファイルの内容を設定
            textArea.setText(text);
            //タイトルを開いたファイル名へ変更
            this.setTitle(file.getName());
        } finally {
            if (fiStream != null) {
                fiStream.close();
            }
            if (baoStream != null) {
                baoStream.close();
            }
        }
    }

    public void insertText(int pos, String str){
        textArea.insert(str, pos);
    }

    public boolean SendPermission(){
        return send;
    }

    public void changeToFalseSend(){
        send = false;
    }

    public void protectToExcessiveInsert(){canWrite = false;}

    public boolean deleteFrag(){return dFrag;}

    public void changeToFalseDeleteFrag(){dFrag = false;};

    public int returnOffset(){
        return sendLoc;
    }

    public String returnString(){return inserted_string;}

    public int returnEndOffset(){return endLoc;}

    public void delete(int pos, int ePos) {
        if (pos == ePos) {
            textArea.replaceRange("", pos, pos + 1);
        } else {
            textArea.replaceRange("", pos, pos + 1);
        }
    }
    public class MyDocumentListener implements DocumentListener {
        public void insertUpdate(DocumentEvent e) {
            if(canWrite == true) {
                Document doc = e.getDocument();
                loc = e.getOffset();
                sendLoc = loc;
                try {
                    inserted_string = doc.getText(loc, 1);
                    System.out.println("string = " + doc.getText(loc, 1));
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
                send = true;
            }
            canWrite = true;

        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            if(canWrite == true) {
                Document doc = e.getDocument();
                sendLoc = e.getOffset();
                int e_length = e.getLength();
                endLoc = sendLoc + e_length;
                    if (e_length == 1) {
                        System.out.println("delete " + sendLoc);
                    } else {
                    System.out.println("delete " + sendLoc + " to " + endLoc);
                }
                dFrag = true;
            }
            canWrite = true;
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }

    }



    public static void StartEditor(){
        MainFrame mainFrame = MainFrame.getInstance();
        mainFrame.setVisible(true);
    }
}