0001: package net.sourceforge.squirrel_sql.client;
0002:
0003: /*
0004: * TODO: finish i18n
0005: */
0006:
0007: /*
0008: * Copyright (C) 2001-2006 Colin Bell
0009: * colbell@users.sourceforge.net
0010: *
0011: * Modifications Copyright (C) 2003-2004 Jason Height
0012: *
0013: * This library is free software; you can redistribute it and/or
0014: * modify it under the terms of the GNU Lesser General Public
0015: * License as published by the Free Software Foundation; either
0016: * version 2.1 of the License, or (at your option) any later version.
0017: *
0018: * This library is distributed in the hope that it will be useful,
0019: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0021: * Lesser General Public License for more details.
0022: *
0023: * You should have received a copy of the GNU Lesser General Public
0024: * License along with this library; if not, write to the Free Software
0025: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0026: */
0027: import java.awt.AWTEvent;
0028: import java.awt.EventQueue;
0029: import java.awt.Toolkit;
0030: import java.beans.PropertyChangeEvent;
0031: import java.beans.PropertyChangeListener;
0032: import java.io.File;
0033: import java.io.FileNotFoundException;
0034: import java.io.FileOutputStream;
0035: import java.io.FileWriter;
0036: import java.io.IOException;
0037: import java.io.PrintStream;
0038: import java.io.PrintWriter;
0039: import java.sql.DriverManager;
0040: import java.util.Calendar;
0041: import java.util.Iterator;
0042:
0043: import javax.swing.Action;
0044: import javax.swing.JComponent;
0045: import javax.swing.JMenu;
0046: import javax.swing.JOptionPane;
0047: import javax.swing.PopupFactory;
0048: import javax.swing.ToolTipManager;
0049: import javax.swing.UIManager;
0050: import javax.swing.plaf.metal.MetalLookAndFeel;
0051:
0052: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
0053: import net.sourceforge.squirrel_sql.client.gui.FileViewerFactory;
0054: import net.sourceforge.squirrel_sql.client.gui.SplashScreen;
0055: import net.sourceforge.squirrel_sql.client.gui.WindowManager;
0056: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
0057: import net.sourceforge.squirrel_sql.client.gui.db.DataCache;
0058: import net.sourceforge.squirrel_sql.client.gui.laf.AllBluesBoldMetalTheme;
0059: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
0060: import net.sourceforge.squirrel_sql.client.mainframe.action.ConnectToStartupAliasesCommand;
0061: import net.sourceforge.squirrel_sql.client.mainframe.action.ViewHelpCommand;
0062: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
0063: import net.sourceforge.squirrel_sql.client.plugin.PluginLoadInfo;
0064: import net.sourceforge.squirrel_sql.client.plugin.PluginManager;
0065: import net.sourceforge.squirrel_sql.client.preferences.PreferenceType;
0066: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
0067: import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
0068: import net.sourceforge.squirrel_sql.client.session.DefaultSQLEntryPanelFactory;
0069: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanelFactory;
0070: import net.sourceforge.squirrel_sql.client.session.SessionManager;
0071: import net.sourceforge.squirrel_sql.client.session.mainpanel.SQLHistory;
0072: import net.sourceforge.squirrel_sql.client.session.mainpanel.SQLHistoryItem;
0073: import net.sourceforge.squirrel_sql.client.session.properties.EditWhereCols;
0074: import net.sourceforge.squirrel_sql.client.session.schemainfo.SchemaInfoCacheSerializer;
0075: import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
0076: import net.sourceforge.squirrel_sql.fw.datasetviewer.CellImportExportInfoSaver;
0077: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.DTProperties;
0078: import net.sourceforge.squirrel_sql.fw.gui.CursorChanger;
0079: import net.sourceforge.squirrel_sql.fw.gui.ErrorDialog;
0080: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverManager;
0081: import net.sourceforge.squirrel_sql.fw.util.BareBonesBrowserLaunch;
0082: import net.sourceforge.squirrel_sql.fw.util.BaseException;
0083: import net.sourceforge.squirrel_sql.fw.util.ClassLoaderListener;
0084: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
0085: import net.sourceforge.squirrel_sql.fw.util.ProxyHandler;
0086: import net.sourceforge.squirrel_sql.fw.util.StringManager;
0087: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
0088: import net.sourceforge.squirrel_sql.fw.util.TaskThreadPool;
0089: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
0090: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
0091: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
0092: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
0093:
0094: /**
0095: * Defines the API to do callbacks on the application.
0096: *
0097: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
0098: * @author Lynn Pye
0099: */
0100: class Application implements IApplication {
0101: /** Logger for this class. */
0102: private static ILogger s_log;
0103:
0104: /** Internationalized strings for this class. */
0105: private static final StringManager s_stringMgr = StringManagerFactory
0106: .getStringManager(Application.class);
0107:
0108: private SquirrelPreferences _prefs;
0109: private SQLDriverManager _driverMgr;
0110: private DataCache _cache;
0111: private ActionCollection _actions;
0112:
0113: /** Applications main frame. */
0114: // private MainFrame _mainFrame;
0115: /** Object to manage plugins. */
0116: private PluginManager _pluginManager;
0117:
0118: private final DummyAppPlugin _dummyPlugin = new DummyAppPlugin();
0119:
0120: private SquirrelResources _resources;
0121:
0122: /** Thread pool for long running tasks. */
0123: private final TaskThreadPool _threadPool = new TaskThreadPool();
0124:
0125: /** This object manages the open sessions.*/
0126: private SessionManager _sessionManager;
0127:
0128: /** This object manages the windows for this application.*/
0129: private WindowManager _windowManager;
0130:
0131: private LoggerController _loggerFactory;
0132:
0133: /** Factory used to create SQL entry panels. */
0134: private ISQLEntryPanelFactory _sqlEntryFactory = new DefaultSQLEntryPanelFactory();
0135:
0136: /** Output stream for JDBC debug logging. */
0137: private PrintStream _jdbcDebugOutputStream;
0138:
0139: /** Output writer for JDBC debug logging. */
0140: private PrintWriter _jdbcDebugOutputWriter;
0141:
0142: /** Contains info about fonts for squirrel. */
0143: private final FontInfoStore _fontInfoStore = new FontInfoStore();
0144:
0145: /** Application level SQL History. */
0146: private SQLHistory _sqlHistory;
0147:
0148: /** Current type of JDBC debug logging that we are doing. */
0149: private int _jdbcDebugType = SquirrelPreferences.IJdbcDebugTypes.NONE;
0150:
0151: /** contains info about files and directories used by the application. */
0152: private ApplicationFiles _appFiles = null;
0153:
0154: private EditWhereCols editWhereCols = new EditWhereCols();
0155:
0156: /**
0157: * Default ctor.
0158: */
0159: Application() {
0160: super ();
0161: }
0162:
0163: /**
0164: * Application is starting up.
0165: */
0166: public void startup() {
0167: LoggerController
0168: .registerLoggerFactory(new SquirrelLoggerFactory());
0169: s_log = LoggerController.createLogger(getClass());
0170:
0171: EventQueue q = Toolkit.getDefaultToolkit()
0172: .getSystemEventQueue();
0173: q.push(new EventQueue() {
0174: protected void dispatchEvent(AWTEvent event) {
0175: try {
0176: super .dispatchEvent(event);
0177: } catch (Throwable t) {
0178: if (s_log.isDebugEnabled()) {
0179: t.printStackTrace();
0180: }
0181: s_log.error("Exception occured dispatching Event "
0182: + event, t);
0183: }
0184: }
0185: });
0186:
0187: final ApplicationArguments args = ApplicationArguments
0188: .getInstance();
0189:
0190: // Setup the applications Look and Feel.
0191: setupLookAndFeel(args);
0192:
0193: editWhereCols.setApplication(this );
0194:
0195: // TODO: Make properties file Application.properties so we can use class
0196: // name to generate properties file name.
0197: _resources = new SquirrelResources(
0198: "net.sourceforge.squirrel_sql.client.resources.squirrel");
0199: _prefs = SquirrelPreferences.load();
0200: preferencesHaveChanged(null);
0201: _prefs.addPropertyChangeListener(new PropertyChangeListener() {
0202: public void propertyChange(PropertyChangeEvent evt) {
0203: preferencesHaveChanged(evt);
0204: }
0205: });
0206:
0207: SplashScreen splash = null;
0208: if (args.getShowSplashScreen()) {
0209: splash = new SplashScreen(_resources, 15, _prefs);
0210: }
0211:
0212: try {
0213: CursorChanger chg = null;
0214: if (splash != null) {
0215: chg = new CursorChanger(splash);
0216: chg.show();
0217: }
0218: try {
0219: executeStartupTasks(splash, args);
0220: } finally {
0221: if (chg != null) {
0222: chg.restore();
0223: }
0224: }
0225: } finally {
0226: if (splash != null) {
0227: splash.dispose();
0228: }
0229: }
0230: }
0231:
0232: /**
0233: * Application is shutting down.
0234: */
0235: public boolean shutdown() {
0236: s_log.info(s_stringMgr.getString("Application.shutdown",
0237: Calendar.getInstance().getTime()));
0238:
0239: saveApplicationState();
0240:
0241: if (!closeAllSessions()) {
0242: return false;
0243: }
0244: _pluginManager.unloadPlugins();
0245:
0246: closeAllViewers();
0247:
0248: closeOutputStreams();
0249:
0250: SchemaInfoCacheSerializer.waitTillStoringIsDone();
0251:
0252: String msg = s_stringMgr.getString(
0253: "Application.shutdowncomplete", Calendar.getInstance()
0254: .getTime());
0255: s_log.info(msg);
0256: LoggerController.shutdown();
0257:
0258: return true;
0259: }
0260:
0261: /**
0262: * Saves off preferences and all state present in the application.
0263: */
0264: public void saveApplicationState() {
0265: saveGlobalPreferences();
0266:
0267: saveDrivers();
0268:
0269: saveAliases();
0270:
0271: // Save Application level SQL history.
0272: saveSQLHistory();
0273:
0274: // Save options selected for Cell Import Export operations
0275: saveCellImportExportInfo();
0276:
0277: // Save options selected for Edit Where Columns
0278: saveEditWhereColsInfo();
0279:
0280: // Save options selected for DataType-specific properties
0281: saveDataTypePreferences();
0282: }
0283:
0284: /**
0285: * Remember the currently selected entries in the aliases and drivers
0286: * windows.
0287: */
0288: private void saveGlobalPreferences() {
0289: // JASON: Do in WindowManager and do much better
0290: // final MainFrame mf = _windowManager.getMainFrame();
0291:
0292: int idx = _windowManager.getAliasesListInternalFrame()
0293: .getSelectedIndex();
0294: _prefs.setAliasesSelectedIndex(idx);
0295: idx = _windowManager.getDriversListInternalFrame()
0296: .getSelectedIndex();
0297: _prefs.setDriversSelectedIndex(idx);
0298:
0299: // No longer the first run of SQuirrel.
0300: _prefs.setFirstRun(false);
0301:
0302: _prefs.save();
0303: }
0304:
0305: /**
0306: *
0307: */
0308: private void closeOutputStreams() {
0309: if (_jdbcDebugOutputStream != null) {
0310: _jdbcDebugOutputStream.close();
0311: _jdbcDebugOutputStream = null;
0312: }
0313:
0314: if (_jdbcDebugOutputWriter != null) {
0315: _jdbcDebugOutputWriter.close();
0316: _jdbcDebugOutputWriter = null;
0317: }
0318: }
0319:
0320: /**
0321: * Saves alias definitions that are in memory to the aliases file.
0322: */
0323: private void saveAliases() {
0324: try {
0325: final File file = _appFiles.getDatabaseAliasesFile();
0326: _cache.saveAliases(file);
0327: } catch (Throwable th) {
0328: String thMsg = th.getMessage();
0329: if (thMsg == null) {
0330: thMsg = "";
0331: }
0332: String msg = s_stringMgr.getString(
0333: "Application.error.aliassave", th.getMessage());
0334: showErrorDialog(msg, th);
0335: s_log.error(msg, th);
0336: }
0337: }
0338:
0339: /**
0340: * Saves the driver definitions that are in memory to the drivers file.
0341: */
0342: private void saveDrivers() {
0343: try {
0344: final File file = _appFiles.getDatabaseDriversFile();
0345: _cache.saveDrivers(file);
0346: } catch (Throwable th) {
0347: String msg = s_stringMgr.getString(
0348: "Application.error.driversave", th.getMessage());
0349: showErrorDialog(msg, th);
0350: s_log.error(msg, th);
0351: }
0352: }
0353:
0354: /**
0355: *
0356: */
0357: private void closeAllViewers() {
0358: try {
0359: FileViewerFactory.getInstance().closeAllViewers();
0360: } catch (Throwable t) {
0361: //i18n[Application.error.closeFileViewers=Unable to close all file viewers]
0362: s_log
0363: .error(
0364: s_stringMgr
0365: .getString("Application.error.closeFileViewers"),
0366: t);
0367: }
0368: }
0369:
0370: /**
0371: * Returns true is closing all sessions was successful.
0372: * @return
0373: */
0374: private boolean closeAllSessions() {
0375: boolean result = false;
0376: try {
0377: if (!_sessionManager.closeAllSessions()) {
0378: s_log.info(s_stringMgr.getString(
0379: "Application.shutdownCancelled", Calendar
0380: .getInstance().getTime()));
0381: } else {
0382: result = true;
0383: }
0384: } catch (Throwable t) {
0385: String msg = s_stringMgr.getString(
0386: "Application.error.closeAllSessions", t
0387: .getMessage());
0388: s_log.error(msg, t);
0389: }
0390: return result;
0391: }
0392:
0393: public PluginManager getPluginManager() {
0394: return _pluginManager;
0395: }
0396:
0397: /**
0398: * Return the manager responsible for windows.
0399: *
0400: * @return the manager responsible for windows.
0401: */
0402: public WindowManager getWindowManager() {
0403: return _windowManager;
0404: }
0405:
0406: public ActionCollection getActionCollection() {
0407: return _actions;
0408: }
0409:
0410: public SQLDriverManager getSQLDriverManager() {
0411: return _driverMgr;
0412: }
0413:
0414: public DataCache getDataCache() {
0415: return _cache;
0416: }
0417:
0418: public IPlugin getDummyAppPlugin() {
0419: return _dummyPlugin;
0420: }
0421:
0422: public SquirrelResources getResources() {
0423: return _resources;
0424: }
0425:
0426: public IMessageHandler getMessageHandler() {
0427: return getMainFrame().getMessagePanel();
0428: }
0429:
0430: public SquirrelPreferences getSquirrelPreferences() {
0431: return _prefs;
0432: }
0433:
0434: public MainFrame getMainFrame() {
0435: // return _mainFrame;
0436: return _windowManager.getMainFrame();
0437: }
0438:
0439: /**
0440: * Retrieve the object that manages sessions.
0441: *
0442: * @return <TT>SessionManager</TT>.
0443: */
0444: public SessionManager getSessionManager() {
0445: return _sessionManager;
0446: }
0447:
0448: /**
0449: * Display an error message dialog.
0450: *
0451: * @param msg The error msg.
0452: */
0453: public void showErrorDialog(String msg) {
0454: new ErrorDialog(getMainFrame(), msg).setVisible(true);
0455: }
0456:
0457: /**
0458: * Display an error message dialog.
0459: *
0460: * @param th The Throwable that caused the error
0461: */
0462: public void showErrorDialog(Throwable th) {
0463: new ErrorDialog(getMainFrame(), th).setVisible(true);
0464: }
0465:
0466: /**
0467: * Display an error message dialog.
0468: *
0469: * @param msg The error msg.
0470: * @param th The Throwable that caused the error
0471: */
0472: public void showErrorDialog(String msg, Throwable th) {
0473: new ErrorDialog(getMainFrame(), msg, th).setVisible(true);
0474: }
0475:
0476: /**
0477: * Return the collection of <TT>FontInfo </TT> objects for this app.
0478: *
0479: *
0480: *@return the collection of <TT>FontInfo </TT> objects for this app.
0481: */
0482: public FontInfoStore getFontInfoStore() {
0483: return _fontInfoStore;
0484: }
0485:
0486: /**
0487: * Return the thread pool for this app.
0488: *
0489: * @return the thread pool for this app.
0490: */
0491: public TaskThreadPool getThreadPool() {
0492: return _threadPool;
0493: }
0494:
0495: public LoggerController getLoggerFactory() {
0496: return _loggerFactory;
0497: }
0498:
0499: /**
0500: * Return the factory object used to create the SQL entry panel.
0501: *
0502: * @return the factory object used to create the SQL entry panel.
0503: */
0504: public ISQLEntryPanelFactory getSQLEntryPanelFactory() {
0505: return _sqlEntryFactory;
0506: }
0507:
0508: /**
0509: * Set the factory object used to create the SQL entry panel.
0510: *
0511: * @param factory the factory object used to create the SQL entry panel.
0512: */
0513: public void setSQLEntryPanelFactory(ISQLEntryPanelFactory factory) {
0514: _sqlEntryFactory = factory != null ? factory
0515: : new DefaultSQLEntryPanelFactory();
0516: }
0517:
0518: /**
0519: * Retrieve the application level SQL History object.
0520: *
0521: * @return the application level SQL History object.
0522: */
0523: public SQLHistory getSQLHistory() {
0524: return _sqlHistory;
0525: }
0526:
0527: public synchronized void addToMenu(int menuId, JMenu menu) {
0528: final MainFrame mf = getMainFrame();
0529: if (mf != null) {
0530: mf.addToMenu(menuId, menu);
0531: } else {
0532: throw new IllegalStateException(s_stringMgr
0533: .getString("Application.error.menuadding"));
0534: }
0535: }
0536:
0537: public synchronized void addToMenu(int menuId, Action action) {
0538: final MainFrame mf = getMainFrame();
0539: if (mf != null) {
0540: mf.addToMenu(menuId, action);
0541: } else {
0542: throw new IllegalStateException(s_stringMgr
0543: .getString("Application.error.menuadding"));
0544: }
0545: }
0546:
0547: /**
0548: * Add component to the main frames status bar.
0549: *
0550: * @param comp Component to add.
0551: */
0552: public void addToStatusBar(JComponent comp) {
0553: final MainFrame mf = getMainFrame();
0554: if (mf != null) {
0555: mf.addToStatusBar(comp);
0556: } else {
0557: throw new IllegalStateException(s_stringMgr
0558: .getString("Application.error.compadding"));
0559: }
0560: }
0561:
0562: /**
0563: * Remove component to the main frames status bar.
0564: *
0565: * @param comp Component to remove.
0566: */
0567: public void removeFromStatusBar(JComponent comp) {
0568: final MainFrame mf = getMainFrame();
0569: if (mf != null) {
0570: mf.removeFromStatusBar(comp);
0571: } else {
0572: throw new IllegalStateException(s_stringMgr
0573: .getString("Application.error.compremoving"));
0574: }
0575: }
0576:
0577: /**
0578: * Launches the specified url in the system default web-browser
0579: *
0580: * @param url the URL of the web page to display.
0581: */
0582: public void openURL(String url) {
0583: BareBonesBrowserLaunch.openURL(url);
0584: }
0585:
0586: /**
0587: * Execute the taks required to start SQuirreL. Each of these is displayed
0588: * as a message on the splash screen (if one is being used) in order to let the
0589: * user know what is happening.
0590: *
0591: * @param splash The splash screen (can be null).
0592: * @param args Application arguments.
0593: *
0594: * @throws IllegalArgumentException
0595: * Thrown if <TT>ApplicationArguments<.TT> is null.
0596: */
0597: private void executeStartupTasks(SplashScreen splash,
0598: ApplicationArguments args) {
0599: if (args == null) {
0600: throw new IllegalArgumentException(
0601: "ApplicationArguments == null");
0602: }
0603:
0604: indicateNewStartupTask(splash, s_stringMgr
0605: .getString("Application.splash.createSessionManager"));
0606: // AliasMaintSheetFactory.initialize(this);
0607: // DriverMaintSheetFactory.initialize(this);
0608: _sessionManager = new SessionManager(this );
0609:
0610: indicateNewStartupTask(splash, s_stringMgr
0611: .getString("Application.splash.loadingprefs"));
0612:
0613: final boolean loadPlugins = args.getLoadPlugins();
0614: if (loadPlugins) {
0615: indicateNewStartupTask(splash, s_stringMgr
0616: .getString("Application.splash.loadingplugins"));
0617: } else {
0618: indicateNewStartupTask(splash, s_stringMgr
0619: .getString("Application.splash.notloadingplugins"));
0620: }
0621:
0622: UIFactory.initialize(_prefs, this );
0623: _pluginManager = new PluginManager(this );
0624: if (args.getLoadPlugins()) {
0625: if (null != splash
0626: && _prefs.getShowPluginFilesInSplashScreen()) {
0627: ClassLoaderListener listener = splash
0628: .getClassLoaderListener();
0629: _pluginManager.setClassLoaderListener(listener);
0630: }
0631: _pluginManager.loadPlugins();
0632: }
0633:
0634: indicateNewStartupTask(splash, s_stringMgr
0635: .getString("Application.splash.loadingactions"));
0636: _actions = new ActionCollection(this );
0637:
0638: indicateNewStartupTask(splash, s_stringMgr
0639: .getString("Application.splash.loadinguseracc"));
0640: _actions.loadActionKeys(_prefs.getActionKeys());
0641:
0642: indicateNewStartupTask(splash, s_stringMgr
0643: .getString("Application.splash.createjdbcmgr"));
0644: _driverMgr = new SQLDriverManager();
0645:
0646: // TODO: pass in a message handler so user gets error msgs.
0647: indicateNewStartupTask(splash, s_stringMgr
0648: .getString("Application.splash.loadingjdbc"));
0649: _appFiles = new ApplicationFiles();
0650:
0651: String errMsg = FileTransformer.transform(_appFiles);
0652: if (null != errMsg) {
0653: System.err.println(errMsg);
0654: JOptionPane.showMessageDialog(null, errMsg,
0655: "SQuirreL failed to start",
0656: JOptionPane.ERROR_MESSAGE);
0657: System.exit(-1);
0658: }
0659:
0660: _cache = new DataCache(_driverMgr, _appFiles
0661: .getDatabaseDriversFile(), _appFiles
0662: .getDatabaseAliasesFile(), _resources
0663: .getDefaultDriversUrl(), this );
0664:
0665: indicateNewStartupTask(splash, s_stringMgr
0666: .getString("Application.splash.createWindowManager"));
0667: _windowManager = new WindowManager(this );
0668:
0669: // _mainFrame = new MainFrame(this);
0670:
0671: indicateNewStartupTask(splash, s_stringMgr
0672: .getString("Application.splash.uifactoryinit"));
0673: // AliasMaintSheetFactory.initialize(this);
0674: // DriverMaintSheetFactory.initialize(this);
0675:
0676: String initializingPlugins = s_stringMgr
0677: .getString("Application.splash.initializingplugins");
0678: String notloadingplugins = s_stringMgr
0679: .getString("Application.splash.notloadingplugins");
0680: String task = (loadPlugins ? initializingPlugins
0681: : notloadingplugins);
0682: indicateNewStartupTask(splash, task);
0683: if (loadPlugins) {
0684: _pluginManager.initializePlugins();
0685: for (Iterator<PluginLoadInfo> it = _pluginManager
0686: .getPluginLoadInfoIterator(); it.hasNext();) {
0687: PluginLoadInfo pli = it.next();
0688: long created = pli.getCreationTime();
0689: long load = pli.getLoadTime();
0690: long init = pli.getInitializeTime();
0691: Object[] params = new Object[] { pli.getInternalName(),
0692: Long.valueOf(created), Long.valueOf(load),
0693: Long.valueOf(init),
0694: Long.valueOf(created + load + init) };
0695: String pluginLoadMsg = s_stringMgr.getString(
0696: "Application.splash.loadplugintime", params);
0697: s_log.info(pluginLoadMsg);
0698: }
0699: }
0700:
0701: // i18n[Application.splash.loadsqlhistory=Loading SQL history...]
0702: indicateNewStartupTask(splash, s_stringMgr
0703: .getString("Application.splash.loadsqlhistory"));
0704: loadSQLHistory();
0705:
0706: // i18n[Application.splash.loadcellselections=Loading Cell Import/Export selections...]
0707: indicateNewStartupTask(splash, s_stringMgr
0708: .getString("Application.splash.loadcellselections"));
0709: loadCellImportExportInfo();
0710:
0711: // i18n[Application.splash.loadeditselections=Loading Edit 'Where' Columns selections...]
0712: indicateNewStartupTask(splash, s_stringMgr
0713: .getString("Application.splash.loadeditselections"));
0714: loadEditWhereColsInfo();
0715:
0716: // i18n[Application.splash.loaddatatypeprops=Loading Data Type Properties...]
0717: indicateNewStartupTask(splash, s_stringMgr
0718: .getString("Application.splash.loaddatatypeprops"));
0719: loadDTProperties();
0720:
0721: // i18n[Application.splash.showmainwindow=Showing main window...]
0722: indicateNewStartupTask(splash, s_stringMgr
0723: .getString("Application.splash.showmainwindow"));
0724: _windowManager.moveToFront(_windowManager.getMainFrame());
0725: _threadPool.setParentForMessages(_windowManager.getMainFrame());
0726:
0727: // _mainFrame.setVisible(true);
0728: // _mainFrame.toFront(); // Required on Linux
0729:
0730: new ConnectToStartupAliasesCommand(this ).execute();
0731:
0732: if (_prefs.isFirstRun()) {
0733: try {
0734: new ViewHelpCommand(this ).execute();
0735: } catch (BaseException ex) {
0736: // i18n[Application.error.showhelpwindow=Error showing help window]
0737: s_log.error(s_stringMgr
0738: .getString("Application.error.showhelpwindow"),
0739: ex);
0740: }
0741: }
0742: }
0743:
0744: /**
0745: * If we are running with a splash screen then indicate in the splash
0746: * screen that a new task has commenced.
0747: *
0748: * @param splash Splash screen.
0749: * @param taskDescription Description of new task.
0750: */
0751: private void indicateNewStartupTask(SplashScreen splash,
0752: String taskDescription) {
0753: if (splash != null) {
0754: splash.indicateNewTask(taskDescription);
0755: }
0756: }
0757:
0758: private void preferencesHaveChanged(PropertyChangeEvent evt) {
0759: final String propName = evt != null ? evt.getPropertyName()
0760: : null;
0761:
0762: if (propName == null
0763: || propName
0764: .equals(SquirrelPreferences.IPropertyNames.SHOW_TOOLTIPS)) {
0765: ToolTipManager.sharedInstance().setEnabled(
0766: _prefs.getShowToolTips());
0767: }
0768:
0769: if (propName == null
0770: || propName
0771: .equals(SquirrelPreferences.IPropertyNames.JDBC_DEBUG_TYPE)) {
0772: setupJDBCLogging();
0773: }
0774:
0775: if (propName == null
0776: || propName
0777: .equals(SquirrelPreferences.IPropertyNames.LOGIN_TIMEOUT)) {
0778: DriverManager.setLoginTimeout(_prefs.getLoginTimeout());
0779: }
0780:
0781: if (propName == null
0782: || propName == SquirrelPreferences.IPropertyNames.PROXY) {
0783: new ProxyHandler().apply(_prefs.getProxySettings());
0784: }
0785: }
0786:
0787: /**
0788: * Load application level SQL History for the current user.
0789: */
0790: @SuppressWarnings("unchecked")
0791: private void loadSQLHistory() {
0792: try {
0793: XMLBeanReader doc = new XMLBeanReader();
0794: doc.load(new ApplicationFiles().getUserSQLHistoryFile());
0795: Iterator it = doc.iterator();
0796: if (it.hasNext()) {
0797: _sqlHistory = (SQLHistory) it.next();
0798: }
0799: } catch (FileNotFoundException ignore) {
0800: // History file not found for user - first time user ran pgm.
0801: } catch (Exception ex) {
0802: // i18n[Application.error.loadsqlhistory=Unable to load SQL history from persistant storage.]
0803: s_log.error(s_stringMgr
0804: .getString("Application.error.loadsqlhistory"), ex);
0805: } finally {
0806: if (_sqlHistory == null) {
0807: _sqlHistory = new SQLHistory();
0808: }
0809: }
0810: }
0811:
0812: /**
0813: * Save application level SQL history for current user.
0814: */
0815: private void saveSQLHistory() {
0816: // Get the history into an array.
0817: try {
0818: if (_prefs.getSessionProperties()
0819: .getLimitSQLEntryHistorySize())
0820: ;
0821: {
0822: SQLHistoryItem[] data = _sqlHistory.getData();
0823:
0824: int maxSize = _prefs.getSessionProperties()
0825: .getSQLEntryHistorySize();
0826: if (data.length > maxSize) {
0827: SQLHistoryItem[] reducedData = new SQLHistoryItem[maxSize];
0828: System.arraycopy(data, data.length - maxSize,
0829: reducedData, 0, maxSize);
0830: _sqlHistory.setData(reducedData);
0831: }
0832: }
0833:
0834: XMLBeanWriter wtr = new XMLBeanWriter(_sqlHistory);
0835: wtr.save(new ApplicationFiles().getUserSQLHistoryFile());
0836: } catch (Exception ex) {
0837: // i18n[Application.error.savesqlhistory=Unable to write SQL queries to persistant storage.]
0838: s_log.error(s_stringMgr
0839: .getString("Application.error.savesqlhistory"), ex);
0840: }
0841: }
0842:
0843: /**
0844: * Load the options previously selected by user for import/export of
0845: * data in various Cells.
0846: */
0847: @SuppressWarnings("unchecked")
0848: private void loadCellImportExportInfo() {
0849: CellImportExportInfoSaver saverInstance = null;
0850: try {
0851: XMLBeanReader doc = new XMLBeanReader();
0852: doc.load(new ApplicationFiles()
0853: .getCellImportExportSelectionsFile());
0854: Iterator it = doc.iterator();
0855: if (it.hasNext()) {
0856: saverInstance = (CellImportExportInfoSaver) it.next();
0857: }
0858: } catch (FileNotFoundException ignore) {
0859: // Cell Import/Export file not found for user - first time user ran pgm.
0860: } catch (Exception ex) {
0861: // i18n[Application.error.loadcellselections=Unable to load Cell Import/Export selections from persistant storage.]
0862: s_log.error(s_stringMgr
0863: .getString("Application.error.loadcellselections"),
0864: ex);
0865: } finally {
0866: // set the singleton instance of the Saver class to be the
0867: // instance just created by the XMLBeanReader
0868: CellImportExportInfoSaver.setInstance(saverInstance);
0869: }
0870: }
0871:
0872: /**
0873: * Save the options selected by user for Cell Import Export.
0874: */
0875: private void saveCellImportExportInfo() {
0876: try {
0877: XMLBeanWriter wtr = new XMLBeanWriter(
0878: CellImportExportInfoSaver.getInstance());
0879: wtr.save(new ApplicationFiles()
0880: .getCellImportExportSelectionsFile());
0881: } catch (Exception ex) {
0882: // i18n[Application.error.writecellselections=Unable to write Cell Import/Export options to persistant storage.]
0883: s_log
0884: .error(
0885: s_stringMgr
0886: .getString("Application.error.writecellselections"),
0887: ex);
0888: }
0889: }
0890:
0891: /**
0892: * Load the options previously selected by user for specific cols to use
0893: * in WHERE clause when editing cells.
0894: */
0895: @SuppressWarnings("all")
0896: private void loadEditWhereColsInfo() {
0897:
0898: try {
0899: XMLBeanReader doc = new XMLBeanReader();
0900: doc.load(new ApplicationFiles().getEditWhereColsFile());
0901: Iterator it = doc.iterator();
0902: if (it.hasNext()) {
0903: editWhereCols = (EditWhereCols) it.next();
0904: editWhereCols.setApplication(this );
0905: }
0906: } catch (FileNotFoundException ignore) {
0907: // Cell Import/Export file not found for user - first time user ran pgm.
0908: } catch (Exception ex) {
0909: // i18n[Application.error.loadcolsinfo=Unable to load Edit 'Where' Columns selections.]
0910: s_log.error(s_stringMgr
0911: .getString("Application.error.loadcolsinfo"), ex);
0912: } finally {
0913: // nothing needed here??
0914: }
0915: }
0916:
0917: /**
0918: * Save the options selected by user for Cell Import Export.
0919: */
0920: private void saveEditWhereColsInfo() {
0921: try {
0922: XMLBeanWriter wtr = new XMLBeanWriter(editWhereCols);
0923: wtr.save(new ApplicationFiles().getEditWhereColsFile());
0924: } catch (Exception ex) {
0925: // i18n[Application.error.savecolsinfo=Unable to write Edit Where Cols options to persistant storage.]
0926: s_log.error(s_stringMgr
0927: .getString("Application.error.savecolsinfo"), ex);
0928: }
0929: }
0930:
0931: /**
0932: * Load the options previously selected by user for specific cols to use
0933: * in WHERE clause when editing cells.
0934: */
0935: @SuppressWarnings("all")
0936: private void loadDTProperties() {
0937: DTProperties saverInstance = null;
0938: try {
0939: XMLBeanReader doc = new XMLBeanReader();
0940: doc.load(new ApplicationFiles().getDTPropertiesFile());
0941: Iterator<Object> it = doc.iterator();
0942: if (it.hasNext()) {
0943: saverInstance = (DTProperties) it.next();
0944: DTProperties x = saverInstance;
0945: }
0946: } catch (FileNotFoundException ignore) {
0947: // Cell Import/Export file not found for user - first time user ran pgm.
0948: } catch (Exception ex) {
0949: // i18n[Application.error.loaddatatypeprops=Unable to load DataType Properties selections from persistant storage.]
0950: s_log.error(s_stringMgr
0951: .getString("Application.error.loaddatatypeprops"),
0952: ex);
0953: } finally {
0954: // nothing needed here??
0955: }
0956: }
0957:
0958: /**
0959: * Save the options selected by user for Cell Import Export.
0960: */
0961: private void saveDataTypePreferences() {
0962: try {
0963: XMLBeanWriter wtr = new XMLBeanWriter(new DTProperties());
0964: wtr.save(new ApplicationFiles().getDTPropertiesFile());
0965: } catch (Exception ex) {
0966: //i18n[Application.error.savedatatypeprops=Unable to write DataType properties to persistant storage.]
0967: s_log.error(s_stringMgr
0968: .getString("Application.error.savedatatypeprops"),
0969: ex);
0970: }
0971: }
0972:
0973: /**
0974: * Persists the specified category of preferences to file if the user has
0975: * the "always save preferences immediately" preference checked.
0976: *
0977: * @param preferenceType the enumerated type that indicates what category
0978: * of preferences to be persisted.
0979: */
0980: public void savePreferences(PreferenceType preferenceType) {
0981: if (!_prefs.getSavePreferencesImmediately()) {
0982: return;
0983: }
0984: switch (preferenceType) {
0985: case ALIAS_DEFINITIONS:
0986: saveAliases();
0987: break;
0988: case DRIVER_DEFINITIONS:
0989: saveDrivers();
0990: break;
0991: case GLOBAL_PREFERENCES:
0992: saveGlobalPreferences();
0993: break;
0994: case DATATYPE_PREFERENCES:
0995: saveDataTypePreferences();
0996: break;
0997: case CELLIMPORTEXPORT_PREFERENCES:
0998: saveCellImportExportInfo();
0999: break;
1000: case SQLHISTORY:
1001: saveSQLHistory();
1002: break;
1003: case EDITWHERECOL_PREFERENCES:
1004: saveEditWhereColsInfo();
1005: break;
1006: default:
1007: s_log.error("Unknown preference type: " + preferenceType);
1008: }
1009: }
1010:
1011: /**
1012: * Setup applications Look and Feel.
1013: */
1014: private void setupLookAndFeel(ApplicationArguments args) {
1015: /*
1016: * Don't prevent the user from overriding the laf is they choose to use
1017: * Swing's default laf prop
1018: */
1019: String userSpecifiedOverride = System
1020: .getProperty("swing.defaultlaf");
1021: if (userSpecifiedOverride != null
1022: && !"".equals(userSpecifiedOverride)) {
1023: return;
1024: }
1025:
1026: String lafClassName = args.useNativeLAF() ? UIManager
1027: .getSystemLookAndFeelClassName()
1028: : MetalLookAndFeel.class.getName();
1029:
1030: if (!args.useDefaultMetalTheme()) {
1031: MetalLookAndFeel
1032: .setCurrentTheme(new AllBluesBoldMetalTheme());
1033: }
1034:
1035: try {
1036: // The following is a work-around for the problem on Mac OS X where
1037: // the Apple LAF delegates to the Swing Popup factory but then
1038: // tries to set a 90% alpha on the underlying Cocoa window, which
1039: // will always be null if you're using JGoodies L&F
1040: // see http://www.caimito.net/pebble/2005/07/26/1122392314480.html#comment1127522262179
1041: // This has no effect on Linux/Windows
1042: PopupFactory.setSharedInstance(new PopupFactory());
1043:
1044: UIManager.setLookAndFeel(lafClassName);
1045: } catch (Exception ex) {
1046: // i18n[Application.error.setlaf=Error setting LAF]
1047: s_log.error(s_stringMgr
1048: .getString("Application.error.setlaf"), ex);
1049: }
1050: }
1051:
1052: @SuppressWarnings("deprecation")
1053: private void setupJDBCLogging() {
1054: // If logging has changed.
1055: if (_jdbcDebugType != _prefs.getJdbcDebugType()) {
1056: final ApplicationFiles appFiles = new ApplicationFiles();
1057: final File outFile = appFiles.getJDBCDebugLogFile();
1058:
1059: // Close any previous logging.
1060: DriverManager.setLogStream(null);
1061: if (_jdbcDebugOutputStream != null) {
1062: _jdbcDebugOutputStream.close();
1063: _jdbcDebugOutputStream = null;
1064: }
1065: DriverManager.setLogWriter(null);
1066: if (_jdbcDebugOutputWriter != null) {
1067: _jdbcDebugOutputWriter.close();
1068: _jdbcDebugOutputWriter = null;
1069: }
1070:
1071: if (_prefs.isJdbcDebugToStream()) {
1072: try {
1073: // i18n[Application.info.setjdbcdebuglog=Attempting to set JDBC debug log to output stream]
1074: s_log
1075: .debug(s_stringMgr
1076: .getString("Application.info.setjdbcdebuglog"));
1077: _jdbcDebugOutputStream = new PrintStream(
1078: new FileOutputStream(outFile));
1079: DriverManager.setLogStream(_jdbcDebugOutputStream);
1080: // i18n[Application.info.setjdbcdebuglogsuccess=JDBC debug log set to output stream successfully]
1081: s_log
1082: .debug(s_stringMgr
1083: .getString("Application.info.setjdbcdebuglogsuccess"));
1084: } catch (IOException ex) {
1085: final String msg = s_stringMgr
1086: .getString("Application.error.jdbcstream");
1087: s_log.error(msg, ex);
1088: showErrorDialog(msg, ex);
1089: DriverManager.setLogStream(System.out);
1090: }
1091: }
1092:
1093: if (_prefs.isJdbcDebugToWriter()) {
1094: try {
1095: // i18n[Application.info.jdbcwriter=Attempting to set JDBC debug log to writer]
1096: s_log.debug(s_stringMgr
1097: .getString("Application.info.jdbcwriter"));
1098: _jdbcDebugOutputWriter = new PrintWriter(
1099: new FileWriter(outFile));
1100: DriverManager.setLogWriter(_jdbcDebugOutputWriter);
1101: // i18n[Application.info.jdbcwritersuccess=JDBC debug log set to writer successfully]
1102: s_log
1103: .debug(s_stringMgr
1104: .getString("Application.info.jdbcwritersuccess"));
1105: } catch (IOException ex) {
1106: final String msg = s_stringMgr
1107: .getString("Application.error.jdbcwriter");
1108: s_log.error(msg, ex);
1109: showErrorDialog(msg, ex);
1110: DriverManager.setLogWriter(new PrintWriter(
1111: System.out));
1112: }
1113: }
1114:
1115: _jdbcDebugType = _prefs.getJdbcDebugType();
1116: }
1117: }
1118: }
|