From 26b65b36e5490c24ff72133595c6e9b0b42eede3 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Tue, 3 Nov 2020 16:45:43 +0800 Subject: [PATCH] add option: Reset Automatically Signed-off-by: pengzhile --- build.gradle | 29 +++++++------ .../intellij/ier/action/ResetAction.java | 43 ++++++++++++++++--- .../intellij/ier/action/RestartAction.java | 19 ++++++++ .../intellij/ier/common/Resetter.java | 9 ++++ .../intellij/ier/component/ResetTimer.java | 36 +++++++++++++--- .../intellij/ier/helper/Constants.java | 5 ++- .../intellij/ier/helper/ProjectHelper.java | 29 +++++++++++++ .../intellij/ier/ui/form/MainForm.form | 12 ++++-- .../intellij/ier/ui/form/MainForm.java | 26 ++++++++--- src/main/resources/META-INF/plugin.xml | 5 --- 10 files changed, 175 insertions(+), 38 deletions(-) create mode 100644 src/main/java/io/zhile/research/intellij/ier/action/RestartAction.java create mode 100644 src/main/java/io/zhile/research/intellij/ier/helper/ProjectHelper.java diff --git a/build.gradle b/build.gradle index d292e73..3d21211 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'io.zhile.research.intellij' -version '2.0.0' +version '2.0.1' sourceCompatibility = 1.7 targetCompatibility = 1.7 @@ -28,18 +28,21 @@ intellij { } patchPluginXml { - changeNotes """ - release v2.0.0 - 1. add ui - 2. more stable and accurate - release v1.0.5 - 1. update for 2020.2.x - release v1.0.4 - 1. reset completely - release v1.0.3 - 1. bug fix - release v1.0.2 - 1. compatibility fix""" + changeNotes """
+Release v2.0.1
+  1. add option: Reset Automatically
+Release v2.0.0
+  1. add ui
+  2. more stable and accurate
+Release v1.0.5
+  1. update for 2020.2.x
+Release v1.0.4
+  1. reset completely
+Release v1.0.3
+  1. bug fix
+Release v1.0.2
+  1. compatibility fix
+
""" sinceBuild "145.258" untilBuild null diff --git a/src/main/java/io/zhile/research/intellij/ier/action/ResetAction.java b/src/main/java/io/zhile/research/intellij/ier/action/ResetAction.java index c87214a..6f76859 100644 --- a/src/main/java/io/zhile/research/intellij/ier/action/ResetAction.java +++ b/src/main/java/io/zhile/research/intellij/ier/action/ResetAction.java @@ -1,29 +1,62 @@ package io.zhile.research.intellij.ier.action; import com.intellij.icons.AllIcons; +import com.intellij.notification.Notification; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DataKey; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.ToolWindow; +import com.intellij.openapi.wm.ToolWindowAnchor; +import com.intellij.openapi.wm.ToolWindowEP; import com.intellij.openapi.wm.ToolWindowManager; +import com.intellij.openapi.wm.ex.ToolWindowManagerEx; import io.zhile.research.intellij.ier.component.ResetTimer; +import io.zhile.research.intellij.ier.helper.Constants; +import io.zhile.research.intellij.ier.helper.ProjectHelper; +import io.zhile.research.intellij.ier.tw.MainToolWindowFactory; import io.zhile.research.intellij.ier.ui.dialog.MainDialog; import org.jetbrains.annotations.NotNull; public class ResetAction extends AnAction implements DumbAware { + private static final String ACTION_NAME = "Eval Reset"; + private static final DataKey NOTIFICATION_KEY = DataKey.create("Notification"); + public ResetAction() { - super("Eval Reset", "Reset my IDE eval information", AllIcons.General.Reset); + super(ACTION_NAME, "Reset my IDE eval information", AllIcons.General.Reset); new ResetTimer().start(this); } @Override - public void actionPerformed(@NotNull AnActionEvent anActionEvent) { - Project project = anActionEvent.getProject(); + public void actionPerformed(@NotNull AnActionEvent e) { + Project project = ProjectHelper.getProject(e); + + Notification notification = NOTIFICATION_KEY.getData(e.getDataContext()); + if (null != notification) { + notification.expire(); + } + if (project == null) { MainDialog mainDialog = new MainDialog(); mainDialog.show(); - } else { - ToolWindowManager.getInstance(project).getToolWindow("Eval Reset").show(null); + + return; } + + ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ACTION_NAME); + if (null == toolWindow) { + ToolWindowEP ep = new ToolWindowEP(); + ep.id = ACTION_NAME; + ep.anchor = ToolWindowAnchor.BOTTOM.toString(); + ep.icon = "AllIcons.General.Reset"; + ep.factoryClass = MainToolWindowFactory.class.getName(); + ep.setPluginDescriptor(Constants.PLUGIN_DESC); + ToolWindowManagerEx.getInstanceEx(project).initToolWindow(ep); + + toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ACTION_NAME); + } + + toolWindow.show(null); } } diff --git a/src/main/java/io/zhile/research/intellij/ier/action/RestartAction.java b/src/main/java/io/zhile/research/intellij/ier/action/RestartAction.java new file mode 100644 index 0000000..38aa413 --- /dev/null +++ b/src/main/java/io/zhile/research/intellij/ier/action/RestartAction.java @@ -0,0 +1,19 @@ +package io.zhile.research.intellij.ier.action; + +import com.intellij.icons.AllIcons; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.DumbAware; +import org.jetbrains.annotations.NotNull; + +public class RestartAction extends AnAction implements DumbAware { + public RestartAction() { + super("Restart IDE", "Restart my IDE", AllIcons.Actions.Restart); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + ApplicationManager.getApplication().restart(); + } +} diff --git a/src/main/java/io/zhile/research/intellij/ier/common/Resetter.java b/src/main/java/io/zhile/research/intellij/ier/common/Resetter.java index 6f5e9b1..544d563 100644 --- a/src/main/java/io/zhile/research/intellij/ier/common/Resetter.java +++ b/src/main/java/io/zhile/research/intellij/ier/common/Resetter.java @@ -29,6 +29,7 @@ public class Resetter { private static final String DEVICE_ID_KEY = DEFAULT_VENDOR + ".device_id"; private static final String IDE_EVAL_PREFIX = DEFAULT_VENDOR + "/" + Constants.IDE_NAME_LOWER + "/" + Constants.IDE_HASH; private static final String EVAL_KEY = "evlsprt"; + private static final String AUTO_RESET_KEY = Constants.PLUGIN_PREFS_PREFIX + ".auto_reset"; private static final PropertiesComponentImpl PROPS = (PropertiesComponentImpl) PropertiesComponent.getInstance(); @@ -151,6 +152,14 @@ public class Resetter { } } + public static boolean isAutoReset() { + return Prefs.getBoolean(AUTO_RESET_KEY, false); + } + + public static void setAutoReset(boolean isAutoReset) { + Prefs.putBoolean(AUTO_RESET_KEY, isAutoReset); + } + protected static File getSharedFile(String fileName) { String appData = System.getenv("APPDATA"); if (appData == null) { diff --git a/src/main/java/io/zhile/research/intellij/ier/component/ResetTimer.java b/src/main/java/io/zhile/research/intellij/ier/component/ResetTimer.java index b59f319..303defe 100644 --- a/src/main/java/io/zhile/research/intellij/ier/component/ResetTimer.java +++ b/src/main/java/io/zhile/research/intellij/ier/component/ResetTimer.java @@ -4,6 +4,10 @@ import com.intellij.ide.Prefs; import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import io.zhile.research.intellij.ier.action.RestartAction; +import io.zhile.research.intellij.ier.common.Resetter; import io.zhile.research.intellij.ier.helper.Constants; import io.zhile.research.intellij.ier.helper.DateTime; import io.zhile.research.intellij.ier.helper.NotificationHelper; @@ -13,7 +17,7 @@ import java.util.TimerTask; public class ResetTimer { private static final long RESET_PERIOD = 2160000000L; // 25 days - private static final String RESET_KEY = "Ide-Eval-Reset." + Constants.IDE_NAME_LOWER + "." + Constants.IDE_HASH; + private static final String RESET_KEY = Constants.PLUGIN_PREFS_PREFIX + "." + Constants.IDE_NAME_LOWER + "." + Constants.IDE_HASH; public static long getLastResetTime() { return Prefs.getLong(RESET_KEY, 0L); @@ -49,12 +53,34 @@ public class ResetTimer { @Override public void run() { - if (System.currentTimeMillis() - lastResetTime > RESET_PERIOD) { + do { + if (System.currentTimeMillis() - lastResetTime <= RESET_PERIOD) { + break; + } + + AnAction action = resetAction; String message = "It has been a long time since the last reset!\nWould you like to reset it again?"; + + if (Resetter.isAutoReset()) { + Resetter.reset(Resetter.getEvalRecords()); + ResetTimer.resetLastResetTime(); + + action = new RestartAction(); + message = "Automatic reset successfully!\nWould like to restart your IDE?"; + } + Notification notification = NotificationHelper.NOTIFICATION_GROUP.createNotification(Constants.PLUGIN_NAME, null, message, NotificationType.INFORMATION); - notification.addAction(resetAction); - notification.notify(null); - } + notification.addAction(action); + + Project[] projects = ProjectManager.getInstance().getOpenProjects(); + if (projects.length == 0) { + notification.notify(null); + } else { + for (Project project : projects) { + notification.notify(project); + } + } + } while (false); new Timer().schedule(new ResetTimerTask(lastResetTime, resetAction), 3600000); // 60 min } diff --git a/src/main/java/io/zhile/research/intellij/ier/helper/Constants.java b/src/main/java/io/zhile/research/intellij/ier/helper/Constants.java index a832228..8adec0b 100644 --- a/src/main/java/io/zhile/research/intellij/ier/helper/Constants.java +++ b/src/main/java/io/zhile/research/intellij/ier/helper/Constants.java @@ -1,5 +1,6 @@ package io.zhile.research.intellij.ier.helper; +import com.intellij.ide.plugins.IdeaPluginDescriptor; import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.plugins.cl.PluginClassLoader; import com.intellij.openapi.application.ApplicationNamesInfo; @@ -10,8 +11,10 @@ import com.intellij.openapi.util.io.FileUtil; public class Constants { public static final PluginClassLoader CLASS_LOADER = (PluginClassLoader) Constants.class.getClassLoader(); public static final PluginId PLUGIN_ID = CLASS_LOADER.getPluginId(); - public static final String PLUGIN_NAME = PluginManager.getPlugin(PLUGIN_ID).getName(); + public static final IdeaPluginDescriptor PLUGIN_DESC = PluginManager.getPlugin(PLUGIN_ID); + public static final String PLUGIN_NAME = PLUGIN_DESC.getName(); public static final String IDE_NAME = ApplicationNamesInfo.getInstance().getProductName(); public static final String IDE_NAME_LOWER = IDE_NAME.toLowerCase(); public static final String IDE_HASH = Integer.toHexString(FileUtil.pathHashCode(PathManager.getHomePath())); + public static final String PLUGIN_PREFS_PREFIX = "Ide-Eval-Reset"; } diff --git a/src/main/java/io/zhile/research/intellij/ier/helper/ProjectHelper.java b/src/main/java/io/zhile/research/intellij/ier/helper/ProjectHelper.java new file mode 100644 index 0000000..cccac7e --- /dev/null +++ b/src/main/java/io/zhile/research/intellij/ier/helper/ProjectHelper.java @@ -0,0 +1,29 @@ +package io.zhile.research.intellij.ier.helper; + +import com.intellij.ide.DataManager; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; + +public class ProjectHelper { + public static Project getCurrentProject() { + if (ProjectManager.getInstance().getOpenProjects().length == 0) { + return null; + } + + DataContext dataContext = DataManager.getInstance().getDataContextFromFocus().getResultSync(); + return CommonDataKeys.PROJECT.getData(dataContext); + } + + public static Project getProject(AnActionEvent e) { + Project project = e.getProject(); + + if (project == null) { + return getCurrentProject(); + } + + return project; + } +} diff --git a/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.form b/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.form index adf08c4..40d07db 100644 --- a/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.form +++ b/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.form @@ -31,10 +31,16 @@ - + - + + + + + + + @@ -74,7 +80,7 @@ - + diff --git a/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.java b/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.java index 3445e54..eb74757 100644 --- a/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.java +++ b/src/main/java/io/zhile/research/intellij/ier/ui/form/MainForm.java @@ -19,9 +19,10 @@ public class MainForm { private JButton btnReset; private JList lstMain; private JLabel lblLastResetTime; - private JButton btnReloadList; + private JButton btnReload; private JLabel lblFound; private JLabel lblLastResetTimeLabel; + private JCheckBox chkResetAuto; private final DialogWrapper dialogWrapper; private final DefaultListModel listModel = new DefaultListModel<>(); @@ -33,14 +34,23 @@ public class MainForm { public JPanel getContent() { boldFont(lblFound); boldFont(lblLastResetTimeLabel); + reloadLastResetTime(); - lblLastResetTime.setText(ResetTimer.getLastResetTimeStr()); - lstMain.setModel(listModel); - - reloadRecordItems(); - btnReloadList.addActionListener(new AbstractAction() { + chkResetAuto.setSelected(Resetter.isAutoReset()); + chkResetAuto.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { + Resetter.setAutoReset(chkResetAuto.isSelected()); + } + }); + + lstMain.setModel(listModel); + reloadRecordItems(); + + btnReload.addActionListener(new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + reloadLastResetTime(); reloadRecordItems(); } }); @@ -61,6 +71,10 @@ public class MainForm { return rootPanel; } + private void reloadLastResetTime() { + lblLastResetTime.setText(ResetTimer.getLastResetTimeStr()); + } + private void reloadRecordItems() { listModel.clear(); diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 74e55a9..0a7bfd3 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -10,11 +10,6 @@ com.intellij.modules.ultimate - - - -