001/***************************************************************************** 002 * Copyright by The HDF Group. * 003 * Copyright by the Board of Trustees of the University of Illinois. * 004 * All rights reserved. * 005 * * 006 * This file is part of the HDF Java Products distribution. * 007 * The full copyright notice, including terms governing use, modification, * 008 * and redistribution, is contained in the file COPYING. * 009 * COPYING can be found at the root of the source code distribution tree. * 010 * If you do not have access to this file, you may request a copy from * 011 * help@hdfgroup.org. * 012 ****************************************************************************/ 013 014package hdf.view; 015 016import java.awt.Toolkit; 017import java.io.File; 018import java.util.Iterator; 019import java.util.List; 020 021import javax.swing.JFileChooser; 022import javax.swing.JFrame; 023import javax.swing.JOptionPane; 024 025import hdf.object.FileFormat; 026 027/** 028 * NewFileDialog shows a message dialog requesting user input for creating a new 029 * HDF4/5 file. 030 * 031 * @author Peter X. Cao 032 * @version 2.4 9/6/2007 033 */ 034public class NewFileDialog extends JFileChooser // JDialog 035// implements ActionListener 036{ 037 private static final long serialVersionUID = 4796246032789504234L; 038 039 /** flag if the new file is an HDF5 */ 040 private String fileType; 041 042 /** The current working directory */ 043 private String currentDir; 044 045 /** The view working directory */ 046 private String viewDir; 047 048 private boolean fileCreated; 049 050 private List fileList; 051 052 private final Toolkit toolkit; 053 054 private final JFrame viewer; 055 056 private boolean isH5 = false; 057 058 private boolean isH4 = false; 059 060 /** 061 * constructs an NewFileDialog. 062 * 063 * @param owner 064 * The owner of the dialog. 065 * @param dir 066 * The default directory of the new file. 067 * @param type 068 * The type of file format. 069 * @param openFiles 070 * The list of current open files. It is used to make sure the 071 * new file cannot be any file in use. 072 */ 073 public NewFileDialog(JFrame owner, String dir, String type, List openFiles) { 074 super(dir); 075 076 currentDir = dir; 077 viewer = owner; 078 viewDir = dir; 079 fileType = type; 080 fileCreated = false; 081 fileList = openFiles; 082 toolkit = Toolkit.getDefaultToolkit(); 083 084 if (currentDir != null) { 085 currentDir += File.separator; 086 } 087 else { 088 currentDir = ""; 089 } 090 091 if (fileType == FileFormat.FILE_TYPE_HDF4) { 092 isH4 = true; 093 setSelectedFile(Tools.checkNewFile(currentDir, ".hdf")); 094 setFileFilter(DefaultFileFilter.getFileFilterHDF4()); 095 } 096 else if (fileType == FileFormat.FILE_TYPE_HDF5) { 097 isH5 = true; 098 setSelectedFile(Tools.checkNewFile(currentDir, ".h5")); 099 setFileFilter(DefaultFileFilter.getFileFilterHDF5()); 100 } 101 102 103 this.setAcceptAllFileFilterUsed(false); 104 this.showSaveDialog(owner); 105 } 106 107 @Override 108 protected void fireActionPerformed(String command) { 109 super.fireActionPerformed(command); 110 111 if (command.equals("ApproveSelection")) { 112 fileCreated = createNewFile(); 113 } 114 else { 115 fileCreated = false; 116 } 117 } 118 119 /** create a new HDF file with default file creation properties */ 120 private boolean createNewFile() { 121 File f = this.getSelectedFile(); 122 if (f == null) { 123 return false; 124 } 125 126 String fname = f.getAbsolutePath(); 127 128 if (fname == null) { 129 return false; 130 } 131 132 fname = fname.trim(); 133 if ((fname == null) || (fname.length() == 0)) { 134 toolkit.beep(); 135 JOptionPane.showMessageDialog(this, "Invalid file name.", viewer 136 .getTitle(), JOptionPane.ERROR_MESSAGE); 137 return false; 138 } 139 140 String extensions = FileFormat.getFileExtensions(); 141 boolean noExtension = true; 142 if ((extensions != null) && (extensions.length() > 0)) { 143 java.util.StringTokenizer currentExt = new java.util.StringTokenizer( 144 extensions, ","); 145 String extension = ""; 146 String tmpFilename = fname.toLowerCase(); 147 while (currentExt.hasMoreTokens() && noExtension) { 148 extension = currentExt.nextToken().trim().toLowerCase(); 149 noExtension = !tmpFilename.endsWith("." + extension); 150 } 151 } 152 153 if (noExtension) { 154 if (isH4) { 155 fname += ".hdf"; 156 f = new File(fname); 157 setSelectedFile(f); 158 } 159 else if (isH5) { 160 fname += ".h5"; 161 f = new File(fname); 162 setSelectedFile(f); 163 } 164 } 165 166 if (f.exists() && f.isDirectory()) { 167 toolkit.beep(); 168 JOptionPane.showMessageDialog(this, "File is a directory.", viewer 169 .getTitle(), JOptionPane.ERROR_MESSAGE); 170 return false; 171 } 172 173 File pfile = f.getParentFile(); 174 if (pfile == null) { 175 fname = viewDir + File.separator + fname; 176 f = new File(fname); 177 } 178 else if (!pfile.exists()) { 179 toolkit.beep(); 180 JOptionPane.showMessageDialog(this, "File path does not exist at\n" 181 + pfile.getPath(), viewer.getTitle(), 182 JOptionPane.ERROR_MESSAGE); 183 return false; 184 } 185 186 // check if the file is in use 187 if (fileList != null) { 188 FileFormat theFile = null; 189 Iterator iterator = fileList.iterator(); 190 while (iterator.hasNext()) { 191 theFile = (FileFormat) iterator.next(); 192 if (theFile.getFilePath().equals(fname)) { 193 toolkit.beep(); 194 JOptionPane 195 .showMessageDialog( 196 this, 197 "Unable to create the new file. \nThe file is being used.", 198 viewer.getTitle(), 199 JOptionPane.ERROR_MESSAGE); 200 return false; 201 } 202 } 203 } 204 205 int newFileFlag = -1; 206 if (f.exists()) { 207 newFileFlag = JOptionPane.showConfirmDialog(this, 208 "File exists. Do you want to replace it ?", viewer 209 .getTitle(), JOptionPane.YES_NO_OPTION); 210 if (newFileFlag == JOptionPane.NO_OPTION) { 211 return false; 212 } 213 } 214 215 currentDir = f.getParent(); 216 try { 217 int aFlag = FileFormat.FILE_CREATE_DELETE; 218 if (ViewProperties.isEarlyLib()) 219 aFlag = FileFormat.FILE_CREATE_DELETE | FileFormat.FILE_CREATE_EARLY_LIB; 220 FileFormat.getFileFormat(fileType).createFile(fname, aFlag); 221 } 222 catch (Exception ex) { 223 toolkit.beep(); 224 JOptionPane.showMessageDialog(this, ex.getMessage(), viewer 225 .getTitle(), JOptionPane.ERROR_MESSAGE); 226 return false; 227 } 228 229 return true; 230 } 231 232 public boolean isFileCreated() { 233 return fileCreated; 234 } 235 236 public String getFile() { 237 String fname = null; 238 File f = this.getSelectedFile(); 239 if (f != null) { 240 fname = f.getAbsolutePath(); 241 } 242 243 return fname; 244 } 245}