OSDN Git Service

2019-02-11
[hayashilib/hayashi.git] / src / hayashi / yuu / tools / mail / gui / SendMailGUI.java
1 package hayashi.yuu.tools.mail.gui;\r
2 \r
3 import hayashi.yuu.tools.logger.LoggerFactory;\r
4 import hayashi.yuu.tools.mail.SendMail;\r
5 import hayashi.yuu.tools.mail.SiteData;\r
6 import hayashi.yuu.tools.properties.Encrypt;\r
7 \r
8 import java.awt.*;\r
9 import java.awt.event.*;\r
10 import java.io.FileNotFoundException;\r
11 import java.io.IOException;\r
12 import java.util.logging.Logger;\r
13 \r
14 import javax.swing.JFrame;\r
15 import javax.swing.JLabel;\r
16 import javax.swing.JMenu;\r
17 import javax.swing.JMenuBar;\r
18 import javax.swing.JMenuItem;\r
19 import javax.swing.JPanel;\r
20 import javax.swing.JScrollPane;\r
21 import javax.swing.JTextArea;\r
22 import javax.swing.WindowConstants;\r
23 \r
24 \r
25 @SuppressWarnings("serial")\r
26 public class SendMailGUI extends JFrame implements ActionListener\r
27 {\r
28     public static SendMailGUI mainFrame;\r
29     boolean fComponentsAdjusted;\r
30     JPanel headPanel;\r
31     JMenuBar mainMenuBar;       // [メニュー]\r
32     JMenu menuFile;                     // [メニュー]->[ファイル(F)]\r
33     JMenuItem miSetting;        // [メニュー]->[ファイル(F)]->[設定...]\r
34     JMenuItem miExit;           // [メニュー]->[ファイル(F)]->[終了(X)]\r
35     \r
36     public Logger logger;\r
37     public static SiteData mailProp;\r
38     public static String propertiesFileName;\r
39     \r
40     /**\r
41      * 「メール送信(SendMail)」\r
42      * 起動オプション:\r
43      *  第一: メールアカウントの設定ファイル名\r
44      * @param args      properties File Name\r
45      * @throws  Exception       error\r
46      */\r
47     public static void main(String args[]) throws Exception {\r
48                 String fileName = "sendmail.properties";\r
49         if (args.length > 0) {\r
50                 fileName = args[0];\r
51         }\r
52         (new SendMailGUI(fileName)).setVisible(true);\r
53     }\r
54     \r
55     public SendMailGUI(String propertiesName) throws Exception {\r
56         Encrypt.PASSWORD_KEY = "hayashihimitukagi";\r
57         logger = LoggerFactory.getInstance();   // Loggerオブジェクトの生成\r
58         SendMailGUI.propertiesFileName = propertiesName;\r
59         SendMailGUI.mailProp = new SiteData(SendMailGUI.propertiesFileName);\r
60 \r
61         // ログ出力\r
62         logger.info("SendMailGUI - プログラム起動");\r
63         \r
64         // 初期設定\r
65         fComponentsAdjusted = false;\r
66         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);\r
67         Container container = getContentPane();\r
68         container.setLayout(new BorderLayout());\r
69         setTitle("SendMail GUI");\r
70         \r
71         mainFrame = this;\r
72         \r
73         // [メニュー]->[ファイル(F)]\r
74         menuFile = new JMenu("ファイル(F)");\r
75         menuFile.setMnemonic('F');\r
76         menuFile.setFont(new Font("Dialog", 0, 12));\r
77         \r
78         // [メニュー]->[ファイル(F)]->[設定...]\r
79         miSetting = new JMenuItem("設定...");\r
80         miSetting.addActionListener(this);\r
81         miSetting.setFont(new Font("Dialog", 0, 12));\r
82         menuFile.add(miSetting);\r
83         \r
84         // [メニュー]->[ファイル(F)]->[------------]\r
85         menuFile.addSeparator();\r
86 \r
87         // [メニュー]->[ファイル(F)]->[終了(X)]\r
88         miExit = new JMenuItem("終了(X)");\r
89         miExit.addActionListener(this);\r
90         miExit.setMnemonic('X');\r
91         miExit.setFont(new Font("Dialog", 0, 12));\r
92         menuFile.add(miExit);\r
93         \r
94         // [メニュー]\r
95         mainMenuBar = new JMenuBar();\r
96         mainMenuBar.add(menuFile);\r
97         getRootPane().setJMenuBar(mainMenuBar);\r
98 \r
99         // 上部にヘッダパネル\r
100         headPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));\r
101         getContentPane().add(BorderLayout.NORTH, headPanel);\r
102         JLabel label1 = new JLabel("SendMail GUI - サンプルプログラム", JLabel.CENTER);\r
103         label1.setBounds(10,10,340,20);\r
104         \r
105         // 中央に、メインパネルを配置\r
106         JPanel mainPanel = new JPanel(new FlowLayout());\r
107         //mainPanel.setPreferredSize(new Dimension(500, 400));\r
108         getContentPane().add(mainPanel, BorderLayout.CENTER);\r
109         \r
110         JTextArea textArea = new JTextArea(20, 60);\r
111         textArea.setEditable(false);\r
112         textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));\r
113                 textArea.append("メール送信設定 サンプルプログラム\n");\r
114                 textArea.setCaretPosition(0);\r
115                 JScrollPane scrollPane = new JScrollPane(textArea);\r
116                 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);\r
117                 mainPanel.add(scrollPane, BorderLayout.CENTER);\r
118         \r
119         // 下部にコピーライトを配置\r
120         JPanel southPanel = new JPanel(new FlowLayout());\r
121         getContentPane().add(BorderLayout.SOUTH, southPanel);\r
122         \r
123         JLabel label2 = new JLabel("All rights reserved.Copyright(c) 2010,, Hayashi,Yuu.", JLabel.CENTER);\r
124         label2.setBounds(10,40,340,20);\r
125         southPanel.add(label2);\r
126         \r
127                 pack();\r
128                 setLocationRelativeTo(null);\r
129                 setVisible(true);\r
130     }\r
131     \r
132     public SendMail getMailInstance() {\r
133                 return new SendMail(SendMailGUI.mailProp);\r
134     }\r
135     \r
136     public static String getPropertiesFileName() {\r
137         return propertiesFileName;\r
138     }\r
139 \r
140         public void addNotify() {\r
141         Dimension d = getSize();\r
142         super.addNotify();\r
143         if (fComponentsAdjusted) {\r
144             return;\r
145         }\r
146         setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);\r
147         Component components[] = getComponents();\r
148         for (int i = 0; i < components.length; i++) {\r
149             Point p = components[i].getLocation();\r
150             p.translate(getInsets().left, getInsets().top);\r
151             components[i].setLocation(p);\r
152         }\r
153 \r
154         fComponentsAdjusted = true;\r
155     }\r
156 \r
157         /**\r
158          * [メニュー]アクション: \r
159          * @param event         イベント\r
160          */\r
161         public void actionPerformed(ActionEvent event) {\r
162         Object object = event.getSource();\r
163         if(object == miExit) {\r
164             miExit_Action(event);\r
165         }\r
166         else if (object == miSetting) {\r
167             miSetting_Action(event);\r
168         }\r
169     }\r
170         \r
171     /**\r
172      * アクション:\r
173      * [メニュー]->[ファイル]->[終了]\r
174      * \r
175      * @param event\r
176      */\r
177     void miExit_Action(ActionEvent event) {\r
178         setVisible(false);\r
179         logger.info("SendMailGUI - プログラム終了");\r
180         System.exit(0);\r
181     }\r
182 \r
183     /**\r
184      * アクション:\r
185      * [メニュー]->[ファイル]->[設定...]\r
186      * \r
187      * @param event\r
188      */\r
189     void miSetting_Action(ActionEvent event) {\r
190         try {\r
191                         (new SettingDialog(this, true, SendMailGUI.getPropertiesFileName(), logger)).setVisible(true);\r
192                 }\r
193         catch (FileNotFoundException e) {\r
194                         // 回復不能な重大なエラー\r
195                         e.printStackTrace();\r
196                 }\r
197         catch (IOException e) {\r
198                         // 回復不能な重大なエラー\r
199                         e.printStackTrace();\r
200                 }\r
201     }\r
202 }\r