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 JPanel buttonPanel; // ボタン配置パネル (下部)
25 JButton closeButton; // [クローズ]ボタン
26 JButton doButton; // [実行]ボタン
27 JTextArea textArea; // 実行結果を表示するJTextArea (中央)
30 @SuppressWarnings("OverridableMethodCallInConstructor")
31 public DoDialog(String[] args) {
32 super(); // モーダルダイアログを基盤にする
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);
44 buttonPanel = new JPanel();
46 doButton = new JButton("実行");
47 doButton.setToolTipText("処理を実行します.");
48 doButton.setEnabled(true);
49 doButton.addActionListener((ActionEvent event) -> {
51 // ボタンの文字列を変更し,使用不可にする
52 doButton.setText("処理中...");
53 doButton.setEnabled(false);
55 // SwingWorker を生成し,実行する
56 LongTaskWorker worker = new LongTaskWorker(doButton);
59 buttonPanel.add(doButton);
61 closeButton = new JButton("閉じる");
62 closeButton.setToolTipText("処理を終了します.");
63 closeButton.addActionListener((ActionEvent event) -> {
66 buttonPanel.add(closeButton);
68 this.getContentPane().add("South", buttonPanel);
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);
78 textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture");
79 for (String arg : args) {
80 textArea.append(" '" + arg + "'");
82 textArea.append("\n\n");
85 System.out.println(e.toString());
89 //parentFrame.setVisible(true);
93 * Shows or hides the component depending on the boolean flag b.
94 * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す.
95 * @see java.awt.Component#isVisible
98 public void setVisible(boolean b) {
106 public void addNotify() {
107 // Record the size of the window prior to calling parents addNotify.
108 Dimension d = getSize();
112 if (fComponentsAdjusted) {
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);
124 fComponentsAdjusted = true;
129 * JTextAreaに書き出すOutputStream
131 public static class JTextAreaOutputStream extends OutputStream {
132 private final ByteArrayOutputStream os;
135 private final JTextArea textArea;
136 private final String encode;
138 public JTextAreaOutputStream(JTextArea textArea, String encode) {
139 this.textArea = textArea;
140 this.encode = encode;
141 this.os = new ByteArrayOutputStream();
145 * OutputStream#write(byte[])のオーバーライド
147 * @throws java.io.IOException
150 public void write(int arg) throws IOException {
155 * flush()でJTextAreaに書き出す
156 * @throws java.io.IOException
159 public void flush() throws IOException {
161 final String str = new String(this.os.toByteArray(), this.encode);
163 SwingUtilities.invokeLater(
167 JTextAreaOutputStream.this.textArea.append(str);
176 // 非同期に行う処理を記述するためのクラス
177 class LongTaskWorker extends SwingWorker<Object, Object> {
178 private final JButton button;
180 public LongTaskWorker(JButton button) {
181 this.button = button;
186 @SuppressWarnings("SleepWhileInLoop")
187 public Object doInBackground() {
189 PrintStream defOut = System.out;
190 PrintStream defErr = System.err;
192 OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8");
193 PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく
195 // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定
196 System.setOut(stdout);
197 System.setErr(stdout);
200 Command command = new Command(osm.jp.gpx.ImportPicture.class);
201 command.setArgs(args);
202 command.start(); // コマンドを実行
203 while (command.isAlive()) {
206 } catch (InterruptedException e) {}
210 e.printStackTrace(stdout);
213 System.setOut(defOut);
214 System.setErr(defErr);
215 doButton.setEnabled(true);
223 protected void done() {
224 // 処理が終了したので,文字列を元に戻し
226 button.setText("実行");
227 button.setEnabled(true);