KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > api > explorer > DatabaseMetaDataTransfer


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.db.api.explorer;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import org.netbeans.api.db.explorer.DatabaseConnection;
24 import org.netbeans.api.db.explorer.JDBCDriver;
25 import org.netbeans.modules.db.explorer.DbMetaDataTransferProvider;
26
27 /**
28  * This class contains data flavors and classes for transferring database metadata
29  * from the Database Explorer. The current version only supports transferring
30  * database tables. Support for other database objects, such as columns or views
31  * might be added in the future.
32  *
33  * @author Andrei Badea, Jan Stola
34  *
35  * @since 1.1
36  */

37 public final class DatabaseMetaDataTransfer {
38
39     /**
40      * The {@link DataFlavor} representing a database connection.
41      */

42     public static DataFlavor JavaDoc CONNECTION_FLAVOR;
43
44     /**
45      * The {@link DataFlavor} representing a database table.
46      */

47     public static DataFlavor JavaDoc TABLE_FLAVOR;
48
49     /**
50      * The {@link DataFlavor} representing a database view.
51      */

52     public static DataFlavor JavaDoc VIEW_FLAVOR;
53
54     /**
55      * The {@link DataFlavor} representing a database column.
56      */

57     public static DataFlavor JavaDoc COLUMN_FLAVOR;
58
59     /**
60      * The implementation of DbMetaDataTransferProvider, returned by
61      * {@link #getProvider}.
62      */

63     private static DbMetaDataTransferProvider PROVIDER;
64
65     static {
66         try {
67             CONNECTION_FLAVOR = new DataFlavor JavaDoc("application/x-java-netbeans-dbexplorer-connection;class=org.netbeans.modules.db.api.explorer.DatabaseMetaDataTransfer$Connection"); // NOI18N
68
TABLE_FLAVOR = new DataFlavor JavaDoc("application/x-java-netbeans-dbexplorer-table;class=org.netbeans.modules.db.api.explorer.DatabaseMetaDataTransfer$Table"); // NOI18N
69
VIEW_FLAVOR = new DataFlavor JavaDoc("application/x-java-netbeans-dbexplorer-view;class=org.netbeans.modules.db.api.explorer.DatabaseMetaDataTransfer$View"); // NOI18N
70
COLUMN_FLAVOR = new DataFlavor JavaDoc("application/x-java-netbeans-dbexplorer-column;class=org.netbeans.modules.db.api.explorer.DatabaseMetaDataTransfer$Column"); // NOI18N
71
} catch (ClassNotFoundException JavaDoc e) {
72             throw new AssertionError JavaDoc(e);
73         }
74     }
75
76     /**
77      * This method is referenced from the layer to register the provider
78      * implementation.
79      */

80     private static synchronized DbMetaDataTransferProvider getProvider() {
81         if (PROVIDER == null) {
82             PROVIDER = new DbMetaDataTransferProviderImpl();
83         }
84         return PROVIDER;
85     }
86
87     /**
88      * Represents a database connection during a drag and drop transfer.
89      */

90     public static final class Connection {
91
92         private final DatabaseConnection dbconn;
93         private final JDBCDriver jdbcDriver;
94
95         private Connection(DatabaseConnection dbconn, JDBCDriver jdbcDriver) {
96             this.dbconn = dbconn;
97             this.jdbcDriver = jdbcDriver;
98         }
99
100         /**
101          * Returns the {@link DatabaseConnection}.
102          */

103         public DatabaseConnection getDatabaseConnection() {
104             return dbconn;
105         }
106
107         /**
108          * Returns the {@link JDBCDriver} which was used to connect to {@link #getDatabaseConnection}.
109          */

110         public JDBCDriver getJDBCDriver() {
111             return jdbcDriver;
112         }
113
114         public String JavaDoc toString() {
115             return "Connection[databaseConnection=" + dbconn + ",jdbcDriver=" + jdbcDriver + "]"; // NOI18N
116
}
117     }
118
119     /**
120      * Represents a table during a drag and drop transfer.
121      */

122     public static final class Table {
123
124         private final DatabaseConnection dbconn;
125         private final JDBCDriver jdbcDriver;
126         private final String JavaDoc tableName;
127
128         private Table(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc tableName) {
129             this.dbconn = dbconn;
130             this.jdbcDriver = jdbcDriver;
131             this.tableName = tableName;
132         }
133
134         /**
135          * Returns the {@link DatabaseConnection} this table comes from.
136          */

137         public DatabaseConnection getDatabaseConnection() {
138             return dbconn;
139         }
140
141         /**
142          * Returns the {@link JDBCDriver} which was used to connect to {@link #getDatabaseConnection}.
143          */

144         public JDBCDriver getJDBCDriver() {
145             return jdbcDriver;
146         }
147
148         /**
149          * Returns the name of this table.
150          */

151         public String JavaDoc getTableName() {
152             return tableName;
153         }
154
155         public String JavaDoc toString() {
156             return "Table[databaseConnection=" + dbconn + ",jdbcDriver=" + jdbcDriver + ",tableName=" + tableName + "]"; // NOI18N
157
}
158     }
159
160     /**
161      * Represents a view during a drag and drop transfer.
162      */

163     public static final class View {
164
165         private final DatabaseConnection dbconn;
166         private final JDBCDriver jdbcDriver;
167         private final String JavaDoc viewName;
168
169         private View(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc viewName) {
170             this.dbconn = dbconn;
171             this.jdbcDriver = jdbcDriver;
172             this.viewName = viewName;
173         }
174
175         /**
176          * Returns the {@link DatabaseConnection} this view comes from.
177          */

178         public DatabaseConnection getDatabaseConnection() {
179             return dbconn;
180         }
181
182         /**
183          * Returns the {@link JDBCDriver} which was used to connect to {@link #getDatabaseConnection}.
184          */

185         public JDBCDriver getJDBCDriver() {
186             return jdbcDriver;
187         }
188
189         /**
190          * Returns the name of this view.
191          */

192         public String JavaDoc getViewName() {
193             return viewName;
194         }
195
196         public String JavaDoc toString() {
197             return "View[databaseConnection=" + dbconn + ",jdbcDriver=" + jdbcDriver + ",viewName=" + viewName + "]"; // NOI18N
198
}
199     }
200
201     /**
202      * Represents a column during a drag and drop transfer.
203      */

204     public static final class Column {
205
206         private final DatabaseConnection dbconn;
207         private final JDBCDriver jdbcDriver;
208         private final String JavaDoc tableName;
209         private final String JavaDoc columnName;
210
211         private Column(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc tableName, String JavaDoc columnName) {
212             this.dbconn = dbconn;
213             this.jdbcDriver = jdbcDriver;
214             this.tableName = tableName;
215             this.columnName = columnName;
216         }
217
218         /**
219          * Returns the {@link DatabaseConnection} this column comes from.
220          */

221         public DatabaseConnection getDatabaseConnection() {
222             return dbconn;
223         }
224
225         /**
226          * Returns the {@link JDBCDriver} which was used to connect to {@link #getDatabaseConnection}.
227          */

228         public JDBCDriver getJDBCDriver() {
229             return jdbcDriver;
230         }
231
232         /**
233          * Returns the name of the table that contains this column.
234          */

235         public String JavaDoc getTableName() {
236             return tableName;
237         }
238
239         /**
240          * Returns the name of this column.
241          */

242         public String JavaDoc getColumnName() {
243             return columnName;
244         }
245
246         public String JavaDoc toString() {
247             return "Column[databaseConnection=" + dbconn + ",jdbcDriver=" + jdbcDriver + ",tableName=" + tableName + ",columnName=" + columnName + "]"; // NOI18N
248
}
249     }
250
251     /**
252      * The implementation of DbMetaDataTransferProvider, registered in the
253      * default lookup and used by the Database Explorer.
254      */

255     private static final class DbMetaDataTransferProviderImpl implements DbMetaDataTransferProvider {
256
257         public DataFlavor JavaDoc getConnectionDataFlavor() {
258             return CONNECTION_FLAVOR;
259         }
260
261         public DataFlavor JavaDoc getTableDataFlavor() {
262             return TABLE_FLAVOR;
263         }
264
265         public DataFlavor JavaDoc getViewDataFlavor() {
266             return VIEW_FLAVOR;
267         }
268
269
270         public DataFlavor JavaDoc getColumnDataFlavor() {
271             return COLUMN_FLAVOR;
272         }
273
274         public Object JavaDoc createConnectionData(DatabaseConnection dbconn, JDBCDriver jdbcDriver) {
275             return new Connection(dbconn, jdbcDriver);
276         }
277
278         public Object JavaDoc createTableData(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc tableName) {
279             return new Table(dbconn, jdbcDriver, tableName);
280         }
281
282         public Object JavaDoc createViewData(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc viewName) {
283             return new View(dbconn, jdbcDriver, viewName);
284         }
285
286         public Object JavaDoc createColumnData(DatabaseConnection dbconn, JDBCDriver jdbcDriver, String JavaDoc tableName, String JavaDoc columnName) {
287             return new Column(dbconn, jdbcDriver, tableName, columnName);
288         }
289     }
290 }
291
Popular Tags