KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Describes a key in a table.
23  */

24 public abstract class KeyElement extends DBMemberElement implements ColumnElementHolder {
25     /** Creates a new key element represented in memory.
26      */

27     public KeyElement() {
28         this(new Memory(), null);
29     }
30
31     /** Creates a new key element.
32      * @param impl the pluggable implementation
33      * @param declaringTable declaring table of this key, or
34      * <code>null</code>
35      */

36     public KeyElement(KeyElement.Impl impl, TableElement declaringTable) {
37         super(impl, declaringTable);
38     }
39
40     /** Returns the implementation for the key element.
41      * @return implementation for the key element
42      */

43     final Impl getKeyImpl() {
44         return (Impl)getElementImpl();
45     }
46
47     //================== Columns ===============================
48

49     /** Adds a new column to the key.
50      * @param el the column to add
51      * @throws DBException if impossible
52      */

53     public void addColumn(ColumnElement el) throws DBException {
54         addColumns(new ColumnElement[]{el});
55     }
56
57     /** Adds some new columns to the key.
58      * @param els the columns to add
59      * @throws DBException if impossible
60      */

61     public void addColumns(final ColumnElement[] els) throws DBException {
62         for (int i = 0; i < els.length; i++)
63             if (getColumn(els[i].getName()) != null)
64                 throwAddException("FMT_EXC_AddColumn", els[i]); //NOI18N
65

66         getKeyImpl().changeColumns(els, TableElement.Impl.ADD);
67     }
68
69     /** Removes a column from the key.
70      * @param el the column to remove
71      * @throws DBException if impossible
72      */

73     public void removeColumn(ColumnElement el) throws DBException {
74         removeColumns(new ColumnElement[]{el});
75     }
76
77     /** Removes some columns from the key.
78      * @param els the columns to remove
79      * @throws DBException if impossible
80      */

81     public void removeColumns(final ColumnElement[] els) throws DBException {
82         getKeyImpl().changeColumns(els, TableElement.Impl.REMOVE);
83     }
84
85     /** Sets the columns for this key.
86      * Previous columns are removed.
87      * @param els the new columns
88      * @throws DBException if impossible
89      */

90     public void setColumns(ColumnElement[] els) throws DBException {
91         getKeyImpl().changeColumns(els, TableElement.Impl.SET);
92     }
93
94     /** Gets all columns in this key.
95      * @return the columns
96      */

97     public ColumnElement[] getColumns() {
98         return getKeyImpl().getColumns();
99     }
100
101     /** Finds a column by name.
102      * @param name the name of the column for which to look
103      * @return the element or <code>null</code> if not found
104      */

105     public ColumnElement getColumn(DBIdentifier name) {
106         return getKeyImpl().getColumn(name);
107     }
108     
109     /** This method just throws localized exception. It is used during
110      * adding class element, which already exists in source.
111      * @param formatKey The message format key to localized bundle.
112      * @param element The element which can't be added
113      * @exception DBException is alway thrown from this method.
114      */

115     private void throwAddException(String JavaDoc formatKey, ColumnElement element) throws DBException {
116         //MessageFormat format = new MessageFormat(ElementFormat.bundle.getString(formatKey));
117
String JavaDoc msg = /*format.format(new Object[] { */element.getName().getName();// });
118
throw new DBException(msg);
119     }
120
121     /** Implementation of an key element.
122      * @see KeyElement
123      */

124     public interface Impl extends DBMemberElement.Impl {
125         /** Changes the set of columns.
126         * @param elems the columns to change
127         * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
128         * @exception DBException if the action cannot be handled
129         */

130         public void changeColumns(ColumnElement[] elems, int action) throws DBException;
131
132         /** Gets all columns.
133         * @return the columns
134         */

135         public ColumnElement[] getColumns();
136
137         /** Finds a column by name.
138         * @param name the name for which to look
139         * @return the column, or <code>null</code> if it does not exist
140         */

141         public ColumnElement getColumn(DBIdentifier name);
142     }
143
144     static class Memory extends DBMemberElement.Memory implements Impl {
145         /** collection of columns */
146         private DBMemoryCollection.Column columns;
147
148         /** Default constructor
149          */

150         Memory() {
151         }
152
153         /** Copy constructor.
154         * @param column the object from which to read values
155         */

156         Memory(KeyElement key) {
157             super(key);
158         }
159
160         /** Changes set of elements.
161          * @param elems elements to change
162          * @exception SourceException if the action cannot be handled
163          */

164         public synchronized void changeColumns(ColumnElement[] elems, int action) {
165             initColumns();
166             columns.change(elems, action);
167         }
168
169         /** Gets all columns.
170         * @return the columns
171         */

172         public synchronized ColumnElement[] getColumns() {
173             initColumns();
174             return (ColumnElement[])columns.getElements();
175         }
176
177         /** Finds a column with given name.
178          * @param name the name of column for which to look
179          * @return the element or null if column with such name does not exist
180          */

181         public synchronized ColumnElement getColumn(DBIdentifier name) {
182             initColumns();
183             return (ColumnElement)columns.getElement(name);
184         }
185
186         /** Initializes the collection of columns.
187          */

188         void initColumns() {
189             if (columns == null)
190                 columns = new DBMemoryCollection.Column(this);
191         }
192     }
193 }
194
Popular Tags