1 19 package org.netbeans.modules.xml.refactoring; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import org.netbeans.api.project.SourceGroup; 24 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine; 25 import org.netbeans.modules.xml.xam.Component; 26 import org.netbeans.modules.xml.xam.Model; 27 import org.netbeans.modules.xml.xam.Referenceable; 28 import org.openide.filesystems.FileObject; 29 30 35 36 38 public class UsageGroup { 39 private RefactoringEngine engine; 40 private Model model; 41 private FileObject source; private Referenceable target; 43 private SourceGroup sourceGroup; 44 45 private List <Usage> items = new ArrayList <Usage>(); 46 private List <ErrorItem> errors = new ArrayList <ErrorItem>(); 47 48 public UsageGroup(RefactoringEngine engine, Model model, Referenceable target) { 49 this(engine, target); 50 this.model = model; 51 } 52 53 public UsageGroup(RefactoringEngine engine, FileObject src, Referenceable target) { 54 this(engine, target); 55 this.source = src; 56 } 57 58 private UsageGroup(RefactoringEngine engine, Referenceable target) { 59 if (engine == null || target == null) { 60 throw new IllegalArgumentException ( 61 "Usage constructor needs non-null engine and target arguments"); } 63 this.engine = engine; 64 this.target = target; 65 } 66 67 70 public RefactoringEngine getEngine() { 71 return engine; 72 } 73 74 77 public Model getModel() { 78 return model; 79 } 80 81 84 public Referenceable getTarget() { 85 return target; 86 } 87 88 91 public Component getTargetComponent() { 92 if (target instanceof Component) { 93 return (Component) target; 94 } 95 return null; 96 } 97 98 101 public Model getTargetModel() { 102 if (target instanceof Component) { 103 return ((Component) target).getModel(); 104 } else if (target instanceof Model) { 105 return (Model) target; 106 } 107 return null; 108 } 109 110 113 public List <Usage> getItems() { 114 assert model != null : "Try to retrieve items from an Usage created with null model"; return items; 116 } 117 118 121 public List <Component> getUsageComponents() { 122 assert model != null : "Try to retrieve items from an Usage created with null model"; List <Component> ret = new ArrayList <Component>(); 124 for(Usage i : items) { 125 ret.add(i.getComponent()); 126 } 127 return ret; 128 } 129 130 133 public List <Component> getRefactorComponents() { 134 assert model != null : "Try to retrieve items from an Usage created with null model"; List <Component> ret = new ArrayList <Component>(); 136 for(Usage i : items) { 137 if (i.isIncludedInRefactoring()) { 138 ret.add(i.getComponent()); 139 } 140 } 141 return ret; 142 } 143 144 public void addItem(Component usageComponent) { 145 assert model != null : "Cannot add item to Usage created with null model"; items.add(new Usage(usageComponent, this)); 147 } 148 149 152 public List <ErrorItem> getErrors() { 153 return errors; 154 } 155 156 public void addError(Component errorComponent, String message) { 157 errors.add(new ErrorItem(errorComponent, message)); 158 } 159 160 public void addError(ErrorItem error) { 161 errors.add(error); 162 for (Usage u : items) { 163 if (u.getComponent().equals(error.getComponent())) { 164 u.setIncludedInRefactoring(false); 165 break; 166 } 167 } 168 } 169 170 public boolean isEmpty() { 171 return isEmpty(true); 172 } 173 174 public boolean isEmpty(boolean countErrors) { 175 if (countErrors) { 176 return getItems().isEmpty() && getErrors().isEmpty(); 177 } else { 178 return getItems().isEmpty(); 179 } 180 } 181 182 protected void setSourceGroup(SourceGroup group) { 183 sourceGroup = group; 184 } 185 186 public SourceGroup getSourceGroup() { 187 return sourceGroup; 188 } 189 190 public FileObject getFolder() { 191 return getFileObject().getParent(); 192 } 193 194 public FileObject getFileObject() { 195 if (source == null) { 196 assert model != null : "Invalid Usage object, expecting non-null model."; source = (FileObject) getModel().getModelSource().getLookup().lookup(FileObject.class); 198 assert source != null : "ModelSource should have FileObject in lookup"; } 200 return source; 201 } 202 203 } 204 | Popular Tags |