1 package tim.prune.function.settings;
3 import java.awt.Component;
4 import java.awt.BorderLayout;
5 import java.awt.FlowLayout;
6 import java.awt.GridLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.util.Base64;
11 import javax.swing.BorderFactory;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JPasswordField;
18 import javax.swing.JTextField;
21 import tim.prune.GenericFunction;
22 import tim.prune.I18nManager;
23 import tim.prune.config.Config;
24 import tim.prune.function.srtm.SrtmGl1Source;
25 import tim.prune.function.srtm.SrtmSourceException;
28 * Set authentication data for the NASA Earthdata systems
30 public class SetEarthdataAuthentication extends GenericFunction
32 private JDialog _dialog = null;
33 private JTextField _usernameField = null;
34 private JPasswordField _passwordField = null;
35 private JLabel _authAccepted = null;
39 * @param inApp App object
41 public SetEarthdataAuthentication(App inApp) {
45 /** @return name key */
46 public String getNameKey() {
47 return "function.setearthdataauthentication";
54 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
55 _dialog.setLocationRelativeTo(_parentFrame);
56 // Create Gui and show it
57 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
58 _dialog.getContentPane().add(makeDialogComponents());
62 _dialog.setVisible(true);
66 * Make the dialog components
67 * @return the GUI components for the dialog
69 private JPanel makeDialogComponents()
71 // Blurb to explain to the user
72 JPanel dialogPanel = new JPanel();
73 dialogPanel.setLayout(new BorderLayout());
74 dialogPanel.add(new JLabel("<html>"+I18nManager.getText("dialog.earthdataauth.intro")+"</html>"), BorderLayout.NORTH);
76 // username and password fields
77 JPanel mainPanel = new JPanel();
78 mainPanel.setLayout(new BorderLayout());
79 JPanel usernamePasswordPanel = new JPanel();
80 usernamePasswordPanel.setLayout(new GridLayout(2, 2));
82 JLabel usernameLabel = new JLabel(I18nManager.getText("dialog.earthdataauth.user"));
83 usernamePasswordPanel.add(usernameLabel);
84 _usernameField = new JTextField("");
85 usernamePasswordPanel.add(_usernameField);
87 JLabel passwordLabel = new JLabel(I18nManager.getText("dialog.earthdataauth.password"));
88 usernamePasswordPanel.add(passwordLabel);
89 _passwordField = new JPasswordField("");
90 usernamePasswordPanel.add(_passwordField);
91 mainPanel.add(usernamePasswordPanel, BorderLayout.CENTER);
93 JPanel authStatusPanel = new JPanel();
94 authStatusPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
95 _authAccepted = new JLabel(" ");
96 authStatusPanel.add(_authAccepted);
97 mainPanel.add(authStatusPanel, BorderLayout.SOUTH);
99 mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
100 dialogPanel.add(mainPanel, BorderLayout.CENTER);
102 // ok / cancel buttons at bottom
103 JPanel buttonPanel = new JPanel();
104 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
105 JButton checkButton = new JButton(I18nManager.getText("button.check"));
106 checkButton.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent e) {
108 testUsernameAndPassword();
111 buttonPanel.add(checkButton);
112 JButton okButton = new JButton(I18nManager.getText("button.ok"));
113 okButton.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
118 buttonPanel.add(okButton);
119 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
120 cancelButton.addActionListener(new ActionListener() {
121 public void actionPerformed(ActionEvent e) {
125 buttonPanel.add(cancelButton);
126 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
127 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
131 private String getNewAuthString()
133 String username = _usernameField.getText();
134 String password = _passwordField.getText();
135 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
138 private void finish()
140 Config.setConfigString(Config.KEY_EARTHDATA_AUTH, getNewAuthString());
144 private void prefillCurrentAuth()
146 String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
147 if (authString == null)
149 _usernameField.setText("");
150 _passwordField.setText("");
152 String decoded = new String(Base64.getDecoder().decode(authString));
153 if (decoded.contains(":"))
155 _usernameField.setText(decoded.split(":", 2)[0]);
156 _passwordField.setText(decoded.split(":", 2)[1]);
160 _usernameField.setText("");
161 _passwordField.setText("");
164 _authAccepted.setText(" ");
167 private void testUsernameAndPassword()
169 String username = _usernameField.getText();
170 String password = _passwordField.getText();
171 SrtmGl1Source srtmGL1 = new SrtmGl1Source();
174 _authAccepted.setText("...");
175 srtmGL1.testAuth(getNewAuthString());
176 _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authaccepted"));
178 catch (SrtmSourceException e)
180 _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authrejected") + ": " + e.getMessage());