Skip to content

Commit

Permalink
1.继续优化多Project的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
KADO authored and KADO committed Jan 21, 2017
1 parent dd1b9f0 commit 79a9698
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
9 changes: 8 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>com.jiyuanime.ActivatePowerModeApplicationPlugin</id>
<name>activate-power-mode</name>
<version>0.1.2</version>
<version>0.1.3</version>
<vendor email="" url="https://github.com/ViceFantasyPlace/activate-power-mode">ViceFantasyPlace</vendor>

<description>
Expand All @@ -15,6 +15,13 @@

<change-notes>
<![CDATA[
<h3>v0.1.3</h3>
<ul>
<li>修复在点击enable的时候抛出 java.util.ConcurrentModificationException</li>
<li>修复在ActivatePowerModeManage destroy 的时候 抛出 java.lang.IllegalStateException</li>
<li>继续优化多Project的情况</li>
</ul>
<h3>v0.1.2</h3>
<ul>
<li>增加一个Combo值,达到一定Combo值自动开启效果</li>
Expand Down
4 changes: 2 additions & 2 deletions src/com/jiyuanime/ActivatePowerModeApplicationPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void projectOpened(Project project) {

@Override
public void projectClosed(Project project) {
ActivatePowerModeManage.getInstance().destroy();
ActivatePowerModeManage.getInstance().destroy(project, true);
super.projectClosed(project);
}

Expand All @@ -48,7 +48,7 @@ public boolean canCloseProject(Project project) {

@Override
public void disposeComponent() {
ActivatePowerModeManage.getInstance().destroy();
ActivatePowerModeManage.getInstance().destroyAll();
}

@Override
Expand Down
79 changes: 51 additions & 28 deletions src/com/jiyuanime/ActivatePowerModeManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
import com.jiyuanime.listener.ActivatePowerDocumentListener;
import com.jiyuanime.particle.ParticlePanel;
import com.jiyuanime.shake.ShakeManager;
import org.apache.commons.logging.Log;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
* 效果管理器
* Created by KADO on 2017/1/11.
*/
public class ActivatePowerModeManage {

public static ActivatePowerModeManage sActivatePowerModeManage;
private static ActivatePowerModeManage sActivatePowerModeManage;

public static ActivatePowerModeManage getInstance() {
if (sActivatePowerModeManage == null) {
Expand All @@ -31,34 +34,36 @@ public static ActivatePowerModeManage getInstance() {

private Config.State state = Config.getInstance().state;

private ActivatePowerDocumentListener mActivatePowerDocumentListener;

private Project mProject;
private HashMap<Project, ActivatePowerDocumentListener> mDocListenerMap = new HashMap<>();
private Editor mCurrentEditor;

private long mClickTimeStamp;
private int mClickCombo;

public void init(Project project) {
mProject = project;

if (mProject != null) {
if (project != null) {
// 监听FileEditor的状态
MessageBusConnection connection = mProject.getMessageBus().connect();
MessageBusConnection connection = project.getMessageBus().connect();
connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
super.fileOpened(source, file);

destroyShake();
destroyParticle();
mCurrentEditor = null;

initDocument(source.getProject(), FileDocumentManager.getInstance().getDocument(file));
}

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
super.fileClosed(source, file);

if (mActivatePowerDocumentListener != null)
mActivatePowerDocumentListener.clean(FileDocumentManager.getInstance().getDocument(file), true);
ActivatePowerDocumentListener activatePowerDocumentListener = mDocListenerMap.get(source.getProject());
if (activatePowerDocumentListener != null)
activatePowerDocumentListener.clean(FileDocumentManager.getInstance().getDocument(file), true);
}

@Override
Expand All @@ -69,11 +74,16 @@ public void selectionChanged(@NotNull FileEditorManagerEvent event) {
destroyShake();
destroyParticle();
mCurrentEditor = null;

FileEditorManager fileEditorManager = event.getManager();
VirtualFile virtualFile = event.getNewFile();
if (virtualFile != null)
initDocument(fileEditorManager.getProject(), FileDocumentManager.getInstance().getDocument(virtualFile));
}
}
});

FileEditorManager fileEditorManager = FileEditorManager.getInstance(mProject);
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);

if (fileEditorManager != null) {
Editor editor = fileEditorManager.getSelectedTextEditor();
Expand All @@ -82,12 +92,13 @@ public void selectionChanged(@NotNull FileEditorManagerEvent event) {
destroyParticle();
mCurrentEditor = null;

initDocument(mProject, editor.getDocument());
initDocument(project, editor.getDocument());
}
}

} else
System.out.println("ActivatePowerEnableAction " + "初始化数据失败");

}

private void initEditor(Editor editor) {
Expand All @@ -98,21 +109,30 @@ private void initEditor(Editor editor) {
}

private void initDocument(Project project, Document document) {
if (document != null && project != null) {
if (mActivatePowerDocumentListener == null || mActivatePowerDocumentListener.getProject() != project)
mActivatePowerDocumentListener = new ActivatePowerDocumentListener(project);

if (mActivatePowerDocumentListener.addDocument(document))
document.addDocumentListener(mActivatePowerDocumentListener);
if (project != null && document != null) {
ActivatePowerDocumentListener activatePowerDocumentListener = mDocListenerMap.get(project);
if (activatePowerDocumentListener == null) {
activatePowerDocumentListener = new ActivatePowerDocumentListener(project);
mDocListenerMap.put(project, activatePowerDocumentListener);
}
if (activatePowerDocumentListener.addDocument(document))
document.addDocumentListener(activatePowerDocumentListener);
}
}

public void destroy() {
public void destroy(Project project, boolean isRemoveProject) {
destroyShake();
destroyParticle();
destroyDocumentListener();
destroyDocumentListener(project, isRemoveProject);
mCurrentEditor = null;
destroyProjectMessageBus();
destroyProjectMessageBus(project, isRemoveProject);
}

public void destroyAll() {
for (Project project : mDocListenerMap.keySet()) {
destroy(project, false);
}
mDocListenerMap.clear();
}

private void initShake(JComponent jComponent) {
Expand Down Expand Up @@ -141,10 +161,12 @@ public void resetEditor(Editor editor) {
}
}

private void destroyDocumentListener() {
if (mActivatePowerDocumentListener != null) {
mActivatePowerDocumentListener.destroy();
mActivatePowerDocumentListener = null;
private void destroyDocumentListener(Project project, boolean isRemoveProject) {
ActivatePowerDocumentListener activatePowerDocumentListener = mDocListenerMap.get(project);
if (activatePowerDocumentListener != null) {
activatePowerDocumentListener.destroy();
if (isRemoveProject)
mDocListenerMap.remove(project);
}
}

Expand All @@ -156,9 +178,9 @@ private void destroyParticle() {
ParticlePanel.getInstance().destroy();
}

private void destroyProjectMessageBus() {
if (mProject != null) {
MessageBusConnection connection = mProject.getMessageBus().connect();
private void destroyProjectMessageBus(Project project, boolean isRemoveProject) {
if (project != null) {
MessageBusConnection connection = project.getMessageBus().connect();
connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
Expand All @@ -176,7 +198,8 @@ public void selectionChanged(@NotNull FileEditorManagerEvent event) {
}
});
}
mProject = null;
if (isRemoveProject)
mDocListenerMap.remove(project);
}

public long getClickTimeStamp() {
Expand Down
2 changes: 1 addition & 1 deletion src/com/jiyuanime/action/ActivatePowerEnableAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ActivatePowerEnableAction() {
public void actionPerformed(AnActionEvent e) {
if (state.IS_ENABLE) {
disable(e.getPresentation());
ActivatePowerModeManage.getInstance().destroy();
ActivatePowerModeManage.getInstance().destroyAll();
} else {
enable(e.getPresentation());
ActivatePowerModeManage.getInstance().init(e.getProject());
Expand Down
35 changes: 14 additions & 21 deletions src/com/jiyuanime/listener/ActivatePowerDocumentListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,23 @@ public void beforeDocumentChange(DocumentEvent documentEvent) {

if (manage.getClickCombo() > state.OPEN_FUNCTION_BORDER && mProject != null) {

if (state.IS_SHAKE) {
if (ShakeManager.getInstance().isEnable() && !ShakeManager.getInstance().isShaking())
ShakeManager.getInstance().shake();
}

if (state.IS_SPARK) {

Editor selectedTextEditor = FileEditorManager.getInstance(mProject).getSelectedTextEditor();
if (mEditor == null || mEditor != selectedTextEditor)
mEditor = selectedTextEditor;
if (mEditor != null) {

manage.resetEditor(mEditor);
Editor selectedTextEditor = FileEditorManager.getInstance(mProject).getSelectedTextEditor();
if (mEditor == null || mEditor != selectedTextEditor)
mEditor = selectedTextEditor;
if (mEditor != null) {
manage.resetEditor(mEditor);

if (state.IS_SHAKE) {
if (ShakeManager.getInstance().isEnable() && !ShakeManager.getInstance().isShaking())
ShakeManager.getInstance().shake();
}

if (state.IS_SPARK) {
CaretModel currentCaretModel = mEditor.getCaretModel();
if (mCaretModel == null || mCaretModel != currentCaretModel)
if (mCaretModel == null || mCaretModel != currentCaretModel) {
mCaretModel = currentCaretModel;

mCaretModel.addCaretListener(mActivatePowerCaretListener);
mCaretModel.addCaretListener(mActivatePowerCaretListener);
}

Color color;
if (state.IS_COLORFUL) {
Expand All @@ -86,7 +84,6 @@ public void beforeDocumentChange(DocumentEvent documentEvent) {
if (particlePanel.isEnable()) {
particlePanel.sparkAtPositionAction(color, fontSize);
}

}
}
}
Expand Down Expand Up @@ -135,8 +132,4 @@ public void destroy() {
mActivatePowerCaretListener = null;
mProject = null;
}

public Project getProject() {
return mProject;
}
}

0 comments on commit 79a9698

Please sign in to comment.