OSDN Git Service

2019-02-11
[hayashilib/hayashi.git] / src / hayashi / yuu / tools / mail / gui / SettingDialog.java
1 package hayashi.yuu.tools.mail.gui;\r
2 import hayashi.yuu.tools.properties.Properties;\r
3 import hayashi.yuu.tools.properties.PropertyItem;\r
4 import hayashi.yuu.tools.mail.SendMail;\r
5 import hayashi.yuu.tools.mail.SiteData;\r
6 \r
7 import java.awt.BorderLayout;\r
8 import java.awt.FlowLayout;\r
9 import java.awt.GridLayout;\r
10 import java.awt.Rectangle;\r
11 import java.awt.Toolkit;\r
12 import java.awt.event.WindowEvent;\r
13 import java.awt.event.WindowListener;\r
14 import java.io.FileInputStream;\r
15 import java.io.FileNotFoundException;\r
16 import java.io.FileOutputStream;\r
17 import java.io.IOException;\r
18 import java.util.logging.Logger;\r
19 \r
20 import javax.swing.BorderFactory;\r
21 import javax.swing.JButton;\r
22 import javax.swing.JComponent;\r
23 import javax.swing.JDialog;\r
24 import javax.swing.JFrame;\r
25 import javax.swing.JLabel;\r
26 import javax.swing.JOptionPane;\r
27 import javax.swing.JPanel;\r
28 import javax.swing.JTextArea;\r
29 \r
30 \r
31 /**\r
32  * <p>アプリケーション設定を行うモーダルダイアログフォーム</p>\r
33  * <code>\r
34  * ・タイトルパネル\r
35  *              "メール送信 設定"\r
36  * ・メインパネル\r
37  *              "<メールサーバーの設定>"\r
38  * ・ボタンパネル\r
39  *              [OK]ボタン\r
40  *              [Cancel]ボタン\r
41  * </code>\r
42  * @author hayashi\r
43  *\r
44  */\r
45 @SuppressWarnings("serial")\r
46 public class SettingDialog extends JDialog implements WindowListener\r
47 {\r
48     JLabel label1;\r
49     JLabel label2;\r
50     JTextArea textArea;\r
51     JFrame parent;\r
52     public static Properties prop;\r
53     public static SetupMailServer mailServerPanel;\r
54     public static MailAddrPanel mailAddrPanel;\r
55         public static String fileName;\r
56         static Logger logger;\r
57     \r
58     /*\r
59      * 設定項目:\r
60      *  \r
61      */\r
62     public SettingDialog(JFrame parent, boolean modal, String propertiesFileName, Logger logger) throws FileNotFoundException, IOException {\r
63         super(parent, modal);\r
64         SettingDialog.logger = logger;\r
65         SettingDialog.fileName = propertiesFileName;\r
66         \r
67         // 設定ファイルの読み込み\r
68         prop = new Properties();\r
69         prop.load(new FileInputStream(propertiesFileName));\r
70 \r
71         addWindowListener(this);\r
72         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);\r
73 \r
74         this.parent = parent;\r
75         setTitle("設定");\r
76         getContentPane().setLayout(new BorderLayout());\r
77         setSize(420, SetupMailServer.CATEGORY_HEIGHT + MailAddrPanel.CATEGORY_HEIGHT + (PropertyItem.LINE_HEIGHT * 9));\r
78         \r
79         /*\r
80          * タイトルパネル\r
81          */\r
82                 JPanel topPanel = new JPanel();\r
83         label1 = new JLabel("メール送信 設定", JLabel.CENTER);\r
84         label1.setBounds(10,10,340,20);\r
85         topPanel.add(label1);\r
86         getContentPane().add(topPanel, BorderLayout.NORTH);\r
87         \r
88                 /*\r
89                  * カテゴリ:[ メールサーバーの設定 ]\r
90                  */\r
91         mailServerPanel = new SetupMailServer(prop);\r
92                 \r
93                 /*\r
94                  * カテゴリ:[ メールの配信先設定 ]\r
95                  */\r
96         mailAddrPanel = new MailAddrPanel(prop);\r
97                 \r
98         /*\r
99                  * メインパネル\r
100                  */\r
101                 JPanel mainPanel = new JPanel();\r
102                 mainPanel.setLayout(new FlowLayout());\r
103                 mainPanel.add(makePanel("メールサーバーの設定", mailServerPanel));\r
104                 mainPanel.add(makePanel("メールの配信先", mailAddrPanel));\r
105         getContentPane().add(mainPanel, BorderLayout.CENTER);\r
106 \r
107         /*\r
108                  * [OK]ボタン : このダイアログウインドウを閉じる\r
109                  */\r
110             JButton saveButton = new JButton("保存");\r
111         saveButton.setBounds(145,65,66,27);\r
112         saveButton.addActionListener(new java.awt.event.ActionListener() {\r
113                 public void actionPerformed(java.awt.event.ActionEvent evt) {\r
114                         try {\r
115                                 SettingDialog.prop.store(new FileOutputStream(SettingDialog.fileName), "SendMail GUI");\r
116                                 }\r
117                         catch (FileNotFoundException e) {\r
118                                         // TODO Auto-generated catch block\r
119                                         e.printStackTrace();\r
120                                 }\r
121                         catch (IOException e) {\r
122                                         // TODO Auto-generated catch block\r
123                                         e.printStackTrace();\r
124                                 }\r
125                         \r
126                         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(SettingDialog.this, WindowEvent.WINDOW_CLOSING));\r
127                         setVisible(false);\r
128                 }\r
129         });\r
130 \r
131                 /*\r
132                  * [Cancel]ボタン : このダイアログウインドウを閉じる\r
133                  */\r
134             JButton cancelButton = new JButton("取消");\r
135             cancelButton.setBounds(145,65,66,27);\r
136             cancelButton.addActionListener(new java.awt.event.ActionListener() {\r
137                 public void actionPerformed(java.awt.event.ActionEvent evt) {\r
138                         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(SettingDialog.this, WindowEvent.WINDOW_CLOSING));\r
139                         setVisible(false);\r
140                 }\r
141         });\r
142             \r
143             /*\r
144              * [テスト送信]ボタン : テストメールを送信する\r
145              */\r
146             JButton testButton = new JButton("テスト送信");\r
147             testButton.setBounds(145,65,66,27);\r
148             testButton.addActionListener(new java.awt.event.ActionListener() {\r
149                 public void actionPerformed(java.awt.event.ActionEvent evt) {\r
150                         SiteData siteData = SettingDialog.mailServerPanel.getSiteData();\r
151                         SettingDialog.mailAddrPanel.getSiteData(siteData);\r
152                         SendMail mail = new SendMail(siteData);\r
153                 mail.setSubject("[SendMail] メール送信テスト");\r
154                 mail.setContent("このメールは[SendMail]からのメール送信テストです。");\r
155                         try {\r
156                                         mail.send();\r
157                                         SettingDialog.logger.info("メールを送信しました。");\r
158                         JOptionPane.showMessageDialog(SendMailGUI.mainFrame, "メールを送信しました。", "成功", JOptionPane.INFORMATION_MESSAGE);\r
159                                 } catch (Exception e) {\r
160                                         e.printStackTrace();\r
161                                         SettingDialog.logger.warning("メール送信に失敗しました。");\r
162                         JOptionPane.showMessageDialog(SendMailGUI.mainFrame, "メールを送信できませんでした。", "失敗", JOptionPane.ERROR_MESSAGE);\r
163                                 }\r
164                 }\r
165         });\r
166 \r
167         /*\r
168                  * ボタンパネル\r
169                  */\r
170                 JPanel bottomPanel = new JPanel();\r
171         bottomPanel.add(saveButton);\r
172         bottomPanel.add(cancelButton);\r
173         bottomPanel.add(testButton);\r
174         getContentPane().add(bottomPanel, BorderLayout.SOUTH);\r
175     }\r
176 \r
177         /**\r
178          * 枠で囲まれたパネル\r
179          * @param title\r
180          * @param compo\r
181          * @return\r
182          */\r
183         private JPanel makePanel(String title, JComponent compo) {\r
184         JPanel p = new JPanel(new GridLayout(1,1));\r
185         p.setBorder(BorderFactory.createTitledBorder(title));\r
186         p.add(compo);\r
187         return p;\r
188     }\r
189         \r
190         public String getPropertiesFilename() {\r
191                 return fileName;\r
192         }\r
193 \r
194     /**\r
195     * Shows or hides the component depending on the boolean flag b.\r
196     * @param b  if true, show the component; otherwise, hide the component.\r
197     * @see java.awt.Component#isVisible\r
198     */\r
199     public void setVisible(boolean b) {\r
200         if (b) {\r
201             Rectangle bounds = getParent().getBounds();\r
202             Rectangle abounds = getBounds();\r
203             setLocation(bounds.x + (bounds.width - abounds.width)/ 2, bounds.y + (bounds.height - abounds.height)/2);\r
204         }\r
205         super.setVisible(b);\r
206     }\r
207 \r
208         public void windowClosed(WindowEvent e) {\r
209                 setVisible(false);\r
210     }\r
211 \r
212         public void windowActivated(WindowEvent arg0) {\r
213         }\r
214 \r
215         public void windowClosing(WindowEvent arg0) {\r
216                 setVisible(false);\r
217         }\r
218 \r
219         public void windowDeactivated(WindowEvent arg0) {\r
220         }\r
221 \r
222         public void windowDeiconified(WindowEvent arg0) {\r
223         }\r
224 \r
225         public void windowIconified(WindowEvent arg0) {\r
226         }\r
227 \r
228         public void windowOpened(WindowEvent arg0) {\r
229         }\r
230 }\r