Skip to content

copy statistics to clipboard #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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=<html> <b>{0}</b> for {1} ({2,Number}x missed, {3, Number}x used)</html>
kp.list.suppressed.item=<html> <b>{0}</b> for {1}</html>
kp.meta.default=Alt+
Expand Down
21 changes: 19 additions & 2 deletions src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="2b6d2" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="2b6d2" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -34,9 +34,26 @@
<text value="Snooze Notifications"/>
</properties>
</component>
<component id="4457a" class="javax.swing.JButton" binding="copyStatisticsToClipboardButton">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Copy to clipboard"/>
<text value="&amp;Copy to clipboard"/>
</properties>
</component>
<component id="7b869" class="javax.swing.JCheckBox" binding="useMarkdownFormatCheckBox" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Use Markdown Format"/>
</properties>
</component>
<hspacer id="beba2">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
Expand Down
8 changes: 8 additions & 0 deletions src/de/halirutan/keypromoterx/KeyPromoterToolWindowPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.Messages;
import de.halirutan.keypromoterx.clipboard.CopyToClipboardAction;
import de.halirutan.keypromoterx.statistic.KeyPromoterStatistics;
import de.halirutan.keypromoterx.statistic.StatisticsItem;
import de.halirutan.keypromoterx.statistic.StatisticsList;
Expand All @@ -45,12 +46,15 @@ class KeyPromoterToolWindowPanel implements Disposable, SnoozeNotifier.Handler {
private JPanel panel;
private JList<StatisticsItem> statisticsList;
private JButton resetStatisticsButton;
private JButton copyStatisticsToClipboardButton;
private JList<StatisticsItem> suppressedList;
private JCheckBox snoozeCheckBox;
private JCheckBox useMarkdownFormatCheckBox;
private JSplitPane splitPane;

KeyPromoterToolWindowPanel() {
resetStatisticsButton.addActionListener(e -> resetStats());
copyStatisticsToClipboardButton.addActionListener(e -> copyToClipboardStats());
Topics.subscribe(SnoozeNotifier.Handler.SNOOZE_TOPIC, this, this);
snoozeCheckBox.addItemListener(e -> SnoozeNotifier.setSnoozed(snoozeCheckBox.isSelected()));
}
Expand All @@ -69,6 +73,10 @@ private void resetStats() {
}
}

private void copyToClipboardStats() {
CopyToClipboardAction.copyStatisticsToClipboard(useMarkdownFormatCheckBox.isSelected());
}

private void createUIComponents() {
statisticsList = new StatisticsList();
suppressedList = new SuppressedList();
Expand Down
45 changes: 45 additions & 0 deletions src/de/halirutan/keypromoterx/clipboard/CopyToClipboardAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.halirutan.keypromoterx.clipboard;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
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 = ApplicationManager.getApplication().getService(KeyPromoterStatistics.class);
ArrayList<StatisticsItem> 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")
);
}

}
Original file line number Diff line number Diff line change
@@ -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<StatisticsItem> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<StatisticsItem> statistics);
}
Original file line number Diff line number Diff line change
@@ -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<StatisticsItem> 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<missingWhitespacesCount-1; i++) {
builder.append(" ");
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public StatisticsItem get(KeyPromoterAction action) {
}

@Transient
ArrayList<StatisticsItem> getStatisticItems() {
public ArrayList<StatisticsItem> getStatisticItems() {
final ArrayList<StatisticsItem> items = new ArrayList<>(statistics.values());
Collections.sort(items);
return items;
Expand Down