View Javadoc

1   package org.wcb.common.component;
2   
3   import org.wcb.autohome.AutoHomeAdminSession;
4   import org.wcb.autohome.implementations.EmailHeaderBean;
5   import org.wcb.common.EmailAuthenticator;
6   
7   import javax.swing.*;
8   import java.awt.*;
9   import java.awt.event.ActionListener;
10  import java.awt.event.ActionEvent;
11  
12  /***
13   * Copyright (C) 1999  Walter Bogaardt
14   *
15   * This library is free software; you can redistribute it and/or
16   * modify it under the terms of the GNU Lesser General Public
17   * License as published by the Free Software Foundation; either
18   * version 2 of the License, or (at your option) any later version.
19   *
20   * This library is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   * Lesser General Public License for more details.
24   *
25   * You should have received a copy of the GNU Lesser General Public
26   * License along with this library; if not, write to the Free Software
27   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
28   *
29   * Project: Alice X10 Home Automation
30   * Filename:  $Id: EmailPanel.java,v 1.5 2004/02/27 01:29:53 wbogaardt Exp $<BR>
31   * Abstract: Used to display a panel that the user can edit their email settings
32   *
33   * $Log: EmailPanel.java,v $
34   * Revision 1.5  2004/02/27 01:29:53  wbogaardt
35   * modified classes so they conform to Checkstyle format in readability
36   *
37   * Revision 1.4  2004/02/04 01:15:38  wbogaardt
38   * changed password from texfield to password field and reoganized order of configruation
39   *
40   * Revision 1.3  2004/02/02 23:41:31  wbogaardt
41   * refactored email so that properties are maintained in bean object and added allowing username and password
42   * authentication for email servers that require it.
43   *
44   * Revision 1.2  2004/01/31 07:40:26  wbogaardt
45   * modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api.
46   * The purpose is to reduce dependency on non-standard layout api.
47   *
48   */
49  public class EmailPanel extends JPanel implements ActionListener {
50  
51      private JRadioButton neverRb, eachEventRb, hourlyRb, dailyRb;
52      private JTextField emailToTf, emailFromTf, emailSMTPTf, usernameTf;
53      private JPasswordField passwordTf;
54      private JCheckBox jCkRequiresAuth;
55  
56      /***
57       * Default construct to setup the email panel.
58       */
59      public EmailPanel() {
60          this.setup();
61      }
62  
63      /***
64       * Gives outside classes the ability to save settings from the
65       * email panel.
66       */
67      public void saveSettings()
68      {
69          EmailHeaderBean bean = new EmailHeaderBean(emailToTf.getText(), emailFromTf.getText(), emailSMTPTf.getText());
70          bean.setEmailNoticeFreq(this.getEmailFrequency());
71          if (jCkRequiresAuth.isSelected())
72          {
73               EmailAuthenticator auth = new EmailAuthenticator(usernameTf.getText(),
74                       new String(passwordTf.getPassword()));
75               bean.setEmailAuthenticator(auth);
76          }
77          AutoHomeAdminSession.getInstance().setEmailInformation(bean);
78      }
79  
80      /***
81       * Sets up the components on the panel.
82       */
83      private void setup() {
84          emailToTf = new JTextField(20);
85          emailFromTf = new JTextField(20);
86          emailSMTPTf = new JTextField(20);
87          jCkRequiresAuth = new JCheckBox("Requires Authentication");
88          jCkRequiresAuth.addActionListener(this);
89          usernameTf = new JTextField(20);
90          passwordTf = new JPasswordField(20);
91          ButtonGroup timeGrp = new ButtonGroup();
92          neverRb = new JRadioButton("Never");
93          eachEventRb = new JRadioButton("Every Event");
94          hourlyRb = new JRadioButton("Hourly");
95          dailyRb = new JRadioButton("Daily");
96          timeGrp.add(neverRb);
97          timeGrp.add(eachEventRb);
98          timeGrp.add(hourlyRb);
99          timeGrp.add(dailyRb);
100 
101         GridBagConstraints gridBagConstraints;
102         setLayout(new java.awt.GridBagLayout());
103         setBorder(new javax.swing.border.TitledBorder("EmailNotification"));
104         gridBagConstraints = new GridBagConstraints();
105         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
106         add(new JLabel("TO:"), gridBagConstraints);
107 
108         gridBagConstraints = new GridBagConstraints();
109         gridBagConstraints.gridwidth = 2;
110         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
111         add(emailToTf, gridBagConstraints);
112 
113         gridBagConstraints = new GridBagConstraints();
114         gridBagConstraints.gridx = 0;
115         gridBagConstraints.gridy = 1;
116         add(new JLabel("SMTP HOST"), gridBagConstraints);
117 
118         gridBagConstraints = new GridBagConstraints();
119         gridBagConstraints.gridx = 1;
120         gridBagConstraints.gridy = 1;
121         gridBagConstraints.gridwidth = 2;
122         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
123         add(emailSMTPTf, gridBagConstraints);
124 
125         gridBagConstraints = new GridBagConstraints();
126         gridBagConstraints.gridx = 0;
127         gridBagConstraints.gridy = 2;
128         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
129         add(new JLabel("From:"), gridBagConstraints);
130 
131         gridBagConstraints = new GridBagConstraints();
132         gridBagConstraints.gridx = 1;
133         gridBagConstraints.gridy = 2;
134         gridBagConstraints.gridwidth = 2;
135         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
136         add(emailFromTf, gridBagConstraints);
137 
138         gridBagConstraints = new GridBagConstraints();
139         gridBagConstraints.gridx = 0;
140         gridBagConstraints.gridy = 3;
141         gridBagConstraints.gridwidth = 3;
142         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
143         add(jCkRequiresAuth, gridBagConstraints);
144 
145         gridBagConstraints = new GridBagConstraints();
146         gridBagConstraints.gridx = 0;
147         gridBagConstraints.gridy = 4;
148         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
149         add(new JLabel("User name:"), gridBagConstraints);
150 
151         gridBagConstraints = new GridBagConstraints();
152         gridBagConstraints.gridx = 1;
153         gridBagConstraints.gridy = 4;
154         gridBagConstraints.gridwidth = 2;
155         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
156         add(usernameTf, gridBagConstraints);
157 
158         gridBagConstraints = new GridBagConstraints();
159         gridBagConstraints.gridx = 0;
160         gridBagConstraints.gridy = 5;
161         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
162         add(new JLabel("Password:"), gridBagConstraints);
163 
164         gridBagConstraints = new GridBagConstraints();
165         gridBagConstraints.gridx = 1;
166         gridBagConstraints.gridy = 5;
167         gridBagConstraints.gridwidth = 2;
168         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
169         add(passwordTf, gridBagConstraints);
170 
171         gridBagConstraints = new GridBagConstraints();
172         gridBagConstraints.gridx = 0;
173         gridBagConstraints.gridy = 6;
174         gridBagConstraints.gridwidth = 2;
175         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
176         add(new JLabel("Send notice"), gridBagConstraints);
177 
178         gridBagConstraints = new GridBagConstraints();
179         gridBagConstraints.gridx = 0;
180         gridBagConstraints.gridy = 7;
181         add(neverRb, gridBagConstraints);
182 
183         gridBagConstraints = new GridBagConstraints();
184         gridBagConstraints.gridx = 1;
185         gridBagConstraints.gridy = 7;
186         add(eachEventRb, gridBagConstraints);
187 
188         gridBagConstraints = new GridBagConstraints();
189         gridBagConstraints.gridx = 2;
190         gridBagConstraints.gridy = 7;
191         add(hourlyRb, gridBagConstraints);
192 
193         gridBagConstraints = new GridBagConstraints();
194         gridBagConstraints.gridx = 3;
195         gridBagConstraints.gridy = 7;
196         add(dailyRb, gridBagConstraints);
197         this.showFileSettings();
198     }
199 
200     /***
201      * Used to set up the email panel based on what was last saved.
202      */
203     private void showFileSettings() {
204         EmailHeaderBean bean = AutoHomeAdminSession.getInstance().getEmailInformation();
205         EmailAuthenticator auth = bean.getEmailAuthenticator();
206         emailToTf.setText(bean.getEmailToAddress());
207         emailFromTf.setText(bean.getEmailFromAddress());
208         emailSMTPTf.setText(bean.getEmailHost());
209         if (auth != null)
210         {
211             jCkRequiresAuth.setSelected(true);
212             usernameTf.setText(auth.getPasswordAuthentication().getUserName());
213              passwordTf.setText(auth.getPasswordAuthentication().getPassword());
214         }
215         if (jCkRequiresAuth.isSelected())
216         {
217             usernameTf.setEnabled(true);
218             passwordTf.setEnabled(true);
219         }
220         else
221         {
222             usernameTf.setEnabled(false);
223             passwordTf.setEnabled(false);
224         }
225 
226         String frequency = bean.getEmailNoticeFreq();
227         if (frequency.equalsIgnoreCase("never"))
228         {
229             neverRb.setSelected(true);
230         }
231         if (frequency.equalsIgnoreCase("everytime"))
232         {
233             eachEventRb.setSelected(true);
234         }
235         if (frequency.equalsIgnoreCase("hourly"))
236         {
237             hourlyRb.setSelected(true);
238         }
239         if (frequency.equalsIgnoreCase("daily"))
240         {
241             dailyRb.setSelected(true);
242         }
243     }
244 
245     /***
246      * gets the selected email frequency
247      * notification radio box and returns a string
248      * value for that selected box.
249      * @return email frequency text.
250      */
251     private String getEmailFrequency() {
252         if (eachEventRb.isSelected())
253         {
254             return "everytime";
255         }
256         if (hourlyRb.isSelected())
257         {
258             return "hourly";
259         }
260         if (dailyRb.isSelected())
261         {
262             return "daily";
263         }
264         else
265         {
266             return "never";
267         }
268     }
269 
270     /***
271      * This allows the panel to control its own actions such
272      * as enabling the password and username text fields if the
273      * authorization required check box is marked
274      * @param ae Event taking place on the check box component
275      */
276     public void actionPerformed(ActionEvent ae) {
277         Object src = ae.getSource();
278         if (src.equals(jCkRequiresAuth))
279         {
280             if (jCkRequiresAuth.isSelected())
281             {
282                 usernameTf.setEnabled(true);
283                 passwordTf.setEnabled(true);
284             }
285             else
286             {
287                 usernameTf.setEnabled(false);
288                 passwordTf.setEnabled(false);
289             }
290         }
291     }
292 }