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.Iterator;
023: import java.util.List;
024:
025: import javax.swing.JTabbedPane;
026:
027: import net.sourceforge.squirrel_sql.client.IApplication;
028: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.IObjectTab;
031: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
032: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
033: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
034: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
035:
036: /**
037: * This is the tabbed panel displayed when a node is selected in the
038: * object tree.
039: *
040: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
041: */
042: class ObjectTreeTabbedPane {
043:
044: /** Logger for this class. */
045: private final static ILogger log = LoggerController
046: .createLogger(ObjectTreeTabbedPane.class);
047:
048: /** Keys to client properties stored in the component. */
049: interface IClientPropertiesKeys {
050: String TABBED_PANE_OBJ = ObjectTreeTabbedPane.class.getName()
051: + "/TabPaneObj";
052: }
053:
054: /** The tabbed pane. */
055: private final JTabbedPane _tabPnl = UIFactory.getInstance()
056: .createTabbedPane();
057:
058: /** Application API. */
059: private final IApplication _app;
060:
061: /** ID of the session for this window. */
062: private final IIdentifier _sessionId;
063:
064: /**
065: * Collection of <TT>IObjectTab</TT> objects displayed in
066: * this tabbed panel.
067: */
068: private List<IObjectTab> _tabs = new ArrayList<IObjectTab>();
069:
070: ObjectTreeTabbedPane(ISession session) {
071: super ();
072:
073: if (session == null) {
074: throw new IllegalArgumentException("ISession == null");
075: }
076:
077: _sessionId = session.getIdentifier();
078: _app = session.getApplication();
079:
080: _tabPnl.putClientProperty(
081: IClientPropertiesKeys.TABBED_PANE_OBJ, this );
082: }
083:
084: /**
085: * Retrieve the tabbed pane for this component.
086: *
087: * @return The tabbed pane.
088: */
089: JTabbedPane getTabbedPane() {
090: return _tabPnl;
091: }
092:
093: IObjectTab getTabIfSelected(String title) {
094: IObjectTab tab = getSelectedTab();
095: if ((tab != null) && (tab.getTitle().equals(title))) {
096: return tab;
097: }
098: return null;
099: }
100:
101: IObjectTab getSelectedTab() {
102: IObjectTab tab = _tabs.get(_tabPnl.getSelectedIndex());
103: return tab;
104: }
105:
106: synchronized void addObjectPanelTab(IObjectTab tab) {
107: if (tab == null) {
108: throw new IllegalArgumentException("Null IObjectTab passed");
109: }
110: // For some reason, when the Oracle plugin adds details tabs for
111: // triggers, the _tabPnl's first tab ends up being the trigger details
112: // tab and not the generic database object info tab. This causes the
113: // _tabs length to be 1 tab greater than the tabs that are actually in
114: // the _tabPnl. This throws off the selection such that the tab
115: // selected in the tab panel doesn't get rendered until the tab to the
116: // right of the selected tab is selected. This is a work-around for
117: // this problem until I can determine why the DatabaseObjectInfoTab
118: // never makes it into the _tabPnl in the first place.
119: if (_tabs.size() == 1 && _tabPnl.getTabCount() == 0) {
120: log
121: .debug("addObjectPanelTab: _tabs.size() == 1, but "
122: + "_tabPnl.getTabCount() == 0 - adding first tab component to "
123: + "the tabbed page");
124: IObjectTab firstTab = _tabs.get(0);
125: _tabPnl.addTab(firstTab.getTitle(), null, firstTab
126: .getComponent(), firstTab.getHint());
127: }
128:
129: tab.setSession(_app.getSessionManager().getSession(_sessionId));
130: final String title = tab.getTitle();
131: _tabPnl.addTab(title, null, tab.getComponent(), tab.getHint());
132: _tabs.add(tab);
133: }
134:
135: void selectCurrentTab() {
136: if (_tabPnl.getParent() != null) {
137: int idx = _tabPnl.getSelectedIndex();
138: if (idx != -1 && idx < _tabs.size()) {
139: IObjectTab tab = _tabs.get(idx);
140: if (tab != null) {
141: tab.select();
142: }
143: }
144: }
145: }
146:
147: void setDatabaseObjectInfo(IDatabaseObjectInfo dboInfo) {
148: Iterator<IObjectTab> it = _tabs.iterator();
149: while (it.hasNext()) {
150: IObjectTab tab = it.next();
151: tab.setDatabaseObjectInfo(dboInfo);
152: }
153: }
154:
155: /**
156: * Rebuild the tabs. This usually means that some kind of configuration
157: * data has changed (I.E. the output type has changed from text to table).
158: */
159: synchronized void rebuild() {
160: final int curTabIdx = _tabPnl.getSelectedIndex();
161: final List<IObjectTab> oldTabs = new ArrayList<IObjectTab>();
162: oldTabs.addAll(_tabs);
163: _tabPnl.removeAll();
164: _tabs.clear();
165: Iterator<IObjectTab> it = oldTabs.iterator();
166: while (it.hasNext()) {
167: final IObjectTab tab = it.next();
168: tab.rebuild();
169: addObjectPanelTab(tab);
170: }
171: if (curTabIdx >= 0 && curTabIdx < _tabPnl.getTabCount()) {
172: _tabPnl.setSelectedIndex(curTabIdx);
173: }
174: }
175:
176: }
|