1 19 package org.netbeans.test.editor.app.core; 20 21 import java.beans.*; 22 import javax.swing.JEditorPane ; 23 24 import java.util.Vector ; 25 26 import org.w3c.dom.Element ; 27 import java.io.IOException ; 28 import java.util.Vector ; 29 import java.util.Collection ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import javax.swing.tree.TreeNode ; 33 import org.netbeans.test.editor.app.core.actions.ActionRegistry; 34 import org.netbeans.test.editor.app.core.cookies.Cookie; 35 import org.netbeans.test.editor.app.core.cookies.PerformCookie; 36 import org.netbeans.test.editor.app.core.properties.ArrayProperty; 37 import org.netbeans.test.editor.app.core.properties.BadPropertyNameException; 38 import org.netbeans.test.editor.app.core.properties.Properties; 39 import org.netbeans.test.editor.app.core.properties.StringProperty; 40 import org.netbeans.test.editor.app.gui.actions.TestDeleteAction; 41 import org.netbeans.test.editor.app.gui.actions.TestDownAction; 42 import org.netbeans.test.editor.app.gui.actions.TestPropertiesAction; 43 import org.netbeans.test.editor.app.gui.actions.TestRenameAction; 44 import org.netbeans.test.editor.app.gui.actions.TestUpAction; 45 import org.netbeans.test.editor.app.gui.tree.ActionsCache; 46 import org.netbeans.test.editor.app.gui.tree.TestNodeDelegate; 47 48 53 public abstract class TestNode extends Object implements java.io.Serializable , XMLNode { 54 55 public static final String CHANGE_NAME = "Change Name"; 56 57 public static final String NAME = "Name"; 58 59 protected String name; 60 61 protected boolean isPerforming; 62 63 protected static int nameCounter=1; 64 65 public TestGroup owner; 66 67 protected PropertyChangeSupport propertySupport; 68 69 private TreeNode nodeDelegate = null; 70 71 protected HashMap cookieSet; 72 73 74 public TestNode(String name) { 75 propertySupport = new PropertyChangeSupport( this ); 76 this.name=name; 77 isPerforming=false; 78 registerActions(); 79 registerCookies(); 80 } 81 82 public TestNode(Element node) { 83 this(node.getAttribute(NAME)); 84 } 85 86 public abstract boolean isParent(); 87 public abstract void perform(); 88 public abstract void stop(); 89 protected abstract void registerCookies(); 90 91 public Element toXML(Element node) { 92 node.setAttribute(NAME, name); 93 return node; 94 } 95 96 public JEditorPane getEditor() { 97 return owner.getEditor(); 98 } 99 100 public Logger getLogger() { 101 return owner.getLogger(); 102 } 103 104 public String getName() { 105 return name; 106 } 107 108 public final void setName(String value) { 109 String oldValue = name; 110 name = value; 111 firePropertyChange(CHANGE_NAME, oldValue, name); 112 } 113 114 public void addPropertyChangeListener(PropertyChangeListener listener) { 115 propertySupport.addPropertyChangeListener(listener); 116 } 117 118 public void removePropertyChangeListener(PropertyChangeListener listener) { 119 propertySupport.removePropertyChangeListener(listener); 120 } 121 122 public void firePropertyChange(String name,Object oldV, Object newV) { 123 if (oldV == newV) oldV=null; 124 propertySupport.firePropertyChange(name,oldV,newV); 125 } 126 127 public boolean isPerfoming() { 128 return isPerforming; 129 } 130 131 public void delete() { 132 owner.remove(this); 133 } 134 135 public static int getNameCounter() { 136 return nameCounter++; 137 } 138 139 public TreeNode createNodeDelegate() { 140 return new TestNodeDelegate(this); 141 } 142 143 public final TreeNode getNodeDelegate() { 144 if (nodeDelegate == null) { 145 nodeDelegate = createNodeDelegate(); 146 } 147 return nodeDelegate; 148 } 149 public HashMap getCookieSet() { 151 if (cookieSet == null) { 152 cookieSet=new HashMap (); 153 } 154 return cookieSet; 155 } 156 157 public Cookie getCookie(Class clazz) { 158 return (Cookie)cookieSet.get(clazz); 159 } 160 161 protected void registerActions() { 162 ActionsCache.getDefault().addNodeActions(getClass(), ActionRegistry.getDefault().getActions(getCookieSet().values())); 163 ActionsCache.getDefault().addNodeAction(getClass(), new TestUpAction()); 164 ActionsCache.getDefault().addNodeAction(getClass(), new TestDownAction()); 165 ActionsCache.getDefault().addNodeAction(getClass(), new TestRenameAction()); 166 ActionsCache.getDefault().addNodeAction(getClass(), new TestDeleteAction()); 167 ActionsCache.getDefault().addNodeAction(getClass(), new TestRenameAction()); 168 ActionsCache.getDefault().addNodeAction(getClass(), new TestPropertiesAction()); 169 } 170 171 public final Vector getActions() { 172 Vector v=ActionsCache.getDefault().getActions(getClass()); 173 if (v == null) { 174 registerActions(); 175 v=ActionsCache.getDefault().getActions(getClass()); 176 } 177 return v; 178 } 179 180 public TestGroup getOwner() { 181 return owner; 182 } 183 184 public void fromXML(Element node) throws BadPropertyNameException { 185 name=node.getAttribute(NAME); 186 } 187 188 public Properties getProperties() { 189 Properties ret=new Properties(); 190 ret.put(NAME, new StringProperty(name)); 191 return ret; 192 } 193 194 public Object getProperty(String name) throws BadPropertyNameException { 195 if (name.compareTo(NAME) == 0) { 196 return new StringProperty(name); 197 } else { 198 throw new BadPropertyNameException(name+" isn't name of any property."); 199 } 200 } 201 202 public void setProperty(String name, Object value) throws BadPropertyNameException { 203 if (name.compareTo(NAME) == 0) { 204 setName(((StringProperty)value).getProperty()); 205 } else { 206 throw new BadPropertyNameException(name+" isn't name of any property."); 207 } 208 } 209 210 public String toString() { 211 return name; 212 } 213 214 } | Popular Tags |