1 package fr.ifremer.tutti.ui.swing.content.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import fr.ifremer.tutti.ichtyometer.IchtyometerClient;
26 import fr.ifremer.tutti.ichtyometer.LocalDeviceNotFoundException;
27 import fr.ifremer.tutti.ichtyometer.RemoteDeviceChooser;
28 import fr.ifremer.tutti.ichtyometer.RemoteDeviceNotFoundException;
29 import fr.ifremer.tutti.ichtyometer.RemoteDeviceServiceNotFoundException;
30 import fr.ifremer.tutti.ichtyometer.feed.IchtyometerFeedReader;
31 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
32 import org.nuiton.jaxx.application.ApplicationBusinessException;
33
34 import javax.swing.JOptionPane;
35
36 import static org.nuiton.i18n.I18n.t;
37
38
39
40
41
42
43
44
45
46 public class ConnectIchtyometerAction extends AbstractMainUITuttiAction {
47
48 public ConnectIchtyometerAction(MainUIHandler handler) {
49 super(handler, false);
50 }
51
52 @Override
53 public void doAction() throws Exception {
54
55 IchtyometerClient client = new IchtyometerClient(getConfig().getIchtyometerMaximumNumberOfAttemptToConnect());
56
57 RemoteDeviceChooser remoteDeviceChooser = remoteDeviceNames -> {
58
59 try {
60 Thread.sleep(1000);
61 } catch (InterruptedException e) {
62
63 }
64 return (String) JOptionPane.showInputDialog(
65 getContext().getActionUI(),
66 t("tutti.ichtyometer.choose.remote.device.found"),
67 t("tutti.ichtyometer.title.choose.remote.device"),
68 JOptionPane.QUESTION_MESSAGE,
69 null,
70 remoteDeviceNames.toArray(new String[remoteDeviceNames.size()]),
71 null
72 );
73 };
74 try {
75 client.open(remoteDeviceChooser, getConfig().isFullBluetoothScan());
76 } catch (LocalDeviceNotFoundException e) {
77 throw new ApplicationBusinessException(t("tutti.ichtyometer.error.no.local.device"));
78 } catch (RemoteDeviceNotFoundException e) {
79 throw new ApplicationBusinessException(t("tutti.ichtyometer.error.no.remote.device"));
80 } catch (RemoteDeviceServiceNotFoundException e) {
81 throw new ApplicationBusinessException(t("tutti.ichtyometer.error.no.remote.device.service"));
82 }
83 IchtyometerFeedReader ichtyometerReader = new IchtyometerFeedReader();
84 ichtyometerReader.start(client);
85
86 getContext().setIchtyometerReader(ichtyometerReader);
87 }
88
89 @Override
90 public void postSuccessAction() {
91 super.postSuccessAction();
92
93 IchtyometerFeedReader ichtyometerReader = getContext().getIchtyometerReader();
94 String clientName = ichtyometerReader.getClientName();
95 sendMessage(t("tutti.ichtyometer.connection.establish", clientName));
96
97 displayInfoMessage(
98 t("tutti.ichtyometer.connection.establish.title"),
99 t("tutti.ichtyometer.connection.establish.message", clientName)
100 );
101 }
102 }