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
17
18
19
20
21 public final class PortTextField extends Observable implements Observer {
22
23 private final JTextField portTextField = new JTextField();
24
25
26
27
28
29
30
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
67
68
69
70 public JTextField get() {
71 return portTextField;
72 }
73
74
75
76
77
78
79 public void setText(String portStr) {
80 if (portStr != null && !portStr.isEmpty()) {
81 portTextField.setText(portStr);
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
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 }