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