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.*;
14  import com.nilhcem.fakesmtp.model.UIModel;
15  
16  /**
17   * Button to start the SMTP server.
18   *
19   * @author Nilhcem
20   * @since 1.0
21   */
22  public final class StartServerButton extends Observable implements Observer {
23  	private final I18n i18n = I18n.INSTANCE;
24  
25  	private final JButton button = new JButton(i18n.get("startsrv.start"));
26  
27  	/**
28  	 * Creates a start button to start the SMTP server.
29  	 * <p>
30  	 * If the user selects a wrong port before starting the server, the method will display an error message.
31  	 * </p>
32  	 */
33  	public StartServerButton() {
34  		button.addActionListener(new ActionListener() {
35  			@Override
36  			public void actionPerformed(ActionEvent e) {
37  				toggleButton();
38  			}
39  		});
40  	}
41  
42  	/**
43  	 * Switches the text inside the button and calls the PortTextField observer to enable/disable the port field.
44  	 *
45  	 * @see PortTextField
46  	 */
47  	public void toggleButton() {
48  		try {
49  			UIModel.INSTANCE.toggleButton();
50  		} catch (InvalidHostException ihe) {
51  			displayError(String.format(i18n.get("startsrv.err.invalidHost"), ihe.getHost()));
52  		} catch (InvalidPortException ipe) {
53  			displayError(String.format(i18n.get("startsrv.err.invalidPort")));
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  }