mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
add support for Win/Linux. Use MenuSelectionManager to collect data
This commit is contained in:
+51
-17
@@ -19,6 +19,8 @@ import com.intellij.internal.statistic.UsagesCollector;
|
||||
import com.intellij.internal.statistic.beans.ConvertUsagesUtil;
|
||||
import com.intellij.internal.statistic.beans.GroupDescriptor;
|
||||
import com.intellij.internal.statistic.beans.UsageDescriptor;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.impl.ActionMenu;
|
||||
import com.intellij.openapi.components.*;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -27,11 +29,13 @@ import com.intellij.util.xmlb.annotations.Tag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
@@ -53,30 +57,60 @@ public class MainMenuCollector implements PersistentStateComponent<MainMenuColle
|
||||
myState = state;
|
||||
}
|
||||
|
||||
public void record() {
|
||||
public void record(@NotNull AnAction action) {
|
||||
try {
|
||||
AWTEvent e = EventQueue.getCurrentEvent();
|
||||
String path = null;
|
||||
if (e instanceof ItemEvent) {
|
||||
Object src = e.getSource();
|
||||
StringBuilder path = new StringBuilder();
|
||||
while (src instanceof MenuItem) {
|
||||
String label = ((MenuItem)src).getLabel();
|
||||
path.insert(0, label + ((path.length() == 0) ? "" : " -> "));
|
||||
src = ((MenuItem)src).getParent();
|
||||
}
|
||||
path = getPathFromMenuItem(e, action);
|
||||
}
|
||||
else if (e instanceof MouseEvent) {
|
||||
path = getPathFromMenuSelectionManager(action);
|
||||
}
|
||||
|
||||
if (!StringUtil.isEmpty(path.toString())) {
|
||||
String key = ConvertUsagesUtil.escapeDescriptorName(path.toString());
|
||||
final Integer count = myState.myValues.get(key);
|
||||
int value = count == null ? 1 : count + 1;
|
||||
myState.myValues.put(key, value);
|
||||
}
|
||||
if (!StringUtil.isEmpty(path)) {
|
||||
String key = ConvertUsagesUtil.escapeDescriptorName(path);
|
||||
final Integer count = myState.myValues.get(key);
|
||||
int value = count == null ? 1 : count + 1;
|
||||
myState.myValues.put(key, value);
|
||||
}
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
protected String getPathFromMenuSelectionManager(@NotNull AnAction action) {
|
||||
List<String> groups = Arrays.stream(MenuSelectionManager.defaultManager().getSelectedPath())
|
||||
.filter(o -> o instanceof ActionMenu)
|
||||
.map(o -> ((ActionMenu)o).getText())
|
||||
.collect(Collectors.toList());
|
||||
if (groups.size() > 0) {
|
||||
String text = action.getTemplatePresentation().getText(); //avoid user data in Action Presentation
|
||||
groups.add(text);
|
||||
return convertMenuItemsToKey(groups);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String convertMenuItemsToKey(List<String> menuItems) {
|
||||
return StringUtil.join(menuItems, " -> ");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getPathFromMenuItem(AWTEvent e, AnAction action) {
|
||||
Object src = e.getSource();
|
||||
ArrayList<String> items = new ArrayList<>();
|
||||
while (src instanceof MenuItem) {
|
||||
items.add (0, ((MenuItem)src).getLabel());
|
||||
src = ((MenuItem)src).getParent();
|
||||
}
|
||||
if (items.size() > 1) {
|
||||
items.set(items.size() - 1, action.getTemplatePresentation().getText()); //avoid user data in Action Presentation
|
||||
}
|
||||
return convertMenuItemsToKey(items);
|
||||
}
|
||||
|
||||
|
||||
public static MainMenuCollector getInstance() {
|
||||
return ServiceManager.getService(MainMenuCollector.class);
|
||||
|
||||
Reference in New Issue
Block a user