REO progress

token setup usability
This commit is contained in:
rusefi 2020-06-22 18:30:59 -04:00
parent aeb3cf3db3
commit 43d89b4274
3 changed files with 73 additions and 6 deletions

View File

@ -5,8 +5,12 @@ import com.rusefi.ui.util.URLLabel;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
@ -30,26 +34,86 @@ public class AuthTokenPanel {
String authToken = getAuthToken();
System.out.println("Got from settings: " + authToken);
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
onTextChange();
}
@Override
public void removeUpdate(DocumentEvent e) {
onTextChange();
}
@Override
public void changedUpdate(DocumentEvent e) {
onTextChange();
}
});
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
JButton paste = new JButton("Paste from clipboard");
paste.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
if (AutoTokenUtil.isToken(data)) {
textField.setText(data);
}
} catch (IOException | UnsupportedFlavorException ex) {
// ignoring this exception
}
}
});
clipboard.addFlavorListener(e -> {
setPasteButtonEnabledBasedOnClipboardContent(clipboard, paste);
});
setPasteButtonEnabledBasedOnClipboardContent(clipboard, paste);
top.add(textField);
top.add(paste);
/*
JButton save = new JButton("Save");
save.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setAuthToken(AuthTokenPanel.this.textField.getText());
PersistentConfiguration.getConfig().save();
grabText();
}
});
top.add(textField);
top.add(save);
*/
content.add(top);
if (authToken.trim().isEmpty()) {
authToken = TOKEN_WARNING;
content.add(new URLLabel("Get your token here", TOKEN_PROFILE_URL), BorderLayout.SOUTH);
content.add(new URLLabel("Get your token from your forum profile", TOKEN_PROFILE_URL), BorderLayout.SOUTH);
}
textField.setText(authToken);
}
private void setPasteButtonEnabledBasedOnClipboardContent(Clipboard clipboard, JButton paste) {
try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
paste.setEnabled(AutoTokenUtil.isToken(data));
} catch (IOException | UnsupportedFlavorException ex) {
// ignoring this exception
}
}
private void grabText() {
setAuthToken(AuthTokenPanel.this.textField.getText());
PersistentConfiguration.getConfig().save();
}
private void onTextChange() {
if (AutoTokenUtil.isToken(textField.getText())) {
grabText();
}
}
public static void setAuthToken(String value) {
getConfig().getRoot().setProperty(AUTH_TOKEN, value);
}

View File

@ -2,6 +2,8 @@ package com.rusefi.ui;
public class AutoTokenUtil {
public static boolean isToken(String content) {
if (content == null)
return false;
content = content.trim();
if (content.length() != 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12)
return false;

View File

@ -9,6 +9,7 @@ import static org.junit.Assert.assertTrue;
public class AuthTokenUtilTest {
@Test
public void test() {
assertFalse(AutoTokenUtil.isToken(null));
assertTrue(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
assertFalse(AutoTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));