Mercurial > hg > Database > Christie
changeset 246:824d75bafe67
fix small character java files
hello world now stop correctly
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 24 Jan 2020 18:23:20 +0900 |
parents | aad654568598 |
children | 901d65bad48d |
files | src/main/java/christie/textEditor/MainFrame.java src/main/java/christie/textEditor/MenuActionOpen.java src/main/java/christie/textEditor/TextFrame.java src/test/java/christie/example/HelloWorld/FinishHelloWorld.java src/test/java/christie/example/HelloWorld/HelloWorldCodeGear.java src/test/java/christie/example/HelloWorld/StartHelloWorld.java src/test/java/christie/example/PrefixTree/PrefixNode.java src/test/java/christie/example/RemoteTake/CreateRemoteTakeTest.java |
diffstat | 8 files changed, 305 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/textEditor/MainFrame.java Fri Jan 24 18:23:20 2020 +0900 @@ -0,0 +1,88 @@ +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;; + +class MainFrame extends JFrame { + private JDesktopPane desktop; + + private JFileChooser fileChooser; + + private static MainFrame instance; + + private MainFrame(){ + //タイトルを設定 + super("テキストエディタ") ; + setSize(1024,768); + //ウィンドウを閉じたらアプリを終了する。 + this.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にテキストウィンドウを追加 + this.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 Fri Jan 24 18:23:20 2020 +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 Fri Jan 24 18:23:20 2020 +0900 @@ -0,0 +1,154 @@ +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 boolean canWrite = true; + + 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 int returnOffset(){ + return sendLoc; + } + + public void prohibitDL(){canWrite = false;} + + public String returnString(){return inserted_string;} + + 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) { + Document doc = 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 static void StartEditor(){ + MainFrame mainFrame = MainFrame.getInstance(); + mainFrame.setVisible(true); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/christie/example/HelloWorld/FinishHelloWorld.java Fri Jan 24 18:23:20 2020 +0900 @@ -0,0 +1,15 @@ +package christie.example.HelloWorld; + +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +public class FinishHelloWorld extends CodeGear { + @Take String hello; + @Take String world; + + @Override + protected void run(CodeGearManager cgm) { + cgm.getLocalDGM().finish(); + } +}
--- a/src/test/java/christie/example/HelloWorld/HelloWorldCodeGear.java Fri Jan 24 18:03:02 2020 +0900 +++ b/src/test/java/christie/example/HelloWorld/HelloWorldCodeGear.java Fri Jan 24 18:23:20 2020 +0900 @@ -14,5 +14,6 @@ protected void run(CodeGearManager cgm) { System.out.print(helloWorld + " "); cgm.setup(new HelloWorldCodeGear()); + cgm.getLocalDGM().put(helloWorld,helloWorld); } } \ No newline at end of file
--- a/src/test/java/christie/example/HelloWorld/StartHelloWorld.java Fri Jan 24 18:03:02 2020 +0900 +++ b/src/test/java/christie/example/HelloWorld/StartHelloWorld.java Fri Jan 24 18:23:20 2020 +0900 @@ -12,6 +12,7 @@ public static void main(String[] args){ CodeGearManager cgm = createCGM(10000); cgm.setup(new HelloWorldCodeGear()); + cgm.setup(new FinishHelloWorld()); cgm.getLocalDGM().put("helloWorld","hello"); cgm.getLocalDGM().put("helloWorld","world"); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/christie/example/PrefixTree/PrefixNode.java Fri Jan 24 18:23:20 2020 +0900 @@ -0,0 +1,18 @@ +package christie.example.PrefixTree; + +import christie.codegear.CodeGearManager; +import christie.topology.node.StartTopologyNode; +import christie.topology.node.TopologyNodeConfig; + +public class PrefixNode { + + public static void main(String[] args){ + PrefixTreeNodeConfig prefixTreeNodeConfig = new PrefixTreeNodeConfig(args); + int totalNodeNum = prefixTreeNodeConfig.getTotalNodeNum(); + + StartTopologyNode startTopologyNode = new StartTopologyNode((TopologyNodeConfig) prefixTreeNodeConfig, new CheckMyName()); + + startTopologyNode.put("num", prefixTreeNodeConfig.getI()); + startTopologyNode.put("totalNodeNum", totalNodeNum - 1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/christie/example/RemoteTake/CreateRemoteTakeTest.java Fri Jan 24 18:23:20 2020 +0900 @@ -0,0 +1,11 @@ +package christie.example.RemoteTake; + +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +public class CreateRemoteTakeTest extends CodeGear { + @Override + protected void run(CodeGearManager cgm) { + + } +}