001: /*
002: * DbExplorerWindow.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.dbobjects;
013:
014: import java.awt.event.WindowEvent;
015: import java.awt.event.WindowListener;
016:
017: import javax.swing.JFrame;
018: import javax.swing.WindowConstants;
019:
020: import workbench.WbManager;
021: import workbench.db.ConnectionProfile;
022: import workbench.db.WbConnection;
023: import workbench.gui.WbSwingUtilities;
024: import workbench.gui.components.ConnectionSelector;
025: import workbench.gui.components.RunningJobIndicator;
026: import workbench.interfaces.Connectable;
027: import workbench.interfaces.DbExecutionListener;
028: import workbench.interfaces.ToolWindow;
029: import workbench.resource.ResourceMgr;
030: import workbench.resource.Settings;
031:
032: /**
033: *
034: * @author support@sql-workbench.net
035: */
036: public class DbExplorerWindow extends JFrame implements WindowListener,
037: Connectable, ToolWindow, DbExecutionListener {
038: private DbExplorerPanel panel;
039: private boolean standalone;
040: protected ConnectionSelector connectionSelector;
041: protected RunningJobIndicator jobIndicator;
042:
043: public DbExplorerWindow(DbExplorerPanel aPanel) {
044: this (aPanel, null);
045: }
046:
047: public DbExplorerWindow(DbExplorerPanel aPanel, String aProfileName) {
048: super ();
049: this .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
050: this .panel = aPanel;
051: this .addWindowListener(this );
052: this .getContentPane().add(this .panel);
053: this .setIconImage(ResourceMgr.getImage("Database").getImage());
054: this .setProfileName(aProfileName);
055: this .restorePosition();
056: this .jobIndicator = new RunningJobIndicator(this );
057: this .panel.setDbExecutionListener(this );
058: }
059:
060: public void activate() {
061: setVisible(true);
062: toFront();
063: }
064:
065: public void setProfileName(String aProfileName) {
066: if (aProfileName != null) {
067: this .setTitle(ResourceMgr.getString("TxtDbExplorerTitel")
068: + " - [" + aProfileName + "]");
069: } else {
070: this .setTitle(ResourceMgr.getString("TxtDbExplorerTitel"));
071: }
072: }
073:
074: public void setStandalone(boolean flag) {
075: this .standalone = flag;
076: if (flag) {
077: WbManager.getInstance().registerToolWindow(this );
078: this .connectionSelector = new ConnectionSelector(this , this );
079: this .connectionSelector
080: .setPropertyKey("workbench.dbexplorer.connection.last");
081: this .panel.showConnectButton(this .connectionSelector);
082: }
083: }
084:
085: public void selectConnection() {
086: connectionSelector.selectConnection();
087: }
088:
089: public void executionEnd(WbConnection conn, Object source) {
090: jobIndicator.jobEnded();
091: }
092:
093: public void executionStart(WbConnection conn, Object source) {
094: jobIndicator.jobStarted();
095: }
096:
097: public WbConnection getConnection() {
098: return panel.getConnection();
099: }
100:
101: public void closeWindow() {
102: this .saveSettings();
103: this .disconnect();
104: this .panel.explorerWindowClosed();
105: this .setVisible(false);
106: this .dispose();
107: }
108:
109: public void disconnect() {
110: this .panel.disconnect();
111: }
112:
113: public void saveSettings() {
114: Settings.getInstance().storeWindowPosition(this );
115: Settings.getInstance().storeWindowSize(this );
116: this .panel.saveSettings();
117: }
118:
119: public void restorePosition() {
120: Settings s = Settings.getInstance();
121:
122: if (!s.restoreWindowSize(this )) {
123: this .setSize(800, 600);
124: }
125:
126: if (!s.restoreWindowPosition(this )) {
127: WbSwingUtilities.center(this , null);
128: }
129: }
130:
131: public void windowActivated(WindowEvent e) {
132: }
133:
134: public void windowClosed(WindowEvent e) {
135: if (standalone) {
136: WbManager.getInstance().unregisterToolWindow(this );
137: } else {
138: if (this .panel != null) {
139: panel.explorerWindowClosed();
140: }
141: }
142: }
143:
144: public void windowClosing(WindowEvent e) {
145: this .saveSettings();
146: }
147:
148: public void windowDeactivated(WindowEvent e) {
149: }
150:
151: public void windowDeiconified(WindowEvent e) {
152: }
153:
154: public void windowIconified(WindowEvent e) {
155: }
156:
157: public void windowOpened(WindowEvent e) {
158: }
159:
160: public static DbExplorerWindow showWindow() {
161: DbExplorerPanel panel = new DbExplorerPanel();
162: DbExplorerWindow window = new DbExplorerWindow(panel);
163: window.setStandalone(true);
164:
165: window.restorePosition();
166: panel.restoreSettings();
167:
168: window.setVisible(true);
169: window.selectConnection();
170: return window;
171: }
172:
173: public void connectBegin(ConnectionProfile profile) {
174: }
175:
176: public void connectCancelled() {
177: }
178:
179: public void connectFailed(String error) {
180: this .setProfileName(null);
181: this .panel.setConnection(null);
182: String msg = ResourceMgr.getFormattedString("ErrConnectFailed",
183: error.trim());
184: WbSwingUtilities.showErrorMessage(this , msg);
185: }
186:
187: public void connected(WbConnection conn) {
188: this .setProfileName(conn.getProfile().getName());
189: this .panel.setConnection(conn);
190: }
191:
192: public String getConnectionId(ConnectionProfile profile) {
193: if (this .panel == null)
194: return "DbExplorerWindow";
195: return this .panel.getId();
196: }
197:
198: public void connectEnded() {
199: }
200: }
|