OSDN Git Service

meven
[hayashilib/hayashi.git] / src / main / java / hayashi / yuu / tools / mail / gui / SettingDialog.java
diff --git a/src/main/java/hayashi/yuu/tools/mail/gui/SettingDialog.java b/src/main/java/hayashi/yuu/tools/mail/gui/SettingDialog.java
new file mode 100755 (executable)
index 0000000..a282464
--- /dev/null
@@ -0,0 +1,230 @@
+package hayashi.yuu.tools.mail.gui;\r
+import hayashi.yuu.tools.properties.Properties;\r
+import hayashi.yuu.tools.properties.PropertyItem;\r
+import hayashi.yuu.tools.mail.SendMail;\r
+import hayashi.yuu.tools.mail.SiteData;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.FlowLayout;\r
+import java.awt.GridLayout;\r
+import java.awt.Rectangle;\r
+import java.awt.Toolkit;\r
+import java.awt.event.WindowEvent;\r
+import java.awt.event.WindowListener;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.util.logging.Logger;\r
+\r
+import javax.swing.BorderFactory;\r
+import javax.swing.JButton;\r
+import javax.swing.JComponent;\r
+import javax.swing.JDialog;\r
+import javax.swing.JFrame;\r
+import javax.swing.JLabel;\r
+import javax.swing.JOptionPane;\r
+import javax.swing.JPanel;\r
+import javax.swing.JTextArea;\r
+\r
+\r
+/**\r
+ * <p>アプリケーション設定を行うモーダルダイアログフォーム</p>\r
+ * <code>\r
+ * ・タイトルパネル\r
+ *             "メール送信 設定"\r
+ * ・メインパネル\r
+ *             "<メールサーバーの設定>"\r
+ * ・ボタンパネル\r
+ *             [OK]ボタン\r
+ *             [Cancel]ボタン\r
+ * </code>\r
+ * @author hayashi\r
+ *\r
+ */\r
+@SuppressWarnings("serial")\r
+public class SettingDialog extends JDialog implements WindowListener\r
+{\r
+    JLabel label1;\r
+    JLabel label2;\r
+    JTextArea textArea;\r
+    JFrame parent;\r
+    public static Properties prop;\r
+    public static SetupMailServer mailServerPanel;\r
+    public static MailAddrPanel mailAddrPanel;\r
+       public static String fileName;\r
+       static Logger logger;\r
+    \r
+    /*\r
+     * 設定項目:\r
+     *         \r
+     */\r
+    public SettingDialog(JFrame parent, boolean modal, String propertiesFileName, Logger logger) throws FileNotFoundException, IOException {\r
+        super(parent, modal);\r
+        SettingDialog.logger = logger;\r
+        SettingDialog.fileName = propertiesFileName;\r
+        \r
+        // 設定ファイルの読み込み\r
+        prop = new Properties();\r
+        prop.load(new FileInputStream(propertiesFileName));\r
+\r
+        addWindowListener(this);\r
+        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);\r
+\r
+        this.parent = parent;\r
+        setTitle("設定");\r
+        getContentPane().setLayout(new BorderLayout());\r
+        setSize(420, SetupMailServer.CATEGORY_HEIGHT + MailAddrPanel.CATEGORY_HEIGHT + (PropertyItem.LINE_HEIGHT * 9));\r
+        \r
+        /*\r
+         * タイトルパネル\r
+         */\r
+               JPanel topPanel = new JPanel();\r
+        label1 = new JLabel("メール送信 設定", JLabel.CENTER);\r
+        label1.setBounds(10,10,340,20);\r
+        topPanel.add(label1);\r
+        getContentPane().add(topPanel, BorderLayout.NORTH);\r
+        \r
+               /*\r
+                * カテゴリ:[ メールサーバーの設定 ]\r
+                */\r
+        mailServerPanel = new SetupMailServer(prop);\r
+               \r
+               /*\r
+                * カテゴリ:[ メールの配信先設定 ]\r
+                */\r
+        mailAddrPanel = new MailAddrPanel(prop);\r
+               \r
+        /*\r
+                * メインパネル\r
+                */\r
+               JPanel mainPanel = new JPanel();\r
+               mainPanel.setLayout(new FlowLayout());\r
+               mainPanel.add(makePanel("メールサーバーの設定", mailServerPanel));\r
+               mainPanel.add(makePanel("メールの配信先", mailAddrPanel));\r
+        getContentPane().add(mainPanel, BorderLayout.CENTER);\r
+\r
+        /*\r
+                * [OK]ボタン : このダイアログウインドウを閉じる\r
+                */\r
+           JButton saveButton = new JButton("保存");\r
+        saveButton.setBounds(145,65,66,27);\r
+        saveButton.addActionListener(new java.awt.event.ActionListener() {\r
+               public void actionPerformed(java.awt.event.ActionEvent evt) {\r
+                       try {\r
+                               SettingDialog.prop.store(new FileOutputStream(SettingDialog.fileName), "SendMail GUI");\r
+                               }\r
+                       catch (FileNotFoundException e) {\r
+                                       // TODO Auto-generated catch block\r
+                                       e.printStackTrace();\r
+                               }\r
+                       catch (IOException e) {\r
+                                       // TODO Auto-generated catch block\r
+                                       e.printStackTrace();\r
+                               }\r
+                       \r
+                       Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(SettingDialog.this, WindowEvent.WINDOW_CLOSING));\r
+                       setVisible(false);\r
+               }\r
+        });\r
+\r
+               /*\r
+                * [Cancel]ボタン : このダイアログウインドウを閉じる\r
+                */\r
+           JButton cancelButton = new JButton("取消");\r
+           cancelButton.setBounds(145,65,66,27);\r
+           cancelButton.addActionListener(new java.awt.event.ActionListener() {\r
+               public void actionPerformed(java.awt.event.ActionEvent evt) {\r
+                       Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new WindowEvent(SettingDialog.this, WindowEvent.WINDOW_CLOSING));\r
+                       setVisible(false);\r
+               }\r
+        });\r
+           \r
+           /*\r
+            * [テスト送信]ボタン : テストメールを送信する\r
+            */\r
+           JButton testButton = new JButton("テスト送信");\r
+           testButton.setBounds(145,65,66,27);\r
+           testButton.addActionListener(new java.awt.event.ActionListener() {\r
+               public void actionPerformed(java.awt.event.ActionEvent evt) {\r
+                       SiteData siteData = SettingDialog.mailServerPanel.getSiteData();\r
+                       SettingDialog.mailAddrPanel.getSiteData(siteData);\r
+                       SendMail mail = new SendMail(siteData);\r
+                mail.setSubject("[SendMail] メール送信テスト");\r
+                mail.setContent("このメールは[SendMail]からのメール送信テストです。");\r
+                       try {\r
+                                       mail.send();\r
+                                       SettingDialog.logger.info("メールを送信しました。");\r
+                       JOptionPane.showMessageDialog(SendMailGUI.mainFrame, "メールを送信しました。", "成功", JOptionPane.INFORMATION_MESSAGE);\r
+                               } catch (Exception e) {\r
+                                       e.printStackTrace();\r
+                                       SettingDialog.logger.warning("メール送信に失敗しました。");\r
+                       JOptionPane.showMessageDialog(SendMailGUI.mainFrame, "メールを送信できませんでした。", "失敗", JOptionPane.ERROR_MESSAGE);\r
+                               }\r
+               }\r
+        });\r
+\r
+        /*\r
+                * ボタンパネル\r
+                */\r
+               JPanel bottomPanel = new JPanel();\r
+        bottomPanel.add(saveButton);\r
+        bottomPanel.add(cancelButton);\r
+        bottomPanel.add(testButton);\r
+        getContentPane().add(bottomPanel, BorderLayout.SOUTH);\r
+    }\r
+\r
+       /**\r
+        * 枠で囲まれたパネル\r
+        * @param title\r
+        * @param compo\r
+        * @return\r
+        */\r
+       private JPanel makePanel(String title, JComponent compo) {\r
+        JPanel p = new JPanel(new GridLayout(1,1));\r
+        p.setBorder(BorderFactory.createTitledBorder(title));\r
+        p.add(compo);\r
+        return p;\r
+    }\r
+       \r
+       public String getPropertiesFilename() {\r
+               return fileName;\r
+       }\r
+\r
+    /**\r
+    * Shows or hides the component depending on the boolean flag b.\r
+    * @param b  if true, show the component; otherwise, hide the component.\r
+    * @see java.awt.Component#isVisible\r
+    */\r
+    public void setVisible(boolean b) {\r
+        if (b) {\r
+            Rectangle bounds = getParent().getBounds();\r
+            Rectangle abounds = getBounds();\r
+            setLocation(bounds.x + (bounds.width - abounds.width)/ 2, bounds.y + (bounds.height - abounds.height)/2);\r
+        }\r
+        super.setVisible(b);\r
+    }\r
+\r
+       public void windowClosed(WindowEvent e) {\r
+               setVisible(false);\r
+    }\r
+\r
+       public void windowActivated(WindowEvent arg0) {\r
+       }\r
+\r
+       public void windowClosing(WindowEvent arg0) {\r
+               setVisible(false);\r
+       }\r
+\r
+       public void windowDeactivated(WindowEvent arg0) {\r
+       }\r
+\r
+       public void windowDeiconified(WindowEvent arg0) {\r
+       }\r
+\r
+       public void windowIconified(WindowEvent arg0) {\r
+       }\r
+\r
+       public void windowOpened(WindowEvent arg0) {\r
+       }\r
+}\r