    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 https://support.hdfgroup.org/products/licenses.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.dialog;
    016
    017import java.io.File;
    018import java.util.Iterator;
    019import java.util.List;
    020
    021import org.eclipse.swt.SWT;
    022import org.eclipse.swt.events.DisposeEvent;
    023import org.eclipse.swt.events.DisposeListener;
    024import org.eclipse.swt.events.SelectionAdapter;
    025import org.eclipse.swt.events.SelectionEvent;
    026import org.eclipse.swt.graphics.Font;
    027import org.eclipse.swt.graphics.Point;
    028import org.eclipse.swt.graphics.Rectangle;
    029import org.eclipse.swt.layout.GridData;
    030import org.eclipse.swt.layout.GridLayout;
    031import org.eclipse.swt.widgets.Button;
    032import org.eclipse.swt.widgets.Composite;
    033import org.eclipse.swt.widgets.Dialog;
    034import org.eclipse.swt.widgets.Display;
    035import org.eclipse.swt.widgets.FileDialog;
    036import org.eclipse.swt.widgets.Label;
    037import org.eclipse.swt.widgets.Shell;
    038import org.eclipse.swt.widgets.Text;
    039
    040import hdf.object.FileFormat;
    041import hdf.view.DefaultFileFilter;
    042import hdf.view.Tools;
    043import hdf.view.ViewProperties;
    044
    045/**
    046 * ImageConversionDialog shows a message dialog requesting user input for
    047 * converting files.
    048 *
    049 * @author Jordan T. Henderson
    050 * @version 2.4 1/28/2016
    051 */
    052public class ImageConversionDialog extends Dialog {
    053    private Shell              shell;
    054
    055    private Font               curFont;
    056
    057    private String             fileTypeFrom, fileTypeTo;
    058
    059    private Text               srcFileField, dstFileField;
    060
    061    private boolean            isConverted;
    062
    063    private boolean            isConvertedFromImage;
    064
    065    private String             convertedFile;
    066
    067    private String             toFileExtension;
    068
    069    private List<FileFormat>   fileList;
    070
    071    private String             currentDir;
    072
    073    /**
    074     * Constructs a FileConversionDialog
    075     *
    076     * @param parent
    077     *            The parent shell of the dialog.
    078     * @param typeFrom
    079     *            source file type
    080     * @param typeTo
    081     *            destination file type
    082     * @param dir
    083     *            current file directory
    084     * @param openFiles
    085     *            The list of currently open files
    086     */
    087    public ImageConversionDialog(Shell parent, String typeFrom, String typeTo,
    088            String dir, List<FileFormat> openFiles) {
    089        super(parent, SWT.APPLICATION_MODAL);
    090
    091        try {
    092            curFont = new Font(
    093                    Display.getCurrent(),
    094                    ViewProperties.getFontType(),
    095                    ViewProperties.getFontSize(),
    096                    SWT.NORMAL);
    097        }
    098        catch (Exception ex) {
    099            curFont = null;
    100        }
    101
    102        fileTypeFrom = typeFrom;
    103        fileTypeTo = typeTo;
    104        isConverted = false;
    105        isConvertedFromImage = false;
    106        fileList = openFiles;
    107        toFileExtension = "";
    108        currentDir = dir;
    109    }
    110
    111    public void open() {
    112        Shell parent = getParent();
    113        shell = new Shell(parent, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
    114        shell.setFont(curFont);
    115        shell.setText(parent.getText());
    116        shell.setImage(ViewProperties.getHdfIcon());
    117        shell.setLayout(new GridLayout(1, true));
    118
    119        if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF5)) {
    120            toFileExtension = ".h5";
    121            shell.setText("Convert Image to HDF5 ...");
    122            isConvertedFromImage = true;
    123        }
    124        else if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF4)) {
    125            toFileExtension = ".hdf";
    126            shell.setText("Convert Image to HDF4 ...");
    127            isConvertedFromImage = true;
    128        }
    129
    130
    131        // Create content region
    132        Composite contentComposite = new Composite(shell, SWT.NONE);
    133        contentComposite.setLayout(new GridLayout(3, false));
    134        contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    135
    136        Label label = new Label(contentComposite, SWT.RIGHT);
    137        label.setFont(curFont);
    138        label.setText("IMAGE File: ");
    139
    140        srcFileField = new Text(contentComposite, SWT.SINGLE | SWT.BORDER);
    141        srcFileField.setFont(curFont);
    142        GridData fieldData = new GridData(SWT.FILL, SWT.FILL, true, false);
    143        fieldData.minimumWidth = 350;
    144        srcFileField.setLayoutData(fieldData);
    145
    146        Button browseButton = new Button(contentComposite, SWT.PUSH);
    147        browseButton.setFont(curFont);
    148        browseButton.setText("Browse...");
    149        browseButton.addSelectionListener(new SelectionAdapter() {
    150            public void widgetSelected(SelectionEvent e) {
    151                FileDialog fChooser = new FileDialog(shell, SWT.OPEN);
    152                fChooser.setFilterPath(currentDir);
    153
    154                if(isConvertedFromImage) {
    155                    DefaultFileFilter filter = DefaultFileFilter.getImageFileFilter();
    156                    fChooser.setFilterExtensions(new String[] {"*", filter.getExtensions()});
    157                    fChooser.setFilterNames(new String[] {"All Files", filter.getDescription()});
    158                    fChooser.setFilterIndex(1);
    159                } else {
    160                    fChooser.setFilterExtensions(new String[] {"*"});
    161                    fChooser.setFilterNames(new String[] {"All Files"});
    162                    fChooser.setFilterIndex(0);
    163                }
    164
    165                String filename = fChooser.open();
    166
    167                if(filename == null) {
    168                    return;
    169                }
    170
    171                File chosenFile = new File(filename);
    172
    173                currentDir = chosenFile.getParent();
    174                srcFileField.setText(chosenFile.getAbsolutePath());
    175                dstFileField.setText(chosenFile.getAbsolutePath() + toFileExtension);
    176            }
    177        });
    178
    179        label = new Label(contentComposite, SWT.RIGHT);
    180        label.setFont(curFont);
    181        label.setText("HDF File: ");
    182
    183        dstFileField = new Text(contentComposite, SWT.SINGLE | SWT.BORDER);
    184        dstFileField.setFont(curFont);
    185        dstFileField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    186
    187        browseButton = new Button(contentComposite, SWT.PUSH);
    188        browseButton.setFont(curFont);
    189        browseButton.setText("Browse...");
    190        browseButton.addSelectionListener(new SelectionAdapter() {
    191            public void widgetSelected(SelectionEvent e) {
    192                FileDialog fChooser = new FileDialog(shell, SWT.OPEN);
    193
    194                fChooser.setFilterExtensions(new String[] {"*"});
    195                fChooser.setFilterNames(new String[] {"All Files"});
    196                fChooser.setFilterIndex(0);
    197
    198                String filename = fChooser.open();
    199
    200                if(filename == null) {
    201                    return;
    202                }
    203
    204                dstFileField.setText(filename);
    205            }
    206        });
    207
    208        // Dummy label to fill space as dialog is resized
    209        new Label(shell, SWT.NONE).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    210
    211
    212        // Create Ok/Cancel button
    213        Composite buttonComposite = new Composite(shell, SWT.NONE);
    214        buttonComposite.setLayout(new GridLayout(2, true));
    215        buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    216
    217        Button okButton = new Button(buttonComposite, SWT.PUSH);
    218        okButton.setFont(curFont);
    219        okButton.setText("   &OK   ");
    220        okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
    221        okButton.addSelectionListener(new SelectionAdapter() {
    222            public void widgetSelected(SelectionEvent e) {
    223                isConverted = convert();
    224
    225                if (isConverted) {
    226                    shell.dispose();
    227                }
    228            }
    229        });
    230
    231        Button cancelButton = new Button(buttonComposite, SWT.PUSH);
    232        cancelButton.setFont(curFont);
    233        cancelButton.setText(" &Cancel ");
    234        cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
    235        cancelButton.addSelectionListener(new SelectionAdapter() {
    236            public void widgetSelected(SelectionEvent e) {
    237                isConverted = false;
    238                convertedFile = null;
    239                shell.dispose();
    240            }
    241        });
    242
    243
    244        shell.pack();
    245
    246        shell.addDisposeListener(new DisposeListener() {
    247            public void widgetDisposed(DisposeEvent e) {
    248                if (curFont != null) curFont.dispose();
    249            }
    250        });
    251
    252        shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    253
    254        Rectangle parentBounds = parent.getBounds();
    255        Point shellSize = shell.getSize();
    256        shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2),
    257                          (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2));
    258
    259        shell.open();
    260
    261        Display display = parent.getDisplay();
    262        while(!shell.isDisposed()) {
    263            if (!display.readAndDispatch())
    264                display.sleep();
    265        }
    266    }
    267
    268    /** Convert file */
    269    private boolean convert() {
    270        boolean converted = false;
    271        String srcFile = srcFileField.getText();
    272        String dstFile = dstFileField.getText();
    273
    274        if ((srcFile == null) || (dstFile == null)) {
    275            return false;
    276        }
    277
    278        srcFile = srcFile.trim();
    279        dstFile = dstFile.trim();
    280        if ((srcFile == null) || (srcFile.length() <= 0) || (dstFile == null)
    281                || (dstFile.length() <= 0)) {
    282            return false;
    283        }
    284
    285        // verify the source file
    286        File f = new File(srcFile);
    287        if (!f.exists()) {
    288            shell.getDisplay().beep();
    289            Tools.showError(shell, "Convert", "Source file does not exist.");
    290            return false;
    291        }
    292        else if (f.isDirectory()) {
    293            shell.getDisplay().beep();
    294            Tools.showError(shell, "Convert", "Source file is a directory.");
    295            return false;
    296        }
    297
    298        // verify target file
    299        String srcPath = f.getParent();
    300        f = new File(dstFile);
    301        File pfile = f.getParentFile();
    302        if (pfile == null) {
    303            dstFile = srcPath + File.separator + dstFile;
    304            f = new File(dstFile);
    305        }
    306        else if (!pfile.exists()) {
    307            shell.getDisplay().beep();
    308            Tools.showError(shell, "Convert", "Destination file path does not exist at\n"
    309                    + pfile.getPath());
    310            return false;
    311        }
    312
    313        // check if the file is in use
    314        if (fileList != null) {
    315            FileFormat theFile = null;
    316            Iterator<FileFormat> iterator = fileList.iterator();
    317            while (iterator.hasNext()) {
    318                theFile = (FileFormat) iterator.next();
    319                if (theFile.getFilePath().equals(dstFile)) {
    320                    shell.getDisplay().beep();
    321                    Tools.showError(shell, "Convert", "The destination file is being used.");
    322                    return false;
    323                }
    324            }
    325        }
    326
    327        if (f.exists()) {
    328            if(!Tools.showConfirm(shell, "Convert", "Destination file exists. Do you want to replace it ?"))
    329                return false;
    330        }
    331
    332        try {
    333            Tools.convertImageToHDF(srcFile, dstFile, fileTypeFrom, fileTypeTo);
    334            convertedFile = dstFile;
    335            converted = true;
    336        }
    337        catch (Exception ex) {
    338            convertedFile = null;
    339            converted = false;
    340            shell.getDisplay().beep();
    341            Tools.showError(shell, "Convert", ex.getMessage());
    342            return false;
    343        }
    344
    345        return converted;
    346    }
    347
    348    public boolean isFileConverted() {
    349        return isConverted;
    350    }
    351
    352    public String getConvertedFile() {
    353        return convertedFile;
    354    }
    355}