001: package org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel;
002:
003: import org.apache.ojb.tools.mapping.reversedb2.events.StatusMessageListener;
004:
005: /* Copyright 2002-2005 The Apache Software Foundation
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: /**
021: * TreeModel representing the metadata of the database. Root element of this
022: * model is a DBMetaRootNode.
023: *
024: *
025: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
026: * @version $Id: DatabaseMetaDataTreeModel.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
027: */
028:
029: public class DatabaseMetaDataTreeModel extends
030: javax.swing.tree.DefaultTreeModel implements
031: java.io.Serializable {
032: static final long serialVersionUID = -8045409456918534509L;
033: transient private java.sql.DatabaseMetaData dbMetadata;
034:
035: /** Creates a new instance of DatabaseMetaDataTreeModel. The
036: * model represents the metadata specified by pdbMetadata
037: * @param pdbMetadata the metadata this model represents.
038: * @param pStatusBar a JTextComponent that takes status messages
039: * of this model. This model sometimes needs
040: * some time to finish a request, the status
041: * bar indicates what the model is doing.
042: */
043: public DatabaseMetaDataTreeModel(
044: java.sql.DatabaseMetaData pdbMetadata) {
045: super (new javax.swing.tree.DefaultMutableTreeNode(
046: "initializing"));
047: this .dbMetadata = pdbMetadata;
048: DBMetaRootNode rootNode = new DBMetaRootNode(dbMetadata, this );
049: super .setRoot(rootNode);
050: rootNode.loadWait(true, false, true);
051: }
052:
053: /** Set a status message in the JTextComponent passed to this
054: * model.
055: * @param message The message that should be displayed.
056: */
057: public void setStatusBarMessage(final String message) {
058: // Guaranteed to return a non-null array
059: Object[] listeners = listenerList.getListenerList();
060: // Process the listeners last to first, notifying
061: // those that are interested in this event
062: for (int i = listeners.length - 2; i >= 0; i -= 2) {
063: if (listeners[i] == StatusMessageListener.class) {
064: ((StatusMessageListener) listeners[i + 1])
065: .statusMessageReceived(message);
066: }
067: }
068: }
069:
070: /** Add a listener that receives status messages from
071: * this model.
072: * @param listener The listener that should receive the status messsages
073: */
074: public void addStatusMessageListener(StatusMessageListener listener) {
075: listenerList.add(StatusMessageListener.class, listener);
076: }
077:
078: /** Remove a listener that receives status messages from
079: * this model.
080: * @param listener The listener that shall be removed
081: */
082: public void removeStatusMessageListener(
083: StatusMessageListener listener) {
084: listenerList.remove(StatusMessageListener.class, listener);
085: }
086:
087: /** Method for reporting SQLException. This is used by
088: * the treenodes if retrieving information for a node
089: * is not successful.
090: * @param message The message describing where the error occurred
091: * @param sqlEx The exception to be reported.
092: */
093: public void reportSqlError(String message,
094: java.sql.SQLException sqlEx) {
095: StringBuffer strBufMessages = new StringBuffer();
096: java.sql.SQLException currentSqlEx = sqlEx;
097: do {
098: strBufMessages.append("\n" + sqlEx.getErrorCode() + ":"
099: + sqlEx.getMessage());
100: currentSqlEx = currentSqlEx.getNextException();
101: } while (currentSqlEx != null);
102: System.err.println(message + strBufMessages.toString());
103: sqlEx.printStackTrace();
104: }
105: }
|