OSDN Git Service

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