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