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