package org.yamcs.ui.packetviewer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Arrays;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import org.yamcs.ConfigurationException;
import org.yamcs.YConfiguration;

/**
 * Select a filename and a XTCE db config version to be used in the standalone
 * packet viewer
 */
public class OpenFileDialog extends JDialog implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JComboBox dbConfigCombo;
    private JFileChooser fileChooser;
    private Preferences prefs;
    private int returnValue;

    /**
     * Return value if cancel is chosen
     */
    public static final int CANCEL_OPTION = 1;

    /**
     * Return value if approve (yes, ok) is chosen
     */
    public static final int APPROVE_OPTION = 0;

    public OpenFileDialog() throws ConfigurationException {
        YConfiguration c = YConfiguration.getConfiguration("mdb");
        String[] dbconfigs = c.getKeys().toArray(new String[0]);
        Arrays.sort(dbconfigs);
        prefs = Preferences.userNodeForPackage(PacketViewer.class);

        dbConfigCombo = new JComboBox(dbconfigs);
        dbConfigCombo.setSelectedItem(prefs.get("LastUsedDbConfig", null));
        dbConfigCombo.setEditable(true);

        JPanel xtcePanel = new JPanel();
        xtcePanel.setLayout(new BoxLayout(xtcePanel, BoxLayout.Y_AXIS));

        JPanel opts = new JPanel(new BorderLayout());
        opts.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        JLabel xtceDbLbl = new JLabel("XTCE DB: ");
        xtceDbLbl.setDisplayedMnemonic(KeyEvent.VK_D);
        xtceDbLbl.setLabelFor(dbConfigCombo);
        opts.add(xtceDbLbl, BorderLayout.WEST);
        opts.add(dbConfigCombo, BorderLayout.CENTER);

        xtcePanel.add(opts);
        xtcePanel.add(new JSeparator());

        getContentPane().add(xtcePanel, BorderLayout.NORTH);

        String oldDir = prefs.get("LastUsedDirectory", null);
        fileChooser = new JFileChooser(oldDir);
        fileChooser.setDialogTitle("Open File");
        fileChooser.addActionListener(this);

        getContentPane().add(fileChooser, BorderLayout.CENTER);

        setMinimumSize(new Dimension(500, 400));
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle("Open File");
        setModal(true);
        installActions();
    }

    private void installActions() {
        JRootPane root = getRootPane();
        root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "closeDialog");
        root.getActionMap().put("closeDialog", new AbstractAction() {
            private static final long serialVersionUID = 1L;
            @Override
            public void actionPerformed(ActionEvent e) {
                dispatchEvent(new WindowEvent(OpenFileDialog.this, WindowEvent.WINDOW_CLOSING));
            }
        });
    }

    public int showDialog(Component parent) {
        returnValue = CANCEL_OPTION;
        setLocationRelativeTo(parent);
        setVisible(true);
        return returnValue;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals(JFileChooser.APPROVE_SELECTION)) {
            prefs.put("LastUsedDirectory", fileChooser.getSelectedFile().getParent());
            prefs.put("LastUsedDbConfig", "" + dbConfigCombo.getSelectedItem());
            returnValue = APPROVE_OPTION;
            setVisible(false);
        } else if (cmd.equals(JFileChooser.CANCEL_SELECTION)) {
            returnValue = CANCEL_OPTION;
            setVisible(false);
        }
    }

    public File getSelectedFile() {
        return fileChooser.getSelectedFile();
    }

    public String getSelectedDbConfig() {
        return (String) dbConfigCombo.getSelectedItem();
    }
}

@@ -860,19 +860,16 @@
 andalone
-%0A *
  packet 
@@ -875,16 +875,16 @@
  viewer%0A
+
  */%0Apubl
@@ -1025,16 +1025,24 @@
 ComboBox
+%3CString%3E
  dbConfi
@@ -1428,16 +1428,111 @@
 ption %7B%0A
+        String%5B%5D dbconfigs = new String%5B0%5D;%0A        if (YConfiguration.isDefined(%22mdb%22)) %7B%0A    
         
@@ -1594,32 +1594,27 @@
 %22);%0A        
-String%5B%5D
+   
  dbconfigs =
@@ -1650,16 +1650,26 @@
 ng%5B0%5D);%0A
+        %7D%0A
         
@@ -1757,24 +1757,24 @@
 er.class);%0A%0A
-
         dbCo
@@ -1798,16 +1798,18 @@
 ComboBox
+%3C%3E
 (dbconfi
@@ -3208,16 +3208,32 @@
 APE, 0),
+%0A               
  %22closeD
@@ -3368,24 +3368,25 @@
 onUID = 1L;%0A
+%0A
             

Make mdb.yaml optional in PacketViewer