/* * Copyright (c) 2010-2012 Thiago T. Sá * * This file is part of CloudReports. * * CloudReports is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CloudReports is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * For more information about your rights as a user of CloudReports, * refer to the LICENSE file or see <http://www.gnu.org/licenses/>. */ package cloudreports.gui; import cloudreports.business.SettingBusiness; import cloudreports.dao.SettingDAO; import cloudreports.models.Setting; /** * The MailNotificationSettings form. * Most of its code is generated automatically by the NetBeans IDE. * * @author Thiago T. Sá * @since 1.0 */ public class MailNotificationSettings extends javax.swing.JDialog { /** An instance of settings DAO. */ private SettingDAO sDAO; /** Creates a new MailNotificationSettings. */ public MailNotificationSettings() { setModal(true); initComponents(); this.sDAO = new SettingDAO(); if(SettingBusiness.isMailNotificationEnabled()) { enableCheckBox.setSelected(true); setMailSettingsEnabled(true); } else { enableCheckBox.setSelected(false); setMailSettingsEnabled(false); } Setting receiverAddress; Setting senderPassword; Setting SMTPServer; Setting senderAddress = sDAO.getSetting("SenderAddress"); if (senderAddress == null) { receiverAddress = new Setting("ReceiverAddress", ""); senderAddress = new Setting("SenderAddress", ""); senderPassword = new Setting("EmailPassword", ""); SMTPServer = new Setting("SMTPServer", ""); sDAO.insertSetting(receiverAddress); sDAO.insertSetting(senderAddress); sDAO.insertSetting(senderPassword); sDAO.insertSetting(SMTPServer); receiverTextField.setText(""); senderTextField.setText(""); senderPasswordField.setText(""); serverTextField.setText(""); } else { receiverAddress = sDAO.getSetting("ReceiverAddress"); senderPassword = sDAO.getSetting("EmailPassword"); SMTPServer = sDAO.getSetting("SMTPServer"); receiverTextField.setText(receiverAddress.getValue()); senderTextField.setText(senderAddress.getValue()); senderPasswordField.setText(senderPassword.getValue()); serverTextField.setText(SMTPServer.getValue()); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { enableCheckBox = new javax.swing.JCheckBox(); mailSettingsPanel = new javax.swing.JPanel(); senderLabel = new javax.swing.JLabel(); senderTextField = new javax.swing.JTextField(); passwordLabel = new javax.swing.JLabel(); serverLabel = new javax.swing.JLabel(); serverTextField = new javax.swing.JTextField(); senderPasswordField = new javax.swing.JPasswordField(); okButton = new javax.swing.JButton(); receiverPanel = new javax.swing.JPanel(); receiverLabel = new javax.swing.JLabel(); receiverTextField = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Mail notification settings"); setResizable(false); enableCheckBox.setText("Enable mail notification"); enableCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { enableCheckBoxActionPerformed(evt); } }); mailSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Send from:")); senderLabel.setText("E-mail:"); passwordLabel.setText("Password:"); serverLabel.setText("SMTP Server:"); javax.swing.GroupLayout mailSettingsPanelLayout = new javax.swing.GroupLayout(mailSettingsPanel); mailSettingsPanel.setLayout(mailSettingsPanelLayout); mailSettingsPanelLayout.setHorizontalGroup( mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mailSettingsPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(senderLabel, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(passwordLabel, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(serverLabel, javax.swing.GroupLayout.Alignment.TRAILING)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(senderTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE) .addComponent(serverTextField) .addComponent(senderPasswordField)) .addContainerGap()) ); mailSettingsPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {senderTextField, serverTextField}); mailSettingsPanelLayout.setVerticalGroup( mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mailSettingsPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(senderLabel) .addComponent(senderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(passwordLabel) .addComponent(senderPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(21, 21, 21) .addGroup(mailSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(serverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(serverLabel)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cloudreports/gui/resources/ok.png"))); // NOI18N okButton.setText("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); receiverPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Send to:")); receiverLabel.setText("E-mail:"); javax.swing.GroupLayout receiverPanelLayout = new javax.swing.GroupLayout(receiverPanel); receiverPanel.setLayout(receiverPanelLayout); receiverPanelLayout.setHorizontalGroup( receiverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(receiverPanelLayout.createSequentialGroup() .addGap(50, 50, 50) .addComponent(receiverLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(receiverTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE) .addGap(18, 18, 18)) ); receiverPanelLayout.setVerticalGroup( receiverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(receiverPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(receiverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(receiverLabel) .addComponent(receiverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(receiverPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) .addComponent(enableCheckBox) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(mailSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE) .addContainerGap())) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(enableCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(receiverPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(mailSettingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(okButton) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * Enables/disables the mail notification whenever the enable checkbox is * clicked. * * @param evt an action event. * @since 1.0 */ private void enableCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableCheckBoxActionPerformed if(enableCheckBox.isSelected()) { Setting isMailNotificationEnabled = sDAO.getSetting("EnableMailNotification"); isMailNotificationEnabled.setValue("true"); sDAO.updateSetting(isMailNotificationEnabled); setMailSettingsEnabled(true); } else { Setting isMailNotificationEnabled = sDAO.getSetting("EnableMailNotification"); isMailNotificationEnabled.setValue("false"); sDAO.updateSetting(isMailNotificationEnabled); setMailSettingsEnabled(false); } }//GEN-LAST:event_enableCheckBoxActionPerformed /** * Updates all settings when the OK button is clicked. * * @param evt an action event. * @since 1.0 */ private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed Setting receiverAddress = sDAO.getSetting("ReceiverAddress"); receiverAddress.setValue(receiverTextField.getText()); sDAO.updateSetting(receiverAddress); Setting senderAddress = sDAO.getSetting("SenderAddress"); senderAddress.setValue(senderTextField.getText()); sDAO.updateSetting(senderAddress); Setting senderPassword = sDAO.getSetting("EmailPassword"); senderPassword.setValue(new String(senderPasswordField.getPassword())); sDAO.updateSetting(senderPassword); Setting SMTPServer = sDAO.getSetting("SMTPServer"); SMTPServer.setValue(serverTextField.getText()); sDAO.updateSetting(SMTPServer); dispose(); }//GEN-LAST:event_okButtonActionPerformed /** * Enables/disables the form components used to configure the mail * notification settings. * * @param enabled indicates whether mail notification is enabled or not. * @since 1.0 */ private void setMailSettingsEnabled(boolean enabled) { receiverPanel.setEnabled(enabled); receiverLabel.setEnabled(enabled); receiverTextField.setEnabled(enabled); mailSettingsPanel.setEnabled(enabled); senderLabel.setEnabled(enabled); senderTextField.setEnabled(enabled); passwordLabel.setEnabled(enabled); senderPasswordField.setEnabled(enabled); serverLabel.setEnabled(enabled); serverTextField.setEnabled(enabled); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBox enableCheckBox; private javax.swing.JPanel mailSettingsPanel; private javax.swing.JButton okButton; private javax.swing.JLabel passwordLabel; private javax.swing.JLabel receiverLabel; private javax.swing.JPanel receiverPanel; private javax.swing.JTextField receiverTextField; private javax.swing.JLabel senderLabel; private javax.swing.JPasswordField senderPasswordField; private javax.swing.JTextField senderTextField; private javax.swing.JLabel serverLabel; private javax.swing.JTextField serverTextField; // End of variables declaration//GEN-END:variables }