View Javadoc
1   package com.nilhcem.fakesmtp.gui.info;
2   
3   import com.nilhcem.fakesmtp.core.Configuration;
4   import com.nilhcem.fakesmtp.core.I18n;
5   import com.nilhcem.fakesmtp.model.UIModel;
6   
7   import javax.swing.JTextField;
8   import javax.swing.event.DocumentEvent;
9   import javax.swing.event.DocumentListener;
10  import java.awt.event.ActionEvent;
11  import java.awt.event.ActionListener;
12  import java.util.Observable;
13  import java.util.Observer;
14  
15  /**
16   * Text field in which will be written the desired SMTP port.
17   *
18   * @author Nilhcem
19   * @since 1.0
20   */
21  public final class PortTextField extends Observable implements Observer {
22  
23  	private final JTextField portTextField = new JTextField();
24  
25  	/**
26  	 * Creates the port field object and adds a listener on change to alert the presentation model.
27  	 * <p>
28  	 * The default port's value is defined in the configuration.properties file.<br>
29  	 * Each time the port is modified, the port from the {@link UIModel} will be reset.
30  	 * </p>
31  	 */
32  	public PortTextField() {
33  		portTextField.setToolTipText(I18n.INSTANCE.get("porttextfield.tooltip"));
34  		portTextField.getDocument().addDocumentListener(new DocumentListener() {
35  			@Override
36  			public void removeUpdate(DocumentEvent e) {
37  				warn();
38  			}
39  
40  			@Override
41  			public void insertUpdate(DocumentEvent e) {
42  				warn();
43  			}
44  
45  			@Override
46  			public void changedUpdate(DocumentEvent e) {
47  				warn();
48  			}
49  
50  			private void warn() {
51  				UIModel.INSTANCE.setPort(portTextField.getText());
52  			}
53  		});
54  
55  		portTextField.setText(Configuration.INSTANCE.get("smtp.default.port"));
56  		portTextField.addActionListener(new ActionListener() {
57  			@Override
58  			public void actionPerformed(ActionEvent e) {
59  				setChanged();
60  				notifyObservers();
61  			}
62  		});
63  	}
64  
65  	/**
66  	 * Returns the JTextField object.
67  	 *
68  	 * @return the JTextField object.
69  	 */
70  	public JTextField get() {
71  		return portTextField;
72  	}
73  
74  	/**
75  	 * Sets the specified port in the text field only if this latter is not {@code null}.
76  	 *
77  	 * @param portStr the port to set.
78  	 */
79  	public void setText(String portStr) {
80  		if (portStr != null && !portStr.isEmpty()) {
81  			portTextField.setText(portStr);
82  		}
83  	}
84  
85  	/**
86  	 * Enables or disables the port text field.
87  	 * <p>
88  	 * When the element will receive an action from the {@link StartServerButton} object,
89  	 * it will enable or disable the port, so that the user can't modify it
90  	 * when the server is already launched.
91  	 * </p>
92  	 *
93  	 * @param o the observable element which will notify this class.
94  	 * @param arg optional parameters (not used).
95  	 */
96  	@Override
97  	public void update(Observable o, Object arg) {
98  		if (o instanceof StartServerButton) {
99  			portTextField.setEnabled(!UIModel.INSTANCE.isStarted());
100 		}
101 	}
102 }