]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/ConfigureSrtmSources.java
Configure SRTM sources for 1-sec option
[GpsPrune.git] / src / tim / prune / function / srtm / ConfigureSrtmSources.java
1 package tim.prune.function.srtm;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.util.Base64;
9
10 import javax.swing.BorderFactory;
11 import javax.swing.Box;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.JCheckBox;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19
20 import tim.prune.App;
21 import tim.prune.GenericFunction;
22 import tim.prune.I18nManager;
23 import tim.prune.config.Config;
24 import tim.prune.function.browser.BrowserLauncher;
25
26 /**
27  * Configure SRTM sources, including authentication for the NASA Earthdata systems
28  * @author fperrin, activityworkshop
29  */
30 public class ConfigureSrtmSources extends GenericFunction
31 {
32         private JDialog _dialog = null;
33         private JCheckBox _oneSecondCheck = null;
34         private boolean _oneSecondOriginallyOn = false;
35         private String _authString = null;
36         private JButton _okButton = null;
37
38
39         /**
40          * Constructor
41          * @param inApp App object
42          */
43         public ConfigureSrtmSources(App inApp) {
44                 super(inApp);
45         }
46
47         /** @return name key */
48         public String getNameKey() {
49                 return "function.configuresrtmsources";
50         }
51
52         /**
53          * Function entry point
54          */
55         public void begin()
56         {
57                 if (_dialog == null)
58                 {
59                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
60                         _dialog.setLocationRelativeTo(_parentFrame);
61                         // Create Gui and show it
62                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
63                         _dialog.getContentPane().add(makeDialogComponents());
64                         _dialog.pack();
65                 }
66                 prefillCurrentAuth();
67                 _dialog.setVisible(true);
68         }
69
70         /**
71          * Make the dialog components
72          * @return the GUI components for the dialog
73          */
74         private JPanel makeDialogComponents()
75         {
76                 JPanel dialogPanel = new JPanel();
77                 dialogPanel.setLayout(new BorderLayout());
78
79                 // Make a central panel with boxes for each of 3-second and 1-second sources
80                 JPanel mainPanel = new JPanel();
81                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
82                 JPanel introPanel = new JPanel();
83                 introPanel.setLayout(new BoxLayout(introPanel, BoxLayout.Y_AXIS));
84                 introPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.intro1")));
85                 introPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.intro2")));
86                 mainPanel.add(introPanel);
87                 mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
88
89                 // 3-second source
90                 JPanel threeSecondPanel = new JPanel();
91                 threeSecondPanel.setBorder(BorderFactory.createTitledBorder(""));
92                 threeSecondPanel.setLayout(new BoxLayout(threeSecondPanel, BoxLayout.Y_AXIS));
93                 JCheckBox threeSecondCheck = new JCheckBox(I18nManager.getText("dialog.configuresrtm.threesecond"));
94                 threeSecondCheck.setSelected(true);
95                 threeSecondCheck.setEnabled(false);
96                 threeSecondPanel.add(threeSecondCheck);
97                 threeSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.threesecond.desc")));
98
99                 mainPanel.add(threeSecondPanel);
100                 mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
101
102                 // 1-second source
103                 JPanel oneSecondPanel = new JPanel();
104                 oneSecondPanel.setBorder(BorderFactory.createTitledBorder(""));
105                 oneSecondPanel.setLayout(new BoxLayout(oneSecondPanel, BoxLayout.Y_AXIS));
106
107                 _oneSecondCheck = new JCheckBox(I18nManager.getText("dialog.configuresrtm.onesecond"));
108                 _oneSecondCheck.setSelected(true);
109                 oneSecondPanel.add(_oneSecondCheck);
110                 _oneSecondCheck.addActionListener(new ActionListener() {
111                         public void actionPerformed(ActionEvent inEvent) {
112                                 if (_oneSecondCheck.isSelected()) {
113                                         setupEarthdataAuth();
114                                 }
115                                 _okButton.setEnabled(_oneSecondCheck.isSelected() != _oneSecondOriginallyOn);
116                         }
117                 });
118                 oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc1")));
119                 oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc2")));
120
121                 mainPanel.add(oneSecondPanel);
122                 mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
123                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
124
125                 // ok / cancel buttons at bottom
126                 JPanel buttonPanel = new JPanel();
127                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
128                 _okButton = new JButton(I18nManager.getText("button.ok"));
129                 _okButton.addActionListener(new ActionListener() {
130                         public void actionPerformed(ActionEvent e) {
131                                 finish();
132                         }
133                 });
134                 buttonPanel.add(_okButton);
135                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
136                 cancelButton.addActionListener(new ActionListener() {
137                         public void actionPerformed(ActionEvent e) {
138                                 _dialog.dispose();
139                         }
140                 });
141                 buttonPanel.add(cancelButton);
142                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
143                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
144                 return dialogPanel;
145         }
146
147         /**
148          * User has activated the 1-second checkbox, so we need to ask for username / password details
149          * and save the result
150          */
151         private void setupEarthdataAuth()
152         {
153                 Object[] buttonTexts = {I18nManager.getText("button.yes"), I18nManager.getText("button.no")};
154                 int showWebsiteAnswer = JOptionPane.showOptionDialog(_parentFrame,
155                         I18nManager.getText("dialog.configuresrtm.showregistrationwebsite"),
156                         I18nManager.getText(getNameKey()), JOptionPane.YES_NO_OPTION,
157                         JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[1]);
158                 if (showWebsiteAnswer == JOptionPane.YES_OPTION) {
159                         BrowserLauncher.launchBrowser("https://urs.earthdata.nasa.gov/users/new");
160                 }
161                 // Now get the registered user id
162                 Object userId = JOptionPane.showInputDialog(_app.getFrame(),
163                         I18nManager.getText("dialog.configuresrtm.userid"),
164                         I18nManager.getText(getNameKey()),
165                         JOptionPane.QUESTION_MESSAGE, null, null, "");
166                 if (userId == null || userId.equals(""))
167                 {
168                         _oneSecondCheck.setSelected(false);
169                         return;
170                 }
171                 Object password = JOptionPane.showInputDialog(_app.getFrame(),
172                         I18nManager.getText("dialog.configuresrtm.password"),
173                         I18nManager.getText(getNameKey()),
174                         JOptionPane.QUESTION_MESSAGE, null, null, "");
175                 if (password == null || password.equals(""))
176                 {
177                         _oneSecondCheck.setSelected(false);
178                         return;
179                 }
180                 String authString = Base64.getEncoder().encodeToString((userId.toString() + ":" + password.toString()).getBytes());
181                 if (isAuthValid(authString)) {
182                         _authString = authString;
183                 }
184                 else
185                 {
186                         JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.configuresrtm.loginfailed"),
187                                 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
188                         _oneSecondCheck.setSelected(false);
189                 }
190         }
191
192         /**
193          * React to 'OK' being pressed, to save the config
194          */
195         private void finish()
196         {
197                 if (_oneSecondCheck.isSelected())
198                 {
199                         if (_authString != null) {
200                                 Config.setConfigString(Config.KEY_EARTHDATA_AUTH, _authString);
201                         }
202                 }
203                 else {
204                         Config.setConfigString(Config.KEY_EARTHDATA_AUTH, null);
205                 }
206                 _dialog.dispose();
207         }
208
209         /**
210          * Init the dialog according to the saved authentication config
211          */
212         private void prefillCurrentAuth()
213         {
214                 _oneSecondCheck.setSelected(isAuthValid(Config.getConfigString(Config.KEY_EARTHDATA_AUTH)));
215                 _authString = null;
216                 _oneSecondOriginallyOn = _oneSecondCheck.isSelected();
217                 _okButton.setEnabled(false);
218         }
219
220         /**
221          * Check the given config string for successful authentication using the NASA server
222          */
223         private boolean isAuthValid(String inConfigString)
224         {
225                 // TODO: Use this string to login to NASA server and check success or failure
226                 return inConfigString != null && !inConfigString.isEmpty();
227         }
228 }