add option: Reset Automatically

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile
2020-11-03 16:45:43 +08:00
parent 7932de383a
commit 26b65b36e5
10 changed files with 175 additions and 38 deletions
@@ -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> 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);
}
}
@@ -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();
}
}
@@ -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) {
@@ -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
}
@@ -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";
}
@@ -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;
}
}
@@ -31,10 +31,16 @@
<properties/>
<border type="none"/>
<children>
<component id="382d3" class="javax.swing.JButton" binding="btnReloadList">
<component id="3e8db" class="javax.swing.JCheckBox" binding="chkResetAuto">
<constraints/>
<properties>
<text value="⟳ Reload List"/>
<text value="Reset Automatically (per 25days)"/>
</properties>
</component>
<component id="382d3" class="javax.swing.JButton" binding="btnReload">
<constraints/>
<properties>
<text value="⟳ Reload"/>
</properties>
</component>
<component id="7f8d9" class="javax.swing.JButton" binding="btnReset">
@@ -74,7 +80,7 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="empty"/>
<border type="none"/>
<children>
<component id="5b0c2" class="javax.swing.JList" binding="lstMain">
<constraints/>
@@ -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<String> 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();
-5
View File
@@ -10,11 +10,6 @@
<depends>com.intellij.modules.ultimate</depends>
<extensions defaultExtensionNs="com.intellij">
<toolWindow id="Eval Reset" anchor="bottom" icon="AllIcons.General.Reset"
factoryClass="io.zhile.research.intellij.ier.tw.MainToolWindowFactory"/>
</extensions>
<actions>
<action class="io.zhile.research.intellij.ier.action.ResetAction"
id="io.zhile.research.intellij.ier.action.ResetAction">