]> gitweb.fperrin.net Git - GpsPrune.git/commitdiff
Add a settings entry to configure Earthdata account github/srtm-multi-sources
authorFrédéric Perrin <fred@fperrin.net>
Sat, 30 Nov 2019 11:38:02 +0000 (11:38 +0000)
committerFrédéric Perrin <fred@fperrin.net>
Sat, 30 Nov 2019 11:38:02 +0000 (11:38 +0000)
src/tim/prune/FunctionLibrary.java
src/tim/prune/function/settings/SetEarthdataAuthentication.java [new file with mode: 0644]
src/tim/prune/gui/MenuManager.java
src/tim/prune/lang/prune-texts_en.properties

index 183c58149a0ea175b5bbbac54d929f037bcc43a4..ea78e9a175e0ba7b7f3d4edb6e95ac23c854bd78 100644 (file)
@@ -56,6 +56,7 @@ import tim.prune.function.settings.SaveConfig;
 import tim.prune.function.settings.SetAltitudeTolerance;
 import tim.prune.function.settings.SetColours;
 import tim.prune.function.settings.SetDisplaySettings;
+import tim.prune.function.settings.SetEarthdataAuthentication;
 import tim.prune.function.settings.SetLanguage;
 import tim.prune.function.settings.SetMapBgFunction;
 import tim.prune.function.settings.SetPathsFunction;
@@ -144,6 +145,7 @@ public abstract class FunctionLibrary
        public static GenericFunction FUNCTION_SET_COLOURS = null;
        public static GenericFunction FUNCTION_SET_LANGUAGE = null;
        public static SingleNumericParameterFunction FUNCTION_SET_ALTITUDE_TOLERANCE = null;
+       public static GenericFunction FUNCTION_SET_EARTHDATA_AUTH = null;
        public static GenericFunction FUNCTION_SET_TIMEZONE = null;
        public static GenericFunction FUNCTION_HELP   = null;
        public static GenericFunction FUNCTION_SHOW_KEYS = null;
@@ -224,6 +226,7 @@ public abstract class FunctionLibrary
                FUNCTION_SET_LANGUAGE = new SetLanguage(inApp);
                FUNCTION_SET_ALTITUDE_TOLERANCE = new SetAltitudeTolerance(inApp);
                FUNCTION_SET_TIMEZONE = new SelectTimezoneFunction(inApp);
+               FUNCTION_SET_EARTHDATA_AUTH = new SetEarthdataAuthentication(inApp);
                FUNCTION_HELP   = new HelpScreen(inApp);
                FUNCTION_SHOW_KEYS = new ShowKeysScreen(inApp);
                FUNCTION_ABOUT  = new AboutScreen(inApp);
diff --git a/src/tim/prune/function/settings/SetEarthdataAuthentication.java b/src/tim/prune/function/settings/SetEarthdataAuthentication.java
new file mode 100644 (file)
index 0000000..d4a166a
--- /dev/null
@@ -0,0 +1,183 @@
+package tim.prune.function.settings;
+
+import java.awt.Component;
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.Base64;
+
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JPasswordField;
+import javax.swing.JTextField;
+
+import tim.prune.App;
+import tim.prune.GenericFunction;
+import tim.prune.I18nManager;
+import tim.prune.config.Config;
+import tim.prune.function.srtm.SrtmGl1Source;
+import tim.prune.function.srtm.SrtmSourceException;
+
+/**
+ * Set authentication data for the NASA Earthdata systems
+ */
+public class SetEarthdataAuthentication extends GenericFunction
+{
+       private JDialog _dialog = null;
+       private JTextField _usernameField = null;
+       private JPasswordField _passwordField = null;
+       private JLabel _authAccepted = null;
+
+       /**
+        * Constructor
+        * @param inApp App object
+        */
+       public SetEarthdataAuthentication(App inApp) {
+               super(inApp);
+       }
+
+       /** @return name key */
+       public String getNameKey() {
+               return "function.setearthdataauthentication";
+       }
+
+       public void begin()
+       {
+               if (_dialog == null)
+               {
+                       _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
+                       _dialog.setLocationRelativeTo(_parentFrame);
+                       // Create Gui and show it
+                       _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+                       _dialog.getContentPane().add(makeDialogComponents());
+                       _dialog.pack();
+               }
+               prefillCurrentAuth();
+               _dialog.setVisible(true);
+       }
+
+       /**
+        * Make the dialog components
+        * @return the GUI components for the dialog
+        */
+       private JPanel makeDialogComponents()
+       {
+               // Blurb to explain to the user
+               JPanel dialogPanel = new JPanel();
+               dialogPanel.setLayout(new BorderLayout());
+               dialogPanel.add(new JLabel("<html>"+I18nManager.getText("dialog.earthdataauth.intro")+"</html>"), BorderLayout.NORTH);
+
+               // username and password fields
+               JPanel mainPanel = new JPanel();
+               mainPanel.setLayout(new BorderLayout());
+               JPanel usernamePasswordPanel = new JPanel();
+               usernamePasswordPanel.setLayout(new GridLayout(2, 2));
+
+               JLabel usernameLabel = new JLabel(I18nManager.getText("dialog.earthdataauth.user"));
+               usernamePasswordPanel.add(usernameLabel);
+               _usernameField = new JTextField("");
+               usernamePasswordPanel.add(_usernameField);
+
+               JLabel passwordLabel = new JLabel(I18nManager.getText("dialog.earthdataauth.password"));
+               usernamePasswordPanel.add(passwordLabel);
+               _passwordField = new JPasswordField("");
+               usernamePasswordPanel.add(_passwordField);
+               mainPanel.add(usernamePasswordPanel, BorderLayout.CENTER);
+
+               JPanel authStatusPanel = new JPanel();
+               authStatusPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
+               _authAccepted = new JLabel(" ");
+               authStatusPanel.add(_authAccepted);
+               mainPanel.add(authStatusPanel, BorderLayout.SOUTH);
+
+               mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
+               dialogPanel.add(mainPanel, BorderLayout.CENTER);
+
+               // ok / cancel buttons at bottom
+               JPanel buttonPanel = new JPanel();
+               buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
+               JButton checkButton = new JButton(I18nManager.getText("button.check"));
+               checkButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               testUsernameAndPassword();
+                       }
+               });
+               buttonPanel.add(checkButton);
+               JButton okButton = new JButton(I18nManager.getText("button.ok"));
+               okButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               finish();
+                       }
+               });
+               buttonPanel.add(okButton);
+               JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
+               cancelButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               _dialog.dispose();
+                       }
+               });
+               buttonPanel.add(cancelButton);
+               dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
+               dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
+               return dialogPanel;
+       }
+
+       private String getNewAuthString()
+       {
+               String username = _usernameField.getText();
+               String password = _passwordField.getText();
+               return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
+       }
+
+       private void finish()
+       {
+               Config.setConfigString(Config.KEY_EARTHDATA_AUTH, getNewAuthString());
+               _dialog.dispose();
+       }
+
+       private void prefillCurrentAuth()
+       {
+               String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
+               if (authString == null)
+               {
+                       _usernameField.setText("");
+                       _passwordField.setText("");
+               }
+               String decoded = new String(Base64.getDecoder().decode(authString));
+               if (decoded.contains(":"))
+               {
+                       _usernameField.setText(decoded.split(":", 2)[0]);
+                       _passwordField.setText(decoded.split(":", 2)[1]);
+               }
+               else
+               {
+                       _usernameField.setText("");
+                       _passwordField.setText("");
+               }
+
+               _authAccepted.setText(" ");
+       }
+
+       private void testUsernameAndPassword()
+       {
+               String username = _usernameField.getText();
+               String password = _passwordField.getText();
+               SrtmGl1Source srtmGL1 = new SrtmGl1Source();
+               try
+               {
+                       _authAccepted.setText("...");
+                       srtmGL1.testAuth(getNewAuthString());
+                       _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authaccepted"));
+               }
+               catch (SrtmSourceException e)
+               {
+                       _authAccepted.setText(I18nManager.getText("dialog.earthdataauth.authrejected") + ": " + e.getMessage());
+               }
+       }
+}
index 89903d8d92b7473816face59b60601045d067330..b24c7184998a67c7d04f5cf8f8260c269002d6c9 100644 (file)
@@ -661,6 +661,8 @@ public class MenuManager implements DataSubscriber
                settingsMenu.add(makeMenuItem(new ChooseSingleParameter(_app, FunctionLibrary.FUNCTION_SET_ALTITUDE_TOLERANCE)));
                // Set timezone
                settingsMenu.add(makeMenuItem(FunctionLibrary.FUNCTION_SET_TIMEZONE));
+               // Set Earthdata authentication
+               settingsMenu.add(makeMenuItem(FunctionLibrary.FUNCTION_SET_EARTHDATA_AUTH));
                settingsMenu.addSeparator();
                // Save configuration
                settingsMenu.add(makeMenuItem(FunctionLibrary.FUNCTION_SAVECONFIG));
index 9e540eb20b60e02907443733221b7eb3bf6fa4be..736c145151a73dffe163871ef251fa95069c5a62 100644 (file)
@@ -153,6 +153,7 @@ function.managetilecache=Manage tile cache
 function.getweatherforecast=Get weather forecast
 function.setaltitudetolerance=Set altitude tolerance
 function.selecttimezone=Set timezone
+function.setearthdataauthentication=Set Earthdata authentication
 
 # Dialogs
 dialog.exit.confirm.title=Exit GpsPrune
@@ -578,6 +579,11 @@ dialog.displaysettings.size.small=Small
 dialog.displaysettings.size.medium=Medium
 dialog.displaysettings.size.large=Large
 dialog.downloadosm.desc=Confirm to download the raw OSM data for the specified area:
+dialog.earthdataauth.intro=<p>Configure username and password to access your NASA Earthdata login account.</p><p> Create an account at <tt>https://urs.earthdata.nasa.gov/users/new</tt>.</p>
+dialog.earthdataauth.user=Username
+dialog.earthdataauth.password=Password
+dialog.earthdataauth.authaccepted=Username and password accepted
+dialog.earthdataauth.authrejected=Username and password rejected
 dialog.searchwikipedianames.search=Search for:
 dialog.weather.location=Location
 dialog.weather.update=Forecast updated