KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResourceBundle JavaDoc;
23
24 /** Describes an index in a table.
25  */

26 public final class IndexElement extends DBMemberElement implements ColumnElementHolder {
27     /** Creates a new index element represented in memory.
28      */

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

38     public IndexElement(Impl impl, TableElement declaringTable) {
39         super(impl, declaringTable);
40     }
41
42     /** Returns the implementation for the index.
43      * @return implementation for the index
44      */

45     final Impl getIndexImpl() {
46         return (Impl)getElementImpl();
47     }
48
49     /** Gets the unique flag of the index.
50      * @return true if it is a unique index, false otherwise
51      */

52     public boolean isUnique() {
53         return getIndexImpl().isUnique();
54     }
55
56     /** Sets the unique flag of the index.
57      * @param flag the flag
58      * @throws DBException if impossible
59      */

60     public void setUnique(boolean flag) throws DBException {
61         getIndexImpl().setUnique(flag);
62     }
63
64     //================== Columns ===============================
65

66     /** Adds a new column to the index.
67      * @param el the column to add
68      * @throws DBException if impossible
69      */

70     public void addColumn(ColumnElement el) throws DBException {
71         addColumns(new ColumnElement[]{el});
72     }
73
74     /** Adds some new columns to the index.
75      * @param els the columns to add
76      * @throws DBException if impossible
77      */

78     public void addColumns(final ColumnElement[] els) throws DBException {
79         for (int i = 0; i < els.length; i++)
80             if (getColumn(els[i].getName()) != null)
81                 throwAddException("FMT_EXC_AddColumn", els[i]); //NOI18N
82

83         getIndexImpl().changeColumns(els, TableElement.Impl.ADD);
84     }
85
86     /** Removes a column from the index.
87      * @param el the column to remove
88      * @throws DBException if impossible
89      */

90     public void removeColumn (ColumnElement el) throws DBException {
91         removeColumns(new ColumnElement[]{el});
92     }
93
94     /** Removes some columns from the index.
95      * @param els the columns to remove
96      * @throws DBException if impossible
97      */

98     public void removeColumns (final ColumnElement[] els) throws DBException {
99         getIndexImpl().changeColumns(els, TableElement.Impl.REMOVE);
100     }
101
102     /** Sets the columns for this index.
103      * Previous columns are removed.
104      * @param els the new columns
105      * @throws DBException if impossible
106      */

107     public void setColumns (ColumnElement[] els) throws DBException {
108         if (els == null)
109             throw new NullPointerException JavaDoc(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("NulIndexes")); //NOI18N
110

111         getIndexImpl().changeColumns(els, TableElement.Impl.SET);
112     }
113
114     /** Gets all columns in this index.
115      * @return the columns
116      */

117     public ColumnElement[] getColumns () {
118         return getIndexImpl().getColumns();
119     }
120
121     /** Find a column by name.
122      * @param name the name of the column for which to look
123      * @return the element or <code>null</code> if not found
124      */

125     public ColumnElement getColumn (DBIdentifier name) {
126         return getIndexImpl().getColumn(name);
127     }
128     
129     /** This method just throws localized exception. It is used during
130      * adding class element, which already exists in source.
131      * @param formatKey The message format key to localized bundle.
132      * @param element The element which can't be added
133      * @exception DBException is alway thrown from this method.
134      */

135     private void throwAddException (String JavaDoc formatKey, ColumnElement element) throws DBException {
136         //MessageFormat format = new MessageFormat(ElementFormat.bundle.getString(formatKey));
137
String JavaDoc msg = /*format.format(new Object[] { */element.getName().getName();// });
138
throw new DBException(msg);
139     }
140
141     /** Implementation of an index element.
142      * @see IndexElement
143      */

144     public interface Impl extends DBMemberElement.Impl {
145         /** Gets the unique flag of the index.
146          * @return true if it is a unique index, false otherwise
147          */

148         public boolean isUnique ();
149
150         /** Sets the unique flag of the index.
151          * @param flag the flag
152          * @throws DBException if impossible
153          */

154         public void setUnique (boolean flag) throws DBException;
155
156         /** Changes the set of columns.
157         * @param elems the columns to change
158         * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
159         * @exception DBException if the action cannot be handled
160         */

161         public void changeColumns (ColumnElement[] elems, int action) throws DBException;
162
163         /** Gets all columns.
164         * @return the columns
165         */

166         public ColumnElement[] getColumns ();
167
168         /** Finds a column by name.
169         * @param name the name for which to look
170         * @return the column, or <code>null</code> if it does not exist
171         */

172         public ColumnElement getColumn (DBIdentifier name);
173     }
174
175     static class Memory extends DBMemberElement.Memory implements Impl {
176         /** Unique flag of index */
177         private boolean _unique;
178
179         /** collection of columns */
180         private DBMemoryCollection.Column columns;
181
182         /** Default constructor
183          */

184         Memory() {
185             super();
186             _unique = true;
187         }
188
189         /** Copy constructor.
190         * @param column the object from which to read values
191         */

192         Memory(IndexElement index) {
193             super(index);
194             _unique = index.isUnique();
195         }
196
197         /** Gets the unique flag of the index.
198          * @return true if it is a unique index, false otherwise
199          */

200         public boolean isUnique() {
201             return _unique;
202         }
203
204         /** Sets the unique flag of the index.
205          * @param flag the flag
206          */

207         public void setUnique(boolean flag) {
208             boolean old = _unique;
209
210             _unique = flag;
211             firePropertyChange(PROP_UNIQUE, Boolean.valueOf(old), Boolean.valueOf(flag));
212         }
213
214         /** Changes the set of elements.
215          * @param elems elements to change
216          * @param action the action to do
217          */

218         public synchronized void changeColumns(ColumnElement[] elems, int action) {
219             initColumns();
220             columns.change(elems, action);
221         }
222
223         /** Gets all columns.
224         * @return the columns
225         */

226         public synchronized ColumnElement[] getColumns() {
227             initColumns();
228             return (ColumnElement[])columns.getElements();
229         }
230
231         /** Finds a column with given name.
232          * @param name the name of column for which to look
233          * @return the element or null if column with such name does not exist
234          */

235         public synchronized ColumnElement getColumn(DBIdentifier name) {
236             initColumns();
237             return (ColumnElement)columns.getElement(name);
238         }
239
240         /** Initializes the collection of columns.
241          */

242         void initColumns() {
243             if (columns == null)
244                 columns = new DBMemoryCollection.Column(this);
245         }
246     }
247 }
248
Popular Tags