001 // This class is based on the EnvFileChooser class, version 1 August 2002 002 // by Julie Zenekski 003 004 // Original copyright notice: 005 006 // AP(r) Computer Science Marine Biology Simulation: 007 // The EnvFileChooser class is copyright(c) 2002 College Entrance 008 // Examination Board (www.collegeboard.com). 009 // 010 // This class is free software; you can redistribute it and/or modify 011 // it under the terms of the GNU General Public License as published by 012 // the Free Software Foundation. 013 // 014 // This class is distributed in the hope that it will be useful, 015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 // GNU General Public License for more details. 018 019 package view; 020 021 import javax.swing.*; 022 import javax.swing.filechooser.FileFilter; 023 import java.awt.*; 024 import java.io.File; 025 026 /** 027 * Environment file chooser class. 028 * 029 * @author Mathias Ricken 030 */ 031 032 public class EnvFileChooser extends JFileChooser { 033 /** 034 * Standard extension. 035 */ 036 private static final String ENV_EXT = ".txt"; 037 038 /** 039 * Open accessory. 040 */ 041 private JPanel openAccessory; 042 043 /** 044 * Create a new EnvFileChooser. The default starting directory will be the "DataFiles" subdirectory on the current 045 * working directory. 046 */ 047 public EnvFileChooser() { 048 super(new File(System.getProperty("user.dir") + File.separator + "DataFiles")); 049 setFileFilter(new FileFilter() { 050 public boolean accept(File f) { 051 return (f.getName().toLowerCase().endsWith(ENV_EXT) || f.isDirectory()); 052 } 053 054 public String getDescription() { 055 return ("RiceMBS data files (*" + ENV_EXT + ')'); 056 } 057 }); 058 setFileSelectionMode(JFileChooser.FILES_ONLY); 059 } 060 061 /** 062 * Bring up a modal file chooser dialog allowing the user to choose the environment file and specify the 063 * bounded/unbounded environment class. 064 * 065 * @param parent the parent of the dialog 066 * 067 * @return the return state of the dialog once finished 068 */ 069 public int showOpenDialog(Component parent) { 070 setDialogTitle("Open environment file"); 071 setAccessory(openAccessory); 072 rescanCurrentDirectory(); 073 return super.showOpenDialog(parent); 074 } 075 076 077 /** 078 * Bring up a modal file chooser dialog allowing the user to save to an environment file. 079 * 080 * @param parent the parent of the dialog 081 * 082 * @return the return state of the dialog once finished 083 */ 084 public int showSaveDialog(Component parent) { 085 setDialogTitle("Save environment file"); 086 setAccessory(null); 087 rescanCurrentDirectory(); 088 return super.showSaveDialog(parent); 089 } 090 091 092 /** 093 * Called when the user hits the approve button (Save/Open) to confirm the selected file is acceptable. Overrides 094 * the JFileChooser to confirm overwrite if choosing a file that already exists for a save action. 095 */ 096 public void approveSelection() { 097 if (SAVE_DIALOG == getDialogType()) { 098 File file = getSelectedFile(); 099 if (!file.getName().endsWith(ENV_EXT)) // add extension if missing 100 { 101 setSelectedFile(file = new File(file.getAbsolutePath() + ENV_EXT)); 102 } 103 if (file.exists() && JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(this, 104 "File " + file.getName() + " exists. Overwrite?", 105 "Confirm overwrite", 106 JOptionPane.OK_CANCEL_OPTION)) { 107 return; 108 } 109 } 110 super.approveSelection(); //if we get here, this will dismiss the dialog 111 } 112 }