Mercurial > hg > Database > Christie
changeset 221:7e5f18a5e612
add RemoteEditor & TextEditor
author | ichikitakahiro <e165713@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 19 Nov 2019 16:51:26 +0900 |
parents | 4e89fc999de9 |
children | 3bee0ab985a4 |
files | src/main/java/christie/codegear/CodeGear.java src/main/java/christie/remotingTextEditor/CatchDG.java src/main/java/christie/remotingTextEditor/SetInstance.java src/main/java/christie/remotingTextEditor/StartTwoEditor.java src/main/java/christie/textEditor/StartEditor.java src/main/java/christie/textEditor/mainFrame.java src/main/java/christie/textEditor/menuActionOpen.java src/main/java/christie/textEditor/textFrame.java |
diffstat | 8 files changed, 319 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/christie/codegear/CodeGear.java Fri Mar 15 19:41:44 2019 +0900 +++ b/src/main/java/christie/codegear/CodeGear.java Tue Nov 19 16:51:26 2019 +0900 @@ -57,7 +57,7 @@ Command command = new CommandBuilder().init(type).cg(this) .cgmID(cgm.cgmID) .toDgmName(toDgmName) - .key(key) + .key(key) .dg(dg) .build(); commandList.add(command);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/remotingTextEditor/CatchDG.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,18 @@ +package christie.remotingTextEditor; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +public class CatchDG extends CodeGear { + @Take + int loc; + + + + @Override + protected void run(CodeGearManager cgm) { + System.out.println(loc); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/remotingTextEditor/SetInstance.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,37 @@ +package christie.remotingTextEditor; + +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.*; +import christie.textEditor.*; + +import java.io.FileWriter; + + +public class SetInstance extends CodeGear { + + protected void run(CodeGearManager cgm){ + TextFrame myEditor = new TextFrame(); + myEditor.StartEditor(); + } + + /* + @Override + public void insertUpdate(DocumentEvent e) { + System.out.println("catch"); + } + + @Override + public void removeUpdate(DocumentEvent e) { + } + + @Override + public void changedUpdate(DocumentEvent e) { + } + */ +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/remotingTextEditor/StartTwoEditor.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,18 @@ +package christie.remotingTextEditor; + +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.codegear.StartCodeGear; + +public class StartTwoEditor extends StartCodeGear{ + public StartTwoEditor(CodeGearManager cgm) { + super(cgm); + } + + public static void main(String[] args){ + CodeGearManager editor1 = createCGM(10001); + CodeGearManager editor2 = createCGM(10002); + editor1.setup(new SetInstance()); + editor2.setup(new SetInstance()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/textEditor/StartEditor.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,14 @@ +package christie.textEditor; + +public class StartEditor { + public int loc; + public static void main(String[] args) { + MainFrame mainFrame = MainFrame.getInstance(); + mainFrame.setVisible(true); + } + + public void StartEditor(){ + MainFrame mainFrame = MainFrame.getInstance(); + mainFrame.setVisible(true); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/textEditor/mainFrame.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,87 @@ +package christie.textEditor; + +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.event.KeyEvent; + +import java.io.File; +import java.io.IOException; + +import javax.swing.JDesktopPane; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.KeyStroke;; + +public class MainFrame extends JFrame{ + private JDesktopPane desktop; + + private JFileChooser fileChooser; + + private static MainFrame instance; + + private MainFrame(){ + //タイトルを設定 + super("テキストエディタ") ; + setSize(1024,768); + //ウィンドウを閉じたらアプリを終了する。 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + //メインウィンドウにコンポーネントを配置する領域とのこと。 + Container container = this.getContentPane(); + + //メニューバーを初期化 + JMenuBar menuBar = new JMenuBar(); + //コンテナにメニューバーを配置する(ウインドウ上部に) + container.add(menuBar, BorderLayout.NORTH); + JMenu menuFile = new JMenu("ファイル"); + menuBar.add(menuFile); + //開くメニューの作成 + JMenuItem menuOpen = new JMenuItem(new MenuActionOpen()); + //CTRT + oショートカットキーの設定 + menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_DOWN_MASK)); + //ファイルメニューに開くを追加する + menuFile.add(menuOpen); + + desktop = new JDesktopPane(); + container.add(desktop); + + TextFrame textFrame = new TextFrame(); + desktop.add(textFrame); + textFrame.setVisible(true); + + //ファイル選択ダイアグの初期化 + fileChooser = new JFileChooser(); + //テキストファイル(*.txt)のみ表示するフィルターを追加 + //fileChooser.addChoosableFileFilter(new TextFileFilter()); + } + + void openFile(){ + //ファイルを開くダイアログを表示する + int result = fileChooser.showOpenDialog(this); + //ファイル選択時の処理 + if (JFileChooser.APPROVE_OPTION == result) { + File selectedFile = fileChooser.getSelectedFile(); + System.out.println(selectedFile); + try{ + TextFrame textFrame = new TextFrame(selectedFile); + //JDesktopPaneにテキストウィンドウを追加 + desktop.add(textFrame); + textFrame.setVisible(true); + }catch(IOException e){ + JOptionPane.showMessageDialog(this, "IOExeption: ファイルを開くのに失敗しました。"); + } + } + } + + public static MainFrame getInstance(){ + if (instance == null){ + instance = new MainFrame(); + } + return instance; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/textEditor/menuActionOpen.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,17 @@ +package christie.textEditor; + +import java.awt.event.ActionEvent; + +import javax.swing.AbstractAction; + +class MenuActionOpen extends AbstractAction{ + + @Override + public void actionPerformed(ActionEvent e){ + MainFrame.getInstance().openFile(); + } + + MenuActionOpen(){ + super("開く"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/textEditor/textFrame.java Tue Nov 19 16:51:26 2019 +0900 @@ -0,0 +1,127 @@ +package christie.textEditor; + +import christie.remotingTextEditor.SetInstance; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import javax.swing.JInternalFrame; +import javax.swing.JTextArea; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; + + +public class TextFrame extends JInternalFrame { + private JTextArea textArea; + + private static String DEFAULT_CHARACTER_CODE = "Shift_JIS"; + + public int loc = 0; + + SetInstance setInstance = new SetInstance(); + + 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 class MyDocumentListener implements DocumentListener { + public void insertUpdate(DocumentEvent e) { + Document doc = (Document) e.getDocument(); + loc = e.getOffset(); + System.out.print("location = " + loc + "\n"); + ReturnToChristie(loc); + try { + System.out.print("string = " + doc.getText(loc, 1) + "\n"); + + } catch (BadLocationException e1) { + e1.printStackTrace(); + } + } + + @Override + public void removeUpdate(DocumentEvent e) { + Document doc = (Document) e.getDocument(); + int loc = e.getOffset(); + int e_length = e.getLength(); + int del_loc_end = loc + e_length - 1; + if (e_length == 1) { + System.out.println("delete " + loc); + } else { + System.out.println("delete " + loc + " to " + del_loc_end); + } + } + + @Override + public void changedUpdate(DocumentEvent e) { + } + } + + public void ReturnToChristie(int loc){ + System.out.println("return" + loc); + } + + public void StartEditor(){ + MainFrame mainFrame = MainFrame.getInstance(); + mainFrame.setVisible(true); + System.out.println("move"); + } +}