OSDN Git Service

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