From 5f52b9203ecfba83907babc043aa178a524eae09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20L=C3=B6venich?= Date: Thu, 19 Nov 2020 13:16:43 +0100 Subject: [PATCH 1/2] added copy to clipboard function --- .../messages/KeyPromoterBundle.properties | 2 + .../KeyPromoterToolWindowPanel.form | 19 +++++++- .../KeyPromoterToolWindowPanel.java | 8 ++++ .../clipboard/CopyToClipboardAction.java | 43 +++++++++++++++++++ .../renderer/MarkdownStatisticsRenderer.java | 25 +++++++++++ .../renderer/StatisticsRenderer.java | 9 ++++ .../renderer/TextStatisticsRenderer.java | 39 +++++++++++++++++ .../statistic/KeyPromoterStatistics.java | 2 +- 8 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/de/halirutan/keypromoterx/clipboard/CopyToClipboardAction.java create mode 100644 src/de/halirutan/keypromoterx/clipboard/renderer/MarkdownStatisticsRenderer.java create mode 100644 src/de/halirutan/keypromoterx/clipboard/renderer/StatisticsRenderer.java create mode 100644 src/de/halirutan/keypromoterx/clipboard/renderer/TextStatisticsRenderer.java diff --git a/resources/de/halirutan/keypromoterx/messages/KeyPromoterBundle.properties b/resources/de/halirutan/keypromoterx/messages/KeyPromoterBundle.properties index 2f41ddd..8f326b0 100644 --- a/resources/de/halirutan/keypromoterx/messages/KeyPromoterBundle.properties +++ b/resources/de/halirutan/keypromoterx/messages/KeyPromoterBundle.properties @@ -11,6 +11,8 @@ # kp.dialog.reset.statistic.text=Do you really want to reset your Key Promoter X statistics? This cannot be undone! kp.dialog.reset.statistic.title=Reset Statistics +kp.dialog.copyclipboard.statistic.text=Your Key Promoter X statistics are now available in your clipboard! +kp.dialog.copyclipboard.statistic.title=Copy Statistics to clipboard kp.list.item= {0} for {1} ({2,Number}x missed, {3, Number}x used) kp.list.suppressed.item= {0} for {1} kp.meta.default=Alt+ diff --git a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form index 82a544d..81f8bd1 100644 --- a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form +++ b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form @@ -39,7 +39,7 @@ - + @@ -70,6 +70,23 @@ + + + + + + + + + + + + + + + diff --git a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.java b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.java index 24fde6b..40ed15e 100644 --- a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.java +++ b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.java @@ -26,6 +26,7 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.ui.Messages; +import de.halirutan.keypromoterx.clipboard.CopyToClipboardAction; import de.halirutan.keypromoterx.statistic.KeyPromoterStatistics; import de.halirutan.keypromoterx.statistic.StatisticsList; import de.halirutan.keypromoterx.statistic.SuppressedList; @@ -44,11 +45,14 @@ class KeyPromoterToolWindowPanel implements Disposable, SnoozeNotifier.Handler { private JPanel panel; private JList statisticsList; private JButton resetStatisticsButton; + private JButton copyStatisticsToClipboardButton; private JList suppressedList; private JCheckBox snoozeCheckBox; + private JCheckBox useMarkdownFormatCheckBox; KeyPromoterToolWindowPanel() { resetStatisticsButton.addActionListener(e -> resetStats()); + copyStatisticsToClipboardButton.addActionListener(e -> copyToClipboardStats()); Topics.subscribe(SnoozeNotifier.Handler.SNOOZE_TOPIC, this, this); snoozeCheckBox.addItemListener(e -> SnoozeNotifier.setSnoozed(snoozeCheckBox.isSelected())); } @@ -67,6 +71,10 @@ private void resetStats() { } } + private void copyToClipboardStats() { + CopyToClipboardAction.copyStatisticsToClipboard(useMarkdownFormatCheckBox.isSelected()); + } + private void createUIComponents() { statisticsList = new StatisticsList(); suppressedList = new SuppressedList(); diff --git a/src/de/halirutan/keypromoterx/clipboard/CopyToClipboardAction.java b/src/de/halirutan/keypromoterx/clipboard/CopyToClipboardAction.java new file mode 100644 index 0000000..60aba1f --- /dev/null +++ b/src/de/halirutan/keypromoterx/clipboard/CopyToClipboardAction.java @@ -0,0 +1,43 @@ +package de.halirutan.keypromoterx.clipboard; + +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.ui.Messages; +import de.halirutan.keypromoterx.KeyPromoterBundle; +import de.halirutan.keypromoterx.clipboard.renderer.MarkdownStatisticsRenderer; +import de.halirutan.keypromoterx.clipboard.renderer.StatisticsRenderer; +import de.halirutan.keypromoterx.clipboard.renderer.TextStatisticsRenderer; +import de.halirutan.keypromoterx.statistic.KeyPromoterStatistics; +import de.halirutan.keypromoterx.statistic.StatisticsItem; + +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.util.ArrayList; + +public class CopyToClipboardAction { + + public static Clipboard getSystemClipboard() { + return Toolkit.getDefaultToolkit().getSystemClipboard(); + } + + public static void copyStatisticsToClipboard(boolean useMarkdownFormat) { + final KeyPromoterStatistics statService = ServiceManager.getService(KeyPromoterStatistics.class); + ArrayList statistics = statService.getStatisticItems(); + + StatisticsRenderer statisticsRenderer; + if (useMarkdownFormat) { + statisticsRenderer = new MarkdownStatisticsRenderer(); + } else { + statisticsRenderer = new TextStatisticsRenderer(); + } + String clipboardContent = statisticsRenderer.renderStatistics(statistics); + + Clipboard clipboard = getSystemClipboard(); + clipboard.setContents(new StringSelection(clipboardContent), null); + Messages.showInfoMessage( + KeyPromoterBundle.message("kp.dialog.copyclipboard.statistic.text"), + KeyPromoterBundle.message("kp.dialog.copyclipboard.statistic.title") + ); + } + +} diff --git a/src/de/halirutan/keypromoterx/clipboard/renderer/MarkdownStatisticsRenderer.java b/src/de/halirutan/keypromoterx/clipboard/renderer/MarkdownStatisticsRenderer.java new file mode 100644 index 0000000..b5d72c9 --- /dev/null +++ b/src/de/halirutan/keypromoterx/clipboard/renderer/MarkdownStatisticsRenderer.java @@ -0,0 +1,25 @@ +package de.halirutan.keypromoterx.clipboard.renderer; + +import de.halirutan.keypromoterx.statistic.StatisticsItem; + +import java.util.ArrayList; +import java.util.Comparator; + +public class MarkdownStatisticsRenderer implements StatisticsRenderer { + @Override + public String renderStatistics(ArrayList statistics) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("# Key Promoter X Shortcuts\n\n"); + stringBuilder.append("|Description|Shortcut|\n|---|:---|\n"); + + statistics.stream().sorted(Comparator.comparing(StatisticsItem::getCount).reversed()).forEach( + item -> { + stringBuilder.append(item.description); + stringBuilder.append("|"); + stringBuilder.append(item.shortCut); + stringBuilder.append("\n"); + } + ); + return stringBuilder.toString(); + } +} diff --git a/src/de/halirutan/keypromoterx/clipboard/renderer/StatisticsRenderer.java b/src/de/halirutan/keypromoterx/clipboard/renderer/StatisticsRenderer.java new file mode 100644 index 0000000..e803f8c --- /dev/null +++ b/src/de/halirutan/keypromoterx/clipboard/renderer/StatisticsRenderer.java @@ -0,0 +1,9 @@ +package de.halirutan.keypromoterx.clipboard.renderer; + +import de.halirutan.keypromoterx.statistic.StatisticsItem; + +import java.util.ArrayList; + +public interface StatisticsRenderer { + String renderStatistics(ArrayList statistics); +} diff --git a/src/de/halirutan/keypromoterx/clipboard/renderer/TextStatisticsRenderer.java b/src/de/halirutan/keypromoterx/clipboard/renderer/TextStatisticsRenderer.java new file mode 100644 index 0000000..6be32d4 --- /dev/null +++ b/src/de/halirutan/keypromoterx/clipboard/renderer/TextStatisticsRenderer.java @@ -0,0 +1,39 @@ +package de.halirutan.keypromoterx.clipboard.renderer; + +import de.halirutan.keypromoterx.statistic.StatisticsItem; + +import java.util.ArrayList; +import java.util.Comparator; + +public class TextStatisticsRenderer implements StatisticsRenderer { + + private static final int DEFAULT_INDENTION = 25; + + @Override + public String renderStatistics(ArrayList statistics) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Key Promoter X Shortcuts\n"); + stringBuilder.append("==========================================\n\n"); + stringBuilder.append("Description Shortcut\n"); + stringBuilder.append("------------------------------------------\n\n"); + + statistics.stream().sorted(Comparator.comparing(StatisticsItem::getCount).reversed()).forEach( + item -> { + stringBuilder.append(item.description); + stringBuilder.append(fillWithWhitespaces(item.description)); + stringBuilder.append(item.shortCut); + stringBuilder.append("\n"); + } + ); + return stringBuilder.toString(); + } + + private String fillWithWhitespaces(String text) { + int missingWhitespacesCount = DEFAULT_INDENTION - text.length(); + StringBuilder builder = new StringBuilder(" "); + for (int i=0; i getStatisticItems() { + public ArrayList getStatisticItems() { final ArrayList items = new ArrayList<>(statistics.values()); Collections.sort(items); return items; From 1914f4b5e56e7cb275585214e255e81b0cfccde9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20L=C3=B6venich?= Date: Thu, 19 Nov 2020 13:24:15 +0100 Subject: [PATCH 2/2] fixed spacer --- .../keypromoterx/KeyPromoterToolWindowPanel.form | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form index 81f8bd1..1390b6e 100644 --- a/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form +++ b/src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form @@ -65,11 +65,6 @@ - - - - - @@ -87,6 +82,11 @@ + + + + +