View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Establish a connection to an ichtyometer.
40   *
41   * Created on 1/29/14.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 3.1
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                  // Don't care
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 }