1 package tim.prune.function.srtm;
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;
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;
21 import tim.prune.GenericFunction;
22 import tim.prune.I18nManager;
23 import tim.prune.config.Config;
24 import tim.prune.function.browser.BrowserLauncher;
27 * Configure SRTM sources, including authentication for the NASA Earthdata systems
28 * @author fperrin, activityworkshop
30 public class ConfigureSrtmSources extends GenericFunction
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;
41 * @param inApp App object
43 public ConfigureSrtmSources(App inApp) {
47 /** @return name key */
48 public String getNameKey() {
49 return "function.configuresrtmsources";
53 * Function entry point
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());
67 _dialog.setVisible(true);
71 * Make the dialog components
72 * @return the GUI components for the dialog
74 private JPanel makeDialogComponents()
76 JPanel dialogPanel = new JPanel();
77 dialogPanel.setLayout(new BorderLayout());
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)));
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")));
99 mainPanel.add(threeSecondPanel);
100 mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
103 JPanel oneSecondPanel = new JPanel();
104 oneSecondPanel.setBorder(BorderFactory.createTitledBorder(""));
105 oneSecondPanel.setLayout(new BoxLayout(oneSecondPanel, BoxLayout.Y_AXIS));
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();
115 _okButton.setEnabled(_oneSecondCheck.isSelected() != _oneSecondOriginallyOn);
118 oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc1")));
119 oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc2")));
121 mainPanel.add(oneSecondPanel);
122 mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
123 dialogPanel.add(mainPanel, BorderLayout.CENTER);
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) {
134 buttonPanel.add(_okButton);
135 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
136 cancelButton.addActionListener(new ActionListener() {
137 public void actionPerformed(ActionEvent e) {
141 buttonPanel.add(cancelButton);
142 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
143 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
148 * User has activated the 1-second checkbox, so we need to ask for username / password details
149 * and save the result
151 private void setupEarthdataAuth()
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");
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(""))
168 _oneSecondCheck.setSelected(false);
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(""))
177 _oneSecondCheck.setSelected(false);
180 String authString = Base64.getEncoder().encodeToString((userId.toString() + ":" + password.toString()).getBytes());
181 if (isAuthValid(authString)) {
182 _authString = authString;
186 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.configuresrtm.loginfailed"),
187 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
188 _oneSecondCheck.setSelected(false);
193 * React to 'OK' being pressed, to save the config
195 private void finish()
197 if (_oneSecondCheck.isSelected())
199 if (_authString != null) {
200 Config.setConfigString(Config.KEY_EARTHDATA_AUTH, _authString);
204 Config.setConfigString(Config.KEY_EARTHDATA_AUTH, null);
210 * Init the dialog according to the saved authentication config
212 private void prefillCurrentAuth()
214 _oneSecondCheck.setSelected(isAuthValid(Config.getConfigString(Config.KEY_EARTHDATA_AUTH)));
216 _oneSecondOriginallyOn = _oneSecondCheck.isSelected();
217 _okButton.setEnabled(false);
221 * Check the given config string for successful authentication using the NASA server
223 private boolean isAuthValid(String inConfigString)
225 // TODO: Use this string to login to NASA server and check success or failure
226 return inConfigString != null && !inConfigString.isEmpty();