001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema;
043:
044: import java.util.*;
045:
046: /** Support class that manages set of objects and fires events
047: */
048: class DBMemoryCollection {
049: /** Object to fire info about changes to */
050: protected DBElement.Memory/*TableElement.Memory*/_memory;
051:
052: /** name of property to fire */
053: private String _propertyName;
054:
055: /** array template to return */
056: private Object[] _template;
057:
058: private DBElement[] _elms;
059:
060: /** Default constructor
061: */
062: public DBMemoryCollection() {
063: }
064:
065: /** Creates a new collection.
066: * @param memory memory element to fire changes
067: * @param propertyName name of property to fire when array changes
068: * @param emptyArray emptyArray instance that provides the type of arrays that should be returned by toArray method
069: */
070: public DBMemoryCollection(DBElement.Memory memory,
071: String propertyName, Object[] emptyArray) {
072: _memory = memory;
073: _propertyName = propertyName;
074: _template = emptyArray;
075: }
076:
077: /** Changes the content of this object.
078: * @param arr array of objects to change
079: * @param action the action to do
080: */
081: public void change(DBElement[] arr, int action) {
082: change(Arrays.asList(arr), action);
083: }
084:
085: /** Changes the content of this object.
086: * @param c collection of objects to change
087: * @param action the action to do
088: */
089: protected void change(List c, int action) {
090: boolean hasChange = false;
091:
092: try {
093: DBElement[] oldElements = getElements();
094: int oldLength = (oldElements == null) ? 0
095: : oldElements.length;
096: int newLength = (c == null) ? 0 : c.size();
097: List list = null;
098:
099: switch (action) {
100: case DBElement.Impl.ADD:
101: if (newLength > 0) {
102: list = ((oldLength == 0) ? new ArrayList()
103: : new ArrayList(Arrays.asList(oldElements)));
104: list.addAll(c);
105: hasChange = true;
106: }
107: break;
108: case TableElement.Impl.SET:
109: list = c;
110: hasChange = true;
111: break;
112: case TableElement.Impl.REMOVE:
113: if (newLength > 0 && oldLength > 0) {
114: list = new ArrayList(Arrays.asList(oldElements));
115: list.removeAll(c);
116: hasChange = true;
117: }
118: break;
119: }
120: if (hasChange)
121: _elms = (DBElement[]) list.toArray(_template);
122: } finally {
123: if (hasChange)
124: _memory.firePropertyChange(_propertyName, null, null);
125: }
126: }
127:
128: /** Returns an array containing all of the elements in this collection.
129: * @return an array containing all of the elements in this collection
130: */
131: public DBElement[] getElements() {
132: if (_elms != null)
133: return _elms;
134: else
135: return (DBElement[]) Arrays.asList(_template).toArray(
136: new DBElement[_template.length]);
137: }
138:
139: /** Returns an element specified by the name from this collection.
140: * @return an element
141: */
142: public DBElement getElement(DBIdentifier name) {
143: DBElement[] elms = getElements();
144: int count = ((elms != null) ? elms.length : 0);
145:
146: for (int i = 0; i < count; i++) {
147: DBElement elm = elms[i];
148: if (name.getName().equals(elm.getName().getName()))
149: return elm;
150: }
151:
152: return null;
153: }
154:
155: /** Collection for members. Assignes to each class its members.
156: */
157: static abstract class Member extends DBMemoryCollection {
158: /** Default constructor.
159: */
160: public Member() {
161: }
162:
163: /** Creates a new member.
164: * @param memory memory element to fire changes to
165: * @param propertyName name of property to fire when array changes
166: * @param emptyArray emptyArray instance that provides the type of
167: * arrays that should be returned by toArray method
168: */
169: public Member(DBElement.Memory memory, String propertyName,
170: Object[] emptyArray) {
171: super (memory, propertyName, emptyArray);
172: }
173:
174: /** Gets a table element.
175: * @return a table element
176: */
177: protected TableElement getTableElement() {
178: if (_memory instanceof TableElement.Memory)
179: return ((TableElement.Memory) _memory)
180: .getTableElement();
181:
182: if (_memory instanceof DBMemberElement.Memory)
183: return ((DBMemberElement) ((DBMemberElement.Memory) _memory)._element)
184: .getDeclaringTable();
185:
186: return null;
187: }
188:
189: /** Clones the object.
190: * @param obj object to clone
191: * @return cloned object
192: */
193: protected abstract DBMemberElement clone(Object obj);
194: }
195:
196: /** Collection of tables.
197: */
198: static class Table extends DBMemoryCollection {
199: private static final TableElement[] EMPTY = new TableElement[0];
200:
201: /** Default constructor
202: */
203: public Table() {
204: }
205:
206: /** Creates a new table.
207: * @param el table element memory impl to work in
208: */
209: public Table(DBElement.Memory el) {
210: super (el, DBElementProperties.PROP_TABLES, EMPTY);
211: }
212:
213: /** Clones the object.
214: * @param obj object to clone
215: * @return cloned object
216: */
217: protected TableElement clone(Object obj) {
218: return new TableElement(new TableElement.Memory(
219: (TableElement) obj), getSchemaElement());
220: }
221:
222: /** Gets a schema element.
223: * @return a schema element
224: */
225: protected SchemaElement getSchemaElement() {
226: if (_memory instanceof SchemaElement.Memory)
227: return ((SchemaElement.Memory) _memory)
228: .getSchemaElement();
229:
230: if (_memory instanceof TableElement.Memory)
231: return ((TableElement) ((TableElement.Memory) _memory)._element)
232: .getDeclaringSchema();
233:
234: return null;
235: }
236: }
237:
238: /** Collection of columns.
239: */
240: static class Column extends Member {
241: private static final ColumnElement[] EMPTY = new ColumnElement[0];
242:
243: /** Default constructor
244: */
245: public Column() {
246: }
247:
248: /** Creates a new column.
249: * @param el table element memory impl to work in
250: */
251: public Column(DBElement.Memory el) {
252: super (el, DBElementProperties.PROP_COLUMNS, EMPTY);
253: }
254:
255: /** Clones the object.
256: * @param obj object to clone
257: * @return cloned object
258: */
259: protected DBMemberElement clone(Object obj) {
260: return new ColumnElement(new ColumnElement.Memory(
261: (ColumnElement) obj), getTableElement());
262: }
263: }
264:
265: /** Collection of column pairs.
266: */
267: static class ColumnPair extends Member {
268: private static final ColumnPairElement[] EMPTY = new ColumnPairElement[0];
269:
270: /** Default constructor
271: */
272: public ColumnPair() {
273: }
274:
275: /** Creates a new column pair.
276: * @param el table element memory impl to work in
277: */
278: public ColumnPair(DBElement.Memory el) {
279: super (el, DBElementProperties.PROP_COLUMN_PAIRS, EMPTY);
280: }
281:
282: /** Clones the object.
283: * @param obj object to clone
284: * @return cloned object
285: */
286: protected DBMemberElement clone(Object obj) {
287: // return new ColumnPairElement(new ColumnPairElement.Memory((ColumnPairElement)obj), null, null, getTableElement());
288: return null;
289: }
290: }
291:
292: /** Collection of indexes.
293: */
294: static class Index extends Member {
295: private static final IndexElement[] EMPTY = new IndexElement[0];
296:
297: /** Default constructor
298: */
299: public Index() {
300: }
301:
302: /** Creates a new index.
303: * @param el table element memory impl to work in
304: */
305: public Index(DBElement.Memory el) {
306: super (el, DBElementProperties.PROP_INDEXES, EMPTY);
307: }
308:
309: /** Clones the object.
310: * @param obj object to clone
311: * @return cloned object
312: */
313: protected DBMemberElement clone(Object obj) {
314: return new IndexElement(new IndexElement.Memory(
315: (IndexElement) obj), getTableElement());
316: }
317: }
318:
319: /** Collection of keys.
320: */
321: static class Key extends Member {
322: private static final KeyElement[] EMPTY = new KeyElement[0];
323:
324: /** Default constructor
325: */
326: public Key() {
327: }
328:
329: /** Creates a new key.
330: * @param el table element memory impl to work in
331: */
332: public Key(DBElement.Memory el) {
333: super (el, DBElementProperties.PROP_KEYS, EMPTY);
334: }
335:
336: /** Clones the object.
337: * @param obj object to clone
338: * @return cloned object
339: */
340: protected DBMemberElement clone(Object obj) {
341: return null;
342: }
343: }
344: }
|