OSDN Git Service

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