1 package com.nilhcem.fakesmtp.gui;
2
3 import com.nilhcem.fakesmtp.core.ArgsHandler;
4 import com.nilhcem.fakesmtp.core.I18n;
5 import com.nilhcem.fakesmtp.gui.info.ClearAllButton;
6 import com.nilhcem.fakesmtp.gui.info.NbReceivedLabel;
7 import com.nilhcem.fakesmtp.gui.info.PortTextField;
8 import com.nilhcem.fakesmtp.gui.info.SaveMsgField;
9 import com.nilhcem.fakesmtp.gui.info.StartServerButton;
10 import com.nilhcem.fakesmtp.gui.tab.LastMailPane;
11 import com.nilhcem.fakesmtp.gui.tab.LogsPane;
12 import com.nilhcem.fakesmtp.gui.tab.MailsListPane;
13 import com.nilhcem.fakesmtp.server.MailSaver;
14 import com.nilhcem.fakesmtp.server.SMTPServerHandler;
15 import net.miginfocom.swing.MigLayout;
16
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JTabbedPane;
20 import java.util.Observable;
21
22
23
24
25
26
27
28 public final class MainPanel {
29
30 private final I18n i18n = I18n.INSTANCE;
31
32
33 private final MigLayout layout = new MigLayout(
34 "",
35 "[] 10 [] [] [grow,fill]",
36 "[] [] 5 [] 5 [grow,fill] []");
37 private final JPanel mainPanel = new JPanel(layout);
38
39
40 private final DirChooser dirChooser = new DirChooser(mainPanel);
41
42
43 private final JLabel portLabel = new JLabel(i18n.get("mainpanel.listening.port"));
44 private final PortTextField portText = new PortTextField();
45 private final StartServerButton startServerBtn = new StartServerButton();
46
47
48 private final JLabel receivedLabel = new JLabel(i18n.get("mainpanel.messages.received"));
49 private final NbReceivedLabel nbReceivedLabel = new NbReceivedLabel();
50
51
52 private final JLabel saveMessages = new JLabel(i18n.get("mainpanel.save.messages"));
53 private final SaveMsgField saveMsgTextField = new SaveMsgField();
54
55
56 private final JTabbedPane tabbedPane = new JTabbedPane();
57 private final LogsPane logsPane = new LogsPane();
58 private final MailsListPane mailsListPane = new MailsListPane();
59 private final LastMailPane lastMailPane = new LastMailPane();
60
61
62 private final ClearAllButton clearAll = new ClearAllButton();
63
64
65
66
67
68
69
70
71
72
73
74 public MainPanel(Observable menu) {
75 assignLabelsToFields();
76 addObservers(menu);
77 buildGUI();
78 checkArgs();
79 }
80
81
82
83
84
85
86 public JPanel get() {
87 return mainPanel;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 private void addObservers(Observable menu) {
106
107 menu.addObserver(dirChooser);
108 saveMsgTextField.addObserver(dirChooser);
109
110
111 startServerBtn.addObserver(portText);
112
113
114 portText.addObserver(startServerBtn);
115
116
117 dirChooser.addObserver(saveMsgTextField);
118
119
120 MailSaver mailSaver = SMTPServerHandler.INSTANCE.getMailSaver();
121 mailSaver.addObserver(nbReceivedLabel);
122 mailSaver.addObserver(mailsListPane);
123 mailSaver.addObserver(lastMailPane);
124 mailSaver.addObserver(clearAll);
125
126
127 clearAll.addObserver(nbReceivedLabel);
128 clearAll.addObserver(mailsListPane);
129 clearAll.addObserver(logsPane);
130 clearAll.addObserver(lastMailPane);
131 }
132
133
134
135
136 private void buildGUI() {
137
138 mainPanel.add(portLabel);
139 mainPanel.add(portText.get(), "w 60!");
140 mainPanel.add(startServerBtn.get(), "span, w 165!");
141
142
143 mainPanel.add(saveMessages);
144 mainPanel.add(saveMsgTextField.get(), "span, w 230!");
145
146
147 mainPanel.add(receivedLabel);
148 mainPanel.add(nbReceivedLabel.get(), "span");
149
150
151 tabbedPane.add(mailsListPane.get(), i18n.get("mainpanel.tab.mailslist"));
152 tabbedPane.add(logsPane.get(), i18n.get("mainpanel.tab.smtplog"));
153 tabbedPane.add(lastMailPane.get(), i18n.get("mainpanel.tab.lastmessage"));
154 mainPanel.add(tabbedPane, "span, grow");
155
156
157 mainPanel.add(clearAll.get(), "span, center");
158 }
159
160
161
162
163
164
165
166
167 private void checkArgs() {
168 ArgsHandler args = ArgsHandler.INSTANCE;
169
170 if (args.getPort() != null) {
171 portText.setText(args.getPort());
172 }
173
174 if (args.shouldStartServerAtLaunch()) {
175 startServerBtn.toggleButton();
176 }
177
178 if (args.memoryModeEnabled()) {
179 saveMsgTextField.get().setEnabled(false);
180 }
181 }
182
183
184
185
186 private void assignLabelsToFields() {
187 portLabel.setLabelFor(portText.get());
188 saveMessages.setLabelFor(saveMsgTextField.get());
189 receivedLabel.setLabelFor(nbReceivedLabel.get());
190 }
191
192
193
194
195
196
197 public PortTextField getPortText() {
198 return portText;
199 }
200
201
202
203
204
205
206 public SaveMsgField getSaveMsgTextField() {
207 return saveMsgTextField;
208 }
209 }