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;
8 import java.awt.event.ActionEvent;
15 @SuppressWarnings("serial")
16 public class DoDialog extends JDialog {
17 public static final String TITLE = "Do Command";
19 // Used for addNotify check.
20 boolean fComponentsAdjusted = false;
24 JPanel buttonPanel; // ボタン配置パネル (下部)
25 JButton closeButton; // [クローズ]ボタン
26 JButton doButton; // [実行]ボタン
27 JTextArea textArea; // 実行結果を表示するJTextArea (中央)
30 public DoDialog(String[] args) {
31 super(); // モーダルダイアログを基盤にする
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);
41 buttonPanel = new JPanel();
43 doButton = new JButton("実行");
44 doButton.setToolTipText("処理を実行します.");
45 doButton.setEnabled(true);
46 doButton.addActionListener((ActionEvent event) -> {
48 // ボタンの文字列を変更し,使用不可にする
49 doButton.setText("処理中...");
50 doButton.setEnabled(false);
52 // SwingWorker を生成し,実行する
53 LongTaskWorker worker = new LongTaskWorker(doButton);
56 buttonPanel.add(doButton);
58 closeButton = new JButton("閉じる");
59 closeButton.setToolTipText("処理を終了します.");
60 closeButton.addActionListener((ActionEvent event) -> {
63 buttonPanel.add(closeButton);
65 this.getContentPane().add("South", buttonPanel);
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);
75 textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture");
76 for (String arg : args) {
77 textArea.append(" '" + arg + "'");
79 textArea.append("\n\n");
82 System.out.println(e.toString());
87 * Shows or hides the component depending on the boolean flag b.
88 * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
89 * @see java.awt.Component#isVisible
92 public void setVisible(boolean b) {
100 public void addNotify() {
101 // Record the size of the window prior to calling parents addNotify.
102 Dimension d = getSize();
106 if (fComponentsAdjusted) {
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);
118 fComponentsAdjusted = true;
123 * JTextAreaに書き出すOutputStream
125 public static class JTextAreaOutputStream extends OutputStream {
126 private final ByteArrayOutputStream os;
129 private final JTextArea textArea;
130 private final String encode;
132 public JTextAreaOutputStream(JTextArea textArea, String encode) {
133 this.textArea = textArea;
134 this.encode = encode;
135 this.os = new ByteArrayOutputStream();
139 * OutputStream#write(byte[])のオーバーライド
141 * @throws java.io.IOException
144 public void write(int arg) throws IOException {
149 * flush()でJTextAreaに書き出す
150 * @throws java.io.IOException
153 public void flush() throws IOException {
155 final String str = new String(this.os.toByteArray(), this.encode);
157 SwingUtilities.invokeLater(
161 JTextAreaOutputStream.this.textArea.append(str);
170 // 非同期に行う処理を記述するためのクラス
171 class LongTaskWorker extends SwingWorker<Object, Object> {
172 private final JButton button;
174 public LongTaskWorker(JButton button) {
175 this.button = button;
180 public Object doInBackground() {
182 PrintStream defOut = System.out;
183 PrintStream defErr = System.err;
185 OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8");
186 PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく
188 // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定
189 System.setOut(stdout);
190 System.setErr(stdout);
193 Command command = new Command(osm.surveyor.matchtime.Restamp.class);
194 command.setArgs(args);
195 command.start(); // コマンドを実行
196 while (command.isAlive()) {
199 } catch (InterruptedException e) {}
203 e.printStackTrace(stdout);
206 System.setOut(defOut);
207 System.setErr(defErr);
208 doButton.setEnabled(true);
216 protected void done() {
217 // 処理が終了したので,文字列を元に戻し
219 button.setText("実行");
220 button.setEnabled(true);