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.Iterator;
018import java.util.List;
019import java.util.Vector;
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.events.TraverseEvent;
027import org.eclipse.swt.events.TraverseListener;
028import org.eclipse.swt.graphics.Point;
029import org.eclipse.swt.graphics.Rectangle;
030import org.eclipse.swt.layout.GridData;
031import org.eclipse.swt.layout.GridLayout;
032import org.eclipse.swt.widgets.Button;
033import org.eclipse.swt.widgets.Combo;
034import org.eclipse.swt.widgets.Composite;
035import org.eclipse.swt.widgets.Display;
036import org.eclipse.swt.widgets.Label;
037import org.eclipse.swt.widgets.Shell;
038import org.eclipse.swt.widgets.Text;
039
040import hdf.object.Datatype;
041import hdf.object.Group;
042import hdf.object.HObject;
043import hdf.view.Tools;
044import hdf.view.ViewProperties;
045
046/**
047 * NewDatatypeDialog shows a message dialog requesting user input for creating a
048 * new HDF5 datatype.
049 *
050 * @author Jordan T. Henderson
051 * @version 2.4 1/1/2016
052 */
053public class NewDatatypeDialog extends NewDataObjectDialog {
054
055    private Combo             parentChoice;
056
057    /** TextField for entering the name of the object */
058    protected Text            nameField;
059
060    /** a list of current groups */
061    private List<Group>       groupList;
062
063    /**
064     * Constructs a NewDatatypeDialog with specified list of possible parent
065     * groups.
066     *
067     * @param parent
068     *            the parent shell of the dialog
069     * @param pGroup
070     *            the parent group which the new group is added to.
071     * @param objs
072     *            the list of all objects.
073     */
074    public NewDatatypeDialog(Shell parent, Group pGroup, List<?> objs) {
075        super(parent, pGroup, objs);
076    }
077
078    /**
079     * Open the NewDatatypeDialog for adding a new datatype.
080     */
081    public void open() {
082        Shell parent = getParent();
083        shell = new Shell(parent, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
084        shell.setFont(curFont);
085        shell.setText("New Datatype...");
086        shell.setImages(ViewProperties.getHdfIcons());
087        shell.setLayout(new GridLayout(1, false));
088
089
090        // Create Datatype name / Parent group region
091        Composite fieldComposite = new Composite(shell, SWT.NONE);
092        fieldComposite.setLayout(new GridLayout(2, false));
093        fieldComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
094
095        Label label = new Label(fieldComposite, SWT.LEFT);
096        label.setFont(curFont);
097        label.setText("Datatype name: ");
098
099        nameField = new Text(fieldComposite, SWT.SINGLE | SWT.BORDER);
100        nameField.setFont(curFont);
101        nameField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
102        nameField.addTraverseListener(new TraverseListener() {
103            @Override
104            public void keyTraversed(TraverseEvent e) {
105                if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
106                    e.doit = true;
107                }
108            }
109        });
110
111        label = new Label(fieldComposite, SWT.LEFT);
112        label.setFont(curFont);
113        label.setText("Parent Group: ");
114
115        parentChoice = new Combo(fieldComposite, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
116        parentChoice.setFont(curFont);
117        parentChoice.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
118        parentChoice.addSelectionListener(new SelectionAdapter() {
119            @Override
120            public void widgetSelected(SelectionEvent e) {
121                parentObj = groupList.get(parentChoice.getSelectionIndex());
122            }
123        });
124
125        groupList = new Vector<>(objList.size());
126        Object obj = null;
127        Iterator<?> iterator = objList.iterator();
128        while (iterator.hasNext()) {
129            obj = iterator.next();
130            if (obj instanceof Group) {
131                Group g = (Group) obj;
132                groupList.add(g);
133                if (g.isRoot()) {
134                    parentChoice.add(HObject.SEPARATOR);
135                }
136                else {
137                    parentChoice.add(g.getPath() + g.getName() + HObject.SEPARATOR);
138                }
139            }
140        }
141
142        if (((Group) parentObj).isRoot()) {
143            parentChoice.select(parentChoice.indexOf(HObject.SEPARATOR));
144        }
145        else {
146            parentChoice.select(parentChoice.indexOf(parentObj.getPath() + parentObj.getName() + HObject.SEPARATOR));
147        }
148
149        // Create Datatype settings region
150        createDatatypeWidget();
151
152        // Create Ok/Cancel button region
153        Composite buttonComposite = new Composite(shell, SWT.NONE);
154        buttonComposite.setLayout(new GridLayout(2, true));
155        buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
156
157        Button okButton = new Button(buttonComposite, SWT.PUSH);
158        okButton.setFont(curFont);
159        okButton.setText("   &OK   ");
160        okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
161        okButton.addSelectionListener(new SelectionAdapter() {
162            @Override
163            public void widgetSelected(SelectionEvent e) {
164                newObject = createNewDatatype();
165
166                if (newObject != null) {
167                    shell.dispose();
168                }
169            }
170        });
171
172        Button cancelButton = new Button(buttonComposite, SWT.PUSH);
173        cancelButton.setFont(curFont);
174        cancelButton.setText(" &Cancel ");
175        cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
176        cancelButton.addSelectionListener(new SelectionAdapter() {
177            @Override
178            public void widgetSelected(SelectionEvent e) {
179                newObject = null;
180                shell.dispose();
181            }
182        });
183
184        //Control[] controls = new Control[] { okButton, cancelButton };
185        //shell.setTabList(controls);
186        shell.pack();
187
188        shell.addDisposeListener(new DisposeListener() {
189            @Override
190            public void widgetDisposed(DisposeEvent e) {
191                if (curFont != null) curFont.dispose();
192            }
193        });
194
195        shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
196
197        Rectangle parentBounds = parent.getBounds();
198        Point shellSize = shell.getSize();
199        shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2),
200                          (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2));
201
202        shell.open();
203
204        Display display = shell.getDisplay();
205        while (!shell.isDisposed())
206            if (!display.readAndDispatch())
207                display.sleep();
208    }
209
210    /**
211     * Create the datatype specified by the settings.
212     *
213     * @return the new object created.
214     */
215    public Datatype createNewDatatype() {
216        String name = null;
217        Group pgroup = null;
218
219        name = nameField.getText().trim();
220        if ((name == null) || (name.length() < 1)) {
221            shell.getDisplay().beep();
222            Tools.showError(shell, "Create", "Datatype name is not specified.");
223            return null;
224        }
225
226        if (name.indexOf(HObject.SEPARATOR) >= 0) {
227            shell.getDisplay().beep();
228            Tools.showError(shell, "Create", "Datatype name cannot contain path.");
229            return null;
230        }
231
232        pgroup = groupList.get(parentChoice.getSelectionIndex());
233
234        if (pgroup == null) {
235            shell.getDisplay().beep();
236            Tools.showError(shell, "Create", "Parent group is null.");
237            return null;
238        }
239
240        Datatype datatype = null;
241        try {
242            String fullPath = HObject.SEPARATOR;
243            if (pgroup.isRoot()) {
244                fullPath += name;
245            }
246            else {
247                fullPath = pgroup.getPath() + HObject.SEPARATOR + pgroup.getName() + HObject.SEPARATOR + name;
248            }
249            datatype = super.createNewDatatype(fullPath);
250        }
251        catch (Exception ex) {
252            shell.getDisplay().beep();
253            Tools.showError(shell, "Create", ex.getMessage());
254            return null;
255        }
256
257        return datatype;
258    }
259}