📄 jremcontextdoc.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.server.gui.model;import java.util.ArrayList;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.Extension;import fildiv.jremcntl.common.core.SupportExtension;import fildiv.jremcntl.common.util.JRemUtils;import fildiv.jremcntl.server.core.JRemProperties;import fildiv.jremcntl.server.core.JRemPropertyDomainValue;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;public class JRemContextDoc extends AbstractDocument implements Context, JRemPropertyManager { public static final int MODIFIED_CONTENT_TYPE_ADD_COMMAND = 1; public static final int MODIFIED_CONTENT_TYPE_REM_COMMAND = 2; public static final int MODIFIED_CONTENT_TYPE_CHILDREN_MOD = 3; private Config config; protected int id; private String desc = ""; private String baseDir = ""; private short defView; private Vector commands; private JRemExtensionDoc extDoc = null; private DocumentListener listener; protected JRemContextDoc(Config config) { if (config == null) throw new IllegalArgumentException(); this.config = config; commands = new Vector(); listener = new DocumentListener() { public void actionPerformed(DocumentEvent event) { JRemContextDoc.this.actionPerformed(event); } }; } public JRemContextDoc(Config conf, String desc, short defView, String baseDir) { this(conf, 0, desc, defView, baseDir, true); } protected JRemContextDoc(Config config, int id, String desc, short defView, String baseDir) { this(config, id, desc, defView, baseDir, true); } protected JRemContextDoc(Config config, int id, String desc, short defView, String baseDir, boolean setModified) { this(config); this.id = id; this.desc = desc; this.defView = defView; this.baseDir = baseDir; setModified(setModified); } public Command addCommand(Command command) { JRemCommandDoc commandDoc = (JRemCommandDoc) command; commands.insertElementAt(commandDoc, 0); setupCommand(commandDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newAddObjSupport(commandDoc); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_COMMAND, opSupport); return commandDoc; } public Command insertCommand(Command newCommand, Command command, boolean insertAfter) { if (command == null) return addCommand(newCommand); JRemCommandDoc newCommandDoc = (JRemCommandDoc) newCommand; JRemCommandDoc commandDoc = (JRemCommandDoc) command; JRemContextDoc context = (JRemContextDoc) commandDoc.getContext(); int index = context.getIndex(commandDoc); if (index < 0) return null; if (insertAfter) index++; commands.insertElementAt(newCommandDoc, index); setupCommand(newCommandDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newInsertObjSupport( newCommandDoc, commandDoc, insertAfter); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_COMMAND, opSupport); return newCommandDoc; } public Command appendCommand(Command command) { JRemCommandDoc commandDoc = (JRemCommandDoc) command; commands.addElement(commandDoc); setupCommand(commandDoc); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newAppendObjSupport(commandDoc); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_ADD_COMMAND, opSupport); return commandDoc; } public Command removeCommand(Command command) { JRemCommandDoc commandDoc = (JRemCommandDoc) command; JRemCommandDoc commandToRemove = null; int index; for (index = 0; index < commands.size(); ++index) { commandToRemove = (JRemCommandDoc) commands.get(index); if (commandToRemove == commandDoc) break; } if (index >= commands.size()) return null; commandToRemove.removeActionListener(listener); commands.remove(index); AddRemoveOpSupport opSupport = AddRemoveOpSupport.newRemoveObjSupport(commandToRemove); setModified(true); fireModifyEvent(MODIFIED_CONTENT_TYPE_REM_COMMAND, opSupport); return commandToRemove; } public Command findCommand(int cmdID) { Vector commands = getCommands(); for (int index = 0; index < commands.size(); ++index) { Command cmd = (Command) commands.elementAt(index); if (cmd.getID() == cmdID) return cmd; } return null; } public String getBaseDir() { return baseDir; } public void setBaseDir(String baseDir) { if (checkModified(this.baseDir, baseDir)) { this.baseDir = baseDir; setModified(true); fireGenericModifyEvent(); } } public Vector getCommands() { return commands; } public Config getConfig() { return config; } public short getDefView() { return defView; } public void setDefView(short defView) { if (checkModified(this.defView, defView)) { this.defView = defView; setModified(true); fireGenericModifyEvent(); } } public String getDesc() { return desc; } public void setDesc(String desc) { if (checkModified(this.desc, desc)) { this.desc = desc; setModified(true); fireGenericModifyEvent(); } } public int getID() { return id; } public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof JRemContextDoc)) return false; JRemContextDoc ctx = (JRemContextDoc) obj; return id == ctx.getID(); } public void moveCommandAfter(JRemCommandDoc cmdSource, JRemCommandDoc cmdDest) { try { setSilentMode(true); if (cmdSource == cmdDest) return; JRemContextDoc context = (JRemContextDoc) cmdSource.getContext(); context.removeCommand(cmdSource); insertCommand(cmdSource, cmdDest, true); } finally { setSilentMode(false); } } public void moveCommandAtTop(JRemCommandDoc cmdSource) { try { setSilentMode(true); JRemContextDoc context = (JRemContextDoc) cmdSource.getContext(); context.removeCommand(cmdSource); addCommand(cmdSource); } finally { setSilentMode(false); } } public int hashCode() { return id; } public String toString() { return desc; } protected void actionPerformed(DocumentEvent event) { if (!(event instanceof DocumentModifiedEvent)) return; fireModifyEvent(event, MODIFIED_CONTENT_TYPE_CHILDREN_MOD); } private int getIndex(Command command) { for (int index = 0; index < commands.size(); ++index) { Command currCommand = (Command) commands.get(index); if (currCommand == command) return index; } return -1; } private void setupCommand(JRemCommandDoc command) { command.setContext(this); command.addActionListener(listener); } protected void setConfig(JRemConfigDoc config) { this.config = config; } 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 < commands.size(); ++index) { JRemCommandDoc command = (JRemCommandDoc) commands.get(index); if (command.isModified()) return true; } return false; } public String getName() { return id + " " + desc; } public Object[] getUsedKeys() { Map keys = new Hashtable(10); for (int index = 0; index < commands.size(); ++index) { JRemCommandDoc command = (JRemCommandDoc) commands.get(index); String key = command.getKey(); if (keys.get(key) == null && !JRemUtils.isEmptyString(key)) keys.put(key, key); } return (Object[]) keys.values().toArray(); } public boolean isPropertyEditable(String name) { return isManageProperty(name); } public boolean isPropertyEnabled(String name) { return isManageProperty(name); } public Object getPropertyValue(String name) { if (JRemProperties.PROP_ID.equalsIgnoreCase(name)) return String.valueOf(id); if (JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name)) return desc; if (JRemProperties.PROP_DEF_VIEW.equalsIgnoreCase(name)) return new Short(defView); if (JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name)) return baseDir; return null; } public void setPropertyValue(String name, Object value) { if (JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name)) setDesc( (String) value); else if (JRemProperties.PROP_DEF_VIEW.equalsIgnoreCase(name)) setDefView( ((Short) value).shortValue()); else if (JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name)) setBaseDir( (String) value); } public boolean isManageProperty(String name) { return JRemProperties.PROP_ID.equalsIgnoreCase(name) || JRemProperties.PROP_DESCRIPTION.equalsIgnoreCase(name) || JRemProperties.PROP_DEF_VIEW.equalsIgnoreCase(name) || JRemProperties.PROP_BASE_DIR.equalsIgnoreCase(name); } public Collection getPropertyDomainValues(String name) { Collection c = new ArrayList(); if (name.equals(JRemProperties.PROP_DEF_VIEW)) { c.add(new JRemPropertyDomainValue(new Short(Config.TYPE_VIEW_LISTVIEW), JRemUtils.decodeViewType(Config.TYPE_VIEW_LISTVIEW))); c.add(new JRemPropertyDomainValue(new Short(Config.TYPE_VIEW_FASTVIEW), JRemUtils.decodeViewType(Config.TYPE_VIEW_FASTVIEW))); } return c; } public JRemCommandDoc findCommandByKey(String key) { for (int index = 0; index < commands.size(); ++ index) { JRemCommandDoc cmd = (JRemCommandDoc) commands.get(index); if (key.equals(cmd.getKey())) return cmd; } return null; } public ConfigIntegrityInfo checkValid() { if (JRemUtils.isEmptyString(desc)) return ConfigIntegrityInfo.newInvalidIntegrity( this, "\"Description\" cannot be empty."); Map sameDescMap = new Hashtable(commands.size()); for (int index = 0; index < commands.size(); ++index) { JRemCommandDoc cmd = (JRemCommandDoc) commands.get(index); ConfigIntegrityInfo cii = cmd.checkValid(); if (!cii.isValid()) return cii; Command c = (Command) sameDescMap.get(cmd.getDesc()); if (c != null) return ConfigIntegrityInfo.newInvalidIntegrity( cmd, "The commands : \n\n" + c.toString() + ", id = " + c.getID() + "\n" + cmd.toString() + ", id = " + cmd.getID() + "\n\n" + "Has the description."); sameDescMap.put(cmd.getDesc(), cmd); } if (extDoc != null) { ConfigIntegrityInfo cii = extDoc.checkValid(); if (cii != ConfigIntegrityInfo.CONFIG_INTEGRIY_VALID) return ConfigIntegrityInfo.newInvalidIntegrity(this, cii.getReason()); } return ConfigIntegrityInfo.CONFIG_INTEGRIY_VALID; } public void resetState() { super.resetState(); if (extDoc != null) extDoc.resetState(); Vector commands = getCommands(); for (int j = 0; j < commands.size(); ++j) { JRemCommandDoc command = (JRemCommandDoc) commands.get(j); command.resetState(); } } 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 be) { attachExtensionDoc( (JRemExtensionDoc) be); setModified(true); fireGenericModifyEvent(); } public Extension getExtension(boolean parentRecursion) { if (extDoc == null && parentRecursion) return getConfig().getExtension(false); return extDoc; } public boolean isInHerited() { return extDoc == null; } public SupportExtension getParent() { return config; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -