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.ResourceBundle;
045:
046: /** Describes an index in a table.
047: */
048: public final class IndexElement extends DBMemberElement implements
049: ColumnElementHolder {
050: /** Creates a new index element represented in memory.
051: */
052: public IndexElement() {
053: this (new Memory(), null);
054: }
055:
056: /** Creates a new index element.
057: * @param impl the pluggable implementation
058: * @param declaringTable declaring table of this index, or
059: * <code>null</code>
060: */
061: public IndexElement(Impl impl, TableElement declaringTable) {
062: super (impl, declaringTable);
063: }
064:
065: /** Returns the implementation for the index.
066: * @return implementation for the index
067: */
068: final Impl getIndexImpl() {
069: return (Impl) getElementImpl();
070: }
071:
072: /** Gets the unique flag of the index.
073: * @return true if it is a unique index, false otherwise
074: */
075: public boolean isUnique() {
076: return getIndexImpl().isUnique();
077: }
078:
079: /** Sets the unique flag of the index.
080: * @param flag the flag
081: * @throws DBException if impossible
082: */
083: public void setUnique(boolean flag) throws DBException {
084: getIndexImpl().setUnique(flag);
085: }
086:
087: //================== Columns ===============================
088:
089: /** Adds a new column to the index.
090: * @param el the column to add
091: * @throws DBException if impossible
092: */
093: public void addColumn(ColumnElement el) throws DBException {
094: addColumns(new ColumnElement[] { el });
095: }
096:
097: /** Adds some new columns to the index.
098: * @param els the columns to add
099: * @throws DBException if impossible
100: */
101: public void addColumns(final ColumnElement[] els)
102: throws DBException {
103: for (int i = 0; i < els.length; i++)
104: if (getColumn(els[i].getName()) != null)
105: throwAddException("FMT_EXC_AddColumn", els[i]); //NOI18N
106:
107: getIndexImpl().changeColumns(els, TableElement.Impl.ADD);
108: }
109:
110: /** Removes a column from the index.
111: * @param el the column to remove
112: * @throws DBException if impossible
113: */
114: public void removeColumn(ColumnElement el) throws DBException {
115: removeColumns(new ColumnElement[] { el });
116: }
117:
118: /** Removes some columns from the index.
119: * @param els the columns to remove
120: * @throws DBException if impossible
121: */
122: public void removeColumns(final ColumnElement[] els)
123: throws DBException {
124: getIndexImpl().changeColumns(els, TableElement.Impl.REMOVE);
125: }
126:
127: /** Sets the columns for this index.
128: * Previous columns are removed.
129: * @param els the new columns
130: * @throws DBException if impossible
131: */
132: public void setColumns(ColumnElement[] els) throws DBException {
133: if (els == null)
134: throw new NullPointerException(ResourceBundle.getBundle(
135: "org.netbeans.modules.dbschema.resources.Bundle")
136: .getString("NulIndexes")); //NOI18N
137:
138: getIndexImpl().changeColumns(els, TableElement.Impl.SET);
139: }
140:
141: /** Gets all columns in this index.
142: * @return the columns
143: */
144: public ColumnElement[] getColumns() {
145: return getIndexImpl().getColumns();
146: }
147:
148: /** Find a column by name.
149: * @param name the name of the column for which to look
150: * @return the element or <code>null</code> if not found
151: */
152: public ColumnElement getColumn(DBIdentifier name) {
153: return getIndexImpl().getColumn(name);
154: }
155:
156: /** This method just throws localized exception. It is used during
157: * adding class element, which already exists in source.
158: * @param formatKey The message format key to localized bundle.
159: * @param element The element which can't be added
160: * @exception DBException is alway thrown from this method.
161: */
162: private void throwAddException(String formatKey,
163: ColumnElement element) throws DBException {
164: //MessageFormat format = new MessageFormat(ElementFormat.bundle.getString(formatKey));
165: String msg = /*format.format(new Object[] { */element
166: .getName().getName();// });
167: throw new DBException(msg);
168: }
169:
170: /** Implementation of an index element.
171: * @see IndexElement
172: */
173: public interface Impl extends DBMemberElement.Impl {
174: /** Gets the unique flag of the index.
175: * @return true if it is a unique index, false otherwise
176: */
177: public boolean isUnique();
178:
179: /** Sets the unique flag of the index.
180: * @param flag the flag
181: * @throws DBException if impossible
182: */
183: public void setUnique(boolean flag) throws DBException;
184:
185: /** Changes the set of columns.
186: * @param elems the columns to change
187: * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
188: * @exception DBException if the action cannot be handled
189: */
190: public void changeColumns(ColumnElement[] elems, int action)
191: throws DBException;
192:
193: /** Gets all columns.
194: * @return the columns
195: */
196: public ColumnElement[] getColumns();
197:
198: /** Finds a column by name.
199: * @param name the name for which to look
200: * @return the column, or <code>null</code> if it does not exist
201: */
202: public ColumnElement getColumn(DBIdentifier name);
203: }
204:
205: static class Memory extends DBMemberElement.Memory implements Impl {
206: /** Unique flag of index */
207: private boolean _unique;
208:
209: /** collection of columns */
210: private DBMemoryCollection.Column columns;
211:
212: /** Default constructor
213: */
214: Memory() {
215: super ();
216: _unique = true;
217: }
218:
219: /** Copy constructor.
220: * @param column the object from which to read values
221: */
222: Memory(IndexElement index) {
223: super (index);
224: _unique = index.isUnique();
225: }
226:
227: /** Gets the unique flag of the index.
228: * @return true if it is a unique index, false otherwise
229: */
230: public boolean isUnique() {
231: return _unique;
232: }
233:
234: /** Sets the unique flag of the index.
235: * @param flag the flag
236: */
237: public void setUnique(boolean flag) {
238: boolean old = _unique;
239:
240: _unique = flag;
241: firePropertyChange(PROP_UNIQUE, Boolean.valueOf(old),
242: Boolean.valueOf(flag));
243: }
244:
245: /** Changes the set of elements.
246: * @param elems elements to change
247: * @param action the action to do
248: */
249: public synchronized void changeColumns(ColumnElement[] elems,
250: int action) {
251: initColumns();
252: columns.change(elems, action);
253: }
254:
255: /** Gets all columns.
256: * @return the columns
257: */
258: public synchronized ColumnElement[] getColumns() {
259: initColumns();
260: return (ColumnElement[]) columns.getElements();
261: }
262:
263: /** Finds a column with given name.
264: * @param name the name of column for which to look
265: * @return the element or null if column with such name does not exist
266: */
267: public synchronized ColumnElement getColumn(DBIdentifier name) {
268: initColumns();
269: return (ColumnElement) columns.getElement(name);
270: }
271:
272: /** Initializes the collection of columns.
273: */
274: void initColumns() {
275: if (columns == null)
276: columns = new DBMemoryCollection.Column(this);
277: }
278: }
279: }
|