1 19 20 package org.netbeans.modules.dbschema; 21 22 import java.util.*; 23 24 26 class DBMemoryCollection { 27 28 protected DBElement.Memory _memory; 29 30 31 private String _propertyName; 32 33 34 private Object [] _template; 35 36 private DBElement[] _elms; 37 38 40 public DBMemoryCollection() { 41 } 42 43 48 public DBMemoryCollection (DBElement.Memory memory, String propertyName, Object [] emptyArray) { 49 _memory = memory; 50 _propertyName = propertyName; 51 _template = emptyArray; 52 } 53 54 58 public void change(DBElement[] arr, int action) { 59 change(Arrays.asList(arr), action); 60 } 61 62 66 protected void change(List c, int action) { 67 boolean hasChange = false; 68 69 try { 70 DBElement[] oldElements = getElements(); 71 int oldLength = (oldElements == null) ? 0 : oldElements.length; 72 int newLength = (c == null) ? 0 : c.size(); 73 List list = null; 74 75 switch (action) { 76 case DBElement.Impl.ADD: 77 if (newLength > 0) { 78 list = ((oldLength == 0) ? new ArrayList() : new ArrayList(Arrays.asList(oldElements))); 79 list.addAll(c); 80 hasChange = true; 81 } 82 break; 83 case TableElement.Impl.SET: 84 list = c; 85 hasChange = true; 86 break; 87 case TableElement.Impl.REMOVE: 88 if (newLength > 0 && oldLength > 0) { 89 list = new ArrayList(Arrays.asList(oldElements)); 90 list.removeAll(c); 91 hasChange = true; 92 } 93 break; 94 } 95 if (hasChange) 96 _elms = (DBElement[]) list.toArray(_template); 97 } finally { 98 if (hasChange) 99 _memory.firePropertyChange(_propertyName, null, null); 100 } 101 } 102 103 106 public DBElement[] getElements() { 107 if (_elms != null) 108 return _elms; 109 else 110 return (DBElement[]) Arrays.asList(_template).toArray(new DBElement[_template.length]); 111 } 112 113 116 public DBElement getElement(DBIdentifier name) { 117 DBElement[] elms = getElements(); 118 int count = ((elms != null) ? elms.length : 0); 119 120 for (int i = 0; i < count; i++) { 121 DBElement elm = elms[i]; 122 if (name.getName().equals(elm.getName().getName())) 123 return elm; 124 } 125 126 return null; 127 } 128 129 131 static abstract class Member extends DBMemoryCollection { 132 134 public Member() { 135 } 136 137 143 public Member (DBElement.Memory memory, String propertyName, Object [] emptyArray) { 144 super(memory, propertyName, emptyArray); 145 } 146 147 150 protected TableElement getTableElement() { 151 if (_memory instanceof TableElement.Memory) 152 return ((TableElement.Memory)_memory).getTableElement(); 153 154 if (_memory instanceof DBMemberElement.Memory) 155 return ((DBMemberElement)((DBMemberElement.Memory)_memory)._element).getDeclaringTable(); 156 157 return null; 158 } 159 160 164 protected abstract DBMemberElement clone(Object obj); 165 } 166 167 169 static class Table extends DBMemoryCollection { 170 private static final TableElement[] EMPTY = new TableElement[0]; 171 172 174 public Table() { 175 } 176 177 180 public Table(DBElement.Memory el) { 181 super(el, DBElementProperties.PROP_TABLES, EMPTY); 182 } 183 184 188 protected TableElement clone (Object obj) { 189 return new TableElement(new TableElement.Memory((TableElement) obj), getSchemaElement()); 190 } 191 192 195 protected SchemaElement getSchemaElement () { 196 if (_memory instanceof SchemaElement.Memory) 197 return ((SchemaElement.Memory)_memory).getSchemaElement(); 198 199 if (_memory instanceof TableElement.Memory) 200 return ((TableElement)((TableElement.Memory)_memory)._element).getDeclaringSchema(); 201 202 return null; 203 } 204 } 205 206 208 static class Column extends Member { 209 private static final ColumnElement[] EMPTY = new ColumnElement[0]; 210 211 213 public Column() { 214 } 215 216 219 public Column(DBElement.Memory el) { 220 super(el, DBElementProperties.PROP_COLUMNS, EMPTY); 221 } 222 223 227 protected DBMemberElement clone(Object obj) { 228 return new ColumnElement(new ColumnElement.Memory((ColumnElement)obj), getTableElement()); 229 } 230 } 231 232 234 static class ColumnPair extends Member { 235 private static final ColumnPairElement[] EMPTY = new ColumnPairElement[0]; 236 237 239 public ColumnPair() { 240 } 241 242 245 public ColumnPair(DBElement.Memory el) { 246 super(el, DBElementProperties.PROP_COLUMN_PAIRS, EMPTY); 247 } 248 249 253 protected DBMemberElement clone(Object obj) { 254 return null; 256 } 257 } 258 259 261 static class Index extends Member { 262 private static final IndexElement[] EMPTY = new IndexElement[0]; 263 264 266 public Index() { 267 } 268 269 272 public Index(DBElement.Memory el) { 273 super(el, DBElementProperties.PROP_INDEXES, EMPTY); 274 } 275 276 280 protected DBMemberElement clone(Object obj) { 281 return new IndexElement(new IndexElement.Memory((IndexElement)obj), getTableElement()); 282 } 283 } 284 285 287 static class Key extends Member { 288 private static final KeyElement[] EMPTY = new KeyElement[0]; 289 290 292 public Key() { 293 } 294 295 298 public Key(DBElement.Memory el) { 299 super(el, DBElementProperties.PROP_KEYS, EMPTY); 300 } 301 302 306 protected DBMemberElement clone(Object obj) { 307 return null; 308 } 309 } 310 } 311 | Popular Tags |