All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.squirrel_sql.fw.gui.Dialogs Maven / Gradle / Ivy

Go to download

The framework library contains utility classes that are generic and useful for building applications that introspect a database via JDBC. These are not intended to be SQuirreLSQL-specific and could be used by other projects JDBC front-end applications. This project is guaranteed to have no code dependencies on other SQuirreLSQL projects and could therefore be used when building a different JDBC front-end application.

There is a newer version: 3.5.0
Show newest version
package net.sourceforge.squirrel_sql.fw.gui;
/*
 * Copyright (C) 2001-2003 Colin Bell
 * [email protected]
 *
 * 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
 */
import java.awt.Component;
import java.awt.Frame;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import net.sourceforge.squirrel_sql.fw.util.FileExtensionFilter;
import net.sourceforge.squirrel_sql.fw.util.StringManager;
import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
/**
 * This class provides some methods for using standard JDK dialogs.
 *
 * @author Colin Bell
 */
public class Dialogs
{
	/** Internationalized strings for this class. */
	private static final StringManager s_stringMgr =
		StringManagerFactory.getStringManager(Dialogs.class);

	public static File selectFileForWriting(Frame parentFrame,
							FileExtensionFilter[] filters)
	{
		return selectFileForWriting(parentFrame, filters, null);
	}

	public static File selectFileForWriting(Frame parentFrame,
							FileExtensionFilter[] filters, JComponent accessory)
	{
		File outFile = null;
		final JFileChooser chooser = new JFileChooser();
		if (filters != null)
		{
			for (int i = 0; i < filters.length; ++i)
			{
				chooser.addChoosableFileFilter(filters[i]);
			}
		}

		if (accessory != null)
		{
			chooser.setAccessory(accessory);
		}

		for (;;)
		{
			outFile = null;
			if (chooser.showSaveDialog(parentFrame) == JFileChooser.APPROVE_OPTION)
			{
				outFile = chooser.getSelectedFile();
				if (canSaveToFile(parentFrame, outFile))
				{
					break;
				}
			}
			else
			{
				break;
			}
		}

		return outFile;
	}

	public static void showNotYetImplemented(Component owner)
	{
		JOptionPane.showMessageDialog(owner, s_stringMgr.getString("Dialogs.nyi"),
										"", JOptionPane.INFORMATION_MESSAGE);
	}

	public static boolean showYesNo(Component owner, String msg)
	{
		return showYesNo(owner, msg, "");
	}

	public static boolean showYesNo(Component owner, String msg, String title)
	{
		int rc = JOptionPane.showConfirmDialog(owner, msg, title,
												JOptionPane.YES_NO_OPTION);
		return rc == JOptionPane.YES_OPTION;
	}

	public static void showOk(Component owner, String msg)
	{
		JOptionPane.showMessageDialog(owner, msg, "", JOptionPane.INFORMATION_MESSAGE);
	}

	private static boolean canSaveToFile(Frame parentFrame, File outFile)
	{
		if (!outFile.exists())
		{
			return true;
		}
		String msg = s_stringMgr.getString("Dialogs.alreadyexists",
											outFile.getAbsolutePath());
		if (!Dialogs.showYesNo(parentFrame, msg))
		{
			return false;
		}
		if (!outFile.canWrite())
		{
			msg = s_stringMgr.getString("Dialogs.cannotwrite",
											outFile.getAbsolutePath());
			Dialogs.showOk(parentFrame, msg);
			return false;
		}
		outFile.delete();
		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy