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.model;
024:
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.Transferable;
027: import java.awt.datatransfer.UnsupportedFlavorException;
028: import java.io.IOException;
029:
030: import javax.swing.tree.DefaultMutableTreeNode;
031:
032: /**
033: * TreeNode class for distinguinshing different nodes by a given type within a tree.
034: * <p>
035: * This node class should alleviate and provide more flexiblility to the structure of the JdbcSchema tree so that nodes
036: * can be indentified by an enumerated type instead of making a guess by the name of the node itself as to what data it
037: * actually represents in the JDBC Schema.
038: *
039: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
040: * @version 1.0
041: */
042: public final class SchemaNode extends DefaultMutableTreeNode implements
043: Transferable {
044:
045: public static final String MIME_TYPE = "java-x-object/org.isqlviewer.model.SchemaNode";
046: public static final DataFlavor DATA_FLAVOR = new DataFlavor(
047: SchemaNode.class, MIME_TYPE);
048:
049: private static final long serialVersionUID = 1L;
050: private SchemaNodeType nodeType = null;
051: private boolean hasError = false;
052:
053: private static DataFlavor[] supportedDataFlavors;
054:
055: static {
056: supportedDataFlavors = new DataFlavor[2];
057: supportedDataFlavors[0] = DATA_FLAVOR;
058: supportedDataFlavors[1] = DataFlavor.stringFlavor;
059: }
060:
061: /**
062: * Creates a tree node that has no parent and no children, but which allows children.
063: * <p>
064: *
065: * @param nodeType type of node this instance represents.
066: * @throws NullPointerException if nodeType is null.
067: */
068: public SchemaNode(SchemaNodeType nodeType) {
069:
070: this (null, true, nodeType);
071: }
072:
073: /**
074: * Creates a tree node with no parent, no children.
075: * <p>
076: * This instance will be one that allows children, and initializes it with the specified user object.
077: *
078: * @param userObject an Object provided by the user that constitutes the node's data
079: * @param nodeType type of node this instance represents.
080: * @throws NullPointerException if nodeType is null.
081: */
082: public SchemaNode(Object userObject, SchemaNodeType nodeType) {
083:
084: this (userObject, true, nodeType);
085: }
086:
087: /**
088: * Creates a tree node with no parent, no children.
089: * <p>
090: * This instance will be initialized with the specified user object, and that allows children only if specified.
091: *
092: * @param userObject an Object provided by the user that constitutes the node's data
093: * @param allowsChildren if true, the node is allowed to have child nodes -- otherwise, it is always a leaf node
094: * @param nodeType type of node this instance represents.
095: * @throws NullPointerException if nodeType is null.
096: */
097: public SchemaNode(Object userObject, boolean allowsChildren,
098: SchemaNodeType nodeType) {
099:
100: super (userObject, allowsChildren);
101: if (nodeType == null) {
102: throw new NullPointerException();
103: }
104: this .nodeType = nodeType;
105: }
106:
107: /**
108: * Gets the current type of schema node this represenents.
109: * <p>
110: *
111: * @return type of node within the JDBC Schema is being represented.
112: */
113: public SchemaNodeType getNodeType() {
114:
115: return nodeType;
116: }
117:
118: /**
119: * Return true if this node loaded with an error.
120: * <p>
121: * Since most schema nodes are loaded via the database this allows a state to be saved to the node if parent or
122: * child had an error during the loading process.
123: *
124: * @return <tt>true</tt> if this node expierenced some sort of SQL Exception during initialization.
125: */
126: public boolean isHasError() {
127:
128: return hasError;
129: }
130:
131: /**
132: * @return
133: */
134: public boolean isNoncached() {
135:
136: if (nodeType == SchemaNodeType.NON_CACHED) {
137: return true;
138: }
139: if (allowsChildren && (children != null && children.size() > 0)) {
140: SchemaNode firstChild = (SchemaNode) children.get(0);
141: return firstChild == null ? false : firstChild
142: .isNoncached();
143: }
144: return false;
145: }
146:
147: @Override
148: public String getUserObject() {
149:
150: Object obj = super .getUserObject();
151: return obj == null ? "" : obj.toString();
152: }
153:
154: /**
155: * Sets the error flag to the appropriate value.
156: * <p>
157: *
158: * @param hasError value to indicate if the node expierenced and error.F
159: */
160: public synchronized void setHasError(boolean hasError) {
161:
162: this .hasError = hasError;
163: }
164:
165: public Object getTransferData(DataFlavor flavor)
166: throws UnsupportedFlavorException, IOException {
167:
168: if (flavor != null) {
169: if (flavor.equals(supportedDataFlavors[0])) {
170: return this ;
171: } else if (flavor.equals(supportedDataFlavors[1])) {
172: return toString();
173: }
174: }
175: throw new UnsupportedFlavorException(flavor);
176: }
177:
178: public DataFlavor[] getTransferDataFlavors() {
179:
180: return supportedDataFlavors;
181: }
182:
183: public boolean isDataFlavorSupported(DataFlavor flavor) {
184:
185: if (flavor != null) {
186: if (flavor.equals(supportedDataFlavors[0])) {
187: return true;
188: } else if (flavor.equals(supportedDataFlavors[1])) {
189: return true;
190: }
191: }
192: return false;
193: }
194:
195: }
|