package com.macmillan.nmeyers; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; /* * SaveDialog.java: Part of the PerfAnal tool * * This class implements a dialog for saving a data analysis file. * * Author: Nathan Meyers, nmeyers@javalinux.net * $Id: SaveDialog.java,v 1.3 1999/11/10 03:36:14 nathanm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * with this program. If not, the license is available from the * GNU project, at http://www.gnu.org. */ class SaveDialog extends JDialog { JFrame parent; DoSave doSave; File file; SaveDialog(JFrame frame, DoSave ds) { super(frame, "Save Analysis Data to a File", true); parent = frame; doSave = ds; JFileChooser fileChooser = new JFileChooser() { public void approveSelection() { file = getSelectedFile(); dispose(); if (file.exists()) new ApproveSaveFile(parent); else doSave.doSave(file); } public void cancelSelection() { dispose(); } }; fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); getContentPane().add(fileChooser); pack(); setVisible(true); } interface DoSave { void doSave(File f); } class ApproveSaveFile extends JDialog { ApproveSaveFile(JFrame p) { super(p, "Overwrite File?", true); getContentPane().add(new JLabel("Overwrite file " + file + "?"), BorderLayout.CENTER); Box box = Box.createHorizontalBox(); getContentPane().add(box, BorderLayout.SOUTH); box.add(Box.createGlue()); JButton button = new JButton("OK"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); doSave.doSave(file); } }); box.add(button); box.add(Box.createGlue()); button = new JButton("Cancel"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); box.add(button); getRootPane().setDefaultButton(button); box.add(Box.createGlue()); pack(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setVisible(true); } } }