1 19 package org.netbeans.modules.xml.refactoring; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import org.netbeans.api.project.SourceGroup; 31 import org.netbeans.modules.xml.xam.Component; 32 import org.netbeans.modules.xml.xam.ComponentEvent; 33 import org.netbeans.modules.xml.xam.ComponentListener; 34 import org.netbeans.modules.xml.xam.Model; 35 import org.netbeans.modules.xml.xam.Referenceable; 36 import org.openide.filesystems.FileObject; 37 import org.openide.util.WeakListeners; 38 39 47 48 public class UsageSet implements PropertyChangeListener , ComponentListener { 49 public static final String VALID_PROPERTY = "valid"; 50 private Model targetModel; 51 private Referenceable target; 52 private Set <UsageGroup> usageGroups; 53 private boolean valid; 54 private PropertyChangeSupport pcs; 55 56 57 public UsageSet(Referenceable target) { 58 assert target != null; 59 this.target = target; 60 targetModel = RefactorRequest.getModel(target); 61 assert targetModel != null; 62 pcs = new PropertyChangeSupport (this); 63 valid = true; 64 usageGroups = new HashSet <UsageGroup>(); 65 addModelListeners(targetModel); 66 } 67 68 private void addModelListeners(Model model) { 69 model.addPropertyChangeListener(WeakListeners.propertyChange(this, model)); 70 model.addComponentListener((ComponentListener) 71 WeakListeners.create(ComponentListener.class, this, model)); 72 } 73 74 public void addPropertyChangeListener(PropertyChangeListener pcl) { 75 pcs.addPropertyChangeListener(pcl); 76 } 77 78 public void removePropertyChangeListener(PropertyChangeListener pcl) { 79 pcs.removePropertyChangeListener(pcl); 80 } 81 82 public Collection <UsageGroup> getUsages() { 83 return usageGroups; 84 } 85 86 public Set <Model> getModels() { 87 Set <Model> models = new HashSet <Model>(); 88 for (UsageGroup ug : usageGroups ) { 89 if (ug.getModel() != null) { 90 models.add(ug.getModel()); 91 } 92 } 93 return models; 94 } 95 96 public Map <SourceGroup, Map <FileObject, Set <UsageGroup>>> getSortedUsages() { 97 Map <SourceGroup,Map <FileObject,Set <UsageGroup>>> result = new HashMap <SourceGroup,Map <FileObject,Set <UsageGroup>>>(); 98 for (UsageGroup u : getUsages()) { 99 Map <FileObject,Set <UsageGroup>> usagesInGroup = result.get(u.getSourceGroup()); 100 if (usagesInGroup == null) { 101 usagesInGroup = new HashMap <FileObject,Set <UsageGroup>>(); 102 result.put(u.getSourceGroup(), usagesInGroup); 103 } 104 105 Set <UsageGroup> usagesInFolder = usagesInGroup.get(u.getFolder()); 106 if (usagesInFolder == null) { 107 usagesInFolder = new HashSet <UsageGroup>(); 108 usagesInGroup.put(u.getFolder(), usagesInFolder); 109 } 110 usagesInFolder.add(u); 111 } 112 return result; 113 } 114 115 public void addUsages(List <UsageGroup> usages) { 116 if (usages == null) return; 117 for (UsageGroup u : usages) { 118 addUsage(u); 119 } 120 } 121 122 public void addUsage(UsageGroup p) { 123 if (p.getErrors().isEmpty()) { 124 assert p.getModel() != null : "Usage group does not have model"; } 126 if (! p.getItems().isEmpty() || ! p.getErrors().isEmpty()) { 127 addModelListeners(p.getModel()); 128 usageGroups.add(p); 129 } 130 } 131 132 public void removeUsage(UsageGroup p) { 133 usageGroups.remove(p); 134 } 135 136 public boolean isEmpty() { 137 return isEmpty(true); 138 } 139 public boolean isEmpty(boolean countErrors) { 140 for (UsageGroup ug : usageGroups) { 141 if (! ug.isEmpty(countErrors)) { 142 return false; 143 } 144 } 145 return true; 146 } 147 148 151 public boolean isValid() { 152 return valid; 153 } 154 155 protected void setValid(boolean valid) { 156 boolean old = isValid(); 157 this.valid = valid; 158 pcs.firePropertyChange(VALID_PROPERTY, old, valid); 159 } 160 161 private boolean isMySource(Object source) { 162 if (source == target) { 163 return true; 164 } 165 Model sourceModel = null; 166 if (source instanceof Model) { 167 sourceModel = (Model) source; 168 } else if (source instanceof Component) { 169 sourceModel = ((Component)source).getModel(); 170 } 171 if (sourceModel == null) { 172 return false; 173 } 174 if (targetModel == sourceModel) { 175 return true; 176 } 177 for (UsageGroup u : getUsages()) { 178 if (u.getModel() == sourceModel) { 179 return true; 180 } 181 } 182 return false; 183 } 184 185 public void propertyChange(PropertyChangeEvent evt) { 186 handleChange(evt.getSource()); 187 } 188 189 private void handleChange(Object source) { 190 if (RefactoringManager.getInstance().isRunning(this) || ! isValid() || ! isMySource(source)) { 193 return; 194 } 195 setValid(false); 196 } 197 198 public void valueChanged(ComponentEvent evt) { 199 handleChange(evt.getSource()); 200 } 201 202 public void childrenDeleted(ComponentEvent evt) { 203 handleChange(evt.getSource()); 204 } 205 206 public void childrenAdded(ComponentEvent evt) { 207 handleChange(evt.getSource()); 208 } 209 } 210 | Popular Tags |