001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui.dnd;
024:
025: import java.awt.Component;
026: import java.awt.Cursor;
027: import java.sql.DatabaseMetaData;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import java.sql.Types;
031:
032: import org.isqlviewer.model.JdbcSchemaTreeModel;
033: import org.isqlviewer.model.SchemaNode;
034: import org.isqlviewer.model.SchemaNodeType;
035: import org.isqlviewer.sql.ResultSetRenderer;
036: import org.isqlviewer.util.LoggableObject;
037:
038: public class SchemaNodeRenderer extends LoggableObject implements
039: Runnable {
040:
041: private Component component = null;
042: private SchemaNode schemaNode = null;
043: private JdbcSchemaTreeModel treeModel = null;
044:
045: private ResultSetRenderer nodeRenderer = null;
046:
047: public SchemaNodeRenderer(ResultSetRenderer nodeRenderer,
048: JdbcSchemaTreeModel treeModel, Component component,
049: SchemaNode schemaNode) {
050:
051: this .nodeRenderer = nodeRenderer;
052: this .treeModel = treeModel;
053: this .component = component;
054: this .schemaNode = schemaNode;
055: }
056:
057: public void run() {
058:
059: renderSchemaPath(component, schemaNode);
060: }
061:
062: private void renderSchemaPath(Component owner, SchemaNode node) {
063:
064: boolean cursorSet = false;
065: if (owner != null) {
066: try {
067: owner.setCursor(Cursor
068: .getPredefinedCursor(Cursor.WAIT_CURSOR));
069: cursorSet = true;
070: } catch (Exception ignored) {
071: }
072: }
073:
074: try {
075:
076: DatabaseMetaData metadata = treeModel.getMetaData();
077: String catalog = treeModel.getCurrentCatlog();
078: String schema = treeModel.getCurrentSchema();
079:
080: SchemaNode parent = (SchemaNode) node.getParent();
081: String selection = node.getUserObject();
082: SchemaNodeType type = node.getNodeType();
083:
084: switch (type) {
085: case DATA_TYPE_SET:
086: modelResultSet(metadata.getTypeInfo());
087: break;
088: case TABLE_TYPE:
089: modelResultSet(metadata.getTables(catalog, schema,
090: null, null));
091: break;
092: case UDT_SET:
093: modelResultSet(metadata.getUDTs(catalog, schema, null,
094: null));
095: break;
096: case UDT_SET_DISTINCT:
097: modelResultSet(metadata.getUDTs(catalog, schema, null,
098: new int[] { Types.DISTINCT }));
099: break;
100: case UDT_SET_STRUCT:
101: modelResultSet(metadata.getUDTs(catalog, schema, null,
102: new int[] { Types.STRUCT }));
103: break;
104: case UDT_SET_JAVA_OBJECT:
105: modelResultSet(metadata.getUDTs(catalog, schema, null,
106: new int[] { Types.JAVA_OBJECT }));
107: break;
108: case UDT:
109: switch (parent.getNodeType()) {
110: case UDT_SET_DISTINCT:
111: modelResultSet(metadata.getUDTs(catalog, schema,
112: selection, new int[] { Types.DISTINCT }));
113: break;
114: case UDT_SET_STRUCT:
115: modelResultSet(metadata.getUDTs(catalog, schema,
116: selection, new int[] { Types.STRUCT }));
117: break;
118: case UDT_SET_JAVA_OBJECT:
119: modelResultSet(metadata.getUDTs(catalog, schema,
120: selection, new int[] { Types.JAVA_OBJECT }));
121: break;
122: default:
123: break;
124: }
125: break;
126: case PROCEDURE_SET:
127: modelResultSet(metadata.getProcedures(catalog, schema,
128: null));
129: break;
130: case PROCEDURE:
131: modelResultSet(metadata.getProcedures(catalog, schema,
132: selection));
133: break;
134: case COLUMN_SET:
135: modelResultSet(metadata.getColumns(catalog, schema,
136: parent.getUserObject(), null));
137: break;
138: case COLUMN:
139: String table = ((SchemaNode) parent.getParent())
140: .getUserObject();
141: modelResultSet(metadata.getColumns(catalog, schema,
142: table, selection));
143: break;
144: case INDEX:
145: modelResultSet(metadata.getIndexInfo(catalog, schema,
146: parent.getUserObject(), false, false));
147: break;
148: case PRIMARY_KEYS:
149: modelResultSet(metadata.getPrimaryKeys(catalog, schema,
150: parent.getUserObject()));
151: break;
152: case EXPORTED_KEYS:
153: modelResultSet(metadata.getExportedKeys(catalog,
154: schema, parent.getUserObject()));
155: break;
156: case IMPORTED_KEYS:
157: modelResultSet(metadata.getImportedKeys(catalog,
158: schema, parent.getUserObject()));
159: break;
160: case TABLE_PRIVILIGES:
161: modelResultSet(metadata.getTablePrivileges(catalog,
162: schema, parent.getUserObject()));
163: break;
164: case TABLE:
165: modelResultSet(metadata.getTables(catalog, schema,
166: selection, new String[] { parent
167: .getUserObject() }));
168: break;
169: case COLUMN_PRIVILIGES:
170: modelResultSet(metadata.getColumnPrivileges(catalog,
171: schema, parent.getUserObject(), null));
172: break;
173: case VERSION_COLUMNS:
174: modelResultSet(metadata.getVersionColumns(catalog,
175: schema, parent.getUserObject()));
176: break;
177: default:
178: org.isqlviewer.swing.SwingUtilities.beep();
179: break;
180: }
181: } catch (SQLException error) {
182: error(node.getNodeType(), error);
183: } finally {
184: if (cursorSet && owner != null) {
185: owner.setCursor(Cursor
186: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
187: }
188: }
189: }
190:
191: private void modelResultSet(ResultSet set) throws SQLException {
192:
193: try {
194: nodeRenderer.initialize();
195: if (set != null) {
196: nodeRenderer.statementInitialized("", set
197: .getStatement());
198: nodeRenderer.processResultSet("", set, "", 0);
199:
200: nodeRenderer.recieveResultsetWarnings(set, set
201: .getWarnings());
202: }
203: } finally {
204: nodeRenderer.finalizeStatement("");
205: try {
206: if (set != null) {
207: set.close();
208: }
209: } catch (Throwable t) {
210: }
211: }
212: }
213: }
|