001: package net.sourceforge.squirrel_sql.client.gui.session;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * Modifications Copyright (C) 2003-2004 Jason Height
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import java.awt.BorderLayout;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.prefs.Preferences;
030:
031: import javax.swing.JPanel;
032: import javax.swing.JTabbedPane;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035:
036: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
037: import net.sourceforge.squirrel_sql.client.session.ISession;
038: import net.sourceforge.squirrel_sql.client.session.mainpanel.IMainPanelTab;
039: import net.sourceforge.squirrel_sql.client.session.mainpanel.ObjectTreeTab;
040: import net.sourceforge.squirrel_sql.client.session.mainpanel.SQLPanel;
041: import net.sourceforge.squirrel_sql.client.session.mainpanel.SQLTab;
042: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreePanel;
043: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
044: import net.sourceforge.squirrel_sql.fw.util.StringManager;
045: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
046: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
047: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
048:
049: /**
050: * This tabbed panel is the main panel within the session window.
051: *
052: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
053: */
054: public class MainPanel extends JPanel {
055: private static final long serialVersionUID = 1L;
056:
057: /**
058: * IDs of tabs.
059: */
060: public interface ITabIndexes {
061: int OBJECT_TREE_TAB = 0;
062: int SQL_TAB = 1;
063: }
064:
065: /** Internationalized strings for this class. */
066: private static final StringManager s_stringMgr = StringManagerFactory
067: .getStringManager(MainPanel.class);
068:
069: /** Logger for this class. */
070: private static final ILogger s_log = LoggerController
071: .createLogger(MainPanel.class);
072:
073: /** Current session. */
074: transient private ISession _session;
075:
076: /** The tabbed pane. */
077: private final JTabbedPane _tabPnl = UIFactory.getInstance()
078: .createTabbedPane();
079:
080: /** Listener to the sessions properties. */
081: transient private PropertyChangeListener _propsListener;
082:
083: /** Listener for changes to the tabbed panel. */
084: transient private ChangeListener _tabPnlListener;
085:
086: /**
087: * Collection of <TT>IMainPanelTab</TT> objects displayed in
088: * this tabbed panel.
089: */
090: private List<IMainPanelTab> _tabs = new ArrayList<IMainPanelTab>();
091:
092: private static final String PREFS_KEY_SELECTED_TAB_IX = "squirrelSql_mainPanel_sel_tab_ix";
093:
094: /**
095: * ctor specifying the current session.
096: *
097: * @param session Current session.
098: *
099: * @throws IllegalArgumentException
100: * If a <TT>null</TT> <TT>ISession</TT> passed.
101: */
102: MainPanel(ISession session) {
103: super (new BorderLayout());
104:
105: if (session == null) {
106: throw new IllegalArgumentException("ISession == null");
107: }
108:
109: _session = session;
110:
111: addMainPanelTab(new ObjectTreeTab(), Integer.valueOf('O'));
112: addMainPanelTab(new SQLTab(_session), Integer.valueOf('Q'));
113:
114: add(_tabPnl, BorderLayout.CENTER);
115:
116: propertiesHaveChanged(null);
117:
118: // Refresh the currently selected tab.
119: (_tabs.get(getTabbedPane().getSelectedIndex())).select();
120: }
121:
122: public void addNotify() {
123: super .addNotify();
124:
125: if (_propsListener == null) {
126: _propsListener = new PropertyChangeListener() {
127: public void propertyChange(PropertyChangeEvent evt) {
128: propertiesHaveChanged(evt.getPropertyName());
129: }
130: };
131: _session.getProperties().addPropertyChangeListener(
132: _propsListener);
133: }
134:
135: _tabPnlListener = new ChangeListener() {
136: public void stateChanged(ChangeEvent evt) {
137: performStateChanged();
138: }
139: };
140: _tabPnl.addChangeListener(_tabPnlListener);
141: }
142:
143: public void removeNotify() {
144: super .removeNotify();
145:
146: if (_propsListener != null) {
147: _session.getProperties().removePropertyChangeListener(
148: _propsListener);
149: _propsListener = null;
150: }
151:
152: if (_tabPnlListener != null) {
153: _tabPnl.removeChangeListener(_tabPnlListener);
154: _tabPnlListener = null;
155: }
156: }
157:
158: /**
159: * Add a tab to this panel. If a tab with this title already exists it is
160: * removed from the tabbed pane and the passed tab inserted in its
161: * place. New tabs are inserted at the end.
162: *
163: * @param tab The tab to be added.
164: *
165: * @return The index of th added tab
166: *
167: * @throws IllegalArgumentException
168: * Thrown if a <TT>null</TT> <TT>ITablePanelTab</TT> passed.
169: */
170: public int addMainPanelTab(IMainPanelTab tab) {
171: return addMainPanelTab(tab, null);
172: }
173:
174: public int addMainPanelTab(IMainPanelTab tab, Integer mnemonic) {
175: if (tab == null) {
176: throw new IllegalArgumentException(
177: "Null IMainPanelTab passed");
178: }
179: tab.setSession(_session);
180: final String title = tab.getTitle();
181: int idx = _tabPnl.indexOfTab(title);
182: if (idx != -1) {
183: _tabPnl.removeTabAt(idx);
184: _tabs.set(idx, tab);
185: } else {
186: idx = _tabPnl.getTabCount();
187: _tabs.add(tab);
188: }
189: _tabPnl.insertTab(title, null, tab.getComponent(), tab
190: .getHint(), idx);
191:
192: int prefIx = Preferences.userRoot().getInt(
193: PREFS_KEY_SELECTED_TAB_IX, ITabIndexes.OBJECT_TREE_TAB);
194: if (idx == prefIx) {
195: _tabPnl.setSelectedIndex(prefIx);
196: }
197:
198: if (null != mnemonic) {
199: _tabPnl.setMnemonicAt(idx, mnemonic.intValue());
200:
201: }
202:
203: return idx;
204: }
205:
206: /**
207: * Add a tab to this panel at the specified index.
208: *
209: * @param tab The tab to be added.
210: * @param idx The index to add the tab at.
211: *
212: * @param selectInsertedTab
213: * @throws IllegalArgumentException
214: * Thrown if a <TT>null</TT> <TT>ITablePanelTab</TT> passed.
215: *
216: * @throws IllegalArgumentException
217: * Thrown if a tab already exists with the same title as the one
218: * passed in.
219: */
220: public void insertMainPanelTab(IMainPanelTab tab, int idx,
221: boolean selectInsertedTab) {
222: if (tab == null) {
223: throw new IllegalArgumentException(
224: "Null IMainPanelTab passed");
225: }
226:
227: tab.setSession(_session);
228: final String title = tab.getTitle();
229: int checkIdx = _tabPnl.indexOfTab(title);
230: if (checkIdx != -1) {
231: throw new IllegalArgumentException(
232: "A tab with the same title already exists at index "
233: + checkIdx);
234: }
235:
236: _tabs.add(idx, tab);
237: _tabPnl.insertTab(title, null, tab.getComponent(), tab
238: .getHint(), idx);
239:
240: if (selectInsertedTab) {
241: _tabPnl.setSelectedIndex(idx);
242: }
243: }
244:
245: public int removeMainPanelTab(IMainPanelTab tab) {
246: if (tab == null) {
247: throw new IllegalArgumentException(
248: "Null IMainPanelTab passed");
249: }
250:
251: final String title = tab.getTitle();
252: int idx = _tabPnl.indexOfTab(title);
253: if (idx == -1) {
254: return idx;
255: }
256:
257: _tabPnl.removeTabAt(idx);
258:
259: return idx;
260: }
261:
262: private void updateState() {
263: _session.getApplication().getActionCollection()
264: .activationChanged(_session.getSessionInternalFrame());
265: }
266:
267: /**
268: * The passed session is closing so tell each tab.
269: *
270: * @param session Session being closed.
271: */
272: void sessionClosing(ISession session) {
273: for (Iterator<IMainPanelTab> it = _tabs.iterator(); it
274: .hasNext();) {
275: try {
276: (it.next()).sessionClosing(session);
277: } catch (Throwable th) {
278: String msg = s_stringMgr
279: .getString("MainPanel.error.sessionclose");
280: _session.getApplication().showErrorDialog(msg, th);
281: s_log.error(msg, th);
282: }
283: }
284: }
285:
286: public void sessionWindowClosing() {
287: getSQLPanel().sessionWindowClosing();
288: getObjectTreePanel().sessionWindowClosing();
289: int selIx = _tabPnl.getSelectedIndex();
290:
291: if (selIx == ITabIndexes.OBJECT_TREE_TAB
292: || selIx == ITabIndexes.SQL_TAB) {
293: Preferences.userRoot().putInt(PREFS_KEY_SELECTED_TAB_IX,
294: selIx);
295: }
296: }
297:
298: /**
299: * Session properties have changed so update GUI if required.
300: *
301: * @param propertyName Name of property that has changed.
302: */
303: private void propertiesHaveChanged(String propertyName) {
304: SessionProperties props = _session.getProperties();
305: if (propertyName == null
306: || propertyName
307: .equals(SessionProperties.IPropertyNames.MAIN_TAB_PLACEMENT)) {
308: _tabPnl.setTabPlacement(props.getMainTabPlacement());
309: }
310: }
311:
312: private void performStateChanged() {
313: // Needed to guarantee other components a focus lost
314: // and to allow to enter the tabs components via tab
315: // key in a well defined way (the user can see where the focus is).
316: _tabPnl.requestFocusInWindow();
317:
318: updateState();
319: int idx = _tabPnl.getSelectedIndex();
320: if (idx != -1) {
321: (_tabs.get(idx)).select();
322: }
323: }
324:
325: ObjectTreePanel getObjectTreePanel() {
326: ObjectTreeTab tab = (ObjectTreeTab) _tabs
327: .get(ITabIndexes.OBJECT_TREE_TAB);
328: return (ObjectTreePanel) tab.getComponent();
329: }
330:
331: SQLPanel getSQLPanel() {
332: return ((SQLTab) _tabs.get(ITabIndexes.SQL_TAB)).getSQLPanel();
333: }
334:
335: /**
336: * Retrieve the tabbed pane for this component.
337: *
338: * @return The tabbed pane.
339: */
340: JTabbedPane getTabbedPane() {
341: return _tabPnl;
342: }
343: }
|