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.BindPortException;
14 import com.nilhcem.fakesmtp.core.exception.InvalidPortException;
15 import com.nilhcem.fakesmtp.core.exception.OutOfRangePortException;
16 import com.nilhcem.fakesmtp.model.UIModel;
17
18
19
20
21
22
23
24 public final class StartServerButton extends Observable implements Observer {
25 private final I18n i18n = I18n.INSTANCE;
26
27 private final JButton button = new JButton(i18n.get("startsrv.start"));
28
29
30
31
32
33
34
35 public StartServerButton() {
36 button.addActionListener(new ActionListener() {
37 @Override
38 public void actionPerformed(ActionEvent e) {
39 toggleButton();
40 }
41 });
42 }
43
44
45
46
47
48
49 public void toggleButton() {
50 try {
51 UIModel.INSTANCE.toggleButton();
52 } catch (InvalidPortException ipe) {
53 displayError(String.format(i18n.get("startsrv.err.invalid")));
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
72
73
74
75 public JButton get() {
76 return button;
77 }
78
79
80
81
82
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 }