KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 public final class UniqueKeyElement extends KeyElement {
25     /** the index to which this element is associated */
26     private IndexElement _associatedIndex;
27
28     /** Creates a new unique key element represented in memory.
29      */

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

39     public UniqueKeyElement(Impl impl, TableElement declaringTable, IndexElement associatedIndex) {
40         super(impl, declaringTable);
41
42         _associatedIndex = associatedIndex;
43     }
44
45     /** Returns the implementation for the unique key.
46      * @return implementation for the unique key
47      */

48     final Impl getUniqueKeyImpl() {
49         return (Impl)getElementImpl();
50     }
51
52     /** Gets the associated index of the unique key.
53      * @return the associated index for this unique key, <code>null</code>
54      * if unattached
55      */

56     public IndexElement getAssociatedIndex() {
57         return _associatedIndex;
58     }
59
60     /** Sets the associated index of the unique key.
61      * @param the associated index for this unique key
62      * @throws DBException if impossible
63      */

64     public void setAssociatedIndex(IndexElement index) throws DBException {
65         _associatedIndex = index;
66     }
67
68     /** Gets the primary key flag of the unique key.
69      * @return true if this unique key is a primary key, false otherwise
70      */

71     public boolean isPrimaryKey() {
72         return getUniqueKeyImpl().isPrimaryKey();
73     }
74
75     /** Sets the primary key flag of the unique key.
76      * @param flag the flag
77      * @throws DBException if impossible
78      */

79     public void setPrimaryKey (boolean flag) throws DBException {
80         getUniqueKeyImpl().setPrimaryKey(flag);
81     }
82
83     /** Implementation of a unique key element.
84      * @see KeyElement
85      */

86     public interface Impl extends KeyElement.Impl {
87         /** Gets the primary key flag of the unique key.
88          * @return true if this unique key is a primary key, false otherwise
89          */

90         public boolean isPrimaryKey ();
91
92         /** Sets the primary key flag of the unique key.
93          * @param flag the flag
94          * @throws DBException if impossible
95          */

96         public void setPrimaryKey (boolean flag) throws DBException;
97     }
98
99     static class Memory extends KeyElement.Memory implements Impl {
100         /** Primary key flag of key */
101         private boolean _pk;
102
103         /** Default constructor
104          */

105         Memory () {
106             _pk = false;
107         }
108
109         /** Copy constructor.
110         * @param column the object from which to read values
111         */

112         Memory (UniqueKeyElement key) {
113             super(key);
114             _pk = key.isPrimaryKey();
115         }
116
117         /** Gets the primary key flag of the unique key.
118          * @return true if this unique key is a primary key, false otherwise
119          */

120         public boolean isPrimaryKey() {
121             return _pk;
122         }
123
124         /** Sets the primary key flag of the unique key.
125          * @param flag the flag
126          * @throws DBException if impossible
127          */

128         public void setPrimaryKey (boolean flag) throws DBException {
129             boolean old = _pk;
130
131             _pk = flag;
132             firePropertyChange(PROP_PK, Boolean.valueOf(old), Boolean.valueOf(flag));
133         }
134     }
135 }
136
Popular Tags