Revision: 6205
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6205&view=rev
Author: manningr
Date: 2011-03-23 00:46:58 +0000 (Wed, 23 Mar 2011)
Log Message:
-----------
Replaced direct java.io.File construction with FileWrapper (FileWrapperImpl), which is a framework that allows tests to control where files are created and/or control whether or not a file is created at all, or just in memory for the life of the test.
Modified Paths:
--------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/ApplicationArguments.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/HelpViewerWindow.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/DefaultPlugin.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/IPlugin.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PluginQueryTokenizerPreferencesManager.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PreferenceUtil.java
trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/FileWrapper.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/FileWrapperFactory.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/FileWrapperFactoryImpl.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/FileWrapperImpl.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/IOUtilities.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/util/IOUtilitiesImpl.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/xml/XMLBeanReader.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/xml/XMLBeanWriter.java
trunk/sql12/plugins/codecompletion/src/main/java/net/sourceforge/squirrel_sql/plugins/codecompletion/CodeCompletionPlugin.java
trunk/sql12/plugins/dataimport/src/main/java/net/sourceforge/squirrel_sql/plugins/dataimport/prefs/PreferencesManager.java
trunk/sql12/plugins/dbcopy/src/main/java/net/sourceforge/squirrel_sql/plugins/dbcopy/prefs/PreferencesManager.java
trunk/sql12/plugins/dbcopy/src/test/java/net/sourceforge/squirrel_sql/plugins/dbcopy/ColTypeMapperTest.java
trunk/sql12/plugins/dbdiff/src/main/java/net/sourceforge/squirrel_sql/plugins/dbdiff/prefs/DefaultPluginPreferencesManager.java
trunk/sql12/plugins/exportconfig/src/main/java/net/sourceforge/squirrel_sql/plugins/exportconfig/ExportConfigPlugin.java
trunk/sql12/plugins/favs/src/main/java/net/sourceforge/squirrel_sql/plugins/favs/FoldersCache.java
trunk/sql12/plugins/firebirdmanager/src/main/java/net/sourceforge/squirrel_sql/plugins/firebirdmanager/pref/PreferencesManager.java
trunk/sql12/plugins/graph/src/main/java/net/sourceforge/squirrel_sql/plugins/graph/xmlbeans/GraphXmlSerializer.java
trunk/sql12/plugins/hibernate/src/main/java/net/sourceforge/squirrel_sql/plugins/hibernate/configuration/HibernateConfigController.java
trunk/sql12/plugins/hibernate/src/main/java/net/sourceforge/squirrel_sql/plugins/hibernate/util/HibernateUtil.java
trunk/sql12/plugins/laf/src/main/java/net/sourceforge/squirrel_sql/plugins/laf/LAFPlugin.java
trunk/sql12/plugins/laf/src/main/java/net/sourceforge/squirrel_sql/plugins/laf/LAFRegister.java
trunk/sql12/plugins/laf/src/main/java/net/sourceforge/squirrel_sql/plugins/laf/SkinLookAndFeelController.java
trunk/sql12/plugins/mssql/src/main/java/net/sourceforge/squirrel_sql/plugins/mssql/MssqlPlugin.java
trunk/sql12/plugins/mssql/src/main/java/net/sourceforge/squirrel_sql/plugins/mssql/prefs/PreferencesManager.java
trunk/sql12/plugins/refactoring/src/main/java/net/sourceforge/squirrel_sql/plugins/refactoring/prefs/RefactoringPreferencesManager.java
trunk/sql12/plugins/sessionscript/src/main/java/net/sourceforge/squirrel_sql/plugins/sessionscript/SessionScriptPlugin.java
trunk/sql12/plugins/sqlbookmark/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlbookmark/BookmarkManager.java
trunk/sql12/plugins/sqlbookmark/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlbookmark/SQLBookmarkPlugin.java
trunk/sql12/plugins/sqlreplace/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlreplace/ReplacementManager.java
trunk/sql12/plugins/sqlreplace/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlreplace/SQLReplacePlugin.java
trunk/sql12/plugins/sqlreplace/src/test/java/net/sourceforge/squirrel_sql/plugins/sqlreplace/ReplacementManagerTest.java
trunk/sql12/plugins/sqlscript/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlscript/prefs/SQLScriptPreferencesManager.java
trunk/sql12/plugins/sqlval/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlval/SQLValidatorPlugin.java
trunk/sql12/plugins/syntax/src/main/java/net/sourceforge/squirrel_sql/plugins/syntax/AutoCorrectProviderImpl.java
trunk/sql12/plugins/syntax/src/main/java/net/sourceforge/squirrel_sql/plugins/syntax/SyntaxPlugin.java
Added Paths:
-----------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IApplicationArguments.java
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/ApplicationArguments.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/ApplicationArguments.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/ApplicationArguments.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -41,7 +41,7 @@
*
* @author <A HREF="mailto:co...@us...">Colin Bell</A>
*/
-public class ApplicationArguments
+public class ApplicationArguments implements IApplicationArguments
{
/**
* Option descriptions.
@@ -201,8 +201,7 @@
}
/**
- * @return override for the user settings directory. Will be
- * <TT>null</TT> if not overridden.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getSquirrelHomeDirectory()
*/
public String getSquirrelHomeDirectory()
{
@@ -210,7 +209,7 @@
}
/**
- * @return The name of the directory that Squirrel is installed into.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getUserSettingsDirectoryOverride()
*/
public String getUserSettingsDirectoryOverride()
{
@@ -218,7 +217,7 @@
}
/**
- * @return <TT>true</TT> if splashscreen should be shown.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getShowSplashScreen()
*/
public boolean getShowSplashScreen()
{
@@ -226,8 +225,7 @@
}
/**
- * @return <TT>true</TT> if help information should be written to
- * standard output.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getShowHelp()
*/
public boolean getShowHelp()
{
@@ -235,8 +233,7 @@
}
/**
- * @return the logging configuration file name. Will be
- * <TT>null</TT> if not passed.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getLoggingConfigFileName()
*/
public String getLoggingConfigFileName()
{
@@ -244,7 +241,7 @@
}
/**
- * @return <TT>true</TT> if the plugins should be loaded.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getLoadPlugins()
*/
public boolean getLoadPlugins()
{
@@ -252,8 +249,7 @@
}
/**
- * @return <TT>true</TT> if the default metal theme should be used
- * rather than the SQuirreL metal theme.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#useDefaultMetalTheme()
*/
public boolean useDefaultMetalTheme()
{
@@ -261,9 +257,7 @@
}
/**
- * Retrieve whether to use the native Look and Feel.
- *
- * @return <TT>true</TT> to use the native LAF.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#useNativeLAF()
*/
public boolean useNativeLAF()
{
@@ -271,7 +265,7 @@
}
/**
- * @return The raw arguments passed on the command line.
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getRawArguments()
*/
public String[] getRawArguments()
{
@@ -279,12 +273,15 @@
}
/**
- * @return a boolean indicating whether or not to enable user interface debugging mode
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getUserInterfaceDebugEnabled()
*/
public boolean getUserInterfaceDebugEnabled() {
return _cmdLine.hasOption(IOptions.UI_DEBUG[0]);
}
+ /**
+ * @see net.sourceforge.squirrel_sql.client.IApplicationArguments#getPluginList()
+ */
public List<String> getPluginList() {
return _pluginList;
}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IApplicationArguments.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IApplicationArguments.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IApplicationArguments.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -0,0 +1,88 @@
+package net.sourceforge.squirrel_sql.client;
+
+import java.util.List;
+
+/*
+ * Copyright (C) 2011 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+public interface IApplicationArguments
+{
+
+ /**
+ * @return override for the user settings directory. Will be
+ * <TT>null</TT> if not overridden.
+ */
+ String getSquirrelHomeDirectory();
+
+ /**
+ * @return The name of the directory that Squirrel is installed into.
+ */
+ String getUserSettingsDirectoryOverride();
+
+ /**
+ * @return <TT>true</TT> if splashscreen should be shown.
+ */
+ boolean getShowSplashScreen();
+
+ /**
+ * @return <TT>true</TT> if help information should be written to
+ * standard output.
+ */
+ boolean getShowHelp();
+
+ /**
+ * @return the logging configuration file name. Will be
+ * <TT>null</TT> if not passed.
+ */
+ String getLoggingConfigFileName();
+
+ /**
+ * @return <TT>true</TT> if the plugins should be loaded.
+ */
+ boolean getLoadPlugins();
+
+ /**
+ * @return <TT>true</TT> if the default metal theme should be used
+ * rather than the SQuirreL metal theme.
+ */
+ boolean useDefaultMetalTheme();
+
+ /**
+ * Retrieve whether to use the native Look and Feel.
+ *
+ * @return <TT>true</TT> to use the native LAF.
+ */
+ boolean useNativeLAF();
+
+ /**
+ * @return The raw arguments passed on the command line.
+ */
+ String[] getRawArguments();
+
+ /**
+ * @return a boolean indicating whether or not to enable user interface debugging mode
+ */
+ boolean getUserInterfaceDebugEnabled();
+
+ /**
+ * @return
+ */
+ List<String> getPluginList();
+
+}
\ No newline at end of file
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/HelpViewerWindow.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/HelpViewerWindow.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/HelpViewerWindow.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -1,4 +1,5 @@
package net.sourceforge.squirrel_sql.client.gui;
+
/*
* Copyright (C) 2003-2004 Colin Bell
* co...@us...
@@ -21,8 +22,6 @@
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
-import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
@@ -47,30 +46,34 @@
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
+import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappers;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappersImpl;
import net.sourceforge.squirrel_sql.fw.gui.StatusBar;
import net.sourceforge.squirrel_sql.fw.util.BaseException;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactoryImpl;
import net.sourceforge.squirrel_sql.fw.util.StringManager;
import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
+import net.sourceforge.squirrel_sql.fw.util.Utilities;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
-import net.sourceforge.squirrel_sql.client.IApplication;
-import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
-import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
-import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
/**
* This window shows the SQuirreL Help files.
- *
+ *
* @author <A HREF="mailto:co...@us...">Colin Bell</A>
*/
public class HelpViewerWindow extends JFrame
{
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- /** Logger for this class. */
- private final static ILogger s_log =
- LoggerController.createLogger(HelpViewerWindow.class);
+ /** Logger for this class. */
+ private final static ILogger s_log = LoggerController.createLogger(HelpViewerWindow.class);
/** Internationalized strings for this class. */
private static final StringManager s_stringMgr =
@@ -94,18 +97,23 @@
/** Collection of the nodes in the tree keyed by the URL.toString(). */
private final Map<String, DefaultMutableTreeNode> _nodes = new HashMap<String, DefaultMutableTreeNode>();
+ /** factory for creating FileWrappers which insulate the application from direct reference to File */
+ private FileWrapperFactory fileWrapperFactory = new FileWrapperFactoryImpl();
+
+ /** A FileWrapper-enhanced version of ApplicationFiles that removes direct references to File */
+ private ApplicationFileWrappers applicationFiles = new ApplicationFileWrappersImpl();
+
/**
* Ctor.
- *
- * @param app Application API.
- *
- * @throws IllegalArgumentException
- * Thrown if <TT>null</TT> <TT>IApplication</TT> passed.
+ *
+ * @param app
+ * Application API.
+ * @throws IllegalArgumentException
+ * Thrown if <TT>null</TT> <TT>IApplication</TT> passed.
*/
- public HelpViewerWindow(IApplication app)
- throws IllegalArgumentException, BaseException
+ public HelpViewerWindow(IApplication app) throws IllegalArgumentException, BaseException
{
- // i18n[HelpViewerWindow.title=SQuirreL SQL Client Help]
+ // i18n[HelpViewerWindow.title=SQuirreL SQL Client Help]
super(s_stringMgr.getString("HelpViewerWindow.title"));
if (app == null)
{
@@ -123,21 +131,42 @@
}
/**
+ * @param fileWrapperFactory
+ * the fileWrapperFactory to set
+ */
+ public void setFileWrapperFactory(FileWrapperFactory fileWrapperFactory)
+ {
+ Utilities.checkNull("setFileWrapperFactory", "fileWrapperFactory", fileWrapperFactory);
+ this.fileWrapperFactory = fileWrapperFactory;
+ }
+
+ /**
+ * @param applicationFiles the applicationFiles to set
+ */
+ public void setApplicationFiles(ApplicationFileWrappers applicationFiles)
+ {
+ Utilities.checkNull("setApplicationFiles", "applicationFiles", applicationFiles);
+ this.applicationFiles = applicationFiles;
+ }
+
+
+ /**
* Set the Document displayed to that defined by the passed URL.
- *
- * @param url URL of document to be displayed.
+ *
+ * @param url
+ * URL of document to be displayed.
*/
private void setSelectedDocument(URL url)
{
try
{
_detailPnl.gotoURL(url);
- // i18n[HelpViewerWindow.pageloaded=Page loaded.]
+ // i18n[HelpViewerWindow.pageloaded=Page loaded.]
_statusBar.setText(s_stringMgr.getString("HelpViewerWindow.pageloaded"));
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.displaydocument=Error displaying document]
+ // i18n[HelpViewerWindow.error.displaydocument=Error displaying document]
s_log.error(s_stringMgr.getString("HelpViewerWindow.error.displaydocument"), ex);
_statusBar.setText(ex.toString());
}
@@ -148,14 +177,14 @@
// Strip local part of URL.
String key = url.toString();
final int idx = key.lastIndexOf('#');
- if ( idx > -1)
+ if (idx > -1)
{
key = key.substring(0, idx);
}
DefaultMutableTreeNode node = _nodes.get(key);
if (node != null) // && node != _tree.getLastSelectedPathComponent())
{
- DefaultTreeModel model = (DefaultTreeModel)_tree.getModel();
+ DefaultTreeModel model = (DefaultTreeModel) _tree.getModel();
TreePath path = new TreePath(model.getPathToRoot(node));
if (path != null)
{
@@ -205,15 +234,15 @@
{
_detailPnl.setHomeURL(_homeURL);
_tree.expandRow(0);
- _tree.expandRow(2);
- if (_app.getSquirrelPreferences().isFirstRun())
- {
- _tree.setSelectionRow(1);
- }
- else
- {
- _tree.setSelectionRow(3);
- }
+ _tree.expandRow(2);
+ if (_app.getSquirrelPreferences().isFirstRun())
+ {
+ _tree.setSelectionRow(1);
+ }
+ else
+ {
+ _tree.setSelectionRow(3);
+ }
_tree.setRootVisible(false);
}
});
@@ -224,6 +253,7 @@
{
selectTreeNodeForURL(evt.getHtmlViewerPanel().getURL());
}
+
public void homeURLHasChanged(HtmlViewerPanelListenerEvent evt)
{
// Nothing to do.
@@ -233,13 +263,13 @@
/**
* Create a tree each node being a link to a document.
- *
- * @return The contents tree.
+ *
+ * @return The contents tree.
*/
private JScrollPane createContentsTree() throws IOException
{
- final ApplicationFiles appFiles = new ApplicationFiles();
- // i18n[HelpViewerWindow.help=Help]
+
+ // i18n[HelpViewerWindow.help=Help]
final FolderNode root = new FolderNode(s_stringMgr.getString("HelpViewerWindow.help"));
_tree = new JTree(new DefaultTreeModel(root));
_tree.setShowsRootHandles(true);
@@ -253,43 +283,41 @@
renderer.setClosedIcon(rsrc.getIcon(SquirrelResources.IImageNames.HELP_TOC_CLOSED));
_tree.setCellRenderer(renderer);
- // First put the Welcome to SQuirreL node.
- File file = appFiles.getWelcomeFile();
- try
- {
- // i18n[HelpViewerWindow.welcome=Welcome]
- DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.welcome"), file);
- root.add(dn);
- _nodes.put(dn.getURL().toString(), dn);
- }
- catch (MalformedURLException ex)
- {
- // i18n[HelpViewerWindow.error.loadwelcomefile=Error retrieving Welcome file URL for {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadwelcomefile",
- file.getAbsolutePath());
- s_log.error(msg, ex);
- }
+ // First put the Welcome to SQuirreL node.
+ FileWrapper file = applicationFiles.getWelcomeFile();
+ try
+ {
+ // i18n[HelpViewerWindow.welcome=Welcome]
+ DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.welcome"), file);
+ root.add(dn);
+ _nodes.put(dn.getURL().toString(), dn);
+ }
+ catch (MalformedURLException ex)
+ {
+ // i18n[HelpViewerWindow.error.loadwelcomefile=Error retrieving Welcome file URL for {0}]
+ String msg = s_stringMgr.getString("HelpViewerWindow.error.loadwelcomefile", file.getAbsolutePath());
+ s_log.error(msg, ex);
+ }
// Add Help, Licence and Change Log nodes to the tree.
- // i18n[HelpViewerWindow.help=Help]
+ // i18n[HelpViewerWindow.help=Help]
final FolderNode helpRoot = new FolderNode(s_stringMgr.getString("HelpViewerWindow.help"));
root.add(helpRoot);
_nodes.put(helpRoot.getURL().toString(), helpRoot);
- // i18n[HelpViewerWindow.licences=Licences]
+ // i18n[HelpViewerWindow.licences=Licences]
final FolderNode licenceRoot = new FolderNode(s_stringMgr.getString("HelpViewerWindow.licences"));
root.add(licenceRoot);
_nodes.put(licenceRoot.getURL().toString(), licenceRoot);
- // i18n[HelpViewerWindow.changelogs=Change Logs]
+ // i18n[HelpViewerWindow.changelogs=Change Logs]
final FolderNode changeLogRoot = new FolderNode(s_stringMgr.getString("HelpViewerWindow.changelogs"));
root.add(changeLogRoot);
_nodes.put(changeLogRoot.getURL().toString(), changeLogRoot);
// Add SQuirreL help to the Help node.
- file = appFiles.getQuickStartGuideFile();
+ file = applicationFiles.getQuickStartGuideFile();
try
{
- // i18n[HelpViewerWindow.squirrel=SQuirreL]
+ // i18n[HelpViewerWindow.squirrel=SQuirreL]
DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.squirrel"), file);
helpRoot.add(dn);
_homeURL = dn.getURL();
@@ -297,46 +325,41 @@
}
catch (MalformedURLException ex)
{
- // i18n[HelpViewerWindow.error.loadwelcomefile=Error retrieving Help file URL for {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadhelpfile",
- file.getAbsolutePath());
+ // i18n[HelpViewerWindow.error.loadwelcomefile=Error retrieving Help file URL for {0}]
+ String msg = s_stringMgr.getString("HelpViewerWindow.error.loadhelpfile", file.getAbsolutePath());
s_log.error(msg, ex);
}
// Add SQuirreL Licence to the Licence node.
- file = appFiles.getLicenceFile();
+ file = applicationFiles.getLicenceFile();
try
{
- // i18n[HelpViewerWindow.squirrel=SQuirreL]
+ // i18n[HelpViewerWindow.squirrel=SQuirreL]
DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.squirrel"), file);
licenceRoot.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
catch (MalformedURLException ex)
{
- // i18n[HelpViewerWindow.error.loadlicencefile=Error retrieving Licence file URL for {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadlicencefile",
- file.getAbsolutePath());
+ // i18n[HelpViewerWindow.error.loadlicencefile=Error retrieving Licence file URL for {0}]
+ String msg = s_stringMgr.getString("HelpViewerWindow.error.loadlicencefile", file.getAbsolutePath());
s_log.error(msg, ex);
}
// Add SQuirreL Change Log to the Licence node.
- file = appFiles.getChangeLogFile();
+ file = applicationFiles.getChangeLogFile();
try
{
- // i18n[HelpViewerWindow.squirrel=SQuirreL]
+ // i18n[HelpViewerWindow.squirrel=SQuirreL]
DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.squirrel"), file);
changeLogRoot.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
catch (MalformedURLException ex)
{
- // i18n[HelpViewerWindow.error.loadchangelogfile=Error retrieving Change Log file URL for {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadchangelogfile",
- file.getAbsolutePath());
+ // i18n[HelpViewerWindow.error.loadchangelogfile=Error retrieving Change Log file URL for {0}]
+ String msg =
+ s_stringMgr.getString("HelpViewerWindow.error.loadchangelogfile", file.getAbsolutePath());
s_log.error(msg, ex);
}
@@ -346,7 +369,7 @@
{
try
{
- final File dir = pi[i].getPlugin().getPluginAppSettingsFolder();
+ final FileWrapper dir = pi[i].getPlugin().getPluginAppSettingsFolder();
final String title = pi[i].getDescriptiveName();
// Help document.
@@ -355,17 +378,16 @@
final String fn = pi[i].getHelpFileName();
if (fn != null && fn.length() > 0)
{
- DocumentNode dn = new DocumentNode(title, new File(dir, fn));
+ DocumentNode dn = new DocumentNode(title, fileWrapperFactory.create(dir, fn));
helpRoot.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.loadpluginhelp=Error generating Help entry for plugin {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadpluginhelp",
- pi[i].getDescriptiveName());
+ // i18n[HelpViewerWindow.error.loadpluginhelp=Error generating Help entry for plugin {0}]
+ String msg =
+ s_stringMgr.getString("HelpViewerWindow.error.loadpluginhelp", pi[i].getDescriptiveName());
s_log.error(msg, ex);
}
@@ -375,17 +397,17 @@
final String fn = pi[i].getLicenceFileName();
if (fn != null && fn.length() > 0)
{
- DocumentNode dn = new DocumentNode(title, new File(dir, fn));
+ DocumentNode dn = new DocumentNode(title, fileWrapperFactory.create(dir, fn));
licenceRoot.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.loadpluginlicence=Error generating Licence entry for plugin {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadpluginlicence",
- pi[i].getDescriptiveName());
+ // i18n[HelpViewerWindow.error.loadpluginlicence=Error generating Licence entry for plugin {0}]
+ String msg =
+ s_stringMgr.getString("HelpViewerWindow.error.loadpluginlicence",
+ pi[i].getDescriptiveName());
s_log.error(msg, ex);
}
@@ -395,45 +417,42 @@
final String fn = pi[i].getChangeLogFileName();
if (fn != null && fn.length() > 0)
{
- DocumentNode dn = new DocumentNode(title, new File(dir, fn));
+ DocumentNode dn = new DocumentNode(title, fileWrapperFactory.create(dir, fn));
changeLogRoot.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.loadchangelog=Error generating Change Log entry for plugin {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadchangelog",
- pi[i].getDescriptiveName());
+ // i18n[HelpViewerWindow.error.loadchangelog=Error generating Change Log entry for plugin {0}]
+ String msg =
+ s_stringMgr.getString("HelpViewerWindow.error.loadchangelog", pi[i].getDescriptiveName());
s_log.error(msg, ex);
}
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.loadpluginsettings=Error retrieving app settings folder for plugin {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadpluginsettings",
- pi[i].getDescriptiveName());
+ // i18n[HelpViewerWindow.error.loadpluginsettings=Error retrieving app settings folder for plugin
+ // {0}]
+ String msg =
+ s_stringMgr.getString("HelpViewerWindow.error.loadpluginsettings", pi[i].getDescriptiveName());
s_log.error(msg, ex);
}
}
// FAQ.
- file = appFiles.getFAQFile();
+ file = applicationFiles.getFAQFile();
try
{
- // i18n[HelpViewerWindow.faq=FAQ]
+ // i18n[HelpViewerWindow.faq=FAQ]
DocumentNode dn = new DocumentNode(s_stringMgr.getString("HelpViewerWindow.faq"), file);
root.add(dn);
_nodes.put(dn.getURL().toString(), dn);
}
catch (MalformedURLException ex)
{
- // i18n[HelpViewerWindow.error.loadfaqfile=Error retrieving FAQ from URL = {0}]
- String msg =
- s_stringMgr.getString("HelpViewerWindow.error.loadfaqfile",
- file.getAbsolutePath());
+ // i18n[HelpViewerWindow.error.loadfaqfile=Error retrieving FAQ from URL = {0}]
+ String msg = s_stringMgr.getString("HelpViewerWindow.error.loadfaqfile", file.getAbsolutePath());
s_log.error(msg, ex);
}
@@ -454,14 +473,13 @@
return _detailPnl;
}
-
private class DocumentNode extends DefaultMutableTreeNode
{
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- private URL _url;
+ private URL _url;
- DocumentNode(String title, File file) throws MalformedURLException
+ DocumentNode(String title, FileWrapper file) throws MalformedURLException
{
super(title, false);
setFile(file);
@@ -477,7 +495,7 @@
return _url;
}
- void setFile(File file) throws MalformedURLException
+ void setFile(FileWrapper file) throws MalformedURLException
{
_url = file.toURI().toURL();
}
@@ -485,15 +503,18 @@
private class FolderNode extends DocumentNode
{
- private static final long serialVersionUID = 1L;
- private final List<String> _docTitles = new ArrayList<String>();
+ private static final long serialVersionUID = 1L;
+
+ private final List<String> _docTitles = new ArrayList<String>();
+
private final List<URL> _docURLs = new ArrayList<URL>();
- private final File _contentsFile;
+ private final FileWrapper _contentsFile;
+
FolderNode(String title) throws IOException
{
super(title, true);
- _contentsFile = File.createTempFile("sqschelp", "html");
+ _contentsFile = fileWrapperFactory.createTempFile("sqschelp", "html");
_contentsFile.deleteOnExit();
setFile(_contentsFile);
}
@@ -503,7 +524,7 @@
super.add(node);
if (node instanceof DocumentNode)
{
- final DocumentNode dn = (DocumentNode)node;
+ final DocumentNode dn = (DocumentNode) node;
final URL docURL = dn.getURL();
if (docURL != null)
{
@@ -522,24 +543,19 @@
{
try
{
- final PrintWriter pw = new PrintWriter(new FileWriter(_contentsFile));
+ final PrintWriter pw = new PrintWriter(_contentsFile.getFileWriter());
try
{
StringBuffer buf = new StringBuffer(50);
- buf.append("<HTML><BODY><H1>")
- .append(toString())
- .append("</H1>");
+ buf.append("<HTML><BODY><H1>").append(toString()).append("</H1>");
pw.println(buf.toString());
for (int i = 0, limit = _docTitles.size(); i < limit; ++i)
{
-// final String docTitle = (String)_docTitles.get(i);
+ // final String docTitle = (String)_docTitles.get(i);
final URL docUrl = _docURLs.get(i);
buf = new StringBuffer(50);
- buf.append("<A HREF=\"")
- .append(docUrl)
- .append("\">")
- .append(_docTitles.get(i))
- .append("</A><BR>");
+ buf.append("<A HREF=\"").append(docUrl).append("\">").append(_docTitles.get(i)).append(
+ "</A><BR>");
pw.println(buf.toString());
}
pw.println("</BODY></HTML");
@@ -551,7 +567,7 @@
}
catch (IOException ex)
{
- // i18n[HelpViewerWindow.error.congen=Error generating Contents file]
+ // i18n[HelpViewerWindow.error.congen=Error generating Contents file]
String msg = s_stringMgr.getString("HelpViewerWindow.error.congen");
s_log.error(msg, ex);
_statusBar.setText(msg);
@@ -560,11 +576,10 @@
}
/**
- * This class listens for changes in the node selected in the tree
- * and displays the appropriate help document for the node.
+ * This class listens for changes in the node selected in the tree and displays the appropriate help
+ * document for the node.
*/
- private final class ObjectTreeSelectionListener
- implements TreeSelectionListener
+ private final class ObjectTreeSelectionListener implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent evt)
{
@@ -574,7 +589,7 @@
Object lastComp = path.getLastPathComponent();
if (lastComp instanceof DocumentNode)
{
- setSelectedDocument(((DocumentNode)lastComp).getURL());
+ setSelectedDocument(((DocumentNode) lastComp).getURL());
}
}
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/DefaultPlugin.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/DefaultPlugin.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/DefaultPlugin.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -17,6 +17,7 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
import java.io.File;
import java.io.IOException;
@@ -26,9 +27,15 @@
import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappers;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappersImpl;
import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactoryImpl;
import net.sourceforge.squirrel_sql.fw.util.StringManager;
import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+import net.sourceforge.squirrel_sql.fw.util.Utilities;
public abstract class DefaultPlugin implements IPlugin
{
@@ -39,6 +46,12 @@
/** Current application API. */
protected IApplication _app;
+ /** A FileWrapper-enhanced version of ApplicationFiles that removes direct references to File */
+ protected ApplicationFileWrappers applicationFiles = new ApplicationFileWrappersImpl();
+
+ /** factory for creating FileWrappers which insulate the application from direct reference to File */
+ protected FileWrapperFactory fileWrapperFactory = new FileWrapperFactoryImpl();
+
/**
* Called on application startup before application started up.
*
@@ -143,7 +156,7 @@
*
* @return The current application API.
*/
- public final IApplication getApplication()
+ public IApplication getApplication()
{
return _app;
}
@@ -164,12 +177,12 @@
* @throws IOException
* An error occured retrieving/creating the folder.
*/
- public synchronized File getPluginAppSettingsFolder()
+ public synchronized FileWrapper getPluginAppSettingsFolder()
throws IllegalStateException, IOException
{
String pluginAppFolderProp = System.getProperty("pluginAppFolder");
if (pluginAppFolderProp != null) {
- return new File(pluginAppFolderProp);
+ return fileWrapperFactory.create(pluginAppFolderProp);
}
final String internalName = getInternalName();
@@ -177,8 +190,8 @@
{
throw new IllegalStateException("IPlugin doesn't have a valid internal name");
}
- final File pluginDir = new ApplicationFiles().getPluginsDirectory();
- final File file = new File(pluginDir, internalName);
+ final FileWrapper pluginDir = applicationFiles.getPluginsDirectory();
+ final FileWrapper file = fileWrapperFactory.create(pluginDir, internalName);
if (!file.exists())
{
file.mkdirs();
@@ -197,12 +210,12 @@
*/
public synchronized String getPluginJarFilePath() throws IllegalStateException {
final String internalName = getInternalName();
- final File pluginDir = new ApplicationFiles().getPluginsDirectory();
+ final FileWrapper pluginDir = applicationFiles.getPluginsDirectory();
if (internalName == null || internalName.trim().length() == 0)
{
throw new IllegalStateException("IPlugin doesn't have a valid internal name");
}
- final File resultFile = new File(pluginDir, internalName + ".jar");
+ final FileWrapper resultFile = fileWrapperFactory.create(pluginDir, internalName + ".jar");
return resultFile.getAbsolutePath();
}
@@ -221,8 +234,10 @@
*
* @throws IOException
* An error occured retrieving/creating the folder.
+ *
+ * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getPluginUserSettingsFolder()
*/
- public synchronized File getPluginUserSettingsFolder()
+ public synchronized FileWrapper getPluginUserSettingsFolder()
throws IllegalStateException, IOException
{
final String internalName = getInternalName();
@@ -235,7 +250,7 @@
+ File.separator
+ internalName
+ File.separator;
- File file = new File(name);
+ FileWrapper file = fileWrapperFactory.create(name);
if (!file.exists())
{
file.mkdirs();
@@ -293,6 +308,24 @@
{
return null;
}
+
+ /**
+ * @param applicationFiles the applicationFiles to set
+ */
+ public void setApplicationFiles(ApplicationFileWrappers applicationFiles)
+ {
+ Utilities.checkNull("setApplicationFiles", "applicationFiles", applicationFiles);
+ this.applicationFiles = applicationFiles;
+ }
+
+ /**
+ * @param fileWrapperFactory the fileWrapperFactory to set
+ */
+ public void setFileWrapperFactory(FileWrapperFactory fileWrapperFactory)
+ {
+ Utilities.checkNull("setFileWrapperFactory", "fileWrapperFactory", fileWrapperFactory);
+ this.fileWrapperFactory = fileWrapperFactory;
+ }
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/IPlugin.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/IPlugin.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/IPlugin.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -17,7 +17,7 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-import java.io.File;
+
import java.io.IOException;
import net.sourceforge.squirrel_sql.client.IApplication;
@@ -25,6 +25,9 @@
import net.sourceforge.squirrel_sql.client.gui.db.aliasproperties.IAliasPropertiesPanelController;
import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappers;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
/**
* Base interface for all plugins.
* <P>
@@ -179,7 +182,7 @@
* @throws IOException
* An error occured retrieving/creating the folder.
*/
- File getPluginAppSettingsFolder() throws IOException, IllegalStateException;
+ FileWrapper getPluginAppSettingsFolder() throws IOException, IllegalStateException;
/**
* Return the folder with the users home directory
@@ -197,7 +200,7 @@
* @throws IOException
* An error occured retrieving/creating the folder.
*/
- File getPluginUserSettingsFolder() throws IllegalStateException, IOException;
+ FileWrapper getPluginUserSettingsFolder() throws IllegalStateException, IOException;
/**
* Allows a plugin to provide services to other plugins.
@@ -214,4 +217,15 @@
*/
String getPluginJarFilePath() throws IllegalStateException;
+ /**
+ * @param applicationFiles the applicationFiles to set
+ */
+ public void setApplicationFiles(ApplicationFileWrappers applicationFiles);
+
+ /**
+ * @param fileWrapperFactory the fileWrapperFactory to set
+ */
+ public void setFileWrapperFactory(FileWrapperFactory fileWrapperFactory);
+
+
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PluginQueryTokenizerPreferencesManager.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PluginQueryTokenizerPreferencesManager.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PluginQueryTokenizerPreferencesManager.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -18,152 +18,181 @@
*/
package net.sourceforge.squirrel_sql.client.plugin;
-import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import net.sourceforge.squirrel_sql.client.Version;
-import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
-import net.sourceforge.squirrel_sql.client.plugin.PluginException;
-import net.sourceforge.squirrel_sql.client.plugin.PreferenceUtil;
import net.sourceforge.squirrel_sql.fw.preferences.IQueryTokenizerPreferenceBean;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactoryImpl;
+import net.sourceforge.squirrel_sql.fw.util.Utilities;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
/**
- * This is intended to be used by plugins that have custom IQueryTokenizers,
- * which require some sort of preference management (loading/storing preference
- * beans to XML file and vice-versa). In reality this functionality isn't
- * specific to IQueryTokenizer preferences, so this could be further generalized.
- * There is no need at the present time to do so.
+ * This is intended to be used by plugins that have custom IQueryTokenizers, which require some sort of
+ * preference management (loading/storing preference beans to XML file and vice-versa). In reality this
+ * functionality isn't specific to IQueryTokenizer preferences, so this could be further generalized. There is
+ * no need at the present time to do so.
*
* @author manningr
*/
-public class PluginQueryTokenizerPreferencesManager {
+public class PluginQueryTokenizerPreferencesManager
+{
- /** Logger for this class. */
- private final static ILogger s_log =
- LoggerController.createLogger(PluginQueryTokenizerPreferencesManager.class);
-
- /** Name of preferences file. */
- private static final String USER_PREFS_FILE_NAME = "prefs.xml";
-
- /** Folder to store user settings in. */
- private File _userSettingsFolder;
-
- /** The bean we will be loading from / storing to */
- private IQueryTokenizerPreferenceBean _prefs = null;
-
- /** Tells us which file to store the preference bean in */
- private IPlugin plugin = null;
-
- /** whether or not we've been initialized */
- private boolean _initialized = false;
-
- /**
- * Constructor.
- */
- public PluginQueryTokenizerPreferencesManager() {
- /* Do Nothing */
- }
-
- /**
- * Initializes this preference manager. This must be done prior to calling
- * other methods.
- *
- * @param thePlugin which plugin we are handling preferences for.
- * @param defaultPrefsBean the bean to use if no preference file currently
- * exists.
- * @throws PluginException
- */
- public void initialize(IPlugin thePlugin,
- IQueryTokenizerPreferenceBean defaultPrefsBean)
- throws PluginException
- {
- if (thePlugin == null) {
- throw new IllegalArgumentException(
- "IPlugin arguement cannot be null");
- }
- if (defaultPrefsBean == null) {
- throw new IllegalArgumentException(
- "IQueryTokenizerPreferenceBean arguement cannot be null");
- }
- plugin = thePlugin;
-
- // Folder to store user settings.
- try {
- _userSettingsFolder = plugin.getPluginUserSettingsFolder();
- } catch (IOException ex) {
- throw new PluginException(ex);
- }
- _prefs = defaultPrefsBean;
- loadPrefs();
- _initialized = true;
- }
-
- /**
- * Returns the preferences bean that this class manages.
- *
- * @return an implementation instance of IQueryTokenizerPreferenceBean
- */
- public IQueryTokenizerPreferenceBean getPreferences() {
- if (!_initialized) {
- throw new IllegalStateException("initialize() must be called first");
- }
- return _prefs;
- }
-
- /**
- * Saves the preferences
- */
- public void unload() {
- savePrefs();
- }
-
- /**
- * Save preferences to disk.
- */
- public void savePrefs() {
- if (!_initialized) {
- throw new IllegalStateException("initialize() must be called first");
- }
- try {
- XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
- wtr.save(new File(_userSettingsFolder, USER_PREFS_FILE_NAME));
- } catch (Exception ex) {
- s_log.error("Error occured writing to preferences file: "
- + USER_PREFS_FILE_NAME, ex);
- }
- }
+ /** Logger for this class. */
+ private final static ILogger s_log =
+ LoggerController.createLogger(PluginQueryTokenizerPreferencesManager.class);
- /**
- * Load from preferences file.
- */
- private void loadPrefs() {
- try {
- XMLBeanReader doc = new XMLBeanReader();
-
- File prefFile = PreferenceUtil.getPreferenceFileToReadFrom(plugin);
-
- doc.load(prefFile, _prefs.getClass().getClassLoader());
-
- Iterator<Object> it = doc.iterator();
-
- if (it.hasNext()) {
- _prefs = (IQueryTokenizerPreferenceBean)it.next();
- }
- } catch (FileNotFoundException ignore) {
- s_log.info(USER_PREFS_FILE_NAME + " not found - will be created");
- } catch (Exception ex) {
- s_log.error("Error occured reading from preferences file: "
- + USER_PREFS_FILE_NAME, ex);
- }
+ /** Name of preferences file. */
+ private static final String USER_PREFS_FILE_NAME = "prefs.xml";
- _prefs.setClientName(Version.getApplicationName() + "/" + plugin.getDescriptiveName());
- _prefs.setClientVersion(Version.getShortVersion() + "/" + plugin.getVersion());
- }
-
+ /** Folder to store user settings in. */
+ private FileWrapper _userSettingsFolder;
+
+ /** The bean we will be loading from / storing to */
+ private IQueryTokenizerPreferenceBean _prefs = null;
+
+ /** Tells us which file to store the preference bean in */
+ private IPlugin plugin = null;
+
+ /** whether or not we've been initialized */
+ private boolean _initialized = false;
+
+ /** factory for creating FileWrappers which insulate the application from direct reference to File */
+ private static FileWrapperFactory fileWrapperFactory = new FileWrapperFactoryImpl();
+
+ /**
+ * Constructor.
+ */
+ public PluginQueryTokenizerPreferencesManager()
+ {
+ /* Do Nothing */
+ }
+
+ /**
+ * Initializes this preference manager. This must be done prior to calling other methods.
+ *
+ * @param thePlugin
+ * which plugin we are handling preferences for.
+ * @param defaultPrefsBean
+ * the bean to use if no preference file currently exists.
+ * @throws PluginException
+ */
+ public void initialize(IPlugin thePlugin, IQueryTokenizerPreferenceBean defaultPrefsBean)
+ throws PluginException
+ {
+ if (thePlugin == null)
+ {
+ throw new IllegalArgumentException("IPlugin arguement cannot be null");
+ }
+ if (defaultPrefsBean == null)
+ {
+ throw new IllegalArgumentException("IQueryTokenizerPreferenceBean arguement cannot be null");
+ }
+ plugin = thePlugin;
+
+ // Folder to store user settings.
+ try
+ {
+ _userSettingsFolder = plugin.getPluginUserSettingsFolder();
+ }
+ catch (IOException ex)
+ {
+ throw new PluginException(ex);
+ }
+ _prefs = defaultPrefsBean;
+ loadPrefs();
+ _initialized = true;
+ }
+
+ /**
+ * Returns the preferences bean that this class manages.
+ *
+ * @return an implementation instance of IQueryTokenizerPreferenceBean
+ */
+ public IQueryTokenizerPreferenceBean getPreferences()
+ {
+ if (!_initialized)
+ {
+ throw new IllegalStateException("initialize() must be called first");
+ }
+ return _prefs;
+ }
+
+ /**
+ * Saves the preferences
+ */
+ public void unload()
+ {
+ savePrefs();
+ }
+
+ /**
+ * Save preferences to disk.
+ */
+ public void savePrefs()
+ {
+ if (!_initialized)
+ {
+ throw new IllegalStateException("initialize() must be called first");
+ }
+ try
+ {
+ XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
+ wtr.save(fileWrapperFactory.create(_userSettingsFolder, USER_PREFS_FILE_NAME));
+ }
+ catch (Exception ex)
+ {
+ s_log.error("Error occured writing to preferences file: " + USER_PREFS_FILE_NAME, ex);
+ }
+ }
+
+ /**
+ * @param fileWrapperFactory
+ * the fileWrapperFactory to set
+ */
+ public void setFileWrapperFactory(FileWrapperFactory fileWrapperFactory)
+ {
+ Utilities.checkNull("setFileWrapperFactory", "fileWrapperFactory", fileWrapperFactory);
+ PluginQueryTokenizerPreferencesManager.fileWrapperFactory = fileWrapperFactory;
+ }
+
+ /**
+ * Load from preferences file.
+ */
+ private void loadPrefs()
+ {
+ try
+ {
+ XMLBeanReader doc = new XMLBeanReader();
+
+ FileWrapper prefFile = PreferenceUtil.getPreferenceFileToReadFrom(plugin);
+
+ doc.load(prefFile, _prefs.getClass().getClassLoader());
+
+ Iterator<Object> it = doc.iterator();
+
+ if (it.hasNext())
+ {
+ _prefs = (IQueryTokenizerPreferenceBean) it.next();
+ }
+ }
+ catch (FileNotFoundException ignore)
+ {
+ s_log.info(USER_PREFS_FILE_NAME + " not found - will be created");
+ }
+ catch (Exception ex)
+ {
+ s_log.error("Error occured reading from preferences file: " + USER_PREFS_FILE_NAME, ex);
+ }
+
+ _prefs.setClientName(Version.getApplicationName() + "/" + plugin.getDescriptiveName());
+ _prefs.setClientVersion(Version.getShortVersion() + "/" + plugin.getVersion());
+ }
+
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PreferenceUtil.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PreferenceUtil.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/plugin/PreferenceUtil.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -21,81 +21,84 @@
import java.io.File;
import java.io.IOException;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactoryImpl;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
-public class PreferenceUtil {
+public class PreferenceUtil
+{
- /** Logger for this class. */
- private final static ILogger s_log =
- LoggerController.createLogger(PreferenceUtil.class);
-
- /** Name of preferences file. */
- private static final String USER_PREFS_FILE_NAME = "prefs.xml";
-
-
- /**
- * Check to see if the user wants to migrate previous settings. If yes, then
- * then oldSQuirreLInstallDir contains the user's latest settings so prefer
- * that one, unless the user settings directory has a file that is newer.
- * @return
- */
- public static File getPreferenceFileToReadFrom(IPlugin p) throws IOException {
- File userSettingsFolder = p.getPluginUserSettingsFolder();
- final File newUserPreferenceFile =
- new File(userSettingsFolder, USER_PREFS_FILE_NAME);
-
- File result = newUserPreferenceFile;
-
- String migratePrefsProperty =
- System.getProperty("migratePreferences", "false");
- if (migratePrefsProperty != null
- && migratePrefsProperty.equalsIgnoreCase("true"))
- {
- String oldSquirrelLocation =
- System.getProperty("oldSQuirreLInstallDir");
-
- if (oldSquirrelLocation == null || oldSquirrelLocation.equals("")) {
- throw new IllegalStateException(
- "migratePreferences was set to true, but " +
- "oldSQuirreLInstallDir wasn't set.");
- }
-
- final File oldAppPreferenceFile =
- new File(oldSquirrelLocation + File.separator + "plugins" +
- File.separator + p.getInternalName() +
- File.separator + USER_PREFS_FILE_NAME);
-
-
- // if the old preference file exists and is newer than the
- // newUserPreference file, then use it for reading preferences
- if (oldAppPreferenceFile.exists()) {
-
- if (oldAppPreferenceFile.lastModified() >
- newUserPreferenceFile.lastModified())
- {
- result = oldAppPreferenceFile;
- s_log.info("-DmigratePreferences was specified; using "+
- oldAppPreferenceFile.getAbsolutePath()+
- " as the source for preferences - will save " +
- "them to "+newUserPreferenceFile.getAbsolutePath());
- } else {
- s_log.info("-DmigratePreferences was specified, but file "+
- newUserPreferenceFile.getAbsolutePath()+ " is newer " +
- "than "+oldAppPreferenceFile.getAbsolutePath() +
- ": migration will be skipped");
- }
-
- } else {
- s_log.info("-DmigratePreferences was specified, but file " +
- oldAppPreferenceFile.getAbsolutePath()+" does not " +
- "exist! Please remove -DmigratePreferences from the " +
- "launch script, or fix -DoldSquirrelLocation to " +
- "point to a valid previous SQuirreL installation " +
- "directory");
- }
- }
- return result;
- }
-
+ /** Logger for this class. */
+ private final static ILogger s_log = LoggerController.createLogger(PreferenceUtil.class);
+
+ /** Name of preferences file. */
+ private static final String USER_PREFS_FILE_NAME = "prefs.xml";
+
+ /** factory for creating FileWrappers which insulate the application from direct reference to File */
+ private static FileWrapperFactory fileWrapperFactory = new FileWrapperFactoryImpl();
+
+ /**
+ * Check to see if the user wants to migrate previous settings. If yes, then then oldSQuirreLInstallDir
+ * contains the user's latest settings so prefer that one, unless the user settings directory has a file
+ * that is newer.
+ *
+ * @return
+ */
+ public static FileWrapper getPreferenceFileToReadFrom(IPlugin p) throws IOException
+ {
+ FileWrapper userSettingsFolder = p.getPluginUserSettingsFolder();
+ final FileWrapper newUserPreferenceFile =
+ fileWrapperFactory.create(userSettingsFolder, USER_PREFS_FILE_NAME);
+
+ FileWrapper result = newUserPreferenceFile;
+
+ String migratePrefsProperty = System.getProperty("migratePreferences", "false");
+ if (migratePrefsProperty != null && migratePrefsProperty.equalsIgnoreCase("true"))
+ {
+ String oldSquirrelLocation = System.getProperty("oldSQuirreLInstallDir");
+
+ if (oldSquirrelLocation == null || oldSquirrelLocation.equals(""))
+ {
+ throw new IllegalStateException("migratePreferences was set to true, but "
+ + "oldSQuirreLInstallDir wasn't set.");
+ }
+
+ final FileWrapper oldAppPreferenceFile =
+ fileWrapperFactory.create(oldSquirrelLocation + File.separator + "plugins" + File.separator
+ + p.getInternalName() + File.separator + USER_PREFS_FILE_NAME);
+
+ // if the old preference file exists and is newer than the
+ // newUserPreference file, then use it for reading preferences
+ if (oldAppPreferenceFile.exists())
+ {
+
+ if (oldAppPreferenceFile.lastModified() > newUserPreferenceFile.lastModified())
+ {
+ result = oldAppPreferenceFile;
+ s_log.info("-DmigratePreferences was specified; using "
+ + oldAppPreferenceFile.getAbsolutePath() + " as the source for preferences - will save "
+ + "them to " + newUserPreferenceFile.getAbsolutePath());
+ }
+ else
+ {
+ s_log.info("-DmigratePreferences was specified, but file "
+ + newUserPreferenceFile.getAbsolutePath() + " is newer " + "than "
+ + oldAppPreferenceFile.getAbsolutePath() + ": migration will be skipped");
+ }
+
+ }
+ else
+ {
+ s_log.info("-DmigratePreferences was specified, but file "
+ + oldAppPreferenceFile.getAbsolutePath() + " does not "
+ + "exist! Please remove -DmigratePreferences from the "
+ + "launch script, or fix -DoldSquirrelLocation to "
+ + "point to a valid previous SQuirreL installation " + "directory");
+ }
+ }
+ return result;
+ }
+
}
Modified: trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java
===================================================================
--- trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java 2011-03-21 22:57:54 UTC (rev 6204)
+++ trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java 2011-03-23 00:46:58 UTC (rev 6205)
@@ -33,6 +33,10 @@
import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
import net.sourceforge.squirrel_sql.client.session.SessionManager;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFileWrappers;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapper;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactory;
+import net.sourceforge.squirrel_sql.fw.util.FileWrapperFactoryImpl;
import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
import net.sourceforge.squirrel_sql.fw.util.IResources;
@@ -79,8 +83,19 @@
@Mock
private Action mockAction;
-
+ @Mock
+ protected ApplicationFileWrappers mockApplicationFileWrappers;
+
+ protected FileWrapperFactory fileWrapperFactory = new FileWrapperFactoryImpl();
+
+ // Locations for various commonly accessed directories
+ protected FileWrapper targetDirectory = fileWrapperFactory.create("./target");
+ protected FileWrapper testHomeDirectory = fileWrapperFactory.create(targetDirectory, "test-home-dir");
+ protected FileWrapper testUpdateDirectory = fileWrapperFactory.create(testHomeDirectory, "update");
+ protected FileWrapper testPluginsDirectory = fileWrapperFactory.create(testHomeDirectory, "plugins");
+ protected FileWrapper testLibDirectory = fileWrapperFactory.create(testHomeDirectory, "lib");
+
/**
* Sub-class tests must implement this to return an instance of the Plugin being tested.
*
@@ -106,6 +121,16 @@
} catch (Exception e) {}
classUnderTest = getPluginToTest();
+
+ classUnderTest.setFileWrapperFactory(fileWrapperFactory);
+ classUnderTest.setApplicationFiles(mockApplicationFileWrappers);
+
+ when(mockApplicationFileWrappers.getPluginsDirectory()).thenReturn(testPluginsDirectory);
+ when(mockApplicationFileWrappers.getLibraryDirectory()).thenReturn(testLibDirectory);
+ when(mockApplicationFileWrappers.getPluginsUserSettingsDirectory()).thenReturn(testPluginsDirectory);
+ when(mockApplicationFileWrappers.getSquirrelHomeDir()).thenReturn(testHomeDirectory)...
[truncated message content] |