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: * Copyright (C) 2003-2004 Jason Height
008: * jmheight@users.sourceforge.net
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or (at your option) any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: */
024: import java.awt.BorderLayout;
025: import java.awt.Component;
026: import java.awt.Window;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.FocusEvent;
030: import java.sql.SQLException;
031:
032: import javax.swing.Icon;
033: import javax.swing.JPanel;
034: import javax.swing.SwingUtilities;
035: import javax.swing.event.InternalFrameAdapter;
036: import javax.swing.event.InternalFrameEvent;
037: import javax.swing.event.TreeSelectionEvent;
038: import javax.swing.event.TreeSelectionListener;
039: import javax.swing.tree.TreePath;
040:
041: import net.sourceforge.squirrel_sql.client.IApplication;
042: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
043: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
044: import net.sourceforge.squirrel_sql.client.session.IObjectTreeInternalFrame;
045: import net.sourceforge.squirrel_sql.client.session.ISession;
046: import net.sourceforge.squirrel_sql.client.session.action.RefreshSchemaInfoAction;
047: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
048: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreePanel;
049: import net.sourceforge.squirrel_sql.fw.gui.SQLCatalogsComboBox;
050: import net.sourceforge.squirrel_sql.fw.gui.ToolBar;
051: import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
052: import net.sourceforge.squirrel_sql.fw.util.StringManager;
053: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
054: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
055: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
056:
057: /* Object Tree frame class*/
058: public class ObjectTreeInternalFrame extends BaseSessionInternalFrame
059: implements IObjectTreeInternalFrame {
060: /** Application API. */
061: private final IApplication _app;
062:
063: private ObjectTreePanel _objTreePanel;
064:
065: /** Toolbar for window. */
066: private ObjectTreeToolBar _toolBar;
067:
068: private boolean _hasBeenVisible = false;
069:
070: /** Internationalized strings for this class. */
071: private static final StringManager s_stringMgr = StringManagerFactory
072: .getStringManager(ObjectTreeInternalFrame.class);
073:
074: public ObjectTreeInternalFrame(ISession session) {
075: super (session, session.getTitle(), true, true, true, true);
076: _app = session.getApplication();
077: setVisible(false);
078: createGUI(session);
079: }
080:
081: public void addNotify() {
082: super .addNotify();
083: if (!_hasBeenVisible) {
084: _hasBeenVisible = true;
085: // Done this late so that plugins have time to register expanders
086: // with the object tree prior to it being built.
087: _objTreePanel.refreshTree();
088: }
089: }
090:
091: public ObjectTreePanel getObjectTreePanel() {
092: return _objTreePanel;
093: }
094:
095: public IObjectTreeAPI getObjectTreeAPI() {
096: return _objTreePanel;
097: }
098:
099: private void createGUI(ISession session) {
100: setVisible(false);
101: final IApplication app = session.getApplication();
102: Icon icon = app.getResources().getIcon(getClass(), "frameIcon"); //i18n
103: if (icon != null) {
104: setFrameIcon(icon);
105: }
106:
107: // This is to fix a problem with the JDK (up to version 1.3)
108: // where focus events were not generated correctly. The sympton
109: // is being unable to key into the text entry field unless you click
110: // elsewhere after focus is gained by the internal frame.
111: // See bug ID 4309079 on the JavaSoft bug parade (plus others).
112: addInternalFrameListener(new InternalFrameAdapter() {
113: public void internalFrameActivated(InternalFrameEvent evt) {
114: Window window = SwingUtilities
115: .windowForComponent(ObjectTreeInternalFrame.this
116: .getObjectTreePanel());
117: Component focusOwner = (window != null) ? window
118: .getFocusOwner() : null;
119: if (focusOwner != null) {
120: FocusEvent lost = new FocusEvent(focusOwner,
121: FocusEvent.FOCUS_LOST);
122: FocusEvent gained = new FocusEvent(focusOwner,
123: FocusEvent.FOCUS_GAINED);
124: window.dispatchEvent(lost);
125: window.dispatchEvent(gained);
126: window.dispatchEvent(lost);
127: focusOwner.requestFocus();
128: }
129: }
130: });
131:
132: _objTreePanel = new ObjectTreePanel(getSession());
133: _objTreePanel
134: .addTreeSelectionListener(new ObjectTreeSelectionListener());
135: _toolBar = new ObjectTreeToolBar(getSession(), _objTreePanel);
136: JPanel contentPanel = new JPanel(new BorderLayout());
137: contentPanel.add(_toolBar, BorderLayout.NORTH);
138: contentPanel.add(_objTreePanel, BorderLayout.CENTER);
139: setContentPane(contentPanel);
140: validate();
141: }
142:
143: /** The class representing the toolbar at the top of a sql internal frame*/
144: private class ObjectTreeToolBar extends ToolBar {
145: /** Internationalized strings for this class. */
146: private final StringManager s_stringMgr = StringManagerFactory
147: .getStringManager(ObjectTreeToolBar.class);
148: private ILogger s_log = LoggerController
149: .createLogger(ObjectTreeToolBar.class);
150: private CatalogsPanel _catalogsPanel;
151:
152: ObjectTreeToolBar(ISession session, ObjectTreePanel panel) {
153: super ();
154: createGUI(session, panel);
155: }
156:
157: private void createGUI(ISession session, ObjectTreePanel panel) {
158: _catalogsPanel = new CatalogsPanel(session, this );
159: _catalogsPanel
160: .addActionListener(new CatalogsComboListener());
161: add(_catalogsPanel);
162:
163: ActionCollection actions = session.getApplication()
164: .getActionCollection();
165: setUseRolloverButtons(true);
166: setFloatable(false);
167: add(actions.get(RefreshSchemaInfoAction.class));
168: }
169: }
170:
171: private final class CatalogsComboListener implements ActionListener {
172: public void actionPerformed(ActionEvent evt) {
173: Object src = evt.getSource();
174: if (src instanceof SQLCatalogsComboBox) {
175: SQLCatalogsComboBox cmb = (SQLCatalogsComboBox) src;
176: String catalog = cmb.getSelectedCatalog();
177: if (catalog != null) {
178: try {
179: getSession().getSQLConnection().setCatalog(
180: catalog);
181: } catch (SQLException ex) {
182: getSession().showErrorMessage(ex);
183: }
184: }
185: }
186: }
187: }
188:
189: /** JASON: this could be added to the objecttreepanel if the status bar was attached
190: * to the application
191: */
192: private final class ObjectTreeSelectionListener implements
193: TreeSelectionListener {
194: public void valueChanged(TreeSelectionEvent evt) {
195: final TreePath selPath = evt.getNewLeadSelectionPath();
196: if (selPath != null) {
197: StringBuffer buf = new StringBuffer();
198: Object[] fullPath = selPath.getPath();
199: for (int i = 0; i < fullPath.length; ++i) {
200: if (fullPath[i] instanceof ObjectTreeNode) {
201: ObjectTreeNode node = (ObjectTreeNode) fullPath[i];
202: buf.append('/').append(node.toString());
203: }
204: }
205: //JASON: have a main application status bar setStatusBarMessage(buf.toString());
206: }
207: }
208: }
209: }
|