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.event;
024:
025: import java.sql.Connection;
026: import java.util.EventObject;
027:
028: /**
029: * Standard event object for notifying actions of the DatabaseConnection.
030: * <p>
031: * This event object more or less works much like the WindowEvent object where there is an ID that correlates to the
032: * type of event is being propagated.
033: * <p>
034: * Most if not all the time the source of these events should be an instance of DatabaseConnection to help determine
035: * which connection these events are coming from.
036: *
037: * @see org.isqlviewer.event.DatabaseListener
038: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
039: * @version 1.0
040: */
041: public class DatabaseEvent extends EventObject {
042:
043: private static final long serialVersionUID = 6586214394143524988L;
044: /**
045: * Inidcates a disconnect from the database.
046: */
047: public final static int DISCONNECT = 0;
048: /**
049: * Indicates that a commit has succesfully occured.
050: */
051: public final static int COMMIT = 1;
052: /**
053: * Indicates a sucesful rollback has occured.
054: */
055: public final static int ROLLBACK = 2;
056: /**
057: * Indicates that the database connection has succesfuly initialized.
058: */
059: public final static int INITIALIZE = 3;
060: /**
061: * Indicates that the auto-commite state has changed.
062: */
063: public final static int AUTO_COMMIT_CHANGE = 4;
064: /**
065: * For when the catalog is manually updated.
066: */
067: public final static int CATALOG_CHANGE = 5;
068:
069: private int eventCode = -1;
070:
071: /**
072: * Default event constructor.
073: * <p>
074: * Basic event constructor with the event code. the code will be checked for validity if not a
075: * IllegalArgumentException will be thrown.
076: *
077: * @throws IllegalArgumentException if code is invalid.
078: * @param source of the event.
079: * @param code for this event.
080: */
081: public DatabaseEvent(Object source, int code) {
082:
083: super (source);
084: if (!(source instanceof Connection)) {
085: throw new IllegalArgumentException();
086: }
087: checkCode(code);
088: eventCode = code;
089: }
090:
091: /**
092: * Returns the code for this event.
093: * <p>
094: * This value will always be some constant value declared in this class.
095: *
096: * @return int representing the event.
097: */
098: public int getID() {
099:
100: return eventCode;
101: }
102:
103: public Connection getConnection() {
104:
105: return (Connection) getSource();
106: }
107:
108: protected static void checkCode(int code)
109: throws IllegalArgumentException {
110:
111: switch (code) {
112: case DISCONNECT:
113: case COMMIT:
114: case ROLLBACK:
115: case CATALOG_CHANGE:
116: case INITIALIZE:
117: case AUTO_COMMIT_CHANGE:
118: return;
119: default:
120: throw new IllegalArgumentException(Integer.toString(code));
121: }
122: }
123:
124: }
|