1 19 package org.netbeans.modules.xml.refactoring; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil; 27 import org.netbeans.modules.xml.refactoring.spi.ChangeExecutor; 28 import org.netbeans.modules.xml.xam.Component; 29 import org.netbeans.modules.xml.xam.EmbeddableRoot; 30 import org.netbeans.modules.xml.xam.Model; 31 import org.netbeans.modules.xml.xam.Referenceable; 32 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 33 import org.netbeans.modules.xml.xam.dom.DocumentModel; 34 import org.openide.filesystems.FileObject; 35 36 40 public abstract class RefactorRequest { 41 private Set <Component> scope; 42 private ChangeExecutor changeExecutor; 43 private UsageSet usages; 44 private boolean autosave = true; 45 private List <ErrorItem> errors; 46 47 public RefactorRequest() { 48 this(null); 49 } 50 51 54 public RefactorRequest(Set <Component> scope) { 55 this.scope = scope; 56 errors = new ArrayList <ErrorItem>(); 57 } 58 59 public Set <Component> getScope() { 60 return scope; 61 } 62 63 public void setScope(Set <Component> searchRoots) { 64 scope = searchRoots; 65 } 66 67 public void setScopeLocal() { 68 if (getTarget() instanceof DocumentModel) { 69 scope = new HashSet <Component>(); 70 scope.add(((DocumentModel)getTarget()).getRootComponent()); 71 } else if (getTarget() instanceof Component) { 72 scope = Collections.singleton(getRootOf((Component)getTarget())); 73 } 74 setAutosave(false); 75 } 76 77 public boolean isScopeLocal() { 78 if (getScope() == null || getScope().size() > 1) { 79 return false; 80 } 81 return getScope().iterator().next() == getRootOf(getTargetComponent()); 82 } 83 84 public String getDescription() { 85 return RefactoringUtil.getDescription(this); 86 } 87 88 91 public abstract Referenceable getTarget(); 92 93 public abstract String getTargetName(); 94 95 public Model getTargetModel() { 96 return getModel(getTarget()); 97 } 98 99 public Component getTargetComponent() { 100 return getComponent(getTarget()); 101 } 102 103 public static Model getModel(Referenceable ref) { 104 if (ref instanceof Model) { 105 return (Model) ref; 106 } else if (ref instanceof Component) { 107 return ((Component)ref).getModel(); 108 } else { 109 return null; 110 } 111 } 112 113 public static Component getComponent(Referenceable ref) { 114 if (ref instanceof Component) { 115 return (Component) ref; 116 } else if (ref instanceof DocumentModel) { 117 return ((DocumentModel)ref).getRootComponent(); 118 } else { 119 throw new IllegalArgumentException ("Invalid target type "+ref.getClass().getName()); 120 } 121 } 122 123 public FileObject getFileObject() { 124 FileObject fo = (FileObject) getTargetModel().getModelSource().getLookup().lookup(FileObject.class); 125 assert fo != null : "Failed to lookup for file object in model source"; return fo; 127 } 128 129 134 public ChangeExecutor getChangeExecutor() { 135 if (changeExecutor == null) { 136 for (ChangeExecutor ce : RefactoringManager.getInstance().getExecutors()) { 137 if (ce.canChange(getType(), getTarget())) { 138 changeExecutor = ce; 139 break; 140 } 141 } 142 } 143 return changeExecutor; 144 } 145 146 void setChangeExecutor(ChangeExecutor executor) { 147 changeExecutor = executor; 148 } 149 150 153 public UsageSet getUsages() { 154 return usages; 155 } 156 157 public void setUsages(UsageSet usageSet) { 158 usages = usageSet; 159 } 160 161 164 public boolean getAutosave() { 165 return autosave; 166 } 167 168 171 public void setAutosave(boolean v) { 172 autosave = v; 173 } 174 175 178 public abstract <T extends RefactorRequest> Class <T> getType(); 179 180 183 public abstract boolean confirmChangePerformed(); 184 185 189 public List <ErrorItem> getChangeErrors() { 190 return errors; 191 } 192 193 196 public List <ErrorItem> getErrors() { 197 List ret = new ArrayList <ErrorItem>(); 198 ret.addAll(errors); 199 if (usages != null) { 200 for (UsageGroup u : usages.getUsages()) { 201 if (u.getErrors() != null) { 202 ret.addAll(u.getErrors()); 203 } 204 } 205 } 206 return ret; 207 } 208 209 213 public void addError(ErrorItem error) { 214 errors.add(error); 215 } 216 217 public void addError(String message) { 218 errors.add(new ErrorItem(getTarget(), message)); 219 } 220 221 public void precheckChange() { 222 clear(); 223 RefactoringUtil.precheckTarget(this); 224 } 225 226 public void precheckUsages() { 227 RefactoringUtil.precheckUsageModels(this); 228 } 229 230 public boolean hasFatalErrors() { 231 for (ErrorItem e : getErrors()) { 232 if (e.getLevel() == ErrorItem.Level.FATAL) { 233 return true; 234 } 235 } 236 return false; 237 } 238 239 protected void clear() { 240 errors = new ArrayList <ErrorItem>(); 241 usages = null; 242 } 243 244 public static Component getEffectiveParent(Component component) { 245 if (component instanceof EmbeddableRoot) { 246 return ((EmbeddableRoot) component).getForeignParent(); 247 } else { 248 return component.getParent(); 249 } 250 } 251 252 public static Component getRootOf(Referenceable referenceable) { 253 return getRootOf(referenceable); 254 } 255 256 public static Component getRootOf(Component component) { 257 Component root = (Component) component; 258 while (root != null) { 259 Component parent = getEffectiveParent(root); 260 if (parent == null) { 261 break; 262 } 263 root = parent; 264 } 265 return root; 266 } 267 268 public Set <Model> getDirtyModels() { 269 HashSet <Model> dirties = new HashSet <Model>(); 270 if (RefactoringUtil.isDirty(getTargetModel())) { 271 dirties.add(getTargetModel()); 272 } 273 if (getUsages() != null) { 274 for (Model model : getUsages().getModels()) { 275 if (RefactoringUtil.isDirty(model)) { 276 dirties.add(model); 277 } 278 } 279 } 280 return dirties; 281 } 282 283 } 284 | Popular Tags |