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 org.eclipse.swt.SWT;
018import org.eclipse.swt.events.DisposeEvent;
019import org.eclipse.swt.events.DisposeListener;
020import org.eclipse.swt.events.SelectionAdapter;
021import org.eclipse.swt.events.SelectionEvent;
022import org.eclipse.swt.graphics.Font;
023import org.eclipse.swt.graphics.Point;
024import org.eclipse.swt.graphics.Rectangle;
025import org.eclipse.swt.layout.GridData;
026import org.eclipse.swt.layout.GridLayout;
027import org.eclipse.swt.widgets.Button;
028import org.eclipse.swt.widgets.Composite;
029import org.eclipse.swt.widgets.Dialog;
030import org.eclipse.swt.widgets.Display;
031import org.eclipse.swt.widgets.Event;
032import org.eclipse.swt.widgets.Label;
033import org.eclipse.swt.widgets.Listener;
034import org.eclipse.swt.widgets.Shell;
035import org.eclipse.swt.widgets.Text;
036
037import hdf.HDFVersions;
038import hdf.view.ViewProperties;
039
040/**
041 * Custom SWT dialog to allow the user to input strings
042 * for various uses.
043 */
044//TODO: Add ability to have custom HDF icons
045public class InputDialog extends Dialog {
046    private Text            inputField;
047    private final String    title;
048    private final String    message;
049    private final String    initialText;
050    private String          result;
051    private Font            curFont;
052
053    public InputDialog(Shell parent) {
054        this(parent, "HDFView " + HDFVersions.getPropertyVersionView(), "");
055    }
056
057    public InputDialog(Shell parent, String title, String message) {
058        this(parent, title, message, "");
059    }
060
061    public InputDialog(Shell parent, String title, String message, int style) {
062        this(parent, title, message, "", style);
063    }
064
065    public InputDialog(Shell parent, String title, String message, String initialText) {
066        this(parent, title, message, initialText, SWT.NONE);
067    }
068
069    public InputDialog(Shell parent, String title, String message, String initialText, int style) {
070        super(parent, style);
071        this.title = title;
072        this.message = message;
073        this.initialText = initialText;
074
075        try {
076            curFont = new Font(
077                    Display.getCurrent(),
078                    ViewProperties.getFontType(),
079                    ViewProperties.getFontSize(),
080                    SWT.NORMAL);
081        }
082        catch (Exception ex) {
083            curFont = null;
084        }
085    }
086
087    /**
088     * Opens the InputDialog and returns the user's input
089     * when the dialog closes.
090     *
091     * @return the user input data
092     */
093    public String open() {
094        Shell parent = getParent();
095        final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL | SWT.RESIZE);
096        shell.setFont(curFont);
097        shell.setText(title);
098        shell.setLayout(new GridLayout(1, true));
099
100        Label label = new Label(shell, SWT.NULL);
101        label.setFont(curFont);
102        label.setText(message);
103
104        inputField = new Text(shell, SWT.SINGLE | SWT.BORDER);
105        inputField.setFont(curFont);
106        inputField.setText(initialText);
107        GridData fieldData = new GridData(SWT.FILL, SWT.FILL, true, false);
108        fieldData.minimumWidth = 300;
109        inputField.setLayoutData(fieldData);
110
111        // Dummy label to fill space as dialog is resized
112        new Label(shell, SWT.NONE).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
113
114        Composite buttonComposite = new Composite(shell, SWT.NONE);
115        buttonComposite.setLayout(new GridLayout(2, true));
116        buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
117
118        Button okButton = new Button(buttonComposite, SWT.PUSH);
119        okButton.setFont(curFont);
120        okButton.setText("   &OK   ");
121        okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
122        okButton.addSelectionListener(new SelectionAdapter() {
123            public void widgetSelected(SelectionEvent e) {
124                shell.dispose();
125            }
126        });
127
128        Button cancelButton = new Button(buttonComposite, SWT.PUSH);
129        cancelButton.setFont(curFont);
130        cancelButton.setText(" &Cancel ");
131        cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
132        cancelButton.addSelectionListener(new SelectionAdapter() {
133            public void widgetSelected(SelectionEvent e) {
134                result = null;
135                shell.dispose();
136            }
137        });
138
139        inputField.addListener(SWT.Modify, new Listener() {
140            public void handleEvent(Event event) {
141                try {
142                    result = inputField.getText();
143                }
144                catch (Exception ex) {
145                    System.err.println("Error retrieving input value.");
146                }
147            }
148        });
149
150        shell.addListener(SWT.Traverse, new Listener() {
151            public void handleEvent(Event event) {
152                if(event.detail == SWT.TRAVERSE_ESCAPE)
153                    event.doit = false;
154            }
155        });
156
157        shell.pack();
158
159        shell.addDisposeListener(new DisposeListener() {
160            public void widgetDisposed(DisposeEvent e) {
161                if (curFont != null) curFont.dispose();
162            }
163        });
164
165        shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
166
167        Rectangle parentBounds = parent.getBounds();
168        Point shellSize = shell.getSize();
169        shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2),
170                          (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2));
171
172        shell.open();
173
174        Display display = parent.getDisplay();
175        while(!shell.isDisposed()) {
176            if (!display.readAndDispatch())
177                display.sleep();
178        }
179
180        // TODO: Display loop should not wait here, but we must wait until
181        // an input is given before returning
182        return result;
183    }
184}