Newer
Older
adjustgpx-gui / src / main / java / osm / jp / gpx / matchtime / gui / DoDialog.java
@haya4 haya4 on 8 Mar 2020 7 KB JDK1.8
  1. package osm.jp.gpx.matchtime.gui;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Point;
  8. import java.awt.event.ActionEvent;
  9. import java.io.*;
  10. import javax.swing.*;
  11.  
  12. /**
  13. * 処理
  14. */
  15. @SuppressWarnings("serial")
  16. public class DoDialog extends JDialog {
  17. public static final String TITLE = "Do Command";
  18. // Used for addNotify check.
  19. boolean fComponentsAdjusted = false;
  20. String[] args;
  21. //{{DECLARE_CONTROLS
  22. JPanel buttonPanel; // ボタン配置パネル (下部)
  23. JButton closeButton; // [クローズ]ボタン
  24. JButton doButton; // [実行]ボタン
  25. JTextArea textArea; // 実行結果を表示するJTextArea (中央)
  26. //}}
  27.  
  28. public DoDialog(String[] args) {
  29. super(); // モーダルダイアログを基盤にする
  30. this.args = args;
  31. // INIT_CONTROLS
  32. Container container = getContentPane();
  33. container.setLayout(new BorderLayout());
  34. //parentFrame.setVisible(false);
  35. setSize(getInsets().left + getInsets().right + 980, getInsets().top + getInsets().bottom + 480);
  36. setTitle(DoDialog.TITLE);
  37. // コントロールパネル
  38. buttonPanel = new JPanel();
  39.  
  40. doButton = new JButton("実行");
  41. doButton.setToolTipText("処理を実行します.");
  42. doButton.setEnabled(true);
  43. doButton.addActionListener((ActionEvent event) -> {
  44. // 処理中であることを示すため
  45. // ボタンの文字列を変更し,使用不可にする
  46. doButton.setText("処理中...");
  47. doButton.setEnabled(false);
  48. // SwingWorker を生成し,実行する
  49. LongTaskWorker worker = new LongTaskWorker(doButton);
  50. worker.execute();
  51. });
  52. buttonPanel.add(doButton);
  53.  
  54. closeButton = new JButton("閉じる");
  55. closeButton.setToolTipText("処理を終了します.");
  56. closeButton.addActionListener((ActionEvent event) -> {
  57. dispose();
  58. });
  59. buttonPanel.add(closeButton);
  60. this.getContentPane().add("South", buttonPanel);
  61. // 説明文
  62. textArea = new JTextArea();
  63. JScrollPane sc=new JScrollPane(textArea);
  64. textArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,12));
  65. textArea.setTabSize(4);
  66. this.getContentPane().add("Center", sc);
  67. try {
  68. textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture");
  69. for (String arg : args) {
  70. textArea.append(" '" + arg + "'");
  71. }
  72. textArea.append("\n\n");
  73. }
  74. catch (Exception e) {
  75. System.out.println(e.toString());
  76. }
  77.  
  78. // JFrameの表示
  79. //parentFrame.setVisible(true);
  80. }
  81. /**
  82. * Shows or hides the component depending on the boolean flag b.
  83. * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
  84. * @see java.awt.Component#isVisible
  85. */
  86. @Override
  87. public void setVisible(boolean b) {
  88. if(b) {
  89. setLocation(80, 80);
  90. }
  91. super.setVisible(b);
  92. }
  93.  
  94. @Override
  95. public void addNotify() {
  96. // Record the size of the window prior to calling parents addNotify.
  97. Dimension d = getSize();
  98.  
  99. super.addNotify();
  100.  
  101. if (fComponentsAdjusted) {
  102. return;
  103. }
  104.  
  105. // Adjust components according to the insets
  106. setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
  107. Component components[] = getComponents();
  108. for (Component component : components) {
  109. Point p = component.getLocation();
  110. p.translate(getInsets().left, getInsets().top);
  111. component.setLocation(p);
  112. }
  113. fComponentsAdjusted = true;
  114. }
  115.  
  116. /**
  117. * JTextAreaに書き出すOutputStream
  118. */
  119. public static class JTextAreaOutputStream extends OutputStream {
  120. private final ByteArrayOutputStream os;
  121. /** 書き出し対象 */
  122. private final JTextArea textArea;
  123. private final String encode;
  124.  
  125. public JTextAreaOutputStream(JTextArea textArea, String encode) {
  126. this.textArea = textArea;
  127. this.encode = encode;
  128. this.os = new ByteArrayOutputStream();
  129. }
  130. /**
  131. * OutputStream#write(byte[])のオーバーライド
  132. * @param arg
  133. * @throws java.io.IOException
  134. */
  135. @Override
  136. public void write(int arg) throws IOException {
  137. this.os.write(arg);
  138. }
  139. /**
  140. * flush()でJTextAreaに書き出す
  141. * @throws java.io.IOException
  142. */
  143. @Override
  144. public void flush() throws IOException {
  145. // 文字列のエンコード
  146. final String str = new String(this.os.toByteArray(), this.encode);
  147. // 実際の書き出し処理
  148. SwingUtilities.invokeLater(
  149. new Runnable(){
  150. @Override
  151. public void run() {
  152. JTextAreaOutputStream.this.textArea.append(str);
  153. }
  154. }
  155. );
  156. // 書き出した内容はクリアする
  157. this.os.reset();
  158. }
  159. }
  160. // 非同期に行う処理を記述するためのクラス
  161. class LongTaskWorker extends SwingWorker<Object, Object> {
  162. private final JButton button;
  163.  
  164. public LongTaskWorker(JButton button) {
  165. this.button = button;
  166. }
  167.  
  168. // 非同期に行われる処理
  169. @Override
  170. public Object doInBackground() {
  171. // ながーい処理
  172. PrintStream defOut = System.out;
  173. PrintStream defErr = System.err;
  174.  
  175. OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8");
  176. PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく
  177.  
  178. // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定
  179. System.setOut(stdout);
  180. System.setErr(stdout);
  181.  
  182. try {
  183. Command command = new Command(osm.jp.gpx.ImportPicture.class);
  184. command.setArgs(args);
  185. command.start(); // コマンドを実行
  186. while (command.isAlive()) {
  187. try {
  188. Thread.sleep(1000);
  189. } catch (InterruptedException e) {}
  190. }
  191. }
  192. catch(Exception e) {
  193. e.printStackTrace(stdout);
  194. }
  195. finally {
  196. System.setOut(defOut);
  197. System.setErr(defErr);
  198. doButton.setEnabled(true);
  199. }
  200.  
  201. return null;
  202. }
  203.  
  204. // 非同期処理後に実行
  205. @Override
  206. protected void done() {
  207. // 処理が終了したので,文字列を元に戻し
  208. // ボタンを使用可能にする
  209. button.setText("実行");
  210. button.setEnabled(true);
  211. }
  212. }
  213. }