1 19 package org.netbeans.modules.j2ee.sun.share.config; 20 import java.util.*; 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyChangeEvent ; 23 24 import javax.enterprise.deploy.model.*; 25 import javax.enterprise.deploy.spi.*; 26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 27 28 import org.openide.*; 29 import org.openide.nodes.*; 30 31 import org.netbeans.modules.j2ee.sun.share.config.ui.ConfigBeanNode; 32 import org.netbeans.modules.j2ee.sun.share.configbean.Base; 33 import org.netbeans.modules.j2ee.sun.share.configbean.DConfigBeanProperties; 34 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration; 35 36 38 public class ConfigBeanStorage implements PropertyChangeListener , Comparable { 39 40 private ConfigurationStorage storage; 41 DConfigBean bean; 42 private ConfigBeanStorage parent; 43 private ConfigBeanNode node = null; 44 private Map childMap = new HashMap(); 45 46 public ConfigBeanStorage(DConfigBean bean, ConfigBeanStorage parent, ConfigurationStorage storage) throws ConfigurationException { 47 this.bean = bean; 48 this.parent = parent; 49 this.storage = storage; 50 initChildren(); 53 StandardDDImpl dd = (StandardDDImpl) bean.getDDBean(); 55 dd.proxy.addConfigBean(this); 56 58 bean.addPropertyChangeListener(this); 59 } 60 61 public Map getChildMap() { 62 return childMap; 63 } 64 65 public ConfigurationStorage getStorage() { 66 return storage; 67 } 68 69 public synchronized Node getNode() { 70 node = new ConfigBeanNode(this); 72 return node; 73 } 74 75 public void propertyChange(PropertyChangeEvent pce) { 76 if (storage != null) { 77 if(!DConfigBeanProperties.PROP_DISPLAY_NAME.equalsIgnoreCase(pce.getPropertyName())) { 80 storage.setChanged(); 81 } 82 } 83 if (DConfigBeanProperties.PROP_DISPLAY_NAME.equalsIgnoreCase(pce.getPropertyName())) { 84 if(node != null) { 85 node.setDisplayName((String ) pce.getNewValue()); 86 } 87 } 88 } 89 90 public void remove() { 91 DDCommon dd = (DDCommon) ((StandardDDImpl)bean.getDDBean()).proxy; 92 dd.removeConfigBean(this); 93 if(parent != null) { 94 try { 95 parent.bean.removeDConfigBean(bean); 96 storage.setChanged(); 97 } catch (Exception e) { 98 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 99 } 100 } 101 } 102 103 private void initChildren() throws ConfigurationException { 104 String [] xpaths = bean.getXpaths(); 105 if(xpaths == null) return; 106 for(int i = 0; i < xpaths.length; i++) { 107 DDBean[] beans = bean.getDDBean().getChildBean(xpaths[i]); 108 for(int j = 0; j < beans.length; j++) { 109 addChild((StandardDDImpl) beans[j]); 110 } 111 } 112 } 113 114 void fireEvent(String relPath, XpathEvent xe) throws ConfigurationException { 115 String [] xpaths = bean.getXpaths(); 116 if(xpaths == null) return; 117 StandardDDImpl eventDD = (StandardDDImpl) xe.getBean(); 118 StandardDDImpl[] targetDDs = null; 119 for(int i = 0 ; i < xpaths.length; i++) { 120 if(xpaths[i].equals(relPath) || xpaths[i].equals(xe.getBean().getXpath())) { 121 targetDDs = new StandardDDImpl[] { eventDD }; 122 break; 123 } 124 } 125 HashSet targetSet = new HashSet(); 126 for (int i=0; targetDDs == null && i < xpaths.length; i++) { 127 if(xpaths[i].startsWith(relPath) && 135 (xpaths[i].length() == relPath.length() || xpaths[i].charAt(relPath.length()) == '/')) { 136 String targetPath = DDCommon.getRelativePath(xpaths[i], relPath); 137 DDBean[] dds = eventDD.getChildBean(targetPath); 138 if (dds == null) 139 continue; 140 for (int j=0; j<dds.length; j++) { 141 if (!(dds[j] instanceof StandardDDImpl)) { 142 continue; 143 } 144 targetSet.add(dds[j]); 145 } 146 if (targetSet.size() > 0) { 147 targetDDs = (StandardDDImpl[]) targetSet.toArray(new StandardDDImpl[targetSet.size()]); 148 } 149 } 150 } 151 if (targetDDs != null) { 152 for (int i=0; i<targetDDs.length; i++) { 153 if (xe.isAddEvent()) { 154 addChild(targetDDs[i]); 155 } else { 156 removeChild(targetDDs[i]); 157 } 158 } 159 } 160 } 161 162 public static final String RESOURCE_REF = "resource-ref"; 164 private void addChild(StandardDDImpl dd) throws ConfigurationException { 165 DConfigBean cb = bean.getDConfigBean(dd); 166 if(cb == null) { 167 return; 168 } 169 if (RESOURCE_REF.equals(dd.proxy.dtdname)) { 170 DeploymentConfiguration dc = storage.getDeploymentConfiguration(); 171 if(dc instanceof SunONEDeploymentConfiguration) { 172 SunONEDeploymentConfiguration config = (SunONEDeploymentConfiguration) dc; 173 config.ensureResourceDefined(dd); 174 } 175 } 176 ConfigBeanStorage cbs = new ConfigBeanStorage(cb, this, storage); 177 Collection c = (Collection) childMap.get(dd.getXpath()); 178 if(c == null) { 179 c = new TreeSet(); 180 childMap.put(dd.getXpath(), c); 181 } 182 c.add(cbs); 183 fireChildBeanAddedEvent(cbs); 184 } 185 186 private void removeChild(DDBean remBean) { 187 Collection c = (Collection) childMap.get(remBean.getXpath()); 188 if(c == null) { 189 return; 190 } 191 for(Iterator i = c.iterator(); i.hasNext(); ) { 192 ConfigBeanStorage cbs = (ConfigBeanStorage) i.next(); 193 if (cbs.bean.getDDBean().equals(remBean)) { 194 cbs.remove(); 195 i.remove(); 196 fireChildBeanRemovedEvent(cbs); 197 } 198 } 199 } 200 201 205 public int compareTo(Object o) { 206 if(o == null) { 207 throw new NullPointerException ("Null argument passed to ConfigBeanStorage.compareTo()"); } 209 210 ConfigBeanStorage target = (ConfigBeanStorage) o; 211 int result = -1; 212 if(this == o) { 213 result = 0; 214 } else if(bean instanceof Base && target.bean instanceof Base) { 215 Base sourceBean = (Base) bean; 216 Base targetBean = (Base) target.bean; 217 218 result = sourceBean.getDisplayName().compareTo(targetBean.getDisplayName()); 219 220 if(result == 0) { 221 result = sourceBean.getIdentity().compareTo(targetBean.getIdentity()); 222 } 223 } else { 224 throw new IllegalStateException ("Source or target bean is null or not derived from Base."); } 226 227 return result; 228 } 229 230 public DConfigBean getConfigBean() { 231 return bean; 232 } 233 234 public static interface ChildrenChangeListener { 235 public void childBeanAdded(ConfigBeanStorage childBeanStorage); 236 public void childBeanRemoved(ConfigBeanStorage childBeanStorage); 237 } 238 239 private List childrenChangeListeners = new Vector(); 240 public void addChildrenChangeListener(ChildrenChangeListener l) { 241 childrenChangeListeners.add(l); 242 } 243 public void removeChildrenChangeListener(ChildrenChangeListener l) { 244 childrenChangeListeners.remove(l); 245 } 246 private void fireChildBeanAddedEvent(ConfigBeanStorage childBeanStorage) { 247 for (Iterator i=childrenChangeListeners.iterator(); i.hasNext();) { 248 ChildrenChangeListener l = (ChildrenChangeListener) i.next(); 249 l.childBeanAdded(childBeanStorage); 250 } 251 } 252 private void fireChildBeanRemovedEvent(ConfigBeanStorage childBeanStorage) { 253 for (Iterator i=childrenChangeListeners.iterator(); i.hasNext();) { 254 ChildrenChangeListener l = (ChildrenChangeListener) i.next(); 255 l.childBeanRemoved(childBeanStorage); 256 } 257 } 258 259 } 260 | Popular Tags |