From: Frédéric Perrin Date: Sat, 30 Nov 2019 11:38:02 +0000 (+0000) Subject: Add a settings entry to configure Earthdata account X-Git-Tag: v19.2.fp1~1^2 X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=commitdiff_plain;h=9c79031b2f544a30fdb4377f365d7d0a348eb008 Add a settings entry to configure Earthdata account --- diff --git a/src/tim/prune/FunctionLibrary.java b/src/tim/prune/FunctionLibrary.java index 183c581..ea78e9a 100644 --- a/src/tim/prune/FunctionLibrary.java +++ b/src/tim/prune/FunctionLibrary.java @@ -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 index 0000000..d4a166a --- /dev/null +++ b/src/tim/prune/function/settings/SetEarthdataAuthentication.java @@ -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(""+I18nManager.getText("dialog.earthdataauth.intro")+""), 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()); + } + } +} diff --git a/src/tim/prune/gui/MenuManager.java b/src/tim/prune/gui/MenuManager.java index 89903d8..b24c718 100644 --- a/src/tim/prune/gui/MenuManager.java +++ b/src/tim/prune/gui/MenuManager.java @@ -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)); diff --git a/src/tim/prune/lang/prune-texts_en.properties b/src/tim/prune/lang/prune-texts_en.properties index 9e540eb..736c145 100644 --- a/src/tim/prune/lang/prune-texts_en.properties +++ b/src/tim/prune/lang/prune-texts_en.properties @@ -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=

Configure username and password to access your NASA Earthdata login account.

Create an account at https://urs.earthdata.nasa.gov/users/new.

+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