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 | 0 | private final I18n i18n = I18n.INSTANCE; |
26 | |
|
27 | 0 | private final JButton button = new JButton(i18n.get("startsrv.start")); |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public StartServerButton() { |
36 | 0 | button.addActionListener(new ActionListener() { |
37 | |
@Override |
38 | |
public void actionPerformed(ActionEvent e) { |
39 | 0 | toggleButton(); |
40 | 0 | } |
41 | |
}); |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public void toggleButton() { |
50 | |
try { |
51 | 0 | UIModel.INSTANCE.toggleButton(); |
52 | 0 | } catch (InvalidPortException ipe) { |
53 | 0 | displayError(String.format(i18n.get("startsrv.err.invalid"))); |
54 | 0 | } catch (BindPortException bpe) { |
55 | 0 | displayError(String.format(i18n.get("startsrv.err.bound"), bpe.getPort())); |
56 | 0 | } catch (OutOfRangePortException orpe) { |
57 | 0 | displayError(String.format(i18n.get("startsrv.err.range"), orpe.getPort())); |
58 | 0 | } catch (RuntimeException re) { |
59 | 0 | displayError(String.format(i18n.get("startsrv.err.default"), re.getMessage())); |
60 | 0 | } |
61 | |
|
62 | 0 | if (UIModel.INSTANCE.isStarted()) { |
63 | 0 | button.setText(i18n.get("startsrv.started")); |
64 | 0 | button.setEnabled(false); |
65 | |
} |
66 | 0 | setChanged(); |
67 | 0 | notifyObservers(); |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public JButton get() { |
76 | 0 | return button; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private void displayError(String error) { |
85 | 0 | JOptionPane.showMessageDialog(button.getParent(), error, |
86 | 0 | String.format(i18n.get("startsrv.err.title"), Configuration.INSTANCE.get("application.name")), |
87 | |
JOptionPane.ERROR_MESSAGE); |
88 | 0 | } |
89 | |
|
90 | |
@Override |
91 | |
public void update(Observable o, Object arg) { |
92 | 0 | if (o instanceof PortTextField) { |
93 | 0 | toggleButton(); |
94 | |
} |
95 | 0 | } |
96 | |
} |