]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/settings/SetEarthdataAuthentication.java
Add a settings entry to configure Earthdata account
[GpsPrune.git] / src / tim / prune / function / settings / SetEarthdataAuthentication.java
1 package tim.prune.function.settings;
2
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;
10
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;
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.srtm.SrtmGl1Source;
25 import tim.prune.function.srtm.SrtmSourceException;
26
27 /**
28  * Set authentication data for the NASA Earthdata systems
29  */
30 public class SetEarthdataAuthentication extends GenericFunction
31 {
32         private JDialog _dialog = null;
33         private JTextField _usernameField = null;
34         private JPasswordField _passwordField = null;
35         private JLabel _authAccepted = null;
36
37         /**
38          * Constructor
39          * @param inApp App object
40          */
41         public SetEarthdataAuthentication(App inApp) {
42                 super(inApp);
43         }
44
45         /** @return name key */
46         public String getNameKey() {
47                 return "function.setearthdataauthentication";
48         }
49
50         public void begin()
51         {
52                 if (_dialog == null)
53                 {
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());
59                         _dialog.pack();
60                 }
61                 prefillCurrentAuth();
62                 _dialog.setVisible(true);
63         }
64
65         /**
66          * Make the dialog components
67          * @return the GUI components for the dialog
68          */
69         private JPanel makeDialogComponents()
70         {
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);
75
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));
81
82                 JLabel usernameLabel = new JLabel(I18nManager.getText("dialog.earthdataauth.user"));
83                 usernamePasswordPanel.add(usernameLabel);
84                 _usernameField = new JTextField("");
85                 usernamePasswordPanel.add(_usernameField);
86
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);
92
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);
98
99                 mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
100                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
101
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();
109                         }
110                 });
111                 buttonPanel.add(checkButton);
112                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
113                 okButton.addActionListener(new ActionListener() {
114                         public void actionPerformed(ActionEvent e) {
115                                 finish();
116                         }
117                 });
118                 buttonPanel.add(okButton);
119                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
120                 cancelButton.addActionListener(new ActionListener() {
121                         public void actionPerformed(ActionEvent e) {
122                                 _dialog.dispose();
123                         }
124                 });
125                 buttonPanel.add(cancelButton);
126                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
127                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
128                 return dialogPanel;
129         }
130
131         private String getNewAuthString()
132         {
133                 String username = _usernameField.getText();
134                 String password = _passwordField.getText();
135                 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
136         }
137
138         private void finish()
139         {
140                 Config.setConfigString(Config.KEY_EARTHDATA_AUTH, getNewAuthString());
141                 _dialog.dispose();
142         }
143
144         private void prefillCurrentAuth()
145         {
146                 String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
147                 if (authString == null)
148                 {
149                         _usernameField.setText("");
150                         _passwordField.setText("");
151                 }
152                 String decoded = new String(Base64.getDecoder().decode(authString));
153                 if (decoded.contains(":"))
154                 {
155                         _usernameField.setText(decoded.split(":", 2)[0]);
156                         _passwordField.setText(decoded.split(":", 2)[1]);
157                 }
158                 else
159                 {
160                         _usernameField.setText("");
161                         _passwordField.setText("");
162                 }
163
164                 _authAccepted.setText(" ");
165         }
166
167         private void testUsernameAndPassword()
168         {
169                 String username = _usernameField.getText();
170                 String password = _passwordField.getText();
171                 SrtmGl1Source srtmGL1 = new SrtmGl1Source();
172                 try
173                 {
174                         _authAccepted.setText("...");
175                         srtmGL1.testAuth(getNewAuthString());
176                         _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authaccepted"));
177                 }
178                 catch (SrtmSourceException e)
179                 {
180                         _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authrejected") + ": " + e.getMessage());
181                 }
182         }
183 }