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.awt.GraphicsEnvironment;
018import java.io.File;
019
020import hdf.view.Tools;
021import hdf.view.ViewProperties;
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import org.eclipse.swt.SWT;
027import org.eclipse.swt.events.SelectionAdapter;
028import org.eclipse.swt.events.SelectionEvent;
029import org.eclipse.swt.graphics.Font;
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.Control;
036import org.eclipse.swt.widgets.DirectoryDialog;
037import org.eclipse.swt.widgets.Display;
038import org.eclipse.swt.widgets.FileDialog;
039import org.eclipse.swt.widgets.Label;
040import org.eclipse.swt.widgets.Text;
041
042/**
043 * UserOptionsHDFPage.java - Configuration page for HDF-specific application
044 * settings.
045 */
046public class UserOptionsHDFPage extends UserOptionsDefaultPage {
047    private static final Logger log = LoggerFactory.getLogger(UserOptionsHDFPage.class);
048
049    private Text fileExtField, pluginField;
050    private Button checkConvertEnum, checkShowRegRefValues, helpButton;
051    private Button checkNativeOrder, checkDecOrder, checkIncOrder;
052    private Button checkIndexName, checkIndexCreateOrder;
053    private Button earlyLibVersion, early18LibVersion, early110LibVersion, early112LibVersion,
054        early114LibVersion, earlyLateLibVersion;
055    private Button lateLibVersion, late18LibVersion, late110LibVersion, late112LibVersion, late114LibVersion,
056        lateLateLibVersion;
057    private Button checkCurrentUserDir, checkUserHomeDir;
058    private Button currentDirButton, userHomeButton;
059
060    /** Default early libversion for files */
061    private static String earlyLibVers;
062
063    /** Default late libversion for files */
064    private static String lateLibVers;
065
066    /** Default index type for files */
067    private static String indexType;
068
069    /** Default index ordering for files */
070    private static String indexOrder;
071
072    /** Path to plugins */
073    private String pluginDir;
074    private boolean isPluginDirChanged;
075
076    /**
077     * Configuration page for HDF-specific application settings.
078     */
079    public UserOptionsHDFPage()
080    {
081        super("HDF Settings");
082        isPluginDirChanged = false;
083    }
084
085    /**
086     * Performs special processing when this page's Defaults button has been pressed.
087     */
088    @Override
089    public void performDefaults()
090    {
091        super.performDefaults();
092        getPreferenceStore();
093    }
094
095    /**
096     * Notifies that the OK button of this page's container has been pressed.
097     *
098     * @return <code>false</code> to abort the container's OK processing and
099     * <code>true</code> to allow the OK to happen
100     */
101    @Override
102    public boolean performOk()
103    {
104        getPreferenceStore();
105
106        if (fileExtField != null) {
107            String ext = fileExtField.getText();
108            if ((ext != null) && (ext.length() > 0)) {
109                ext = ext.trim();
110                ViewProperties.setFileExtension(ext);
111            }
112        }
113
114        log.trace("performOk: save HDF options earlyLibVersion={}", earlyLibVersion);
115        if (earlyLibVersion != null) {
116            if (earlyLibVersion.getSelection())
117                ViewProperties.setEarlyLib("Earliest");
118            else if (early18LibVersion.getSelection())
119                ViewProperties.setEarlyLib("v18");
120            else if (early110LibVersion.getSelection())
121                ViewProperties.setEarlyLib("v110");
122            else if (early112LibVersion.getSelection())
123                ViewProperties.setEarlyLib("v112");
124            else if (early114LibVersion.getSelection())
125                ViewProperties.setEarlyLib("v114");
126            else if (earlyLateLibVersion.getSelection())
127                ViewProperties.setEarlyLib("Latest");
128            else
129                ViewProperties.setEarlyLib("Earliest");
130        }
131
132        log.trace("performOk: save HDF options lateLibVersion={}", lateLibVersion);
133        if (lateLibVersion != null) {
134            if (lateLibVersion.getSelection())
135                ViewProperties.setLateLib("Earliest");
136            else if (late18LibVersion.getSelection())
137                ViewProperties.setLateLib("v18");
138            else if (late110LibVersion.getSelection())
139                ViewProperties.setLateLib("v110");
140            else if (late112LibVersion.getSelection())
141                ViewProperties.setLateLib("v112");
142            else if (late114LibVersion.getSelection())
143                ViewProperties.setLateLib("v114");
144            else if (lateLateLibVersion.getSelection())
145                ViewProperties.setLateLib("Latest");
146            else
147                ViewProperties.setLateLib("Latest");
148        }
149
150        // set index type
151        if (checkIndexName != null) {
152            if (checkIndexName.getSelection())
153                ViewProperties.setIndexType("H5_INDEX_NAME");
154            else
155                ViewProperties.setIndexType("H5_INDEX_CRT_ORDER");
156        }
157
158        // set index order
159        if (checkIncOrder != null) {
160            if (checkIncOrder.getSelection())
161                ViewProperties.setIndexOrder("H5_ITER_INC");
162            else if (checkNativeOrder.getSelection())
163                ViewProperties.setIndexOrder("H5_ITER_NATIVE");
164            else
165                ViewProperties.setIndexOrder("H5_ITER_DEC");
166        }
167
168        if (checkConvertEnum != null)
169            ViewProperties.setConvertEnum(checkConvertEnum.getSelection());
170        if (checkShowRegRefValues != null)
171            ViewProperties.setShowRegRefValue(checkShowRegRefValues.getSelection());
172
173        if (pluginField != null) {
174            String pluginPath = pluginField.getText();
175            if (checkCurrentUserDir.getSelection())
176                pluginPath = System.getProperty("user.dir");
177            else if (checkUserHomeDir.getSelection())
178                pluginPath = System.getProperty("user.home");
179
180            if ((pluginPath != null) && (pluginPath.length() > 0)) {
181                pluginPath         = pluginPath.trim();
182                isPluginDirChanged = !pluginPath.equals(ViewProperties.getPluginDir());
183                ViewProperties.setPluginDir(pluginPath);
184            }
185        }
186
187        return true;
188    }
189
190    /**
191     * Checks if the location of the PluginDir changed.
192     *
193     * @return true if the plugin directory changed.
194     */
195    public boolean isPluginDirChanged() { return isPluginDirChanged; }
196
197    /**
198     * Loads all stored values in the <code>FieldEditor</code>s.
199     */
200    protected void load()
201    {
202        getPreferenceStore();
203
204        fileExtField.setText(ViewProperties.getFileExtension());
205
206        earlyLibVers = ViewProperties.getEarlyLib();
207        log.trace("performOk: load HDF options earlyLibVers={}", earlyLibVers);
208        earlyLibVersion.setSelection(earlyLibVers.compareTo("Earliest") == 0);
209        early18LibVersion.setSelection(earlyLibVers.compareTo("v18") == 0);
210        early110LibVersion.setSelection(earlyLibVers.compareTo("v110") == 0);
211        early112LibVersion.setSelection(earlyLibVers.compareTo("v112") == 0);
212        early114LibVersion.setSelection(earlyLibVers.compareTo("v114") == 0);
213        earlyLateLibVersion.setSelection(earlyLibVers.compareTo("Latest") == 0);
214
215        lateLibVers = ViewProperties.getLateLib();
216        log.trace("performOk: load HDF options lateLibVers={}", lateLibVers);
217        lateLibVersion.setSelection(lateLibVers.compareTo("Earliest") == 0);
218        late18LibVersion.setSelection(lateLibVers.compareTo("v18") == 0);
219        late110LibVersion.setSelection(lateLibVers.compareTo("v110") == 0);
220        late112LibVersion.setSelection(lateLibVers.compareTo("v112") == 0);
221        late114LibVersion.setSelection(lateLibVers.compareTo("v114") == 0);
222        lateLateLibVersion.setSelection(lateLibVers.compareTo("Latest") == 0);
223
224        checkConvertEnum.setSelection(ViewProperties.isConvertEnum());
225        checkShowRegRefValues.setSelection(ViewProperties.showRegRefValues());
226
227        indexType = ViewProperties.getIndexType();
228        checkIndexName.setSelection(indexType.compareTo("H5_INDEX_NAME") == 0);
229        checkIndexCreateOrder.setSelection(indexType.compareTo("H5_INDEX_CRT_ORDER") == 0);
230
231        indexOrder = ViewProperties.getIndexOrder();
232        checkIncOrder.setSelection(indexOrder.compareTo("H5_ITER_INC") == 0);
233        checkDecOrder.setSelection(indexOrder.compareTo("H5_ITER_DEC") == 0);
234        checkNativeOrder.setSelection(indexOrder.compareTo("H5_ITER_NATIVE") == 0);
235
236        pluginDir = ViewProperties.getPluginDir();
237        if (pluginDir == null)
238            pluginDir = rootDir;
239
240        pluginField.setText(pluginDir);
241
242        if (pluginDir.equals(System.getProperty("user.dir"))) {
243            checkCurrentUserDir.setSelection(true);
244            checkUserHomeDir.setSelection(false);
245            pluginField.setEnabled(false);
246        }
247        else if (pluginDir.equals(System.getProperty("user.home"))) {
248            checkCurrentUserDir.setSelection(false);
249            checkUserHomeDir.setSelection(true);
250            pluginField.setEnabled(false);
251        }
252        else {
253            checkCurrentUserDir.setSelection(false);
254            checkUserHomeDir.setSelection(false);
255            pluginField.setEnabled(true);
256        }
257
258        log.trace("UserOptionsHDFlPage: pluginDir={}", pluginDir);
259    }
260
261    /**
262     * Creates and returns the SWT control for the customized body of this
263     * preference page under the given parent composite.
264     *
265     * @param parent the parent composite
266     * @return the new control
267     */
268    @Override
269    protected Control createContents(Composite parent)
270    {
271        shell               = parent.getShell();
272        Composite composite = new Composite(parent, SWT.NONE);
273        composite.setLayout(new GridLayout(1, false));
274
275        org.eclipse.swt.widgets.Group fileExtensionGroup =
276            new org.eclipse.swt.widgets.Group(composite, SWT.NONE);
277        fileExtensionGroup.setLayout(new GridLayout(2, true));
278        fileExtensionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
279        fileExtensionGroup.setFont(curFont);
280        fileExtensionGroup.setText("File Extensions");
281
282        Label label = new Label(fileExtensionGroup, SWT.RIGHT);
283        label.setFont(curFont);
284        label.setText("Extensions: ");
285
286        fileExtField = new Text(fileExtensionGroup, SWT.SINGLE | SWT.BORDER);
287        fileExtField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
288        fileExtField.setFont(curFont);
289
290        org.eclipse.swt.widgets.Group defaultLibVersionGroup =
291            new org.eclipse.swt.widgets.Group(composite, SWT.NONE);
292        defaultLibVersionGroup.setLayout(new GridLayout());
293        defaultLibVersionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
294        defaultLibVersionGroup.setFont(curFont);
295        defaultLibVersionGroup.setText("Default Lib Version");
296
297        org.eclipse.swt.widgets.Group earlyLibVersionGroup =
298            new org.eclipse.swt.widgets.Group(defaultLibVersionGroup, SWT.NONE);
299        earlyLibVersionGroup.setLayout(new GridLayout(4, true));
300        earlyLibVersionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
301        earlyLibVersionGroup.setFont(curFont);
302        earlyLibVersionGroup.setText("Default Early Lib Version");
303
304        earlyLibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
305        earlyLibVersion.setFont(curFont);
306        earlyLibVersion.setText("Earliest");
307        earlyLibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
308
309        early18LibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
310        early18LibVersion.setFont(curFont);
311        early18LibVersion.setText("v18");
312        early18LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
313
314        early110LibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
315        early110LibVersion.setFont(curFont);
316        early110LibVersion.setText("v110");
317        early110LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
318
319        early112LibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
320        early112LibVersion.setFont(curFont);
321        early112LibVersion.setText("v112");
322        early112LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
323
324        early114LibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
325        early114LibVersion.setFont(curFont);
326        early114LibVersion.setText("v114");
327        early114LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
328
329        earlyLateLibVersion = new Button(earlyLibVersionGroup, SWT.RADIO);
330        earlyLateLibVersion.setFont(curFont);
331        earlyLateLibVersion.setText("Latest");
332        earlyLateLibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
333
334        org.eclipse.swt.widgets.Group lateLibVersionGroup =
335            new org.eclipse.swt.widgets.Group(defaultLibVersionGroup, SWT.NONE);
336        lateLibVersionGroup.setLayout(new GridLayout(4, true));
337        lateLibVersionGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
338        lateLibVersionGroup.setFont(curFont);
339        lateLibVersionGroup.setText("Default Late Lib Version");
340
341        lateLibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
342        lateLibVersion.setFont(curFont);
343        lateLibVersion.setText("Earliest");
344        lateLibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
345
346        late18LibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
347        late18LibVersion.setFont(curFont);
348        late18LibVersion.setText("v18");
349        late18LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
350
351        late110LibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
352        late110LibVersion.setFont(curFont);
353        late110LibVersion.setText("v110");
354        late110LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
355
356        late112LibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
357        late112LibVersion.setFont(curFont);
358        late112LibVersion.setText("v112");
359        late112LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
360
361        late114LibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
362        late114LibVersion.setFont(curFont);
363        late114LibVersion.setText("v114");
364        late114LibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
365
366        lateLateLibVersion = new Button(lateLibVersionGroup, SWT.RADIO);
367        lateLateLibVersion.setFont(curFont);
368        lateLateLibVersion.setText("Latest");
369        lateLateLibVersion.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
370
371        org.eclipse.swt.widgets.Group dataGroup = new org.eclipse.swt.widgets.Group(composite, SWT.NONE);
372        dataGroup.setLayout(new GridLayout(4, false));
373        dataGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
374        dataGroup.setFont(curFont);
375        dataGroup.setText("Data");
376
377        helpButton = new Button(dataGroup, SWT.PUSH);
378        helpButton.setImage(ViewProperties.getHelpIcon());
379        helpButton.setToolTipText("Help on Convert Enum");
380        helpButton.addSelectionListener(new SelectionAdapter() {
381            @Override
382            public void widgetSelected(SelectionEvent e)
383            {
384                final String msg = "Convert enum data to strings. \n"
385                                   + "For example, a dataset of an enum type of (R=0, G=, B=2) \n"
386                                   +
387                                   "has values of (0, 2, 2, 2, 1, 1). With conversion, the data values are \n"
388                                   + "shown as (R, B, B, B, G, G).\n\n\n";
389
390                Tools.showInformation(getShell(), "Help", msg);
391            }
392        });
393
394        checkConvertEnum = new Button(dataGroup, SWT.CHECK);
395        checkConvertEnum.setFont(curFont);
396        checkConvertEnum.setText("Convert Enum");
397        checkConvertEnum.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, false));
398
399        checkShowRegRefValues = new Button(dataGroup, SWT.CHECK);
400        checkShowRegRefValues.setFont(curFont);
401        checkShowRegRefValues.setText("Show RegRef Values");
402        checkShowRegRefValues.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
403
404        org.eclipse.swt.widgets.Group displayIndexingGroup =
405            new org.eclipse.swt.widgets.Group(composite, SWT.NONE);
406        displayIndexingGroup.setLayout(new GridLayout());
407        displayIndexingGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
408        displayIndexingGroup.setFont(curFont);
409        displayIndexingGroup.setText("Display Indexing Options");
410
411        org.eclipse.swt.widgets.Group indexingTypeGroup =
412            new org.eclipse.swt.widgets.Group(displayIndexingGroup, SWT.NONE);
413        indexingTypeGroup.setLayout(new GridLayout(2, true));
414        indexingTypeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
415        indexingTypeGroup.setFont(curFont);
416        indexingTypeGroup.setText("Indexing Type");
417
418        checkIndexName = new Button(indexingTypeGroup, SWT.RADIO);
419        checkIndexName.setFont(curFont);
420        checkIndexName.setText("By Name");
421        checkIndexName.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
422
423        checkIndexCreateOrder = new Button(indexingTypeGroup, SWT.RADIO);
424        checkIndexCreateOrder.setFont(curFont);
425        checkIndexCreateOrder.setText("By Creation Order");
426        checkIndexCreateOrder.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
427
428        org.eclipse.swt.widgets.Group indexingOrderGroup =
429            new org.eclipse.swt.widgets.Group(displayIndexingGroup, SWT.NONE);
430        indexingOrderGroup.setLayout(new GridLayout(3, true));
431        indexingOrderGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
432        indexingOrderGroup.setFont(curFont);
433        indexingOrderGroup.setText("Indexing Order");
434
435        checkIncOrder = new Button(indexingOrderGroup, SWT.RADIO);
436        checkIncOrder.setFont(curFont);
437        checkIncOrder.setText("Increments");
438        checkIncOrder.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
439
440        checkDecOrder = new Button(indexingOrderGroup, SWT.RADIO);
441        checkDecOrder.setFont(curFont);
442        checkDecOrder.setText("Decrements");
443        checkDecOrder.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
444
445        checkNativeOrder = new Button(indexingOrderGroup, SWT.RADIO);
446        checkNativeOrder.setFont(curFont);
447        checkNativeOrder.setText("Native");
448        checkNativeOrder.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
449
450        org.eclipse.swt.widgets.Group pluginDirectoryGroup =
451            new org.eclipse.swt.widgets.Group(composite, SWT.NONE);
452        pluginDirectoryGroup.setLayout(new GridLayout(3, false));
453        pluginDirectoryGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
454        pluginDirectoryGroup.setFont(curFont);
455        pluginDirectoryGroup.setText("Default Plugin Directory (*/plugin)");
456
457        checkCurrentUserDir = new Button(pluginDirectoryGroup, SWT.CHECK);
458        checkCurrentUserDir.setFont(curFont);
459        checkCurrentUserDir.setText("\"User Work\" or");
460        checkCurrentUserDir.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
461        checkCurrentUserDir.addSelectionListener(new SelectionAdapter() {
462            @Override
463            public void widgetSelected(SelectionEvent e)
464            {
465                boolean isCheckCurrentUserDirSelected = checkCurrentUserDir.getSelection();
466                if (isCheckCurrentUserDirSelected)
467                    checkUserHomeDir.setSelection(false);
468                pluginField.setEnabled(!isCheckCurrentUserDirSelected);
469                currentDirButton.setEnabled(!isCheckCurrentUserDirSelected);
470            }
471        });
472
473        checkUserHomeDir = new Button(pluginDirectoryGroup, SWT.CHECK);
474        checkUserHomeDir.setFont(curFont);
475        checkUserHomeDir.setText("\"User Home\" or");
476        checkUserHomeDir.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
477        checkUserHomeDir.addSelectionListener(new SelectionAdapter() {
478            @Override
479            public void widgetSelected(SelectionEvent e)
480            {
481                boolean isCheckUserHomeDirSelected = checkUserHomeDir.getSelection();
482                if (isCheckUserHomeDirSelected)
483                    checkCurrentUserDir.setSelection(false);
484                pluginField.setEnabled(!isCheckUserHomeDirSelected);
485                currentDirButton.setEnabled(!isCheckUserHomeDirSelected);
486            }
487        });
488
489        pluginField = new Text(pluginDirectoryGroup, SWT.SINGLE | SWT.BORDER);
490        pluginField.setFont(curFont);
491        pluginField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
492
493        currentDirButton = new Button(pluginDirectoryGroup, SWT.PUSH);
494        currentDirButton.setFont(curFont);
495        currentDirButton.setText("Browse...");
496        currentDirButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
497        currentDirButton.addSelectionListener(new SelectionAdapter() {
498            @Override
499            public void widgetSelected(SelectionEvent e)
500            {
501                final DirectoryDialog dChooser = new DirectoryDialog(shell);
502                dChooser.setFilterPath(pluginDir);
503                dChooser.setText("Select a Base Directory");
504
505                String dir = dChooser.open();
506
507                if (dir == null)
508                    return;
509
510                pluginField.setText(dir + "/plugin");
511            }
512        });
513
514        load();
515        return composite;
516    }
517}