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.BorderLayout;
017import java.awt.Dimension;
018import java.awt.Frame;
019import java.awt.GridLayout;
020import java.awt.Point;
021import java.awt.Toolkit;
022import java.awt.event.ActionEvent;
023import java.awt.event.ActionListener;
024import java.awt.event.KeyEvent;
025import java.io.File;
026import java.util.Iterator;
027import java.util.List;
028
029import javax.swing.BorderFactory;
030import javax.swing.JButton;
031import javax.swing.JDialog;
032import javax.swing.JFileChooser;
033import javax.swing.JLabel;
034import javax.swing.JOptionPane;
035import javax.swing.JPanel;
036import javax.swing.JTextField;
037
038import hdf.object.FileFormat;
039
040/**
041 * FileConversionDialog shows a message dialog requesting user input for
042 * converting files.
043 *
044 * @author Peter X. Cao
045 * @version 2.4 9/6/2007
046 */
047public class FileConversionDialog extends JDialog implements ActionListener {
048    private static final long serialVersionUID = 2645021913986116744L;
049
050    private String fileTypeFrom, fileTypeTo;
051
052    private JTextField srcFileField, dstFileField;
053
054    private boolean isConverted;
055
056    private boolean isConvertedFromImage;
057
058    private String convertedFile;
059
060    private String toFileExtension;
061
062    private List fileList;
063
064    private String currentDir;
065
066    private final Toolkit toolkit;
067
068    /**
069     * Constructs a FileConversionDialog
070     *
071     * @param owner
072     *            The owner of the dialog.
073     * @param typeFrom
074     *            source file type
075     * @param typeTo
076     *            destinatin file type
077     * @param dir
078     *            current file directory
079     * @param openFiles
080     *            The list of current open files
081     */
082    public FileConversionDialog(Frame owner, String typeFrom, String typeTo,
083            String dir, List openFiles) {
084        super(owner, "Convert File...", true);
085
086        fileTypeFrom = typeFrom;
087        fileTypeTo = typeTo;
088        isConverted = false;
089        isConvertedFromImage = false;
090        fileList = openFiles;
091        toFileExtension = "";
092        currentDir = dir;
093        toolkit = Toolkit.getDefaultToolkit();
094
095        String fromName = "Source";
096        if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF5)) {
097            toFileExtension = ".h5";
098            setTitle("Convert Image to HDF5 ...");
099            fromName = "IMAGE";
100            isConvertedFromImage = true;
101        }
102        else if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF4)) {
103            toFileExtension = ".hdf";
104            setTitle("Convert Image to HDF4 ...");
105            fromName = "IMAGE";
106            isConvertedFromImage = true;
107        }
108
109        // layout the components
110        JPanel contentPane = (JPanel) getContentPane();
111        contentPane.setLayout(new BorderLayout(5, 5));
112        contentPane.setBorder(BorderFactory.createEmptyBorder(15, 5, 5, 5));
113        int w = 450 + (ViewProperties.getFontSize() - 12) * 15;
114        int h = 120 + (ViewProperties.getFontSize() - 12) * 10;
115        contentPane.setPreferredSize(new Dimension(w, h));
116
117        // add the top panel for enter file name
118        JPanel p = new JPanel();
119        p.setLayout(new BorderLayout(5, 5));
120
121        JPanel p0 = new JPanel();
122        p0.setLayout(new GridLayout(2, 1, 5, 5));
123        p0.add(new JLabel(fromName + " File: "));
124        p0.add(new JLabel("HDF File: "));
125        p.add(p0, BorderLayout.WEST);
126
127        p0 = new JPanel();
128        p0.setLayout(new GridLayout(2, 1, 5, 5));
129        p0.add(srcFileField = new JTextField());
130        p0.add(dstFileField = new JTextField());
131        p.add(p0, BorderLayout.CENTER);
132
133        p0 = new JPanel();
134        p0.setLayout(new GridLayout(2, 1, 5, 5));
135        JButton jButton = new JButton("Browse...");
136        jButton.setActionCommand("Browse source file");
137        jButton.addActionListener(this);
138        jButton.setName("sourcefilebutton");
139        p0.add(jButton);
140        jButton = new JButton("Browse...");
141        jButton.setActionCommand("Browse target file");
142        jButton.addActionListener(this);
143        p0.add(jButton);
144        p.add(p0, BorderLayout.EAST);
145
146        contentPane.add(p, BorderLayout.CENTER);
147
148        JButton okButton = new JButton("   Ok   ");
149        okButton.setMnemonic(KeyEvent.VK_O);
150        okButton.setActionCommand("Ok");
151        okButton.addActionListener(this);
152        okButton.setName("okbutton");
153
154        JButton cancelButton = new JButton("Cancel");
155        cancelButton.setMnemonic(KeyEvent.VK_C);
156        cancelButton.setActionCommand("Cancel");
157        cancelButton.addActionListener(this);
158
159        p = new JPanel();
160        p.add(okButton);
161        p.add(cancelButton);
162
163        contentPane.add(p, BorderLayout.SOUTH);
164
165        Point l = owner.getLocation();
166        l.x += 250;
167        l.y += 80;
168        setLocation(l);
169        pack();
170    }
171
172    public void actionPerformed(ActionEvent e) {
173        Object source = e.getSource();
174        String cmd = e.getActionCommand();
175
176        if (cmd.equals("Ok")) {
177            isConverted = convert();
178
179            if (isConverted) {
180                dispose();
181            }
182        }
183        else if (cmd.equals("Cancel")) {
184            isConverted = false;
185            convertedFile = null;
186            dispose();
187        }
188        else if (cmd.equals("Browse source file")) {
189            JFileChooser fchooser = new JFileChooser(currentDir);
190            if (isConvertedFromImage)
191                fchooser.setFileFilter(DefaultFileFilter.getImageFileFilter());
192
193            int returnVal = fchooser.showOpenDialog(this);
194
195            if (returnVal != JFileChooser.APPROVE_OPTION) {
196                return;
197            }
198
199            File choosedFile = fchooser.getSelectedFile();
200            if (choosedFile == null) {
201                return;
202            }
203
204            String fname = choosedFile.getAbsolutePath();
205
206            if (fname == null) {
207                return;
208            }
209
210            currentDir = choosedFile.getParent();
211            srcFileField.setText(fname);
212            dstFileField.setText(fname + toFileExtension);
213        }
214        else if (cmd.equals("Browse target file")) {
215            JFileChooser fchooser = new JFileChooser();
216            int returnVal = fchooser.showOpenDialog(this);
217
218            if (returnVal != JFileChooser.APPROVE_OPTION) {
219                return;
220            }
221
222            File choosedFile = fchooser.getSelectedFile();
223            if (choosedFile == null) {
224                return;
225            }
226
227            String fname = choosedFile.getAbsolutePath();
228
229            if (fname == null) {
230                return;
231            }
232
233            dstFileField.setText(fname);
234        }
235    }
236
237    /** convert file */
238    private boolean convert() {
239        boolean converted = false;
240        String srcFile = srcFileField.getText();
241        String dstFile = dstFileField.getText();
242
243        if ((srcFile == null) || (dstFile == null)) {
244            return false;
245        }
246
247        srcFile = srcFile.trim();
248        dstFile = dstFile.trim();
249        if ((srcFile == null) || (srcFile.length() <= 0) || (dstFile == null)
250                || (dstFile.length() <= 0)) {
251            return false;
252        }
253
254        // verify the source file
255        File f = new File(srcFile);
256        if (!f.exists()) {
257            toolkit.beep();
258            JOptionPane.showMessageDialog(this, "Source file does not exist.",
259                    this.getTitle(), JOptionPane.ERROR_MESSAGE);
260            return false;
261        }
262        else if (f.isDirectory()) {
263            toolkit.beep();
264            JOptionPane.showMessageDialog(this, "Source file is a directory.",
265                    this.getTitle(), JOptionPane.ERROR_MESSAGE);
266            return false;
267        }
268
269        // verify target file
270        String srcPath = f.getParent();
271        f = new File(dstFile);
272        File pfile = f.getParentFile();
273        if (pfile == null) {
274            dstFile = srcPath + File.separator + dstFile;
275            f = new File(dstFile);
276        }
277        else if (!pfile.exists()) {
278            toolkit.beep();
279            JOptionPane.showMessageDialog(this,
280                    "Destination file path does not exist at\n"
281                            + pfile.getPath(), this.getTitle(),
282                    JOptionPane.ERROR_MESSAGE);
283            return false;
284        }
285
286        // check if the file is in use
287        if (fileList != null) {
288            FileFormat theFile = null;
289            Iterator iterator = fileList.iterator();
290            while (iterator.hasNext()) {
291                theFile = (FileFormat) iterator.next();
292                if (theFile.getFilePath().equals(dstFile)) {
293                    toolkit.beep();
294                    JOptionPane.showMessageDialog(this,
295                            "The destination file is being used.", getTitle(),
296                            JOptionPane.ERROR_MESSAGE);
297                    return false;
298                }
299            }
300        }
301
302        int newFileFlag = -1;
303        if (f.exists()) {
304            newFileFlag = JOptionPane.showConfirmDialog(this,
305                    "Destination file exists. Do you want to replace it ?",
306                    this.getTitle(), JOptionPane.YES_NO_OPTION);
307            if (newFileFlag == JOptionPane.NO_OPTION) {
308                return false;
309            }
310        }
311
312        try {
313            Tools.convertImageToHDF(srcFile, dstFile, fileTypeFrom, fileTypeTo);
314            convertedFile = dstFile;
315            converted = true;
316        }
317        catch (Exception ex) {
318            convertedFile = null;
319            converted = false;
320            toolkit.beep();
321            JOptionPane.showMessageDialog(this, ex.getMessage(), this
322                    .getTitle(), JOptionPane.ERROR_MESSAGE);
323            return false;
324        }
325
326        return converted;
327    }
328
329    public boolean isFileConverted() {
330        return isConverted;
331    }
332
333    public String getConvertedFile() {
334        return convertedFile;
335    }
336}