OSDN Git Service

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