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