    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.util.ArrayList;
    018import java.util.Iterator;
    019import java.util.List;
    020import java.util.Vector;
    021
    022import org.eclipse.swt.SWT;
    023import org.eclipse.swt.events.DisposeEvent;
    024import org.eclipse.swt.events.DisposeListener;
    025import org.eclipse.swt.events.SelectionAdapter;
    026import org.eclipse.swt.events.SelectionEvent;
    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.Combo;
    033import org.eclipse.swt.widgets.Composite;
    034import org.eclipse.swt.widgets.Display;
    035import org.eclipse.swt.widgets.Label;
    036import org.eclipse.swt.widgets.Shell;
    037import org.eclipse.swt.widgets.Text;
    038
    039import hdf.object.Dataset;
    040import hdf.object.Datatype;
    041import hdf.object.Group;
    042import hdf.object.HObject;
    043import hdf.object.ScalarDS;
    044import hdf.view.Tools;
    045import hdf.view.ViewProperties;
    046
    047/**
    048 * NewImageDialog shows a message dialog requesting user input for creating a
    049 * new HDF4/5 Image.
    050 *
    051 * @author Jordan T. Henderson
    052 * @version 2.4 1/1/2016
    053 */
    054public class NewImageDialog extends NewDataObjectDialog {
    055
    056    private Text        nameField, widthField, heightField;
    057
    058    private Combo       parentChoice;
    059
    060    private Button      checkIndex, checkTrueColor, checkInterlacePixel,
    061                        checkInterlacePlane;
    062
    063    /** A list of current groups */
    064    private List<Group> groupList;
    065
    066    /**
    067     * Constructs a NewImageDialog with specified list of possible parent groups.
    068     *
    069     * @param parent
    070     *            the parent shell of the dialog
    071     * @param pGroup
    072     *            the parent group which the new group is added to.
    073     * @param objs
    074     *            the list of all objects.
    075     */
    076    public NewImageDialog(Shell parent, Group pGroup, List<?> objs) {
    077        super(parent, pGroup, objs);
    078    }
    079
    080    public void open() {
    081        Shell parent = getParent();
    082        shell = new Shell(parent, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
    083        shell.setFont(curFont);
    084        shell.setText("New HDF Image...");
    085        shell.setImage(ViewProperties.getHdfIcon());
    086        shell.setLayout(new GridLayout(1, true));
    087
    088
    089        // Create main content region
    090        Composite content = new Composite(shell, SWT.BORDER);
    091        content.setLayout(new GridLayout(2, false));
    092        content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    093
    094        Label label = new Label(content, SWT.LEFT);
    095        label.setFont(curFont);
    096        label.setText("Image name: ");
    097
    098        nameField = new Text(content, SWT.SINGLE | SWT.BORDER);
    099        nameField.setFont(curFont);
    100        GridData fieldData = new GridData(SWT.FILL, SWT.FILL, true, false);
    101        fieldData.minimumWidth = 300;
    102        nameField.setLayoutData(fieldData);
    103
    104        label = new Label(content, SWT.LEFT);
    105        label.setFont(curFont);
    106        label.setText("Parent Group: ");
    107
    108        parentChoice = new Combo(content, SWT.DROP_DOWN | SWT.READ_ONLY);
    109        parentChoice.setFont(curFont);
    110        parentChoice.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    111        parentChoice.addSelectionListener(new SelectionAdapter() {
    112            @Override
    113            public void widgetSelected(SelectionEvent e) {
    114                parentObj = groupList.get(parentChoice.getSelectionIndex());
    115            }
    116        });
    117
    118        groupList = new ArrayList<>();
    119        Object obj = null;
    120        Iterator<?> iterator = objList.iterator();
    121        while (iterator.hasNext()) {
    122            obj = iterator.next();
    123            if (obj instanceof Group) {
    124                Group g = (Group) obj;
    125                groupList.add(g);
    126                if (g.isRoot()) {
    127                    parentChoice.add(HObject.SEPARATOR);
    128                }
    129                else {
    130                    parentChoice.add(g.getPath() + g.getName()
    131                            + HObject.SEPARATOR);
    132                }
    133            }
    134        }
    135
    136        if (((Group) parentObj).isRoot()) {
    137            parentChoice.select(parentChoice.indexOf(HObject.SEPARATOR));
    138        }
    139        else {
    140            parentChoice.select(parentChoice.indexOf(parentObj.getPath() + parentObj.getName()
    141                    + HObject.SEPARATOR));
    142        }
    143
    144        label = new Label(content, SWT.LEFT);
    145        label.setFont(curFont);
    146        label.setText("Height: ");
    147
    148        heightField = new Text(content, SWT.SINGLE | SWT.BORDER);
    149        heightField.setFont(curFont);
    150        heightField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    151
    152        label = new Label(content, SWT.LEFT);
    153        label.setFont(curFont);
    154        label.setText("Width: ");
    155
    156        widthField = new Text(content, SWT.SINGLE | SWT.BORDER);
    157        widthField.setFont(curFont);
    158        widthField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    159
    160        label = new Label(content, SWT.LEFT);
    161        label.setFont(curFont);
    162        label.setText("Image type: ");
    163
    164        Composite typeComposite = new Composite(content, SWT.BORDER);
    165        typeComposite.setLayout(new GridLayout(2, true));
    166        typeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    167
    168        checkIndex = new Button(typeComposite, SWT.RADIO);
    169        checkIndex.setFont(curFont);
    170        checkIndex.setText("Indexed colormap");
    171        checkIndex.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
    172        checkIndex.addSelectionListener(new SelectionAdapter() {
    173            @Override
    174            public void widgetSelected(SelectionEvent e) {
    175                checkInterlacePixel.setSelection(true);
    176                checkInterlacePlane.setSelection(false);
    177                checkInterlacePixel.setEnabled(false);
    178                checkInterlacePlane.setEnabled(false);
    179            }
    180        });
    181
    182        checkTrueColor = new Button(typeComposite, SWT.RADIO);
    183        checkTrueColor.setFont(curFont);
    184        checkTrueColor.setText("24-bit truecolor");
    185        checkTrueColor.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
    186        checkTrueColor.addSelectionListener(new SelectionAdapter() {
    187            @Override
    188            public void widgetSelected(SelectionEvent e) {
    189                checkInterlacePixel.setEnabled(true);
    190                checkInterlacePlane.setEnabled(true);
    191            }
    192        });
    193
    194        label = new Label(content, SWT.LEFT);
    195        label.setFont(curFont);
    196        label.setText("Data layout: ");
    197
    198        Composite layoutComposite = new Composite(content, SWT.BORDER);
    199        layoutComposite.setLayout(new GridLayout(2, true));
    200        layoutComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    201
    202        checkInterlacePixel = new Button(layoutComposite, SWT.RADIO);
    203        checkInterlacePixel.setFont(curFont);
    204        checkInterlacePixel.setText("Pixel interlace");
    205        checkInterlacePixel.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
    206
    207        checkInterlacePlane = new Button(layoutComposite, SWT.RADIO);
    208        checkInterlacePlane.setFont(curFont);
    209        checkInterlacePlane.setText("Plane interlace");
    210        checkInterlacePlane.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
    211
    212
    213        // Create Ok/Cancel button region
    214        Composite buttonComposite = new Composite(shell, SWT.NONE);
    215        buttonComposite.setLayout(new GridLayout(2, true));
    216        buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    217
    218        Button okButton = new Button(buttonComposite, SWT.PUSH);
    219        okButton.setFont(curFont);
    220        okButton.setText("   &OK   ");
    221        okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
    222        okButton.addSelectionListener(new SelectionAdapter() {
    223            @Override
    224            public void widgetSelected(SelectionEvent e) {
    225                newObject = createHDFimage();
    226                if (newObject != null) {
    227                    shell.dispose();
    228                }
    229            }
    230        });
    231
    232        Button cancelButton = new Button(buttonComposite, SWT.PUSH);
    233        cancelButton.setFont(curFont);
    234        cancelButton.setText(" &Cancel ");
    235        cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
    236        cancelButton.addSelectionListener(new SelectionAdapter() {
    237            @Override
    238            public void widgetSelected(SelectionEvent e) {
    239                newObject = null;
    240                shell.dispose();
    241                ((Vector<Group>) groupList).setSize(0);
    242            }
    243        });
    244
    245        checkIndex.setSelection(true);
    246        checkInterlacePixel.setSelection(true);
    247        checkInterlacePixel.setEnabled(false);
    248        checkInterlacePlane.setEnabled(false);
    249
    250        shell.pack();
    251
    252        shell.addDisposeListener(new DisposeListener() {
    253            @Override
    254            public void widgetDisposed(DisposeEvent e) {
    255                if (curFont != null) curFont.dispose();
    256            }
    257        });
    258
    259        shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    260
    261        Rectangle parentBounds = parent.getBounds();
    262        Point shellSize = shell.getSize();
    263        shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2),
    264                          (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2));
    265
    266        shell.open();
    267
    268        Display display = shell.getDisplay();
    269        while (!shell.isDisposed())
    270            if (!display.readAndDispatch())
    271                display.sleep();
    272    }
    273
    274    private Dataset createHDFimage() {
    275        Dataset dataset = null;
    276
    277        String name = nameField.getText();
    278        if (name != null) {
    279            name = name.trim();
    280        }
    281        if ((name == null) || (name.length() <= 0)) {
    282            shell.getDisplay().beep();
    283            Tools.showError(shell, "Create", "Image name is not specified.");
    284            return null;
    285        }
    286
    287        if (name.indexOf(HObject.SEPARATOR) >= 0) {
    288            shell.getDisplay().beep();
    289            Tools.showError(shell, "Create", "Image name cannot contain path.");
    290            return null;
    291        }
    292
    293        Group pgroup = groupList.get(parentChoice.getSelectionIndex());
    294
    295        if (pgroup == null) {
    296            shell.getDisplay().beep();
    297            Tools.showError(shell, "Create", "Select a parent group.");
    298            return null;
    299        }
    300
    301        int w = 0, h = 0;
    302        try {
    303            w = Integer.parseInt(widthField.getText());
    304            h = Integer.parseInt(heightField.getText());
    305        }
    306        catch (Exception ex) {
    307            shell.getDisplay().beep();
    308            Tools.showError(shell, "Create", ex.getMessage());
    309            return null;
    310        }
    311
    312        long[] dims = null;
    313        int tclass = Datatype.CLASS_CHAR;
    314        int tsign = Datatype.SIGN_NONE;
    315        int tsize = 1;
    316        int torder = Datatype.NATIVE;
    317        int interlace = ScalarDS.INTERLACE_PIXEL;
    318        int ncomp = 2;
    319
    320        if (checkIndex.getSelection()) {
    321            // indexed colormap
    322            if (isH5) {
    323                long[] tmpdims = { h, w };
    324                dims = tmpdims;
    325            }
    326            else {
    327                long[] tmpdims = { w, h };
    328                dims = tmpdims;
    329            }
    330        }
    331        else {
    332            // true color image
    333            if (isH5) {
    334                // HDF5 true color image
    335                if (checkInterlacePixel.getSelection()) {
    336                    long[] tmpdims = { h, w, 3 };
    337                    dims = tmpdims;
    338                }
    339                else {
    340                    interlace = ScalarDS.INTERLACE_PLANE;
    341                    long[] tmpdims = { 3, h, w };
    342                    dims = tmpdims;
    343                }
    344            }
    345            else {
    346                // HDF4 true color image
    347                ncomp = 3;
    348                long[] tmpdims = { w, h };
    349                dims = tmpdims;
    350                if (checkInterlacePlane.getSelection()) {
    351                    interlace = ScalarDS.INTERLACE_PLANE;
    352                }
    353            }
    354        }
    355
    356        try {
    357            Datatype datatype = fileFormat.createDatatype(tclass, tsize, torder, tsign);
    358            dataset = fileFormat.createImage(name, pgroup, datatype, dims, dims, null, -1, ncomp, interlace, null);
    359            dataset.init();
    360        }
    361        catch (Exception ex) {
    362            shell.getDisplay().beep();
    363            Tools.showError(shell, "Create", ex.getMessage());
    364            return null;
    365        }
    366
    367        return dataset;
    368    }
    369}