1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
package tim.prune.function.srtm;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Base64;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.Config;
import tim.prune.function.browser.BrowserLauncher;
/**
* Configure SRTM sources, including authentication for the NASA Earthdata systems
* @author fperrin, activityworkshop
*/
public class ConfigureSrtmSources extends GenericFunction
{
private JDialog _dialog = null;
private JCheckBox _oneSecondCheck = null;
private boolean _oneSecondOriginallyOn = false;
private String _authString = null;
private JButton _okButton = null;
/**
* Constructor
* @param inApp App object
*/
public ConfigureSrtmSources(App inApp) {
super(inApp);
}
/** @return name key */
public String getNameKey() {
return "function.configuresrtmsources";
}
/**
* Function entry point
*/
public void begin()
{
if (_dialog == null)
{
_dialog = new JDialog(_parentFrame, getName(), 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()
{
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new BorderLayout());
// Make a central panel with boxes for each of 3-second and 1-second sources
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel introPanel = new JPanel();
introPanel.setLayout(new BoxLayout(introPanel, BoxLayout.Y_AXIS));
introPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.intro1")));
introPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.intro2")));
mainPanel.add(introPanel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
// 3-second source
JPanel threeSecondPanel = new JPanel();
threeSecondPanel.setBorder(BorderFactory.createTitledBorder(""));
threeSecondPanel.setLayout(new BoxLayout(threeSecondPanel, BoxLayout.Y_AXIS));
JCheckBox threeSecondCheck = new JCheckBox(I18nManager.getText("dialog.configuresrtm.threesecond"));
threeSecondCheck.setSelected(true);
threeSecondCheck.setEnabled(false);
threeSecondPanel.add(threeSecondCheck);
threeSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.threesecond.desc")));
mainPanel.add(threeSecondPanel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
// 1-second source
JPanel oneSecondPanel = new JPanel();
oneSecondPanel.setBorder(BorderFactory.createTitledBorder(""));
oneSecondPanel.setLayout(new BoxLayout(oneSecondPanel, BoxLayout.Y_AXIS));
_oneSecondCheck = new JCheckBox(I18nManager.getText("dialog.configuresrtm.onesecond"));
_oneSecondCheck.setSelected(true);
oneSecondPanel.add(_oneSecondCheck);
_oneSecondCheck.addActionListener(e -> {
if (_oneSecondCheck.isSelected()) {
setupEarthdataAuth();
}
_okButton.setEnabled(_oneSecondCheck.isSelected() != _oneSecondOriginallyOn);
});
oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc1")));
oneSecondPanel.add(new JLabel(I18nManager.getText("dialog.configuresrtm.onesecond.desc2")));
mainPanel.add(oneSecondPanel);
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));
_okButton = new JButton(I18nManager.getText("button.ok"));
_okButton.addActionListener(e -> finish());
buttonPanel.add(_okButton);
JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
cancelButton.addActionListener(e -> _dialog.dispose());
buttonPanel.add(cancelButton);
dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
return dialogPanel;
}
/**
* User has activated the 1-second checkbox, so we need to ask for username / password details
* and save the result
*/
private void setupEarthdataAuth()
{
Object[] buttonTexts = {I18nManager.getText("button.yes"), I18nManager.getText("button.no")};
int showWebsiteAnswer = JOptionPane.showOptionDialog(_parentFrame,
I18nManager.getText("dialog.configuresrtm.showregistrationwebsite"),
getName(), JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[1]);
if (showWebsiteAnswer == JOptionPane.YES_OPTION) {
BrowserLauncher.launchBrowser("https://urs.earthdata.nasa.gov/users/new");
}
// Now get the registered user id
Object userId = JOptionPane.showInputDialog(_app.getFrame(),
I18nManager.getText("dialog.configuresrtm.userid"), getName(),
JOptionPane.QUESTION_MESSAGE, null, null, "");
if (userId == null || userId.equals(""))
{
_oneSecondCheck.setSelected(false);
return;
}
Object password = JOptionPane.showInputDialog(_app.getFrame(),
I18nManager.getText("dialog.configuresrtm.password"), getName(),
JOptionPane.QUESTION_MESSAGE, null, null, "");
if (password == null || password.equals(""))
{
_oneSecondCheck.setSelected(false);
return;
}
String authString = Base64.getEncoder().encodeToString((userId.toString() + ":" + password.toString()).getBytes());
if (isAuthValid(authString)) {
_authString = authString;
}
else
{
JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.configuresrtm.loginfailed"),
getName(), JOptionPane.ERROR_MESSAGE);
_oneSecondCheck.setSelected(false);
}
}
/**
* React to 'OK' being pressed, to save the config
*/
private void finish()
{
if (_oneSecondCheck.isSelected())
{
if (_authString != null) {
getConfig().setConfigString(Config.KEY_EARTHDATA_AUTH, _authString);
}
}
else {
getConfig().setConfigString(Config.KEY_EARTHDATA_AUTH, null);
}
_dialog.dispose();
}
/**
* Init the dialog according to the saved authentication config
*/
private void prefillCurrentAuth()
{
String authString = getConfig().getConfigString(Config.KEY_EARTHDATA_AUTH);
_oneSecondCheck.setSelected(isAuthValid(authString));
_authString = null;
_oneSecondOriginallyOn = _oneSecondCheck.isSelected();
_okButton.setEnabled(false);
}
/**
* Check the given config string for successful authentication using the NASA server
*/
private boolean isAuthValid(String inConfigString)
{
// TODO: Use this string to login to NASA server and check success or failure
return inConfigString != null && !inConfigString.isEmpty();
}
}
|