Browse Source

插件改变时刷新

master
juhaoyu 8 years ago
parent
commit
387dbb5234
  1. 108
      designer_base/src/com/fr/design/fun/impl/GlobalListenerProviderManager.java
  2. 7
      designer_base/src/com/fr/start/BaseDesigner.java

108
designer_base/src/com/fr/design/fun/impl/GlobalListenerProviderManager.java

@ -0,0 +1,108 @@
package com.fr.design.fun.impl;
import com.fr.design.ExtraDesignClassManager;
import com.fr.design.fun.GlobalListenerProvider;
import com.fr.general.GeneralContext;
import com.fr.plugin.context.PluginContext;
import com.fr.plugin.context.PluginRuntime;
import com.fr.plugin.injectable.PluginModule;
import com.fr.plugin.manage.PluginFilter;
import com.fr.plugin.observer.PluginEvent;
import com.fr.plugin.observer.PluginEventListener;
import com.fr.plugin.observer.PluginEventType;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by juhaoyu on 2017/6/15.
* 管理GlobalListenerProvider接口
*/
public class GlobalListenerProviderManager {
private static final GlobalListenerProviderManager INSTANCE = new GlobalListenerProviderManager();
private Map<GlobalListenerProvider, AWTEventListener> map = new HashMap<>();
private GlobalListenerProviderManager() {}
public static GlobalListenerProviderManager getInstance() {
return INSTANCE;
}
public void init() {
Set<GlobalListenerProvider> providers = ExtraDesignClassManager.getInstance().getArray(GlobalListenerProvider.XML_TAG);
addAWTEventListeners(providers);
listenPlugin();
}
private void listenPlugin() {
PluginFilter filter = new PluginFilter() {
@Override
public boolean accept(PluginContext context) {
return context.contain(PluginModule.ExtraDesign, GlobalListenerProvider.XML_TAG);
}
};
PluginEventListener onRun = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
PluginRuntime runtime = event.getContext().getRuntime();
Set<GlobalListenerProvider> providers = runtime.get(PluginModule.ExtraDesign, GlobalListenerProvider.XML_TAG);
addAWTEventListeners(providers);
}
};
PluginEventListener onStop = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
PluginRuntime runtime = event.getContext().getRuntime();
Set<GlobalListenerProvider> providers = runtime.get(PluginModule.ExtraDesign, GlobalListenerProvider.XML_TAG);
removeAWTEventListeners(providers);
}
};
GeneralContext.listenPlugin(PluginEventType.AfterRun, onRun, filter);
GeneralContext.listenPlugin(PluginEventType.BeforeStop, onStop, filter);
}
private void addAWTEventListeners(Set<GlobalListenerProvider> providers) {
AWTEventListener listener;
for (GlobalListenerProvider provider : providers) {
listener = provider.listener();
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
add(provider, listener);
}
}
private void removeAWTEventListeners(Set<GlobalListenerProvider> providers) {
AWTEventListener listener;
for (GlobalListenerProvider provider : providers) {
listener = get(provider);
Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
}
}
private synchronized void add(GlobalListenerProvider provider, AWTEventListener listener) {
map.put(provider, listener);
}
private synchronized AWTEventListener get(GlobalListenerProvider provider) {
return map.get(provider);
}
}

7
designer_base/src/com/fr/start/BaseDesigner.java

@ -13,6 +13,7 @@ import com.fr.design.file.MutilTempalteTabPane;
import com.fr.design.file.TemplateTreePane;
import com.fr.design.fun.DesignerStartOpenFileProcessor;
import com.fr.design.fun.GlobalListenerProvider;
import com.fr.design.fun.impl.GlobalListenerProviderManager;
import com.fr.design.mainframe.DesignerFrame;
import com.fr.design.mainframe.TemplatePane;
import com.fr.design.mainframe.toolbar.ToolBarMenuDock;
@ -114,10 +115,8 @@ public abstract class BaseDesigner extends ToolBarMenuDock {
}
private void bindGlobalListener() {
Set<GlobalListenerProvider> providers = ExtraDesignClassManager.getInstance().getArray(GlobalListenerProvider.XML_TAG);
for (GlobalListenerProvider provider : providers) {
Toolkit.getDefaultToolkit().addAWTEventListener(provider.listener(), AWTEvent.KEY_EVENT_MASK);
}
GlobalListenerProviderManager.getInstance().init();
}
private void showErrorPluginsMessage() {

Loading…
Cancel
Save