KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > DBMemoryCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 import java.util.*;
23
24 /** Support class that manages set of objects and fires events
25  */

26 class DBMemoryCollection {
27     /** Object to fire info about changes to */
28     protected DBElement.Memory/*TableElement.Memory*/ _memory;
29
30     /** name of property to fire */
31     private String JavaDoc _propertyName;
32
33     /** array template to return */
34     private Object JavaDoc[] _template;
35
36     private DBElement[] _elms;
37
38     /** Default constructor
39      */

40     public DBMemoryCollection() {
41     }
42
43     /** Creates a new collection.
44      * @param memory memory element to fire changes
45      * @param propertyName name of property to fire when array changes
46      * @param emptyArray emptyArray instance that provides the type of arrays that should be returned by toArray method
47      */

48     public DBMemoryCollection (DBElement.Memory memory, String JavaDoc propertyName, Object JavaDoc[] emptyArray) {
49         _memory = memory;
50         _propertyName = propertyName;
51         _template = emptyArray;
52     }
53
54     /** Changes the content of this object.
55      * @param arr array of objects to change
56      * @param action the action to do
57      */

58     public void change(DBElement[] arr, int action) {
59         change(Arrays.asList(arr), action);
60     }
61
62     /** Changes the content of this object.
63      * @param c collection of objects to change
64      * @param action the action to do
65      */

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     /** Returns an array containing all of the elements in this collection.
104      * @return an array containing all of the elements in this collection
105      */

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     /** Returns an element specified by the name from this collection.
114      * @return an element
115      */

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     /** Collection for members. Assignes to each class its members.
130      */

131     static abstract class Member extends DBMemoryCollection {
132         /** Default constructor.
133          */

134         public Member() {
135         }
136
137         /** Creates a new member.
138          * @param memory memory element to fire changes to
139          * @param propertyName name of property to fire when array changes
140          * @param emptyArray emptyArray instance that provides the type of
141          * arrays that should be returned by toArray method
142          */

143         public Member (DBElement.Memory memory, String JavaDoc propertyName, Object JavaDoc[] emptyArray) {
144             super(memory, propertyName, emptyArray);
145         }
146
147         /** Gets a table element.
148          * @return a table element
149          */

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         /** Clones the object.
161          * @param obj object to clone
162          * @return cloned object
163          */

164         protected abstract DBMemberElement clone(Object JavaDoc obj);
165     }
166
167     /** Collection of tables.
168      */

169     static class Table extends DBMemoryCollection {
170         private static final TableElement[] EMPTY = new TableElement[0];
171
172         /** Default constructor
173          */

174         public Table() {
175         }
176
177         /** Creates a new table.
178          * @param el table element memory impl to work in
179          */

180         public Table(DBElement.Memory el) {
181             super(el, DBElementProperties.PROP_TABLES, EMPTY);
182         }
183
184         /** Clones the object.
185          * @param obj object to clone
186          * @return cloned object
187          */

188         protected TableElement clone (Object JavaDoc obj) {
189             return new TableElement(new TableElement.Memory((TableElement) obj), getSchemaElement());
190         }
191         
192         /** Gets a schema element.
193          * @return a schema element
194          */

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     /** Collection of columns.
207      */

208     static class Column extends Member {
209         private static final ColumnElement[] EMPTY = new ColumnElement[0];
210
211         /** Default constructor
212          */

213         public Column() {
214         }
215
216         /** Creates a new column.
217          * @param el table element memory impl to work in
218          */

219         public Column(DBElement.Memory el) {
220             super(el, DBElementProperties.PROP_COLUMNS, EMPTY);
221         }
222
223         /** Clones the object.
224          * @param obj object to clone
225          * @return cloned object
226          */

227         protected DBMemberElement clone(Object JavaDoc obj) {
228             return new ColumnElement(new ColumnElement.Memory((ColumnElement)obj), getTableElement());
229         }
230     }
231     
232     /** Collection of column pairs.
233      */

234     static class ColumnPair extends Member {
235         private static final ColumnPairElement[] EMPTY = new ColumnPairElement[0];
236
237         /** Default constructor
238          */

239         public ColumnPair() {
240         }
241
242         /** Creates a new column pair.
243          * @param el table element memory impl to work in
244          */

245         public ColumnPair(DBElement.Memory el) {
246             super(el, DBElementProperties.PROP_COLUMN_PAIRS, EMPTY);
247         }
248
249         /** Clones the object.
250          * @param obj object to clone
251          * @return cloned object
252          */

253         protected DBMemberElement clone(Object JavaDoc obj) {
254 // return new ColumnPairElement(new ColumnPairElement.Memory((ColumnPairElement)obj), null, null, getTableElement());
255
return null;
256         }
257     }
258
259     /** Collection of indexes.
260      */

261     static class Index extends Member {
262         private static final IndexElement[] EMPTY = new IndexElement[0];
263
264         /** Default constructor
265          */

266         public Index() {
267         }
268         
269         /** Creates a new index.
270          * @param el table element memory impl to work in
271          */

272         public Index(DBElement.Memory el) {
273             super(el, DBElementProperties.PROP_INDEXES, EMPTY);
274         }
275
276         /** Clones the object.
277          * @param obj object to clone
278          * @return cloned object
279          */

280         protected DBMemberElement clone(Object JavaDoc obj) {
281             return new IndexElement(new IndexElement.Memory((IndexElement)obj), getTableElement());
282         }
283     }
284
285     /** Collection of keys.
286      */

287     static class Key extends Member {
288         private static final KeyElement[] EMPTY = new KeyElement[0];
289
290         /** Default constructor
291          */

292         public Key() {
293         }
294
295         /** Creates a new key.
296          * @param el table element memory impl to work in
297          */

298         public Key(DBElement.Memory el) {
299             super(el, DBElementProperties.PROP_KEYS, EMPTY);
300         }
301
302         /** Clones the object.
303          * @param obj object to clone
304          * @return cloned object
305          */

306         protected DBMemberElement clone(Object JavaDoc obj) {
307             return null;
308         }
309     }
310 }
311
Popular Tags