View Javadoc
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  	private final I18n i18n = I18n.INSTANCE;
26  
27  	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  	public StartServerButton() {
36  		button.addActionListener(new ActionListener() {
37  			@Override
38  			public void actionPerformed(ActionEvent e) {
39  				toggleButton();
40  			}
41  		});
42  	}
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  			UIModel.INSTANCE.toggleButton();
52  		} catch (InvalidPortException ipe) {
53  			displayError(String.format(i18n.get("startsrv.err.invalid")));
54  		} catch (BindPortException bpe) {
55  			displayError(String.format(i18n.get("startsrv.err.bound"), bpe.getPort()));
56  		} catch (OutOfRangePortException orpe) {
57  			displayError(String.format(i18n.get("startsrv.err.range"), orpe.getPort()));
58  		} catch (RuntimeException re) {
59  			displayError(String.format(i18n.get("startsrv.err.default"), re.getMessage()));
60  		}
61  
62  		if (UIModel.INSTANCE.isStarted()) {
63  			button.setText(i18n.get("startsrv.started"));
64  			button.setEnabled(false);
65  		}
66  		setChanged();
67  		notifyObservers();
68  	}
69  
70  	/**
71  	 * Returns the JButton object.
72  	 *
73  	 * @return the JButton object.
74  	 */
75  	public JButton get() {
76  		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  		JOptionPane.showMessageDialog(button.getParent(), error,
86  			String.format(i18n.get("startsrv.err.title"), Configuration.INSTANCE.get("application.name")),
87  			JOptionPane.ERROR_MESSAGE);
88  	}
89  
90  	@Override
91  	public void update(Observable o, Object arg) {
92  		if (o instanceof PortTextField) {
93  			toggleButton();
94  		}
95  	}
96  }