1 19 20 package org.netbeans.modules.form.codestructure; 21 22 import java.util.*; 23 24 27 28 final class CodeObjectUsage { 29 30 private UsedCodeObject usedObject; 31 private java.util.List usageList; 32 33 public CodeObjectUsage(UsedCodeObject usedObject) { 34 this.usedObject = usedObject; 35 } 36 37 public CodeStructureChange addUsingObject(UsingCodeObject usingObject, 38 int useType, 39 Object useCategory, 40 boolean provideUndoableChange) 41 { 42 if (useCategory == null) 43 throw new IllegalArgumentException (); 44 45 if (usageList == null) 46 usageList = new LinkedList(); 47 48 ObjectUse use = new ObjectUse(usingObject, useType, useCategory); 49 usageList.add(use); 51 usingObject.usageRegistered(usedObject); 52 53 return provideUndoableChange ? 54 new UsageChange(usedObject, use, true) : null; 55 } 56 57 public CodeStructureChange removeUsingObject(UsingCodeObject usingObject, 58 boolean provideUndoableChange) 59 { 60 if (usageList == null) 61 return null; 62 63 ObjectUse removed = null; 64 Iterator it = usageList.iterator(); 65 while (it.hasNext()) { 66 ObjectUse use = (ObjectUse) it.next(); 67 if (usingObject == use.usingObject) { 68 removed = use; 69 it.remove(); 70 } 71 } 72 73 if (removed != null) 74 usingObject.usedObjectRemoved(usedObject); 75 76 return provideUndoableChange && removed != null ? 77 new UsageChange(usedObject, removed, false) : null; 78 } 79 80 public Iterator getUsingObjectsIterator(int useType, Object useCategory) { 81 Iterator it = usageList != null ? usageList.iterator() : null; 82 return new UsageIterator(it, useType, useCategory); 83 } 84 85 public boolean isEmpty() { 86 return usageList == null || usageList.size() == 0; 87 } 88 89 91 private static class ObjectUse { 92 private UsingCodeObject usingObject; 93 private int type; 94 private Object category; 95 96 ObjectUse(UsingCodeObject usingObject, int useType, Object useCategory) { 97 this.usingObject = usingObject; 98 this.type = useType; 99 this.category = useCategory; 100 } 101 102 boolean matches(int type, Object category) { 103 if (type != 0 && type != this.type) 104 return false; 105 if (category == null) 106 return true; 107 return category.equals(this.category); 108 } 109 } 110 111 113 private static class UsageChange implements CodeStructureChange { 114 private UsedCodeObject usedObject; 115 private ObjectUse use; 116 private boolean added; 118 UsageChange(UsedCodeObject usedObject, ObjectUse use, boolean added) { 119 this.usedObject = usedObject; 120 this.use = use; 121 this.added = added; 122 } 123 124 public void undo() { 125 if (added) 126 usedObject.removeUsingObject(use.usingObject); 127 else 128 usedObject.addUsingObject(use.usingObject, use.type, use.category); 129 } 130 131 public void redo() { 132 if (added) 133 usedObject.addUsingObject(use.usingObject, use.type, use.category); 134 else 135 usedObject.removeUsingObject(use.usingObject); 136 } 137 } 138 139 141 private static class UsageIterator implements Iterator { 142 private int useType; 143 private Object useCategory; 144 145 private Iterator iterator; 146 private Object next; 147 148 public UsageIterator(Iterator iterator, 149 int useType, Object useCategory) 150 { 151 this.iterator = iterator; 152 this.useType = useType; 153 this.useCategory = useCategory; 154 } 155 156 public boolean hasNext() { 157 if (iterator == null) 158 return false; 159 if (next != null) 160 return true; 161 162 while (iterator.hasNext()) { 163 ObjectUse use = (ObjectUse) iterator.next(); 164 if (use.matches(useType, useCategory)) { 165 next = use.usingObject; 166 return true; 167 } 168 } 169 return false; 170 } 171 172 public Object next() { 173 if (!hasNext()) 174 throw new NoSuchElementException(); 175 176 Object nextObject = next; 177 next = null; 178 return nextObject; 179 } 180 181 public void remove() { 182 throw new UnsupportedOperationException (); 183 } 184 } 185 } 186 | Popular Tags |