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 file COPYING.                     *
009 * COPYING can be found at the root of the source code distribution tree.    *
010 * If you do not have access to this file, you may request a copy from       *
011 * help@hdfgroup.org.                                                        *
012 ****************************************************************************/
013
014package hdf.view;
015
016import java.awt.BorderLayout;
017import java.awt.Color;
018import java.awt.Dimension;
019import java.awt.GraphicsEnvironment;
020import java.awt.GridBagConstraints;
021import java.awt.GridBagLayout;
022import java.awt.GridLayout;
023import java.awt.Insets;
024import java.awt.Point;
025import java.awt.event.ActionEvent;
026import java.awt.event.ActionListener;
027import java.awt.event.ItemEvent;
028import java.awt.event.ItemListener;
029import java.io.File;
030import java.util.Vector;
031
032import javax.swing.BorderFactory;
033import javax.swing.ButtonGroup;
034import javax.swing.JButton;
035import javax.swing.JCheckBox;
036import javax.swing.JComboBox;
037import javax.swing.JDialog;
038import javax.swing.JFileChooser;
039import javax.swing.JFrame;
040import javax.swing.JLabel;
041import javax.swing.JOptionPane;
042import javax.swing.JPanel;
043import javax.swing.JRadioButton;
044import javax.swing.JTabbedPane;
045import javax.swing.JTextField;
046import javax.swing.border.BevelBorder;
047import javax.swing.border.SoftBevelBorder;
048import javax.swing.border.TitledBorder;
049
050/**
051 * UserOptionsDialog displays components for choosing user options.
052 *
053 * @author Peter X. Cao
054 * @version 2.4 9/6/2007
055 */
056public class UserOptionsDialog extends JDialog implements ActionListener, ItemListener
057{
058    private static final long     serialVersionUID = -8521813136101442590L;
059
060    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UserOptionsDialog.class);
061
062    /**
063     * The main HDFView.
064     */
065    private final JFrame          viewer;
066
067    private String                H4toH5Path;
068    private JTextField            H4toH5Field, UGField, workField, fileExtField, maxMemberField, startMemberField;
069    @SuppressWarnings("rawtypes")
070    private JComboBox             fontSizeChoice, fontTypeChoice, delimiterChoice, imageOriginChoice, indexBaseChoice;
071    @SuppressWarnings("rawtypes")
072    private JComboBox             choiceTreeView, choiceMetaDataView, choiceTextView, choiceTableView, choiceImageView,
073    choicePaletteView;
074    private String                rootDir, workDir;
075    private JCheckBox             checkCurrentUserDir, checkAutoContrast, checkConvertEnum, checkShowValues, checkShowRegRefValues;
076    private JButton               currentDirButton;
077    private JRadioButton          checkReadOnly, checkIndexType, checkIndexOrder, checkIndexNative, checkLibVersion,
078                                  checkReadAll;
079
080    private int                   fontSize;
081
082    private boolean               isFontChanged;
083
084    private boolean               isUserGuideChanged;
085
086    private boolean               isWorkDirChanged;
087
088    /** default index type for files */
089    private static String         indexType;
090
091    /** default index ordering for files */
092    private static String         indexOrder;
093
094    /** a list of tree view implementation. */
095    private static Vector<String> treeViews;
096
097    /** a list of image view implementation. */
098    private static Vector<String> imageViews;
099
100    /** a list of tree table implementation. */
101    private static Vector<String> tableViews;
102
103    /** a list of Text view implementation. */
104    private static Vector<String> textViews;
105
106    /** a list of metadata view implementation. */
107    private static Vector<String> metaDataViews;
108
109    /** a list of palette view implementation. */
110    private static Vector<String> paletteViews;
111
112    // private JList srbJList;
113    // private JTextField srbFields[];
114    // private Vector srbVector;
115
116    /**
117     * constructs an UserOptionsDialog.
118     *
119     * @param view
120     *            The HDFView.
121     * @param viewroot
122     *            The root directory of HDFView
123     */
124    public UserOptionsDialog(JFrame view, String viewroot) {
125        super(view, "User Options", true);
126
127        viewer = view;
128        rootDir = viewroot;
129        isFontChanged = false;
130        isUserGuideChanged = false;
131        isWorkDirChanged = false;
132        // srbJList = null;
133        fontSize = ViewProperties.getFontSize();
134        workDir = ViewProperties.getWorkDir();
135        if (workDir == null) {
136            workDir = rootDir;
137        }
138        log.trace("UserOptionsDialog: workDir={}", workDir);
139        treeViews = ViewProperties.getTreeViewList();
140        metaDataViews = ViewProperties.getMetaDataViewList();
141        textViews = ViewProperties.getTextViewList();
142        tableViews = ViewProperties.getTableViewList();
143        imageViews = ViewProperties.getImageViewList();
144        paletteViews = ViewProperties.getPaletteViewList();
145        // srbVector = ViewProperties.getSrbAccount();
146        indexType = ViewProperties.getIndexType();
147        indexOrder = ViewProperties.getIndexOrder();
148
149        JPanel contentPane = (JPanel) getContentPane();
150        contentPane.setLayout(new BorderLayout(8, 8));
151        contentPane.setBorder(BorderFactory.createEmptyBorder(15, 5, 5, 5));
152
153        int w = 700 + (ViewProperties.getFontSize() - 12) * 15;
154        int h = 550 + (ViewProperties.getFontSize() - 12) * 16;
155        contentPane.setPreferredSize(new Dimension(w, h));
156
157        JTabbedPane tabbedPane = new JTabbedPane();
158
159        tabbedPane.addTab("General Setting", createGeneralOptionPanel());
160        tabbedPane.addTab("Default Module", createModuleOptionPanel());
161
162        /*
163         * try { Class.forName("hdf.srb.SRBFileDialog");
164         * tabbedPane.addTab("SRB Connection", createSrbConnectionPanel()); }
165         * catch (Exception ex) {;}
166         */
167
168        tabbedPane.setSelectedIndex(0);
169
170        JPanel buttonP = new JPanel();
171        JButton b = new JButton("   Ok   ");
172        b.setActionCommand("Set options");
173        b.addActionListener(this);
174        b.setName("Ok");
175        buttonP.add(b);
176        b = new JButton("Cancel");
177        b.setActionCommand("Cancel");
178        b.addActionListener(this);
179        buttonP.add(b);
180
181        contentPane.add("Center", tabbedPane);
182        contentPane.add("South", buttonP);
183
184        // locate the H5Property dialog
185        Point l = getParent().getLocation();
186        l.x += 250;
187        l.y += 80;
188        setLocation(l);
189        validate();
190        pack();
191    }
192
193    public void setVisible(boolean b) {
194        if (b) { // reset flags
195            isFontChanged = false;
196            isUserGuideChanged = false;
197            isWorkDirChanged = false;
198            fontSize = ViewProperties.getFontSize();
199            workDir = ViewProperties.getWorkDir();
200            if (workDir == null) {
201                workDir = rootDir;
202            }
203            log.trace("UserOptionsDialog:setVisible workDir={}", workDir);
204        }
205        super.setVisible(b);
206    }
207
208    @SuppressWarnings({ "unchecked", "rawtypes" })
209    private JPanel createGeneralOptionPanel() {
210        String[] fontSizeChoices = { "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32", "34", "36", "48" };
211        fontSizeChoice = new JComboBox(fontSizeChoices);
212        fontSizeChoice.setSelectedItem(String.valueOf(ViewProperties.getFontSize()));
213
214        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
215        String fname = ViewProperties.getFontType();
216        fontTypeChoice = new JComboBox(fontNames);
217
218        boolean isFontValid = false;
219        if (fontNames != null) {
220            for (int i = 0; i < fontNames.length; i++) {
221                if (fontNames[i].equalsIgnoreCase(fname)) {
222                    isFontValid = true;
223                }
224            }
225        }
226        if (!isFontValid) {
227            fname = (viewer).getFont().getFamily();
228            ViewProperties.setFontType(fname);
229        }
230        fontTypeChoice.setSelectedItem(fname);
231
232        String[] delimiterChoices = { ViewProperties.DELIMITER_TAB, ViewProperties.DELIMITER_COMMA,
233                ViewProperties.DELIMITER_SPACE, ViewProperties.DELIMITER_COLON, ViewProperties.DELIMITER_SEMI_COLON };
234        delimiterChoice = new JComboBox(delimiterChoices);
235        delimiterChoice.setSelectedItem(ViewProperties.getDataDelimiter());
236
237        String[] imageOriginChoices = { ViewProperties.ORIGIN_UL, ViewProperties.ORIGIN_LL, ViewProperties.ORIGIN_UR,
238                ViewProperties.ORIGIN_LR };
239        imageOriginChoice = new JComboBox(imageOriginChoices);
240        imageOriginChoice.setSelectedItem(ViewProperties.getImageOrigin());
241
242        JPanel centerP = new JPanel();
243        GridBagConstraints c = new GridBagConstraints();
244        // natural height, maximum width
245        c.fill = GridBagConstraints.HORIZONTAL;
246        c.weightx = 0.5;
247        c.fill = GridBagConstraints.HORIZONTAL;
248        centerP.setLayout(new GridBagLayout());
249        centerP.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
250
251        JPanel p0 = new JPanel();
252        p0.setLayout(new BorderLayout());
253        p0.add(checkCurrentUserDir = new JCheckBox("\"Current Working Directory\" or", false), BorderLayout.WEST);
254        checkCurrentUserDir.addActionListener(this);
255        checkCurrentUserDir.setActionCommand("Set current dir to user.home");
256        p0.add(workField = new JTextField(workDir), BorderLayout.CENTER);
257        JButton b = new JButton("Browse...");
258        currentDirButton = b;
259        b.setActionCommand("Browse current dir");
260        b.addActionListener(this);
261        p0.add(b, BorderLayout.EAST);
262        TitledBorder tborder = new TitledBorder("Default Working Directory");
263        tborder.setTitleColor(Color.darkGray);
264        p0.setBorder(tborder);
265        c.gridx = 0;
266        c.gridy = 0;
267        centerP.add(p0, c);
268
269        p0 = new JPanel();
270        p0.setLayout(new BorderLayout());
271        p0.add(new JLabel("User's Guide:  "), BorderLayout.WEST);
272        p0.add(UGField = new JTextField(ViewProperties.getUsersGuide()), BorderLayout.CENTER);
273        b = new JButton("Browse...");
274        b.setActionCommand("Browse UG");
275        b.addActionListener(this);
276        p0.add(b, BorderLayout.EAST);
277        tborder = new TitledBorder("Help Document");
278        tborder.setTitleColor(Color.darkGray);
279        p0.setBorder(tborder);
280        c.gridx = 0;
281        c.gridy = 1;
282        centerP.add(p0, c);
283
284        p0 = new JPanel();
285        p0.setLayout(new GridLayout(1, 3, 8, 8));
286
287        JPanel p00 = new JPanel();
288        p00.setLayout(new BorderLayout());
289        p00.add(new JLabel("Extension: "), BorderLayout.WEST);
290        p00.add(fileExtField = new JTextField(ViewProperties.getFileExtension()), BorderLayout.CENTER);
291        tborder = new TitledBorder("File Extension");
292        tborder.setTitleColor(Color.darkGray);
293        p00.setBorder(tborder);
294
295        JPanel p01 = new JPanel();
296        p01.setLayout(new GridLayout(1, 2, 8, 8));
297        p01.add(checkReadOnly = new JRadioButton("Read Only", ViewProperties.isReadOnly()));
298        JRadioButton rw = new JRadioButton("Read/Write", !ViewProperties.isReadOnly());
299        p01.add(rw);
300        ButtonGroup bgrp = new ButtonGroup();
301        bgrp.add(checkReadOnly);
302        bgrp.add(rw);
303        tborder = new TitledBorder("Default File Access Mode");
304        tborder.setTitleColor(Color.darkGray);
305        p01.setBorder(tborder);
306
307        JPanel p02 = new JPanel();
308        p02.setLayout(new GridLayout(1, 2, 8, 8));
309        p02.add(checkLibVersion = new JRadioButton("Earliest", ViewProperties.isEarlyLib()));
310        JRadioButton latestLib = new JRadioButton("Latest", !ViewProperties.isEarlyLib());
311        p02.add(latestLib);
312        bgrp = new ButtonGroup();
313        bgrp.add(checkLibVersion);
314        bgrp.add(latestLib);
315        tborder = new TitledBorder("Default Lib Version");
316        tborder.setTitleColor(Color.darkGray);
317        p02.setBorder(tborder);
318
319        p0.add(p01);
320        p0.add(p00);
321        p0.add(p02);
322        c.gridx = 0;
323        c.gridy = 2;
324        centerP.add(p0, c);
325
326        p0 = new JPanel();
327        p0.setLayout(new GridLayout(1, 2, 8, 8));
328        p00 = new JPanel();
329        p00.setLayout(new BorderLayout());
330        p00.add(new JLabel("Font Size:"), BorderLayout.WEST);
331        p00.add(fontSizeChoice, BorderLayout.CENTER);
332        p0.add(p00);
333        p00 = new JPanel();
334        p00.setLayout(new BorderLayout());
335        p00.add(new JLabel("Font Type:"), BorderLayout.WEST);
336        p00.add(fontTypeChoice, BorderLayout.CENTER);
337        p0.add(p00);
338        tborder = new TitledBorder("Text Font");
339        tborder.setTitleColor(Color.darkGray);
340        p0.setBorder(tborder);
341        c.gridx = 0;
342        c.gridy = 3;
343        centerP.add(p0, c);
344
345        p0 = new JPanel();
346        p0.setLayout(new GridLayout(1, 4, 8, 8));
347
348        p00 = new JPanel();
349        p00.setLayout(new BorderLayout());
350        checkAutoContrast = new JCheckBox("Autogain Image Contrast");
351        checkAutoContrast.setSelected(ViewProperties.isAutoContrast());
352        checkAutoContrast.setName("autogain");
353        p00.add(checkAutoContrast, BorderLayout.CENTER);
354        JButton button = new JButton(ViewProperties.getHelpIcon());
355        button.setToolTipText("Help on Auto Contrast");
356        button.setMargin(new Insets(0, 0, 0, 0));
357        button.addActionListener(this);
358        button.setActionCommand("Help on Auto Contrast");
359        p00.add(button, BorderLayout.WEST);
360        p0.add(p00);
361
362        p0.add(checkShowValues = new JCheckBox("Show Values"));
363        checkShowValues.setSelected(ViewProperties.showImageValues());
364
365        p00 = new JPanel();
366        p00.setLayout(new BorderLayout());
367        p00.add(new JLabel("Image Origin:"), BorderLayout.WEST);
368        p00.add(imageOriginChoice, BorderLayout.CENTER);
369        p0.add(p00);
370
371        tborder = new TitledBorder("Image");
372        tborder.setTitleColor(Color.darkGray);
373        p0.setBorder(tborder);
374        c.gridx = 0;
375        c.gridy = 4;
376        centerP.add(p0, c);
377
378        p0 = new JPanel();
379        p0.setLayout(new GridLayout(2, 3, 20, 4));
380
381        p00 = new JPanel();
382        p00.setLayout(new BorderLayout());
383        button = new JButton(ViewProperties.getHelpIcon());
384        button.setToolTipText("Help on Convert Enum");
385        button.setMargin(new Insets(0, 0, 0, 0));
386        button.addActionListener(this);
387        button.setActionCommand("Help on Convert Enum");
388        p00.add(button, BorderLayout.WEST);
389        checkConvertEnum = new JCheckBox("Convert Enum");
390        checkConvertEnum.setSelected(ViewProperties.isConvertEnum());
391        p00.add(checkConvertEnum, BorderLayout.CENTER);
392        p0.add(p00, BorderLayout.NORTH);
393
394        checkShowRegRefValues = new JCheckBox("Show RegRef Values");
395        checkShowRegRefValues.setSelected(ViewProperties.showRegRefValues());
396        p0.add(checkShowRegRefValues, BorderLayout.NORTH);
397
398        p00 = new JPanel();
399        p00.setLayout(new BorderLayout());
400
401        String[] indexBaseChoices = { "0-based", "1-based" };
402        indexBaseChoice = new JComboBox(indexBaseChoices);
403        if (ViewProperties.isIndexBase1())
404            indexBaseChoice.setSelectedIndex(1);
405        else
406            indexBaseChoice.setSelectedIndex(0);
407
408        p00.add(new JLabel("Index Base: "), BorderLayout.WEST);
409        p00.add(indexBaseChoice, BorderLayout.CENTER);
410        p0.add(p00, BorderLayout.SOUTH);
411
412        p00 = new JPanel();
413        p00.setLayout(new BorderLayout());
414        p00.add(new JLabel("Data Delimiter:"), BorderLayout.WEST);
415        p00.add(delimiterChoice, BorderLayout.CENTER);
416        p0.add(p00, BorderLayout.SOUTH);
417
418        tborder = new TitledBorder("Data");
419        tborder.setTitleColor(Color.darkGray);
420        p0.setBorder(tborder);
421        c.gridx = 0;
422        c.gridy = 5;
423        centerP.add(p0, c);
424
425        p0 = new JPanel();
426        p0.setLayout(new GridLayout(1, 3, 8, 8));
427
428        int nMax = ViewProperties.getMaxMembers();
429        checkReadAll = new JRadioButton("Open All", (nMax<=0) || (nMax==Integer.MAX_VALUE));
430        checkReadAll.addItemListener(this);
431        p0.add(checkReadAll);
432
433        p00 = new JPanel();
434        p00.setLayout(new BorderLayout());
435        p00.add(new JLabel("Start Member: "), BorderLayout.WEST);
436        p00.add(startMemberField = new JTextField(String.valueOf(ViewProperties.getStartMembers())),
437                BorderLayout.CENTER);
438        p0.add(p00);
439
440        p00 = new JPanel();
441        p00.setLayout(new BorderLayout());
442        p00.add(new JLabel("Member Count: "), BorderLayout.WEST);
443        p00.add(maxMemberField = new JTextField(String.valueOf(ViewProperties.getMaxMembers())), BorderLayout.CENTER);
444        p0.add(p00);
445
446          startMemberField.setEnabled(!checkReadAll.isSelected());
447           maxMemberField.setEnabled(!checkReadAll.isSelected());
448
449        tborder = new TitledBorder("Objects to Open");
450        tborder.setTitleColor(Color.darkGray);
451        p0.setBorder(tborder);
452        c.gridx = 0;
453        c.gridy = 6;
454        centerP.add(p0, c);
455
456        p0 = new JPanel();
457        p0.setLayout(new GridLayout(1, 2, 8, 8));
458
459        JPanel pType = new JPanel();
460        pType.setLayout(new GridLayout(1, 2, 8, 8));
461        checkIndexType = new JRadioButton("By Name", indexType.compareTo("H5_INDEX_NAME") == 0);
462        pType.add(checkIndexType);
463        JRadioButton checkIndexCreateOrder = new JRadioButton("By Creation Order",
464                indexType.compareTo("H5_INDEX_CRT_ORDER") == 0);
465        pType.add(checkIndexCreateOrder);
466        ButtonGroup bTypegrp = new ButtonGroup();
467        bTypegrp.add(checkIndexType);
468        bTypegrp.add(checkIndexCreateOrder);
469        tborder = new TitledBorder("Indexing Type");
470        tborder.setTitleColor(Color.darkGray);
471        pType.setBorder(tborder);
472        p0.add(pType);
473
474        JPanel pOrder = new JPanel();
475        pOrder.setLayout(new GridLayout(1, 3, 8, 8));
476        checkIndexOrder = new JRadioButton("Increments", indexOrder.compareTo("H5_ITER_INC") == 0);
477        pOrder.add(checkIndexOrder);
478        JRadioButton checkIndexDecrement = new JRadioButton("Decrements", indexOrder.compareTo("H5_ITER_DEC") == 0);
479        pOrder.add(checkIndexDecrement);
480        checkIndexNative = new JRadioButton("Native", indexOrder.compareTo("H5_ITER_NATIVE") == 0);
481        pOrder.add(checkIndexNative);
482        ButtonGroup bOrdergrp = new ButtonGroup();
483        bOrdergrp.add(checkIndexOrder);
484        bOrdergrp.add(checkIndexDecrement);
485        bOrdergrp.add(checkIndexNative);
486        tborder = new TitledBorder("Indexing Order");
487        tborder.setTitleColor(Color.darkGray);
488        pOrder.setBorder(tborder);
489        p0.add(pOrder);
490
491        tborder = new TitledBorder("Display Indexing Options");
492        tborder.setTitleColor(Color.darkGray);
493        p0.setBorder(tborder);
494        c.gridx = 0;
495        c.gridy = 7;
496        centerP.add(p0, c);
497
498        if (workDir.equals(System.getProperty("user.home"))) {
499            checkCurrentUserDir.setSelected(true);
500            workField.setEnabled(false);
501        }
502
503        return centerP;
504    }
505
506    @SuppressWarnings({ "unchecked", "rawtypes" })
507    private JPanel createModuleOptionPanel() {
508        choiceTreeView = new JComboBox(treeViews);
509        choiceTableView = new JComboBox(tableViews);
510        choiceTextView = new JComboBox(textViews);
511        choiceImageView = new JComboBox(imageViews);
512        choiceMetaDataView = new JComboBox(metaDataViews);
513        choicePaletteView = new JComboBox(paletteViews);
514
515        JPanel moduleP = new JPanel();
516        moduleP.setLayout(new GridLayout(6, 1, 10, 10));
517        moduleP.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
518
519        JPanel treeP = new JPanel();
520        TitledBorder tborder = new TitledBorder("TreeView");
521        tborder.setTitleColor(Color.darkGray);
522        treeP.setBorder(tborder);
523        moduleP.add(treeP);
524        treeP.setLayout(new BorderLayout(5, 5));
525        treeP.add(choiceTreeView, BorderLayout.CENTER);
526
527        JPanel attrP = new JPanel();
528        tborder = new TitledBorder("MetaDataView");
529        tborder.setTitleColor(Color.darkGray);
530        attrP.setBorder(tborder);
531        moduleP.add(attrP);
532        attrP.setLayout(new BorderLayout(5, 5));
533        attrP.add(choiceMetaDataView, BorderLayout.CENTER);
534
535        JPanel textP = new JPanel();
536        tborder = new TitledBorder("TextView");
537        tborder.setTitleColor(Color.darkGray);
538        textP.setBorder(tborder);
539        moduleP.add(textP);
540        textP.setLayout(new BorderLayout(5, 5));
541        textP.add(choiceTextView, BorderLayout.CENTER);
542
543        JPanel tableP = new JPanel();
544        tborder = new TitledBorder("TableView");
545        tborder.setTitleColor(Color.darkGray);
546        tableP.setBorder(tborder);
547        moduleP.add(tableP);
548        tableP.setLayout(new BorderLayout(5, 5));
549        tableP.add(choiceTableView, BorderLayout.CENTER);
550
551        JPanel imageP = new JPanel();
552        tborder = new TitledBorder("ImageView");
553        tborder.setTitleColor(Color.darkGray);
554        imageP.setBorder(tborder);
555        moduleP.add(imageP);
556        imageP.setLayout(new BorderLayout(5, 5));
557        imageP.add(choiceImageView, BorderLayout.CENTER);
558
559        JPanel palP = new JPanel();
560        tborder = new TitledBorder("PaletteView");
561        tborder.setTitleColor(Color.darkGray);
562        palP.setBorder(tborder);
563        moduleP.add(palP);
564        palP.setLayout(new BorderLayout(5, 5));
565        palP.add(choicePaletteView, BorderLayout.CENTER);
566
567        return moduleP;
568    }
569
570    /*
571     * private JPanel createSrbConnectionPanel() { JPanel p = new JPanel();
572     * p.setLayout(new BorderLayout(5,5)); TitledBorder tborder = new
573     * TitledBorder("SRB Connections"); tborder.setTitleColor(Color.darkGray);
574     * p.setBorder(tborder);
575     *
576     * DefaultListModel listModel = new DefaultListModel(); srbJList = new
577     * JList(listModel);
578     * srbJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
579     * srbJList.addListSelectionListener(this);
580     *
581     * srbFields = new JTextField[7];
582     *
583     * if (srbVector!= null) { int n=srbVector.size();
584     *
585     * String srbaccount[] = null; for (int i=0; i<n; i++) { srbaccount =
586     * (String[])srbVector.get(i); if (srbaccount != null) {
587     * listModel.addElement(srbaccount[0]); } } }
588     *
589     * JPanel cp = new JPanel(); cp.setLayout(new BorderLayout(5,5));
590     *
591     * JPanel cpc = new JPanel(); cpc.setLayout(new GridLayout(7,1,5,5));
592     * cpc.add(srbFields[0] = new JTextField()); cpc.add(srbFields[1] = new
593     * JTextField()); cpc.add(srbFields[2] = new JTextField());
594     * cpc.add(srbFields[3] = new JTextField()); cpc.add(srbFields[4] = new
595     * JTextField()); cpc.add(srbFields[5] = new JTextField());
596     * cpc.add(srbFields[6] = new JTextField()); cp.add(cpc,
597     * BorderLayout.CENTER);
598     *
599     * JPanel cpl = new JPanel(); cpl.setLayout(new GridLayout(7,1,5,5));
600     * cpl.add(new JLabel("Host Machine: ", SwingConstants.RIGHT)); cpl.add(new
601     * JLabel("Port Number: ", SwingConstants.RIGHT)); cpl.add(new
602     * JLabel("User Name: ", SwingConstants.RIGHT)); cpl.add(new
603     * JLabel("Password: ", SwingConstants.RIGHT)); cpl.add(new
604     * JLabel("Home Directory: ", SwingConstants.RIGHT)); cpl.add(new
605     * JLabel("Domain Name/Zone: ", SwingConstants.RIGHT)); cpl.add(new
606     * JLabel(" Default Storage Resource: ", SwingConstants.RIGHT)); cp.add(cpl,
607     * BorderLayout.WEST);
608     *
609     * JPanel lp = new JPanel(); lp.setLayout(new BorderLayout(5,5)); JPanel lpb
610     * = new JPanel(); JButton add = new JButton("Save");
611     * add.addActionListener(this); add.setActionCommand("Add srb connsction");
612     * lpb.add(add); JButton del = new JButton("Delete");
613     * del.addActionListener(this);
614     * del.setActionCommand("Delete srb connsction"); lpb.add(del); lp.add(lpb,
615     * BorderLayout.SOUTH); JScrollPane listScroller = new
616     * JScrollPane(srbJList); int w = 120 +
617     * (ViewProperties.getFontSize()-12)*10; int h = 200 +
618     * (ViewProperties.getFontSize()-12)*15; listScroller.setPreferredSize(new
619     * Dimension(w, h)); lp.add(listScroller, BorderLayout.CENTER);
620     *
621     * JPanel sp = new JPanel(); sp.setLayout(new GridLayout(3,1,5,15));
622     * sp.add(new JLabel(" "));
623     *
624     * p.add(cp, BorderLayout.CENTER); p.add(lp, BorderLayout.WEST); p.add(sp,
625     * BorderLayout.SOUTH);
626     *
627     * if ((srbVector !=null) && (srbVector.size()>0)) {
628     * srbJList.setSelectedIndex(0); }
629     *
630     * return p; }
631     */
632
633    @SuppressWarnings("unchecked")
634    public void actionPerformed(ActionEvent e) {
635        Object source = e.getSource();
636        String cmd = e.getActionCommand();
637
638        if (cmd.equals("Set options")) {
639            setUserOptions();
640            setVisible(false);
641        }
642        else if (cmd.equals("Cancel")) {
643            isFontChanged = false;
644            setVisible(false);
645        }
646        else if (cmd.equals("Set current dir to user.home")) {
647            boolean isCheckCurrentUserDirSelected = checkCurrentUserDir.isSelected();
648            workField.setEnabled(!isCheckCurrentUserDirSelected);
649            currentDirButton.setEnabled(!isCheckCurrentUserDirSelected);
650        }
651        else if (cmd.equals("Browse UG")) {
652            final JFileChooser fchooser = new JFileChooser(rootDir);
653            int returnVal = fchooser.showOpenDialog(this);
654
655            if (returnVal != JFileChooser.APPROVE_OPTION) {
656                return;
657            }
658
659            File choosedFile = fchooser.getSelectedFile();
660            if (choosedFile == null) {
661                return;
662            }
663
664            String fname = choosedFile.getAbsolutePath();
665            if (fname == null) {
666                return;
667            }
668            UGField.setText(fname);
669        }
670        else if (cmd.equals("Browse current dir")) {
671            final JFileChooser fchooser = new JFileChooser(workDir);
672            fchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
673            int returnVal = fchooser.showDialog(this, "Select");
674
675            if (returnVal != JFileChooser.APPROVE_OPTION) {
676                return;
677            }
678
679            File choosedFile = fchooser.getSelectedFile();
680            if (choosedFile == null) {
681                return;
682            }
683
684            String fname = choosedFile.getAbsolutePath();
685            if (fname == null) {
686                return;
687            }
688            workField.setText(fname);
689        }
690        else if (cmd.equals("Browse h4toh5")) {
691            final JFileChooser fchooser = new JFileChooser(rootDir);
692            int returnVal = fchooser.showOpenDialog(this);
693
694            if (returnVal != JFileChooser.APPROVE_OPTION) {
695                return;
696            }
697
698            File choosedFile = fchooser.getSelectedFile();
699            if (choosedFile == null) {
700                return;
701            }
702
703            String fname = choosedFile.getAbsolutePath();
704            if (fname == null) {
705                return;
706            }
707            H4toH5Path = fname;
708            H4toH5Field.setText(fname);
709        }
710        else if (cmd.startsWith("Add Module")) {
711            String newModule = JOptionPane.showInputDialog(this, "Type the full path of the new module:", cmd,
712                    JOptionPane.PLAIN_MESSAGE);
713
714            if ((newModule == null) || (newModule.length() < 1)) {
715                return;
716            }
717
718            // enables use of JHDF5 in JNLP (Web Start) applications, the system
719            // class loader with reflection first.
720            try {
721                Class.forName(newModule);
722            }
723            catch (Exception ex) {
724                try {
725                    ViewProperties.loadExtClass().loadClass(newModule);
726                }
727                catch (ClassNotFoundException ex2) {
728                    JOptionPane.showMessageDialog(this, "Cannot find module:\n " + newModule
729                            + "\nPlease check the module name and classpath.", "HDFView", JOptionPane.ERROR_MESSAGE);
730                    return;
731                }
732            }
733
734            if (cmd.endsWith("TreeView") && !treeViews.contains(newModule)) {
735                treeViews.add(newModule);
736                choiceTreeView.addItem(newModule);
737            }
738            else if (cmd.endsWith("MetadataView") && !metaDataViews.contains(newModule)) {
739                metaDataViews.add(newModule);
740                choiceMetaDataView.addItem(newModule);
741            }
742            else if (cmd.endsWith("TextView") && !textViews.contains(newModule)) {
743                textViews.add(newModule);
744                choiceTextView.addItem(newModule);
745            }
746            else if (cmd.endsWith("TableView") && !tableViews.contains(newModule)) {
747                tableViews.add(newModule);
748                choiceTableView.addItem(newModule);
749            }
750            else if (cmd.endsWith("ImageView") && !imageViews.contains(newModule)) {
751                imageViews.add(newModule);
752                choiceImageView.addItem(newModule);
753            }
754            else if (cmd.endsWith("PaletteView") && !paletteViews.contains(newModule)) {
755                paletteViews.add(newModule);
756                choicePaletteView.addItem(newModule);
757            }
758        }
759        else if (cmd.startsWith("Delete Module")) {
760            @SuppressWarnings("rawtypes")
761            JComboBox theChoice = (JComboBox) source;
762
763            if (theChoice.getItemCount() == 1) {
764                JOptionPane.showMessageDialog(this, "Cannot delete the last module.", cmd, JOptionPane.ERROR_MESSAGE);
765                return;
766            }
767
768            int reply = JOptionPane.showConfirmDialog(this, "Do you want to delete the selected module?", cmd,
769                    JOptionPane.YES_NO_OPTION);
770            if (reply == JOptionPane.NO_OPTION) {
771                return;
772            }
773
774            String moduleName = (String) theChoice.getSelectedItem();
775            theChoice.removeItem(moduleName);
776            if (cmd.endsWith("TreeView")) {
777                treeViews.remove(moduleName);
778            }
779            else if (cmd.endsWith("MetadataView")) {
780                metaDataViews.remove(moduleName);
781            }
782            else if (cmd.endsWith("TextView")) {
783                textViews.remove(moduleName);
784            }
785            else if (cmd.endsWith("TableView")) {
786                tableViews.remove(moduleName);
787            }
788            else if (cmd.endsWith("ImageView")) {
789                imageViews.remove(moduleName);
790            }
791            else if (cmd.endsWith("PaletteView")) {
792                paletteViews.remove(moduleName);
793            }
794        }
795        /*
796         * else if (cmd.equals("Add srb connsction")) { String srbaccount[] =
797         * new String[7]; for (int i=0; i<7; i++) { srbaccount[i] =
798         * srbFields[i].getText(); if (srbaccount[i] == null) { return; } }
799         * DefaultListModel lm = (DefaultListModel)srbJList.getModel();
800         *
801         * if (lm.contains(srbaccount[0])) { int n =
802         * srbJList.getSelectedIndex(); if ( n<0 ) return; String
803         * srbaccountOld[] = (String[])srbVector.get(n); for (int i=0; i<7; i++)
804         * srbaccountOld[i] = srbaccount[i]; } else { srbVector.add(srbaccount);
805         * lm.addElement(srbaccount[0]);
806         * srbJList.setSelectedValue(srbaccount[0], true); } } else if
807         * (cmd.equals("Delete srb connsction")) { int n =
808         * srbJList.getSelectedIndex(); if (n<0) { return; }
809         *
810         * int resp = JOptionPane.showConfirmDialog(this,
811         * "Are you sure you want to delete the following SRB connection?\n"+
812         * "            \""+srbJList.getSelectedValue()+"\"",
813         * "Delete SRB Connection", JOptionPane.YES_NO_OPTION); if (resp ==
814         * JOptionPane.NO_OPTION) { return; }
815         *
816         * DefaultListModel lm = (DefaultListModel)srbJList.getModel();
817         * lm.removeElementAt(n); srbVector.remove(n); for (int i=0; i<7; i++) {
818         * srbFields[i].setText(""); } }
819         */
820        else if (cmd.equals("Help on Auto Contrast")) {
821            final String msg = "Auto Contrast does the following to compute a gain/bias \n"
822                    + "that will stretch the pixels in the image to fit the pixel \n"
823                    + "values of the graphics system. For example, it stretches unsigned\n"
824                    + "short data to fit the full range of an unsigned short. Later \n"
825                    + "code simply takes the high order byte and passes it to the graphics\n"
826                    + "system (which expects 0-255). It uses some statistics on the pixels \n"
827                    + "to prevent outliers from throwing off the gain/bias calculations much.\n\n"
828                    + "To compute the gain/bias we... \n"
829                    + "Find the mean and std. deviation of the pixels in the image \n" + "min = mean - 3 * std.dev. \n"
830                    + "max = mean + 3 * std.dev. \n" + "small fudge factor because this tends to overshoot a bit \n"
831                    + "Stretch to 0-USHRT_MAX \n" + "        gain = USHRT_MAX / (max-min) \n"
832                    + "        bias = -min \n" + "\n" + "To apply the gain/bias to a pixel, use the formula \n"
833                    + "data[i] = (data[i] + bias) * gain \n" + "\n"
834                    // +
835                    // "Finally, for auto-ranging the sliders for gain/bias, we do the following \n"
836                    // + "gain_min = 0 \n"
837                    // + "gain_max = gain * 3.0 \n"
838                    // + "bias_min = -fabs(bias) * 3.0 \n"
839                    // + "bias_max = fabs(bias) * 3.0 \n"
840                    + "\n\n";
841            JOptionPane.showMessageDialog(this, msg);
842        }
843        else if (cmd.equals("Help on Convert Enum")) {
844            final String msg = "Convert enum data to strings. \n"
845                    + "For example, a dataset of an enum type of (R=0, G=, B=2) \n"
846                    + "has values of (0, 2, 2, 2, 1, 1). With conversion, the data values are \n"
847                    + "shown as (R, B, B, B, G, G).\n\n\n";
848            JOptionPane.showMessageDialog(this, msg);
849        }
850    }
851
852    /*
853     * public void valueChanged(ListSelectionEvent e) { Object src =
854     * e.getSource();
855     *
856     * if (!src.equals(srbJList)) { return; }
857     *
858     * int n = srbJList.getSelectedIndex(); if ( n<0 ) { return; }
859     *
860     * String srbaccount[] = (String[])srbVector.get(n); if (srbaccount == null)
861     * { return; }
862     *
863     * n = Math.min(7, srbaccount.length); for (int i=0; i<n; i++) {
864     * srbFields[i].setText(srbaccount[i]); } }
865     */
866
867    @SuppressWarnings("unchecked")
868    private void setUserOptions() {
869        String UGPath = UGField.getText();
870        if ((UGPath != null) && (UGPath.length() > 0)) {
871            UGPath = UGPath.trim();
872            isUserGuideChanged = !UGPath.equals(ViewProperties.getUsersGuide());
873            ViewProperties.setUsersGuide(UGPath);
874        }
875
876        String workPath = workField.getText();
877        if (checkCurrentUserDir.isSelected()) {
878            workPath = "user.home";
879        }
880
881        log.trace("UserOptionsDialog:setUserOptions workPath={}", workPath);
882        if ((workPath != null) && (workPath.length() > 0)) {
883            workPath = workPath.trim();
884            isWorkDirChanged = !workPath.equals(ViewProperties.getWorkDir());
885            ViewProperties.setWorkDir(workPath);
886        }
887
888        String ext = fileExtField.getText();
889        if ((ext != null) && (ext.length() > 0)) {
890            ext = ext.trim();
891            ViewProperties.setFileExtension(ext);
892        }
893
894        if (checkReadOnly.isSelected())
895            ViewProperties.setReadOnly(true);
896        else
897            ViewProperties.setReadOnly(false);
898
899        if (checkLibVersion.isSelected())
900            ViewProperties.setEarlyLib(true);
901        else
902            ViewProperties.setEarlyLib(false);
903
904        // set font size
905        int fsize = 12;
906        try {
907            fsize = Integer.parseInt((String) fontSizeChoice.getSelectedItem());
908            ViewProperties.setFontSize(fsize);
909
910            if ((fontSize != ViewProperties.getFontSize())) {
911                isFontChanged = true;
912            }
913        }
914        catch (Exception ex) {
915        }
916
917        // set font type
918        String ftype = (String) fontTypeChoice.getSelectedItem();
919        if (!ftype.equalsIgnoreCase(ViewProperties.getFontType())) {
920            isFontChanged = true;
921            ViewProperties.setFontType(ftype);
922        }
923
924        // set data delimiter
925        ViewProperties.setDataDelimiter((String) delimiterChoice.getSelectedItem());
926        ViewProperties.setImageOrigin((String) imageOriginChoice.getSelectedItem());
927
928        // set index type
929        if (checkIndexType.isSelected())
930            ViewProperties.setIndexType("H5_INDEX_NAME");
931        else
932            ViewProperties.setIndexType("H5_INDEX_CRT_ORDER");
933
934        // set index order
935        if (checkIndexOrder.isSelected())
936            ViewProperties.setIndexOrder("H5_ITER_INC");
937        else if (checkIndexNative.isSelected())
938            ViewProperties.setIndexOrder("H5_ITER_NATIVE");
939        else
940            ViewProperties.setIndexOrder("H5_ITER_DEC");
941
942        if (checkReadAll.isSelected()) {
943            ViewProperties.setStartMembers(0);
944            ViewProperties.setMaxMembers(-1);
945        } else {
946            try {
947                int maxsize = Integer.parseInt(maxMemberField.getText());
948                ViewProperties.setMaxMembers(maxsize);
949            }
950            catch (Exception ex) {
951            }
952
953            try {
954                int startsize = Integer.parseInt(startMemberField.getText());
955                ViewProperties.setStartMembers(startsize);
956            }
957            catch (Exception ex) {
958            }
959        }
960
961        @SuppressWarnings("rawtypes")
962        Vector[] moduleList = { treeViews, metaDataViews, textViews, tableViews, imageViews, paletteViews };
963        @SuppressWarnings("rawtypes")
964        JComboBox[] choiceList = { choiceTreeView, choiceMetaDataView, choiceTextView, choiceTableView,
965                choiceImageView, choicePaletteView };
966        for (int i = 0; i < 6; i++) {
967            Object theModule = choiceList[i].getSelectedItem();
968            moduleList[i].remove(theModule);
969            moduleList[i].add(0, theModule);
970        }
971
972        ViewProperties.setAutoContrast(checkAutoContrast.isSelected());
973        ViewProperties.setShowImageValue(checkShowValues.isSelected());
974        ViewProperties.setConvertEnum(checkConvertEnum.isSelected());
975        ViewProperties.setShowRegRefValue(checkShowRegRefValues.isSelected());
976
977        if (indexBaseChoice.getSelectedIndex() == 0)
978            ViewProperties.setIndexBase1(false);
979        else
980            ViewProperties.setIndexBase1(true);
981    }
982
983    public boolean isFontChanged() {
984        return isFontChanged;
985    }
986
987    public boolean isUserGuideChanged() {
988        return isUserGuideChanged;
989    }
990
991    public boolean isWorkDirChanged() {
992        return isWorkDirChanged;
993    }
994
995    @Override
996    public void itemStateChanged(ItemEvent e) {
997        Object source = e.getSource();
998
999        if (source.equals(checkReadAll)) {
1000            startMemberField.setEnabled(!checkReadAll.isSelected());
1001            maxMemberField.setEnabled(!checkReadAll.isSelected());
1002
1003        }
1004    }
1005}