001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.Frame;
023: import java.io.File;
024:
025: import javax.swing.JComponent;
026: import javax.swing.JFileChooser;
027: import javax.swing.JOptionPane;
028:
029: import net.sourceforge.squirrel_sql.fw.util.FileExtensionFilter;
030: import net.sourceforge.squirrel_sql.fw.util.StringManager;
031: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
032:
033: /**
034: * This class provides some methods for using standard JDK dialogs.
035: *
036: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
037: */
038: public class Dialogs {
039: /** Internationalized strings for this class. */
040: private static final StringManager s_stringMgr = StringManagerFactory
041: .getStringManager(Dialogs.class);
042:
043: public static File selectFileForWriting(Frame parentFrame,
044: FileExtensionFilter[] filters) {
045: return selectFileForWriting(parentFrame, filters, null);
046: }
047:
048: public static File selectFileForWriting(Frame parentFrame,
049: FileExtensionFilter[] filters, JComponent accessory) {
050: File outFile = null;
051: final JFileChooser chooser = new JFileChooser();
052: if (filters != null) {
053: for (int i = 0; i < filters.length; ++i) {
054: chooser.addChoosableFileFilter(filters[i]);
055: }
056: }
057:
058: if (accessory != null) {
059: chooser.setAccessory(accessory);
060: }
061:
062: for (;;) {
063: outFile = null;
064: if (chooser.showSaveDialog(parentFrame) == JFileChooser.APPROVE_OPTION) {
065: outFile = chooser.getSelectedFile();
066: if (canSaveToFile(parentFrame, outFile)) {
067: break;
068: }
069: } else {
070: break;
071: }
072: }
073:
074: return outFile;
075: }
076:
077: public static void showNotYetImplemented(Component owner) {
078: JOptionPane.showMessageDialog(owner, s_stringMgr
079: .getString("Dialogs.nyi"), "",
080: JOptionPane.INFORMATION_MESSAGE);
081: }
082:
083: public static boolean showYesNo(Component owner, String msg) {
084: return showYesNo(owner, msg, "");
085: }
086:
087: public static boolean showYesNo(Component owner, String msg,
088: String title) {
089: int rc = JOptionPane.showConfirmDialog(owner, msg, title,
090: JOptionPane.YES_NO_OPTION);
091: return rc == JOptionPane.YES_OPTION;
092: }
093:
094: public static void showOk(Component owner, String msg) {
095: JOptionPane.showMessageDialog(owner, msg, "",
096: JOptionPane.INFORMATION_MESSAGE);
097: }
098:
099: private static boolean canSaveToFile(Frame parentFrame, File outFile) {
100: if (!outFile.exists()) {
101: return true;
102: }
103: String msg = s_stringMgr.getString("Dialogs.alreadyexists",
104: outFile.getAbsolutePath());
105: if (!Dialogs.showYesNo(parentFrame, msg)) {
106: return false;
107: }
108: if (!outFile.canWrite()) {
109: msg = s_stringMgr.getString("Dialogs.cannotwrite", outFile
110: .getAbsolutePath());
111: Dialogs.showOk(parentFrame, msg);
112: return false;
113: }
114: outFile.delete();
115: return true;
116: }
117: }
|