Coverage Report - com.nilhcem.fakesmtp.gui.info.StartServerButton
 
Classes in this File Line Coverage Branch Coverage Complexity
StartServerButton
0%
0/28
0%
0/4
2
StartServerButton$1
0%
0/3
N/A
2
 
 1  
 package com.nilhcem.fakesmtp.gui.info;
 2  
 
 3  
 import java.awt.event.ActionEvent;
 4  
 import java.awt.event.ActionListener;
 5  
 import java.util.Observable;
 6  
 import java.util.Observer;
 7  
 
 8  
 import javax.swing.JButton;
 9  
 import javax.swing.JOptionPane;
 10  
 
 11  
 import com.nilhcem.fakesmtp.core.Configuration;
 12  
 import com.nilhcem.fakesmtp.core.I18n;
 13  
 import com.nilhcem.fakesmtp.core.exception.BindPortException;
 14  
 import com.nilhcem.fakesmtp.core.exception.InvalidPortException;
 15  
 import com.nilhcem.fakesmtp.core.exception.OutOfRangePortException;
 16  
 import com.nilhcem.fakesmtp.model.UIModel;
 17  
 
 18  
 /**
 19  
  * Button to start the SMTP server.
 20  
  *
 21  
  * @author Nilhcem
 22  
  * @since 1.0
 23  
  */
 24  
 public final class StartServerButton extends Observable implements Observer {
 25  0
         private final I18n i18n = I18n.INSTANCE;
 26  
 
 27  0
         private final JButton button = new JButton(i18n.get("startsrv.start"));
 28  
 
 29  
         /**
 30  
          * Creates a start button to start the SMTP server.
 31  
          * <p>
 32  
          * If the user selects a wrong port before starting the server, the method will display an error message.
 33  
          * </p>
 34  
          */
 35  0
         public StartServerButton() {
 36  0
                 button.addActionListener(new ActionListener() {
 37  
                         @Override
 38  
                         public void actionPerformed(ActionEvent e) {
 39  0
                                 toggleButton();
 40  0
                         }
 41  
                 });
 42  0
         }
 43  
 
 44  
         /**
 45  
          * Switches the text inside the button and calls the PortTextField observer to enable/disable the port field.
 46  
          *
 47  
          * @see PortTextField
 48  
          */
 49  
         public void toggleButton() {
 50  
                 try {
 51  0
                         UIModel.INSTANCE.toggleButton();
 52  0
                 } catch (InvalidPortException ipe) {
 53  0
                         displayError(String.format(i18n.get("startsrv.err.invalid")));
 54  0
                 } catch (BindPortException bpe) {
 55  0
                         displayError(String.format(i18n.get("startsrv.err.bound"), bpe.getPort()));
 56  0
                 } catch (OutOfRangePortException orpe) {
 57  0
                         displayError(String.format(i18n.get("startsrv.err.range"), orpe.getPort()));
 58  0
                 } catch (RuntimeException re) {
 59  0
                         displayError(String.format(i18n.get("startsrv.err.default"), re.getMessage()));
 60  0
                 }
 61  
 
 62  0
                 if (UIModel.INSTANCE.isStarted()) {
 63  0
                         button.setText(i18n.get("startsrv.started"));
 64  0
                         button.setEnabled(false);
 65  
                 }
 66  0
                 setChanged();
 67  0
                 notifyObservers();
 68  0
         }
 69  
 
 70  
         /**
 71  
          * Returns the JButton object.
 72  
          *
 73  
          * @return the JButton object.
 74  
          */
 75  
         public JButton get() {
 76  0
                 return button;
 77  
         }
 78  
 
 79  
         /**
 80  
          * Displays a message dialog displaying the error specified in parameter.
 81  
          *
 82  
          * @param error a string representing the error which will be displayed in a message dialog.
 83  
          */
 84  
         private void displayError(String error) {
 85  0
                 JOptionPane.showMessageDialog(button.getParent(), error,
 86  0
                         String.format(i18n.get("startsrv.err.title"), Configuration.INSTANCE.get("application.name")),
 87  
                         JOptionPane.ERROR_MESSAGE);
 88  0
         }
 89  
 
 90  
         @Override
 91  
         public void update(Observable o, Object arg) {
 92  0
                 if (o instanceof PortTextField) {
 93  0
                         toggleButton();
 94  
                 }
 95  0
         }
 96  
 }