OSDN Git Service

77785ec5dd0a918c229a50600075f4578898af67
[importpicture/importpicture.git] / importPicture / src / osm / jp / gpx / matchtime / gui / DoDialog.java
1 package osm.jp.gpx.matchtime.gui;
2 import java.awt.*;
3 import java.io.*;
4 import javax.swing.*;
5
6 /**
7  *      処理 
8  */
9 @SuppressWarnings("serial")
10 public class DoDialog extends JDialog {
11         public static final String TITLE = "Do Command";
12         
13     // Used for addNotify check.
14     boolean fComponentsAdjusted = false;
15     String[] args;
16     
17     //{{DECLARE_CONTROLS
18     JFrame parentFrame;         // MatchTime.class
19     JPanel buttonPanel;     // ボタン配置パネル (下部)
20     JButton closeButton;      // [クローズ]ボタン
21     JButton doButton;      // [実行]ボタン
22     TextArea textArea;      // 実行結果を表示するJTextArea     (中央)
23     //}}
24
25     class SymAction implements java.awt.event.ActionListener {
26         public void actionPerformed(java.awt.event.ActionEvent event) {
27             Object object = event.getSource();
28             if (object == closeButton) {
29                 closeButton_Action(event);
30             }
31             else if (object == doButton) {
32                 doButton_Action(event);
33             }
34         }
35     }
36
37     public DoDialog(JFrame parentFrame, String[] args) {
38         super(parentFrame, true);   // モーダルダイアログを基盤にする
39         this.parentFrame = parentFrame;
40         this.args = args;
41                 
42         // INIT_CONTROLS
43         Container container = getContentPane();
44         container.setLayout(new BorderLayout());
45         setVisible(false);
46         setSize(getInsets().left + getInsets().right + 500,getInsets().top + getInsets().bottom + 480);
47         setTitle(DoDialog.TITLE);
48         setSize(500,480);
49         
50         // コントロールパネル
51         buttonPanel = new JPanel();
52
53         doButton = new JButton("実行");
54         doButton.setToolTipText("処理を実行します.");
55         doButton.setEnabled(true);
56         buttonPanel.add(doButton);
57
58         closeButton = new JButton("閉じる");
59         closeButton.setToolTipText("処理を終了します.");
60         buttonPanel.add(closeButton);
61         
62         this.getContentPane().add("South", buttonPanel);
63         
64                 // 説明文
65         textArea = new TextArea();
66         textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
67         try {
68             textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture");
69             for (int i = 0; i < args.length; i++) {
70                 textArea.append(" '"+ args[i] +"'");
71             }
72             textArea.append("\n\n");
73         }
74         catch (Exception e) {
75             System.out.println(e.toString());
76         }
77         this.getContentPane().add("Center", textArea);
78
79         //{{REGISTER_LISTENERS
80         SymAction lSymAction = new SymAction();
81         closeButton.addActionListener(lSymAction);
82         doButton.addActionListener(lSymAction);
83         //}}
84     }
85
86     /**
87     * Shows or hides the component depending on the boolean flag b.
88     * @param b  trueのときコンポーネントを表示; その他のとき, componentを隠す.
89     * @see java.awt.Component#isVisible
90     */
91     public void setVisible(boolean b) {
92         if(b) {
93             setLocation(80, 80);
94         }
95         super.setVisible(b);
96     }
97
98     public void addNotify()     {
99         // Record the size of the window prior to calling parents addNotify.
100         Dimension d = getSize();
101
102         super.addNotify();
103
104         if (fComponentsAdjusted)
105             return;
106
107         // Adjust components according to the insets
108         setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
109         Component components[] = getComponents();
110         for (int i = 0; i < components.length; i++)     {
111             Point p = components[i].getLocation();
112             p.translate(getInsets().left, getInsets().top);
113             components[i].setLocation(p);
114         }
115         fComponentsAdjusted = true;
116     }
117
118     /**
119      * [実行]ボタンをクリックしたときの動作
120      * @param event
121      */
122     void doButton_Action(java.awt.event.ActionEvent event) {
123         doButton.setEnabled(false);
124         
125         PrintStream defOut = System.out;
126         PrintStream defErr = System.err;
127
128         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
129         try {
130                 System.setOut(new PrintStream(stdout));
131                 System.setErr(new PrintStream(stdout));
132
133                 Command command = new Command(osm.jp.gpx.ImportPicture.class);
134                 command.setArgs(args);
135                 command.start();                // コマンドを実行
136                 while (command.isAlive()) {
137                         Thread.sleep(1000);
138                 textArea.append(stdout.toString());
139                 stdout.reset();
140                 }
141                 textArea.append(stdout.toString());
142                 JOptionPane.showMessageDialog(this, "'"+ TITLE +"'処理を完了しました。", "処理完了", JOptionPane.INFORMATION_MESSAGE);
143         }
144         catch(Exception e) {
145             e.printStackTrace();
146             JOptionPane.showMessageDialog(this, e.toString(), "Exception", JOptionPane.ERROR_MESSAGE);
147         }
148         finally {
149                 System.setOut(defOut);
150                 System.setErr(defErr);
151             doButton.setEnabled(true);
152         }
153     }
154
155     void closeButton_Action(java.awt.event.ActionEvent event) {
156         dispose();
157     }
158     
159     void changeSQL_Action(java.awt.event.ActionEvent event) {
160         textArea.setText("");
161     }
162 }