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;
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 JFrame parentFrame; // MatchTime.class
25 JPanel buttonPanel; // ボタン配置パネル (下部)
26 JButton closeButton; // [クローズ]ボタン
27 JButton doButton; // [実行]ボタン
28 JTextArea textArea; // 実行結果を表示するJTextArea (中央)
31 @SuppressWarnings("OverridableMethodCallInConstructor")
32 public DoDialog(JFrame parentFrame, String[] args) {
33 super(parentFrame, true); // モーダルダイアログを基盤にする
34 this.parentFrame = parentFrame;
38 @SuppressWarnings("OverridableMethodCallInConstructor")
39 Container container = getContentPane();
40 container.setLayout(new BorderLayout());
41 parentFrame.setVisible(false);
42 setSize(getInsets().left + getInsets().right + 980, getInsets().top + getInsets().bottom + 480);
43 setTitle(DoDialog.TITLE);
46 buttonPanel = new JPanel();
48 doButton = new JButton("実行");
49 doButton.setToolTipText("処理を実行します.");
50 doButton.setEnabled(true);
51 doButton.addActionListener((ActionEvent event) -> {
53 // ボタンの文字列を変更し,使用不可にする
54 doButton.setText("処理中...");
55 doButton.setEnabled(false);
57 // SwingWorker を生成し,実行する
58 LongTaskWorker worker = new LongTaskWorker(doButton);
61 buttonPanel.add(doButton);
63 closeButton = new JButton("閉じる");
64 closeButton.setToolTipText("処理を終了します.");
65 closeButton.addActionListener((ActionEvent event) -> {
68 buttonPanel.add(closeButton);
70 this.getContentPane().add("South", buttonPanel);
73 textArea = new JTextArea();
74 JScrollPane sc=new JScrollPane(textArea);
75 textArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,12));
76 textArea.setTabSize(4);
77 this.getContentPane().add("Center", sc);
80 textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture");
81 for (String arg : args) {
82 textArea.append(" '" + arg + "'");
84 textArea.append("\n\n");
87 System.out.println(e.toString());
91 parentFrame.setVisible(true);
95 * Shows or hides the component depending on the boolean flag b.
96 * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
97 * @see java.awt.Component#isVisible
100 public void setVisible(boolean b) {
108 public void addNotify() {
109 // Record the size of the window prior to calling parents addNotify.
110 Dimension d = getSize();
114 if (fComponentsAdjusted) {
118 // Adjust components according to the insets
119 setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
120 Component components[] = getComponents();
121 for (Component component : components) {
122 Point p = component.getLocation();
123 p.translate(getInsets().left, getInsets().top);
124 component.setLocation(p);
126 fComponentsAdjusted = true;
131 * JTextAreaに書き出すOutputStream
133 public static class JTextAreaOutputStream extends OutputStream {
134 private final ByteArrayOutputStream os;
137 private final JTextArea textArea;
138 private final String encode;
140 public JTextAreaOutputStream(JTextArea textArea, String encode) {
141 this.textArea = textArea;
142 this.encode = encode;
143 this.os = new ByteArrayOutputStream();
147 * OutputStream#write(byte[])のオーバーライド
149 * @throws java.io.IOException
152 public void write(int arg) throws IOException {
157 * flush()でJTextAreaに書き出す
158 * @throws java.io.IOException
161 public void flush() throws IOException {
163 final String str = new String(this.os.toByteArray(), this.encode);
165 SwingUtilities.invokeLater(
169 JTextAreaOutputStream.this.textArea.append(str);
178 // 非同期に行う処理を記述するためのクラス
179 class LongTaskWorker extends SwingWorker<Object, Object> {
180 private final JButton button;
182 public LongTaskWorker(JButton button) {
183 this.button = button;
188 @SuppressWarnings("SleepWhileInLoop")
189 public Object doInBackground() {
191 PrintStream defOut = System.out;
192 PrintStream defErr = System.err;
194 OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8");
195 PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく
197 // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定
198 System.setOut(stdout);
199 System.setErr(stdout);
202 Command command = new Command(osm.jp.gpx.ImportPicture.class);
203 command.setArgs(args);
204 command.start(); // コマンドを実行
205 while (command.isAlive()) {
208 } catch (InterruptedException e) {}
212 e.printStackTrace(stdout);
215 System.setOut(defOut);
216 System.setErr(defErr);
217 doButton.setEnabled(true);
225 protected void done() {
226 // 処理が終了したので,文字列を元に戻し
228 button.setText("実行");
229 button.setEnabled(true);