001: package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.swing.tree.DefaultMutableTreeNode;
025: import javax.swing.tree.MutableTreeNode;
026:
027: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
028: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
029: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
030:
031: import net.sourceforge.squirrel_sql.client.IApplication;
032: import net.sourceforge.squirrel_sql.client.session.ISession;
033:
034: /**
035: * This is a node in the object tree.
036: *
037: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
038: */
039: public class ObjectTreeNode extends DefaultMutableTreeNode {
040: /** Current session. */
041: // private final ISession _session;
042: private static final long serialVersionUID = 1L;
043:
044: /** Application API. */
045: private final IApplication _app;
046:
047: /** ID of the session for this window. */
048: private final IIdentifier _sessionId;
049:
050: /** Describes the database object represented by this node. */
051: private final IDatabaseObjectInfo _dboInfo;
052:
053: /** If <TT>true</TT> node can be expanded. */
054: private boolean _allowsChildren = true;
055:
056: /** Collection of <TT>INodeExpander</TT> objects for this node. */
057: private final List<INodeExpander> _expanders = new ArrayList<INodeExpander>();
058:
059: /**
060: * Ctor that assumes node cannot have children.
061: *
062: * @param session Current session.
063: * @param dbinfo Describes this object in the database.
064: *
065: * @throws IllegalArgumentException
066: * Thrown if <TT>null</TT> <TT>ISession</TT> or
067: * <TT>IDatabaseObjectInfo</TT> passed.
068: */
069: public ObjectTreeNode(ISession session, IDatabaseObjectInfo dboInfo) {
070: super (getNodeTitle(dboInfo));
071: if (session == null) {
072: throw new IllegalArgumentException("ISession == null");
073: }
074: if (dboInfo == null) {
075: throw new IllegalArgumentException(
076: "IDatabaseObjectInfo == null");
077: }
078: // _session = session;
079: _app = session.getApplication();
080: _sessionId = session.getIdentifier();
081: _dboInfo = dboInfo;
082: }
083:
084: public void add(MutableTreeNode newChild) {
085: super .add(newChild);
086: newChild.setParent(this );
087: }
088:
089: /**
090: * Return the current session.
091: *
092: * @return the current session.
093: */
094: public ISession getSession() {
095: return _app.getSessionManager().getSession(_sessionId);
096: }
097:
098: /**
099: * Return the <TT>IDatabaseObjectInfo</TT> object that describes the
100: * database object represented by this node.
101: */
102: public IDatabaseObjectInfo getDatabaseObjectInfo() {
103: return _dboInfo;
104: }
105:
106: /**
107: * Convenience method to get the database object type for this node.
108: *
109: * @return the database object type of this node.
110: */
111: public DatabaseObjectType getDatabaseObjectType() {
112: return _dboInfo.getDatabaseObjectType();
113: }
114:
115: /**
116: * Returns <TT>true</TT> if this node can have children.
117: *
118: * @return <TT>true</TT> if this node can have children.
119: */
120: public boolean getAllowsChildren() {
121: return _allowsChildren;
122: }
123:
124: public boolean isLeaf() {
125: return !_allowsChildren;
126: }
127:
128: /**
129: * Return the expanders for this node. Remember that these are in addition
130: * to the standard expanders stored in the object tree model. Normally
131: * this would be empty.
132: *
133: * @return The <TT>INodeExpander</TT> objects for this node.
134: */
135: public INodeExpander[] getExpanders() {
136: return _expanders.toArray(new INodeExpander[_expanders.size()]);
137: }
138:
139: /**
140: * Adds an expander to this node.
141: *
142: * @param value New <TT>INodeExpander</TT> for this node.
143: *
144: * @throws IllegalArgumentException
145: * Thrown if a <TT>null</TT>INodeExpander</TT> passed.
146: */
147: public void addExpander(INodeExpander value) {
148: if (value == null) {
149: throw new IllegalArgumentException("INodeExpander == null");
150: }
151:
152: _expanders.add(value);
153: }
154:
155: /**
156: * Specify whether this node can have children.
157: *
158: * @param value <TT>true</TT> if this node can have children.
159: */
160: public void setAllowsChildren(boolean value) {
161: super .setAllowsChildren(value);
162: _allowsChildren = value;
163: }
164:
165: private static String getNodeTitle(IDatabaseObjectInfo dbinfo) {
166: if (dbinfo == null) {
167: throw new IllegalArgumentException(
168: "IDatabaseObjectInfo == null");
169: }
170: return dbinfo.toString();
171: }
172: }
|