From 43d89b42744a605c1236d2157cbb3f6df488a377 Mon Sep 17 00:00:00 2001 From: rusefi Date: Mon, 22 Jun 2020 18:30:59 -0400 Subject: [PATCH] REO progress token setup usability --- .../src/com/rusefi/ui/AuthTokenPanel.java | 76 +++++++++++++++++-- .../src/com/rusefi/ui/AutoTokenUtil.java | 2 + .../com/rusefi/ui/test/AuthTokenUtilTest.java | 1 + 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/java_console/shared_ui/src/com/rusefi/ui/AuthTokenPanel.java b/java_console/shared_ui/src/com/rusefi/ui/AuthTokenPanel.java index 2b4dc6e0a1..3fe694c8ef 100644 --- a/java_console/shared_ui/src/com/rusefi/ui/AuthTokenPanel.java +++ b/java_console/shared_ui/src/com/rusefi/ui/AuthTokenPanel.java @@ -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); } diff --git a/java_console/shared_ui/src/com/rusefi/ui/AutoTokenUtil.java b/java_console/shared_ui/src/com/rusefi/ui/AutoTokenUtil.java index 8b9be7a3c1..11c4612717 100644 --- a/java_console/shared_ui/src/com/rusefi/ui/AutoTokenUtil.java +++ b/java_console/shared_ui/src/com/rusefi/ui/AutoTokenUtil.java @@ -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; diff --git a/java_console/shared_ui/src/com/rusefi/ui/test/AuthTokenUtilTest.java b/java_console/shared_ui/src/com/rusefi/ui/test/AuthTokenUtilTest.java index 5d6100f0e3..b12e3789e4 100644 --- a/java_console/shared_ui/src/com/rusefi/ui/test/AuthTokenUtilTest.java +++ b/java_console/shared_ui/src/com/rusefi/ui/test/AuthTokenUtilTest.java @@ -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 "));