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.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    public void open() {
079        Shell parent = getParent();
080        shell = new Shell(parent, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
081        shell.setFont(curFont);
082        shell.setText("New Datatype...");
083        shell.setImage(ViewProperties.getHdfIcon());
084        shell.setLayout(new GridLayout(1, false));
085
086
087        // Create Datatype name / Parent group region
088        Composite fieldComposite = new Composite(shell, SWT.NONE);
089        fieldComposite.setLayout(new GridLayout(2, false));
090        fieldComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
091
092        Label label = new Label(fieldComposite, SWT.LEFT);
093        label.setFont(curFont);
094        label.setText("Datatype name: ");
095
096        nameField = new Text(fieldComposite, SWT.SINGLE | SWT.BORDER);
097        nameField.setFont(curFont);
098        nameField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
099        nameField.addTraverseListener(new TraverseListener() {
100            @Override
101            public void keyTraversed(TraverseEvent e) {
102                if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
103                    e.doit = true;
104                }
105            }
106        });
107
108        label = new Label(fieldComposite, SWT.LEFT);
109        label.setFont(curFont);
110        label.setText("Parent Group: ");
111
112        parentChoice = new Combo(fieldComposite, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
113        parentChoice.setFont(curFont);
114        parentChoice.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
115        parentChoice.addSelectionListener(new SelectionAdapter() {
116            @Override
117            public void widgetSelected(SelectionEvent e) {
118                parentObj = groupList.get(parentChoice.getSelectionIndex());
119            }
120        });
121
122        groupList = new Vector<>(objList.size());
123        Object obj = null;
124        Iterator<?> iterator = objList.iterator();
125        while (iterator.hasNext()) {
126            obj = iterator.next();
127            if (obj instanceof Group) {
128                Group g = (Group) obj;
129                groupList.add(g);
130                if (g.isRoot()) {
131                    parentChoice.add(HObject.SEPARATOR);
132                }
133                else {
134                    parentChoice.add(g.getPath() + g.getName() + 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() + HObject.SEPARATOR));
144        }
145
146        // Create Datatype settings region
147        createDatatypeWidget();
148
149        // Create Ok/Cancel button region
150        Composite buttonComposite = new Composite(shell, SWT.NONE);
151        buttonComposite.setLayout(new GridLayout(2, true));
152        buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
153
154        Button okButton = new Button(buttonComposite, SWT.PUSH);
155        okButton.setFont(curFont);
156        okButton.setText("   &OK   ");
157        okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
158        okButton.addSelectionListener(new SelectionAdapter() {
159            @Override
160            public void widgetSelected(SelectionEvent e) {
161                newObject = createNewDatatype();
162
163                if (newObject != null) {
164                    shell.dispose();
165                }
166            }
167        });
168
169        Button cancelButton = new Button(buttonComposite, SWT.PUSH);
170        cancelButton.setFont(curFont);
171        cancelButton.setText(" &Cancel ");
172        cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
173        cancelButton.addSelectionListener(new SelectionAdapter() {
174            @Override
175            public void widgetSelected(SelectionEvent e) {
176                newObject = null;
177                shell.dispose();
178            }
179        });
180
181        //Control[] controls = new Control[] { okButton, cancelButton };
182        //shell.setTabList(controls);
183        shell.pack();
184
185        shell.addDisposeListener(new DisposeListener() {
186            @Override
187            public void widgetDisposed(DisposeEvent e) {
188                if (curFont != null) curFont.dispose();
189            }
190        });
191
192        shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
193
194        Rectangle parentBounds = parent.getBounds();
195        Point shellSize = shell.getSize();
196        shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2),
197                          (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2));
198
199        shell.open();
200
201        Display display = shell.getDisplay();
202        while (!shell.isDisposed())
203            if (!display.readAndDispatch())
204                display.sleep();
205    }
206
207    public Datatype createNewDatatype() {
208        String name = null;
209        Group pgroup = null;
210
211        name = nameField.getText().trim();
212        if ((name == null) || (name.length() < 1)) {
213            shell.getDisplay().beep();
214            Tools.showError(shell, "Create", "Datatype name is not specified.");
215            return null;
216        }
217
218        if (name.indexOf(HObject.SEPARATOR) >= 0) {
219            shell.getDisplay().beep();
220            Tools.showError(shell, "Create", "Datatype name cannot contain path.");
221            return null;
222        }
223
224        pgroup = groupList.get(parentChoice.getSelectionIndex());
225
226        if (pgroup == null) {
227            shell.getDisplay().beep();
228            Tools.showError(shell, "Create", "Parent group is null.");
229            return null;
230        }
231
232        Datatype datatype = null;
233        try {
234            String fullPath = HObject.SEPARATOR;
235            if (pgroup.isRoot()) {
236                fullPath += name;
237            }
238            else {
239                fullPath = pgroup.getPath() + HObject.SEPARATOR + pgroup.getName() + HObject.SEPARATOR + name;
240            }
241            datatype = super.createNewDatatype(fullPath);
242        }
243        catch (Exception ex) {
244            shell.getDisplay().beep();
245            Tools.showError(shell, "Create", ex.getMessage());
246            return null;
247        }
248
249        return datatype;
250    }
251}