1 19 package org.netbeans.mdr.handlers; 20 21 import java.util.*; 22 import org.netbeans.api.mdr.events.AttributeEvent; 23 import org.netbeans.mdr.util.EventNotifier; 24 25 29 public class AttrCollWrapper extends AttrImmutCollWrapper { 30 protected final EventNotifier.Abstract notifier; 31 protected String attrName = null; 32 protected final boolean isStatic; 33 34 public AttrCollWrapper(FeaturedHandler source, int attrIndex, String attrName) { 35 super(source._getMdrStorage(), source, attrIndex, attrName); 36 EventNotifier en = source._getMdrStorage().getEventNotifier(); 37 this.isStatic = source instanceof ClassProxyHandler; 38 this.notifier = isStatic ? (EventNotifier.Abstract) en.CLASS : (EventNotifier.Abstract) en.INSTANCE; 39 } 40 41 public AttrCollWrapper(FeaturedHandler source, Collection inner) { 42 super(source._getMdrStorage(), inner); 43 this.source = source; 44 EventNotifier en = source._getMdrStorage().getEventNotifier(); 45 this.isStatic = source instanceof ClassProxyHandler; 46 this.notifier = isStatic ? (EventNotifier.Abstract) en.CLASS : (EventNotifier.Abstract) en.INSTANCE; 47 } 48 49 public void setAttrName(String name) { 50 this.attrName = name; 51 } 52 53 public final Iterator iterator() { 54 lock(false); 55 try { 56 return new AttrIteratorWrapper(getInnerCollection().iterator()); 57 } finally { 58 unlock(); 59 } 60 } 61 62 public final void clear() { 63 boolean fail = true; 64 lock(true); 65 try { 66 Object elements[] = getInnerCollection().toArray(); 67 for (int i = 0; i < elements.length; i++) { 68 remove(elements[i]); 69 } 70 fail = false; 71 } finally { 72 unlock(fail); 73 } 74 } 75 76 public final boolean addAll(Collection collection) { 77 boolean fail = true; 78 lock(true); 79 try { 80 boolean result = false; 81 for (Iterator it = collection.iterator(); it.hasNext();) { 82 result |= add(it.next()); 83 } 84 fail = false; 85 return result; 86 } finally { 87 unlock(fail); 88 } 89 } 90 91 public final boolean remove(Object obj) { 92 boolean fail = true; 93 lock(true); 94 try { 95 if (storage.eventsEnabled()) { 96 AttributeEvent event = new AttributeEvent( 97 source, 98 isStatic ? AttributeEvent.EVENT_CLASSATTR_REMOVE : AttributeEvent.EVENT_ATTRIBUTE_REMOVE, 99 attrName, 100 obj, null, 101 AttributeEvent.POSITION_NONE); 102 notifier.firePlannedChange(source, event); 103 } 104 boolean result = getInnerCollection().remove(obj); 105 fail = false; 106 return result; 107 } finally { 108 unlock(fail); 109 } 110 } 111 112 public final boolean add(Object obj) { 113 boolean fail = true; 114 lock(true); 115 try { 116 if (storage.eventsEnabled()) { 117 AttributeEvent event = new AttributeEvent( 118 source, 119 isStatic ? AttributeEvent.EVENT_CLASSATTR_ADD : AttributeEvent.EVENT_ATTRIBUTE_ADD, 120 attrName, 121 null, obj, 122 AttributeEvent.POSITION_NONE); 123 notifier.firePlannedChange(source, event); 124 } 125 boolean result = getInnerCollection().add(obj); 126 fail = false; 127 return result; 128 } finally { 129 unlock(fail); 130 } 131 } 132 133 public boolean retainAll(Collection collection) { 134 boolean fail = true; 135 lock(true); 136 try { 137 boolean result = false; 138 Object elements[] = getInnerCollection().toArray(); 139 for (int i = 0; i < elements.length; i++) { 140 if (!collection.contains(elements[i])) { 141 remove(elements[i]); 142 result = true; 143 } 144 } 145 fail = false; 146 return result; 147 } finally { 148 unlock(fail); 149 } 150 } 151 152 public boolean removeAll(Collection collection) { 153 boolean fail = true; 154 lock(true); 155 try { 156 boolean result = false; 157 for (Iterator it = collection.iterator(); it.hasNext();) { 158 result |= remove(it.next()); 159 } 160 fail = false; 161 return result; 162 } finally { 163 unlock(fail); 164 } 165 } 166 167 protected class AttrIteratorWrapper extends AttrImmutIteratorWrapper { 168 protected Object lastRead = null; 169 170 protected AttrIteratorWrapper(Iterator innerIterator) { 171 super(innerIterator); 172 } 173 174 public Object next() { 175 return (lastRead = super.next()); 176 } 177 178 public void remove() { 179 boolean fail = true; 180 lock(true); 181 try { 182 if (storage.eventsEnabled()) { 183 AttributeEvent event = new AttributeEvent( 184 source, 185 isStatic ? AttributeEvent.EVENT_CLASSATTR_REMOVE : AttributeEvent.EVENT_ATTRIBUTE_REMOVE, 186 attrName, 187 lastRead, null, 188 AttributeEvent.POSITION_NONE); 189 notifier.firePlannedChange(source, event); 190 } 191 innerIterator.remove(); 192 fail = false; 193 } finally { 194 unlock(fail); 195 } 196 } 197 } 198 } 199 | Popular Tags |