diff --git a/src/main/java/info/istlab/IoTP/App.java b/src/main/java/info/istlab/IoTP/App.java index ec2701b..5856993 100644 --- a/src/main/java/info/istlab/IoTP/App.java +++ b/src/main/java/info/istlab/IoTP/App.java @@ -1,25 +1,60 @@ package info.istlab.IoTP; +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; + /** * Hello world! * */ -public class App -{ - static String workingDir = "/Users/miura/php/M5StickCPlus_FactoryTest2022/SampleSrc"; - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); +public class App { + static String userhome; + static ArrayList iotDirs; + static String workingDir; + + static { + userhome = System.getProperty("user.home"); + } - new Launcher(args).setVisible(true); + public static void main(String[] args) { - // SerialPort[] ports = SerialPort.getCommPorts(); - // for(SerialPort sp: ports){ - // String name = sp.getSystemPortName(); - // if (name.startsWith("cu.usbserial")){ - // new SerialWindow(sp); - // } - // } + // IoTPフォルダを探す + iotDirs = new ArrayList(); + findDotGitFileRecursive(new File(userhome), iotDirs, 0); // 指定フォルダ以下の.gitを探索する + + if (iotDirs.size() == 1) { + workingDir = iotDirs.get(0).getAbsolutePath(); + System.out.println(workingDir); + new Launcher(iotDirs.get(0)).setVisible(true); + } else { + System.out.println("フォルダが複数みつかりました"); + for(File f: iotDirs){ + System.out.println(f.getAbsolutePath()); + } + } + + } + + public static void findDotGitFileRecursive(File dir, ArrayList iotDirs, int level) { + if (level > 3) + return; + // System.out.println("checking " + dir.getAbsolutePath()); + File[] list = dir.listFiles(new FileFilter() { + public boolean accept(File path) { + return (path.isDirectory()); + } + }); + if (list == null) + return; + for (File tempdir : list) { + // if (tempdir.getName().equals(".git")) { + if (tempdir.getName().equals("SampleSrc")) { + iotDirs.add(tempdir); + } else { + findDotGitFileRecursive(tempdir, iotDirs, level + 1); + } + } } } diff --git a/src/main/java/info/istlab/IoTP/Editor.java b/src/main/java/info/istlab/IoTP/Editor.java index d0e82e4..2539a90 100644 --- a/src/main/java/info/istlab/IoTP/Editor.java +++ b/src/main/java/info/istlab/IoTP/Editor.java @@ -4,6 +4,7 @@ import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; +import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; @@ -56,13 +57,16 @@ textArea.addKeyListener(this); try { - setTitle(file.getCanonicalPath().replaceAll(App.workingDir, "~")); + setTitle(file.getCanonicalPath().replaceAll(App.workingDir+"/", "")); } catch (IOException e) { e.printStackTrace(); } setSize(new Dimension(800, 400));// pack(); - setLocationRelativeTo(Launcher.theapp); - setLocation(Launcher.theapp.getWidth(), Launcher.theapp.file2editor.size() * 40); + if (Launcher.theapp != null){ + Point launchP = Launcher.theapp.getLocationOnScreen(); + setLocation(launchP.x + Launcher.theapp.getWidth(), launchP.y ); + } + // setLocationRelativeTo(Launcher.theapp); setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); load(); @@ -106,11 +110,11 @@ topP.add(runB); runner = new ScriptRunner(file.getAbsolutePath(), runB, ""); } else if (textArea.getSyntaxEditingStyle() == SyntaxConstants.SYNTAX_STYLE_C) { - compileB = new JButton("Compile"); + // compileB = new JButton("Compile"); execB = new JButton("Upload"); - compileB.addActionListener(this); + // compileB.addActionListener(this); execB.addActionListener(this); - topP.add(compileB); + // topP.add(compileB); topP.add(execB); // comlineOption = new JTextField("arg1 arg2 ...", 15); // comlineOption.setForeground(Color.GRAY); @@ -195,12 +199,8 @@ load(); } else if (e.getActionCommand().equals("Run")) { runner.startstop(); - runB.setText("Stop"); - } else if (e.getActionCommand().equals("Stop")) { - runner.startstop(); - runB.setText("Run"); - } else if (e.getActionCommand().equals("Compile")) { - // new JCompiler(file.getAbsolutePath()); + // } else if (e.getActionCommand().equals("Compile")) { + // new ScriptRunner("Upload.sh", execB, file.getName()).startstop(); } else if (e.getActionCommand().equals("Upload")) { new ScriptRunner("Upload.sh", execB, file.getName()).startstop(); diff --git a/src/main/java/info/istlab/IoTP/FileTreeModel.java b/src/main/java/info/istlab/IoTP/FileTreeModel.java index d454718..f47f334 100644 --- a/src/main/java/info/istlab/IoTP/FileTreeModel.java +++ b/src/main/java/info/istlab/IoTP/FileTreeModel.java @@ -7,16 +7,6 @@ import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; -class MyFile extends File { - public MyFile(String pathname) { - super(pathname); - } - - public String toString() { - return super.toString().replaceAll(App.workingDir, "~"); - } -} - public class FileTreeModel implements TreeModel { @@ -41,7 +31,7 @@ private File[] getChildFiles(File parent) { if (isExcludeClassFiles) { - return parent.listFiles(path -> !path.toString().endsWith(".class")); + return parent.listFiles(path -> !path.toString().endsWith(".txt")); } else { return parent.listFiles(); } diff --git a/src/main/java/info/istlab/IoTP/JTAConsole.java b/src/main/java/info/istlab/IoTP/JTAConsole.java index 8742d0a..ef0856f 100644 --- a/src/main/java/info/istlab/IoTP/JTAConsole.java +++ b/src/main/java/info/istlab/IoTP/JTAConsole.java @@ -1,5 +1,6 @@ package info.istlab.IoTP; +import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; @@ -59,12 +60,11 @@ frame.setLocation(lastFrameP.x + 470, lastFrameP.y); } } else { - // if (Editor.lastOpened != null) { - // Point lastEditorP = Editor.lastOpened.getLocationOnScreen(); - // Dimension lastEditorD = Editor.lastOpened.getSize(); - // frame.setLocation(lastEditorP.x, lastEditorP.y + lastEditorD.height); - // } - + if (Editor.lastOpened != null) { + Point lastEditorP = Editor.lastOpened.getLocationOnScreen(); + Dimension lastEditorD = Editor.lastOpened.getSize(); + frame.setLocation(lastEditorP.x, lastEditorP.y + lastEditorD.height); + } } addKeyListener(this); addMouseListener(this); diff --git a/src/main/java/info/istlab/IoTP/Launcher.java b/src/main/java/info/istlab/IoTP/Launcher.java index 1e51ba2..4e43754 100644 --- a/src/main/java/info/istlab/IoTP/Launcher.java +++ b/src/main/java/info/istlab/IoTP/Launcher.java @@ -27,7 +27,7 @@ public class Launcher extends JFrame implements MouseInputListener, KeyListener { public static Launcher theapp; - public static String version = "0.72"; + public static String version = "0.1"; // JPanel mainP; File root; JTree tree; @@ -37,17 +37,14 @@ // all generated processes (maybe alive only) public static ArrayList allProcs = new ArrayList(); - public Launcher(String[] args) { - super("Kiso NWP Launcher v" + version); + public Launcher(File _root) { + super("IoTP Launcher v" + version); setDefaultCloseOperation(EXIT_ON_CLOSE); // mainP = new JPanel(); file2editor = new Hashtable(); theapp = this; - if (args.length > 0) - root = new MyFile(args[0]); - else - root = new MyFile(App.workingDir); + root = _root; // Create a TreeModel object to represent our tree of files FileTreeModel model = new FileTreeModel(root, false); @@ -74,18 +71,18 @@ JMenu menu = new JMenu("App"); /**************************************************** */ menuBar.add(menu); JMenuItem mi = new JMenuItem("show version"); - mi.addActionListener(ae -> JOptionPane.showMessageDialog(this, "Kiso NWP Launcher v" + version)); + mi.addActionListener(ae -> JOptionPane.showMessageDialog(this, "IoTP Launcher v" + version)); menu.add(mi); - mi = new JMenuItem("最新版をチェック"); - // mi.addActionListener(ae -> downloadLatest()); - menu.add(mi); - - // mi = new JMenuItem("open download site"); - // mi.addActionListener(ae -> openURL(App.downloadurl)); + // mi = new JMenuItem("最新版をチェック"); + // // mi.addActionListener(ae -> downloadLatest()); // menu.add(mi); - mi = new JMenuItem("open JAR folder"); + mi = new JMenuItem("シリアルモニタを開く"); + mi.addActionListener(ae -> SerialWindow.invoke() ); + menu.add(mi); + + mi = new JMenuItem("サンプルコードのフォルダを開く"); mi.addActionListener(ae -> openExecPath()); menu.add(mi); @@ -118,13 +115,13 @@ menu = new JMenu(isEnglish ? "Process" : "プロセス"); /**************************************************** */ menuBar.add(menu); - mi = new JMenuItem("stop alive"); - mi.addActionListener(ae -> stopProcess(false)); - menu.add(mi); + // mi = new JMenuItem("stop alive"); + // mi.addActionListener(ae -> stopProcess(false)); + // menu.add(mi); - mi = new JMenuItem("stop all"); - mi.addActionListener(ae -> stopProcess(true)); - menu.add(mi); + // mi = new JMenuItem("stop all"); + // mi.addActionListener(ae -> stopProcess(true)); + // menu.add(mi); mi = new JMenuItem("jps (Javaのプロセス確認)"); mi.addActionListener(ae -> runCommand(ae.getActionCommand(), null)); @@ -145,6 +142,7 @@ // getContentPane().add(mainP, BorderLayout.WEST); setSize(310, 600); + setLocation(SerialWindow.centerOfScreen(getSize())); } public void renameOrDeleteNWPFolder() { diff --git a/src/main/java/info/istlab/IoTP/MyFile.java b/src/main/java/info/istlab/IoTP/MyFile.java new file mode 100644 index 0000000..14483fc --- /dev/null +++ b/src/main/java/info/istlab/IoTP/MyFile.java @@ -0,0 +1,14 @@ +package info.istlab.IoTP; + +import java.io.File; + +public class MyFile extends File { + public MyFile(String pathname) { + super(pathname); + } + + public String toString() { + return super.toString().replaceAll(App.workingDir+"/", ""); + } +} + diff --git a/src/main/java/info/istlab/IoTP/MyTreeCellRenderer.java b/src/main/java/info/istlab/IoTP/MyTreeCellRenderer.java index 23a4f04..fbae666 100644 --- a/src/main/java/info/istlab/IoTP/MyTreeCellRenderer.java +++ b/src/main/java/info/istlab/IoTP/MyTreeCellRenderer.java @@ -2,6 +2,7 @@ import java.awt.Color; import java.awt.Component; +import java.io.File; import javax.swing.JTree; import javax.swing.tree.DefaultTreeCellRenderer; @@ -14,7 +15,7 @@ super.getTreeCellRendererComponent(tree, value, sel, exp, leaf, row, hasFocus); // Assuming you have a tree of Strings - String node = (String) ((MyFile) value).toString(); + String node = (String) ((File) value).toString(); // If the node is a leaf and ends with "xxx" if (leaf && node.endsWith(".class")) { diff --git a/src/main/java/info/istlab/IoTP/ScriptRunner.java b/src/main/java/info/istlab/IoTP/ScriptRunner.java index 015cb2e..dcfddae 100644 --- a/src/main/java/info/istlab/IoTP/ScriptRunner.java +++ b/src/main/java/info/istlab/IoTP/ScriptRunner.java @@ -16,6 +16,7 @@ Thread thread; Process process; JButton runB; + String originalButtonLabel; public ScriptRunner(String shfile, JButton rB, String targetfile) { scriptfile = shfile; @@ -27,11 +28,12 @@ if (thread == null) { thread = new Thread(this); thread.start(); - runB.setText("Stop"); + // originalButtonLabel = runB.getText(); + // runB.setText("Stop"); } else { process.destroyForcibly(); thread = null; - runB.setText("Run"); + // runB.setText(originalButtonLabel); } } @@ -48,7 +50,7 @@ // ProcessBuilder processBuilder = new ProcessBuilder("pwd"); processBuilder.directory(new File(App.workingDir)); // processBuilder.inheritIO(); - JTAConsole con = new JTAConsole("(Exec) " + scriptfile); + JTAConsole con = new JTAConsole("(Exec) " + scriptfile+ " "+srcfile); // con.setMainSrcByFileName(scriptfile); con.Systemoutprintln("=== 実行開始 ==="); // con.Systemoutprintln(CommandRunner.prompt+"cd ~" + App.nwpsrc+" (注:~ はチルダ記号〜 で,ホームディレクトリを表します)"); @@ -70,7 +72,8 @@ } con.Systemoutprintln("=== 終了 === (ALT+Wで閉じる)"); thread = null; - runB.setText("Run"); + // runB.setText("Run"); + SerialWindow.invoke(); } } diff --git a/src/main/java/info/istlab/IoTP/SerialWindow.java b/src/main/java/info/istlab/IoTP/SerialWindow.java index ff1e703..f18fb87 100644 --- a/src/main/java/info/istlab/IoTP/SerialWindow.java +++ b/src/main/java/info/istlab/IoTP/SerialWindow.java @@ -1,14 +1,19 @@ package info.istlab.IoTP; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; -import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @@ -17,58 +22,80 @@ import com.fazecast.jSerialComm.SerialPortDataListener; import com.fazecast.jSerialComm.SerialPortEvent; -public class SerialWindow extends JFrame implements SerialPortDataListener, ActionListener, Runnable { +public class SerialWindow extends JFrame implements SerialPortDataListener, ActionListener, Runnable, WindowListener { SerialPort sp; JTextField jtf; JTextArea jta; Thread thread; - JButton compileB; + // JButton compileB; + + public static void invoke() { + SerialPort[] ports = SerialPort.getCommPorts(); + for (SerialPort sp : ports) { + String name = sp.getSystemPortName(); + if (name.startsWith("cu.usbserial")) { + new SerialWindow(sp); + } + } + } public SerialWindow(SerialPort _sp) { - super("SerialMonitor "+_sp.getSystemPortName()); + super("SerialMonitor " + _sp.getSystemPortName()); sp = _sp; sp.setBaudRate(115200); sp.addDataListener(this); System.out.println(sp.getSystemPortName()); if (!sp.openPort()) { + JOptionPane.showMessageDialog(null, "他のプログラムがシリアルポート "+sp.getSystemPortName()+" を使用しています。"); System.out.println("Unable to open the port."); return; } - jtf = new JTextField(); + jtf = new JTextField("(ここに入力してEnterを押すと、シリアル送信します)"); + jtf.setForeground(Color.GRAY); + jtf.addFocusListener(new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + if (jtf.getText().equals("(ここに入力してEnterを押すと、シリアル送信します)")) { + jtf.setText(""); + jtf.setForeground(Color.BLACK); + } + } + + @Override + public void focusLost(FocusEvent e) { + if (jtf.getText().isEmpty()) { + jtf.setForeground(Color.GRAY); + jtf.setText("(ここに入力してEnterを押すと、シリアル送信します)"); + } + } + }); + getContentPane().add(jtf, BorderLayout.NORTH); jta = new JTextArea(); getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER); - compileB = new JButton("compile"); - compileB.addActionListener(e -> { compile(e.getActionCommand()); }); - getContentPane().add(compileB, BorderLayout.SOUTH); + // compileB = new JButton("compile"); + // compileB.addActionListener(e -> { + // compile(e.getActionCommand()); + // }); + // getContentPane().add(compileB, BorderLayout.SOUTH); setSize(500, 400); setLocation(centerOfScreen(getSize())); setVisible(true); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); jtf.addActionListener(this); - } - - public void compile(String cmd){ - System.out.println("Compile"); - new ScriptRunner(App.workingDir+"/Upload.sh", compileB,"serial01.ino").startstop(); - - // new JExecutor(file.getAbsolutePath(), comlineOption.getText()); + addWindowListener(this); // Xをおしたらserial closeする } @Override public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; - // throw new UnsupportedOperationException("Unimplemented method - // 'getListeningEvents'"); } @Override public void serialEvent(SerialPortEvent event) { - // throw new UnsupportedOperationException("Unimplemented method - // 'serialEvent'"); try { int evt = event.getEventType(); @@ -82,7 +109,6 @@ closeSerialPort(); return; } - // System.out.println(bytesToRead + " byte(s) available."); byte[] newData = new byte[bytesToRead]; sp.readBytes(newData, bytesToRead); String s = new String(newData, "UTF8"); @@ -94,12 +120,12 @@ closeSerialPort(); } } - void appendToJTA(String s){ - if (jta == null) return; - if (s.contains("waiting for download")){ - // 一旦切断して、再度接続する - closeSerialPort(); + void appendToJTA(String s) { + if (jta == null) + return; + if (s.contains("waiting for download")) { + closeSerialPort(); //切断のみ。再接続はしない } jta.append(s); int len = jta.getDocument().getLength(); @@ -112,8 +138,9 @@ if (sp.isOpen()) { System.out.println("close Serial Port"); sp.closePort(); - thread = new Thread(this); - thread.start(); + jta.setBackground(Color.lightGray); + // thread = new Thread(this); + // thread.start(); } // sp = null; } @@ -129,17 +156,20 @@ public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); System.out.println(action); - try{ - String message = action+""; + try { + String message = action + ""; byte[] b = message.getBytes(); - System.out.println("send "+b.length +" bytes"); + System.out.println("send " + b.length + " bytes"); sp.writeBytes(b, b.length); - } catch (Exception ex){ + } catch (Exception ex) { System.out.println(ex.getMessage()); } } + /** + * 再接続用(つかっていない) + */ @Override public void run() { appendToJTA("(Waiting to reconnect.)\n"); @@ -147,7 +177,7 @@ Thread.sleep(10000); } catch (InterruptedException e) { } - while(thread != null){ + while (thread != null) { if (!sp.openPort()) { System.out.println("Unable to open the port."); return; @@ -159,7 +189,37 @@ Thread.sleep(2000); } catch (InterruptedException e) { } - + } } + + @Override + public void windowOpened(WindowEvent e) { + } + + @Override + public void windowClosing(WindowEvent e) { + closeSerialPort(); + } + + @Override + public void windowClosed(WindowEvent e) { + closeSerialPort(); + } + + @Override + public void windowIconified(WindowEvent e) { + } + + @Override + public void windowDeiconified(WindowEvent e) { + } + + @Override + public void windowActivated(WindowEvent e) { + } + + @Override + public void windowDeactivated(WindowEvent e) { + } }