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;
9 import java.awt.event.ActionEvent;
16 @SuppressWarnings("serial")
17 public class DoRestamp extends JDialog {
18 public static final String TITLE = "Do Command";
20 // Used for addNotify check.
21 boolean fComponentsAdjusted = false;
25 JPanel buttonPanel; // ボタン配置パネル (下部)
26 JButton closeButton; // [クローズ]ボタン
27 JButton doButton; // [実行]ボタン
28 JTextArea textArea; // 実行結果を表示するJTextArea (中央)
31 @SuppressWarnings("OverridableMethodCallInConstructor")
32 public DoRestamp(String[] args) {
33 super(); // 親フォームなしのモーダルダイアログを基盤にする
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);
45 buttonPanel = new JPanel();
47 doButton = new JButton("実行");
48 doButton.setToolTipText("処理を実行します.");
49 doButton.setEnabled(true);
50 doButton.addActionListener((ActionEvent event) -> {
52 // ボタンの文字列を変更し,使用不可にする
53 doButton.setText("処理中...");
54 doButton.setEnabled(false);
56 // SwingWorker を生成し,実行する
57 LongTaskWorker worker = new LongTaskWorker(doButton);
60 buttonPanel.add(doButton);
62 closeButton = new JButton("閉じる");
63 closeButton.setToolTipText("処理を終了します.");
64 closeButton.addActionListener((ActionEvent event) -> {
67 buttonPanel.add(closeButton);
69 this.getContentPane().add("South", buttonPanel);
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);
79 textArea.append("> java -cp import.jar osm.jp.gpx.Restamp");
80 for (String arg : args) {
81 textArea.append(" '" + arg + "'");
83 textArea.append("\n\n");
86 System.out.println(e.toString());
90 //parentFrame.setVisible(true);
94 * Shows or hides the component depending on the boolean flag b.
95 * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
96 * @see java.awt.Component#isVisible
99 public void setVisible(boolean b) {
107 public void addNotify() {
108 // Record the size of the window prior to calling parents addNotify.
109 Dimension d = getSize();
113 if (fComponentsAdjusted) {
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);
125 fComponentsAdjusted = true;
130 * JTextAreaに書き出すOutputStream
132 public static class JTextAreaOutputStream extends OutputStream {
133 private final ByteArrayOutputStream os;
136 private final JTextArea textArea;
137 private final String encode;
139 public JTextAreaOutputStream(JTextArea textArea, String encode) {
140 this.textArea = textArea;
141 this.encode = encode;
142 this.os = new ByteArrayOutputStream();
146 * OutputStream#write(byte[])のオーバーライド
148 * @throws java.io.IOException
151 public void write(int arg) throws IOException {
156 * flush()でJTextAreaに書き出す
157 * @throws java.io.IOException
160 public void flush() throws IOException {
162 final String str = new String(this.os.toByteArray(), this.encode);
164 SwingUtilities.invokeLater(
168 JTextAreaOutputStream.this.textArea.append(str);
177 // 非同期に行う処理を記述するためのクラス
178 class LongTaskWorker extends SwingWorker<Object, Object> {
179 private final JButton button;
181 public LongTaskWorker(JButton button) {
182 this.button = button;
187 @SuppressWarnings("SleepWhileInLoop")
188 public Object doInBackground() {
190 PrintStream defOut = System.out;
191 PrintStream defErr = System.err;
193 OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8");
194 PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく
196 // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定
197 System.setOut(stdout);
198 System.setErr(stdout);
201 Command command = new Command(osm.jp.gpx.Restamp.class);
202 command.setArgs(args);
203 command.start(); // コマンドを実行
204 while (command.isAlive()) {
207 } catch (InterruptedException e) {}
211 e.printStackTrace(stdout);
214 System.setOut(defOut);
215 System.setErr(defErr);
216 doButton.setEnabled(true);
224 protected void done() {
225 // 処理が終了したので,文字列を元に戻し
227 button.setText("実行");
228 button.setEnabled(true);