| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PortTextField |
|
| 1.3333333333333333;1.333 | ||||
| PortTextField$1 |
|
| 1.3333333333333333;1.333 | ||||
| PortTextField$2 |
|
| 1.3333333333333333;1.333 |
| 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 | 0 | public final class PortTextField extends Observable implements Observer { |
| 22 | ||
| 23 | 0 | 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 | 0 | public PortTextField() { |
| 33 | 0 | portTextField.setToolTipText(I18n.INSTANCE.get("porttextfield.tooltip")); |
| 34 | 0 | portTextField.getDocument().addDocumentListener(new DocumentListener() { |
| 35 | @Override | |
| 36 | public void removeUpdate(DocumentEvent e) { | |
| 37 | 0 | warn(); |
| 38 | 0 | } |
| 39 | ||
| 40 | @Override | |
| 41 | public void insertUpdate(DocumentEvent e) { | |
| 42 | 0 | warn(); |
| 43 | 0 | } |
| 44 | ||
| 45 | @Override | |
| 46 | public void changedUpdate(DocumentEvent e) { | |
| 47 | 0 | warn(); |
| 48 | 0 | } |
| 49 | ||
| 50 | private void warn() { | |
| 51 | 0 | UIModel.INSTANCE.setPort(portTextField.getText()); |
| 52 | 0 | } |
| 53 | }); | |
| 54 | ||
| 55 | 0 | portTextField.setText(Configuration.INSTANCE.get("smtp.default.port")); |
| 56 | 0 | portTextField.addActionListener(new ActionListener() { |
| 57 | @Override | |
| 58 | public void actionPerformed(ActionEvent e) { | |
| 59 | 0 | setChanged(); |
| 60 | 0 | notifyObservers(); |
| 61 | 0 | } |
| 62 | }); | |
| 63 | 0 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Returns the JTextField object. | |
| 67 | * | |
| 68 | * @return the JTextField object. | |
| 69 | */ | |
| 70 | public JTextField get() { | |
| 71 | 0 | 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 | 0 | if (portStr != null && !portStr.isEmpty()) { |
| 81 | 0 | portTextField.setText(portStr); |
| 82 | } | |
| 83 | 0 | } |
| 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 | 0 | if (o instanceof StartServerButton) { |
| 99 | 0 | portTextField.setEnabled(!UIModel.INSTANCE.isStarted()); |
| 100 | } | |
| 101 | 0 | } |
| 102 | } |