📄 jremconfigdoc.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.server.gui.model;import java.util.Collection;import java.util.Hashtable;import java.util.Map;import java.util.Vector;import fildiv.jremcntl.common.core.Command;import fildiv.jremcntl.common.core.Config;import fildiv.jremcntl.common.core.ConfigIntegrityInfo;import fildiv.jremcntl.common.core.Context;import fildiv.jremcntl.common.core.DefaultConfigVisitor;import fildiv.jremcntl.common.core.Extension;import fildiv.jremcntl.common.core.JRemRuntimeException;import fildiv.jremcntl.common.core.SupportExtension;import fildiv.jremcntl.common.util.JRemUtils;import fildiv.jremcntl.server.core.AppExtension;import fildiv.jremcntl.server.core.JRemEnv;import fildiv.jremcntl.server.core.JRemExtension;import fildiv.jremcntl.server.core.JRemProperties;import fildiv.jremcntl.server.core.JRemPropertyManager;import fildiv.jremcntl.server.gui.core.AbstractDocument;import fildiv.jremcntl.server.gui.core.AddRemoveOpSupport;import fildiv.jremcntl.server.gui.core.DocumentEvent;import fildiv.jremcntl.server.gui.core.DocumentListener;import fildiv.jremcntl.server.gui.core.DocumentModifiedEvent;import fildiv.jremcntl.server.gui.core.IDGen;public class JRemConfigDoc extends AbstractDocument implements Config, JRemPropertyManager { public static final int MODIFIED_CONTENT_TYPE_ADD_CONTEXT = 1; public static final int MODIFIED_CONTENT_TYPE_REM_CONTEXT = 2; public static final int MODIFIED_CONTENT_TYPE_MOD_CONTEXT = 3; private JRemEnv env; private String name = "unamed"; private String baseDir = ""; private String onConnectExePath = ""; private String onDisconnectExePath = ""; private Vector contexts; private DocumentListener listener; private JRemExtensionDoc extDoc = null; private IDGen idGen; private DefaultConfigVisitor reindexVisitor; protected JRemConfigDoc() { env = JRemEnv.getInstance(); contexts = new Vector(); listener = new DocumentListener() { public void actionPerformed(DocumentEvent event) { JRemConfigDoc.this.actionPerformed(event); } }; idGen = new IDGen(); reindexVisitor = new DefaultConfigVisitor() { protected void onContext(Context context) { JRemContextDoc ctx = (JRemContextDoc) context; ctx.id = idGen.getNextID(); } protected void onCommand(Command command) { JRemCommandDoc cmd = (JRemCommandDoc) command; cmd.id = idGen.getNextID(); } }; // Default extension assignDefaultExtension(); } private void assignDefaultExtension() { AppExtension appExt = env.getAppExtensions().getDefault(); if (appExt == null) throw new JRemRuntimeException( "Default extension doesn't not exist."); JRemExtension ext = env.getExtensionLoader().getExtension( appExt.getClassName()); JRemExtensionDoc extDoc = new JRemExtensionDoc(this, ext); setExtension(extDoc); } public JRemConfigDoc(String name, String baseDir) { this(name, baseDir, true); setModified(false); } public JRemConfigDoc(String name, String baseDir, boolean setModified) { this(); this.name = name; this.baseDir = baseDir; setModified(setModified); } public Context addContext(Context context) { JRemContextDoc contextDoc = (JRemContextDoc) context; contexts.insertElementAt(context, 0); setupContext(contextDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newAddObjSupport(contextDoc); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_CONTEXT, opSupport); return contextDoc; } private void setupContext(JRemContextDoc context) { context.setConfig(this); context.addActionListener(listener); } public Context appendContext(Context context) { if (context.getConfig() != this) throw new IllegalArgumentException(); JRemContextDoc contextDoc = (JRemContextDoc) context; contexts.addElement(context); setupContext(contextDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newAppendObjSupport(contextDoc); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_CONTEXT, opSupport); return contextDoc; } public Context insertContext(Context newContext, Context context, boolean insertAfter) { if (context == null) return addContext(newContext); JRemContextDoc newContextDoc = (JRemContextDoc) newContext; JRemContextDoc contextDoc = (JRemContextDoc) context; int index = -1; if (contextDoc == null) index = 0; else index = getIndex(contextDoc); if (index < 0) return null; if (insertAfter) index++; contexts.insertElementAt(newContextDoc, index); setupContext(newContextDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newInsertObjSupport(newContextDoc, contextDoc, insertAfter); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_CONTEXT, opSupport); return newContext; } public Context removeContext(Context context) { JRemContextDoc contextSource = (JRemContextDoc) context; JRemContextDoc contextToRemote = null; int index; for (index = 0; index < contexts.size(); ++index) { contextToRemote = (JRemContextDoc) contexts.get(index); if (contextToRemote == contextSource) break; } if (index >= contexts.size()) return null; contextToRemote.removeActionListener(listener); contexts.remove(index); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newRemoveObjSupport(contextToRemote); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_REM_CONTEXT, opSupport); return contextToRemote; } public Command findCommand(int cmdID) { for (int index = 0; index < contexts.size(); ++index) { Context ctx = (Context) contexts.elementAt(index); Command cmd = ctx.findCommand(cmdID); if (cmd != null) return cmd; } return null; } public String getName() { return name; } public void setName(String name) { if (checkModified(this.name, name)) { this.name = name; setModified(true); fireGenericModifyEvent(); } } public String getBaseDir() { return baseDir; } public void setBaseDir(String baseDir) { if (checkModified(this.baseDir, baseDir)) { this.baseDir = baseDir; setModified(true); fireGenericModifyEvent(); } } public String getOnConnectExePath() { return onConnectExePath; } public void setOnConnectExePath(String onConnectExePath) { if (checkModified(this.onConnectExePath, onConnectExePath)) { this.onConnectExePath = onConnectExePath; setModified(true); fireGenericModifyEvent(); } } public String getOnDisconnectExePath() { return onDisconnectExePath; } public void setOnDisconnectExePath(String onDisconnectExePath) { if (checkModified(this.onDisconnectExePath, onDisconnectExePath)) { this.onDisconnectExePath = onDisconnectExePath; setModified(true); fireGenericModifyEvent(); } } public int getCmdsCount() { Vector contexts = getContexts(); int cmdsCount = 0; for (int index = 0; index < contexts.size(); ++index ) { Context ctx = (Context) contexts.elementAt(index); Vector commands = ctx.getCommands(); cmdsCount+= commands.size(); } return cmdsCount; } public Vector getContexts() { return contexts; } public String toString() { return name; } protected void actionPerformed(DocumentEvent event) { reindexAll(); if (!(event instanceof DocumentModifiedEvent)) return; fireModifyEvent(event, MODIFIED_CONTENT_TYPE_MOD_CONTEXT); } private void reindexAll() { idGen.reset(); reindexVisitor.visit(this); } private int getIndex(JRemContextDoc Context) { for (int index = 0; index < contexts.size(); ++index) { JRemContextDoc currContext = (JRemContextDoc) contexts.get(index); if (currContext == Context) return index; } return -1; } public void moveContextAtTop(JRemContextDoc context) { try { setSilentMode(true); if (getIndex(context) == 0) return; JRemConfigDoc confDoc = (JRemConfigDoc) context.getConfig(); confDoc.removeContext(context); addContext(context); } finally { setSilentMode(false); } } public void moveContextAfter(JRemContextDoc ctxSource, JRemContextDoc ctxDest) { try { setSilentMode(true); if (ctxSource == ctxDest) return; JRemConfigDoc confDoc = (JRemConfigDoc) ctxSource.getConfig(); confDoc.removeContext(ctxSource); insertContext(ctxSource, ctxDest, true); } finally { setSilentMode(false); } } protected void setModified(boolean modified) { super.setModified(modified); } public boolean isModified() { if (super.isModified()) return true; if (extDoc != null && extDoc.isModified()) return true; for (int index = 0; index < contexts.size(); ++index) { JRemContextDoc context = (JRemContextDoc) contexts.get(index); if (context.isModified()) return true; } return false; } public void resetState() { super.resetState(); JRemExtensionDoc extDoc = (JRemExtensionDoc) getExtension(false); if (extDoc != null) extDoc.resetState(); for (int i = 0; i < contexts.size(); ++i) { JRemContextDoc context = (JRemContextDoc) contexts.get(i); context.resetState(); } fireGenericModifyEvent(); } public boolean isPropertyEditable(String name) { return isManageProperty(name); } public boolean isPropertyEnabled(String name) { return isManageProperty(name); } public Object getPropertyValue(String name) { if (JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name)) return baseDir; if (JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name)) return this.name; if (JRemProperties.PROP_ON_CONNECT.equalsIgnoreCase(name)) return this.onConnectExePath; if (JRemProperties.PROP_ON_DISCONNECT.equalsIgnoreCase(name)) return this.onDisconnectExePath; return null; } public void setPropertyValue(String name, Object value) { if (JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name)) setBaseDir( (String) value ); else if (JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name)) setName( (String) value); else if (JRemProperties.PROP_ON_CONNECT.equalsIgnoreCase(name)) { setOnConnectExePath( (String) value); } else if (JRemProperties.PROP_ON_DISCONNECT.equalsIgnoreCase(name)) { setOnDisconnectExePath( (String) value); } } public boolean isManageProperty(String name) { return JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name) || JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name) || JRemProperties.PROP_ON_CONNECT.equalsIgnoreCase(name) || JRemProperties.PROP_ON_DISCONNECT.equalsIgnoreCase(name); } public Collection getPropertyDomainValues(String name) { return null; } public ConfigIntegrityInfo checkValid() { if (JRemUtils.isEmptyString(name)) { return ConfigIntegrityInfo.newInvalidIntegrity( this, "\"Description\" cannot be empty"); } Map sameDescMap = new Hashtable(contexts.size()); for (int index = 0; index < contexts.size(); ++index) { JRemContextDoc ctx = (JRemContextDoc) contexts.elementAt(index); ConfigIntegrityInfo cii = ctx.checkValid(); if (!cii.isValid()) return cii; Context c = (Context) sameDescMap.get(ctx.getDesc()); if (c != null) return ConfigIntegrityInfo.newInvalidIntegrity( ctx, "The contexts : \n\n" + c.toString() + ", id = " + c.getID() + "\n" + ctx.toString() + ", id = " + ctx.getID() + "\n\n" + "Have the the description."); sameDescMap.put(ctx.getDesc(), ctx); } if (extDoc != null) { ConfigIntegrityInfo cii = extDoc.checkValid(); if (cii != ConfigIntegrityInfo.CONFIG_INTEGRIY_VALID) return ConfigIntegrityInfo.newInvalidIntegrity(this, cii.getReason()); } return ConfigIntegrityInfo.CONFIG_INTEGRIY_VALID; } protected void attachExtensionDoc(JRemExtensionDoc extDoc) { if (this.extDoc != null) this.extDoc.removeActionListener(listener); this.extDoc = extDoc; if (extDoc != null) extDoc.addActionListener(listener); } public void setExtension(Extension extDoc) { attachExtensionDoc( (JRemExtensionDoc) extDoc); setModified(true); fireGenericModifyEvent(); } public Extension getExtension(boolean parentRecursion) { assert extDoc != null; return extDoc; } public boolean isInHerited() { return false; } public SupportExtension getParent() { return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -