parent
aeb3cf3db3
commit
43d89b4274
|
@ -5,8 +5,12 @@ import com.rusefi.ui.util.URLLabel;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.DocumentEvent;
|
||||||
|
import javax.swing.event.DocumentListener;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
|
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
|
||||||
|
|
||||||
|
@ -30,26 +34,86 @@ public class AuthTokenPanel {
|
||||||
String authToken = getAuthToken();
|
String authToken = getAuthToken();
|
||||||
System.out.println("Got from settings: " + authToken);
|
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");
|
JButton save = new JButton("Save");
|
||||||
save.addActionListener(new AbstractAction() {
|
save.addActionListener(new AbstractAction() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
setAuthToken(AuthTokenPanel.this.textField.getText());
|
grabText();
|
||||||
PersistentConfiguration.getConfig().save();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
top.add(textField);
|
|
||||||
top.add(save);
|
top.add(save);
|
||||||
|
*/
|
||||||
content.add(top);
|
content.add(top);
|
||||||
if (authToken.trim().isEmpty()) {
|
if (authToken.trim().isEmpty()) {
|
||||||
authToken = TOKEN_WARNING;
|
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);
|
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) {
|
public static void setAuthToken(String value) {
|
||||||
getConfig().getRoot().setProperty(AUTH_TOKEN, value);
|
getConfig().getRoot().setProperty(AUTH_TOKEN, value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package com.rusefi.ui;
|
||||||
|
|
||||||
public class AutoTokenUtil {
|
public class AutoTokenUtil {
|
||||||
public static boolean isToken(String content) {
|
public static boolean isToken(String content) {
|
||||||
|
if (content == null)
|
||||||
|
return false;
|
||||||
content = content.trim();
|
content = content.trim();
|
||||||
if (content.length() != 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12)
|
if (content.length() != 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12)
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -9,6 +9,7 @@ import static org.junit.Assert.assertTrue;
|
||||||
public class AuthTokenUtilTest {
|
public class AuthTokenUtilTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
assertFalse(AutoTokenUtil.isToken(null));
|
||||||
assertTrue(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
|
assertTrue(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
|
||||||
|
|
||||||
assertFalse(AutoTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));
|
assertFalse(AutoTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));
|
||||||
|
|
Loading…
Reference in New Issue