Search in sources :

Example 1 with HpcAccountEditor

use of edu.cmu.tetradapp.app.hpc.editor.HpcAccountEditor in project tetrad by cmu-phil.

the class HpcAccountSettingAction method buildHpcAccountSettingComponent.

private static JComponent buildHpcAccountSettingComponent(final HpcAccountManager manager) {
    // Get ComputingAccount from DB
    final DefaultListModel<HpcAccount> listModel = new DefaultListModel<HpcAccount>();
    for (HpcAccount account : manager.getHpcAccounts()) {
        listModel.addElement(account);
    }
    // JSplitPane
    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    // Left pane -> JList (parent pane)
    JPanel leftPanel = new JPanel(new BorderLayout());
    // Right pane -> ComputingAccountEditor
    final JPanel accountDetailPanel = new JPanel(new BorderLayout());
    accountDetailPanel.add(new HpcAccountEditor(splitPane, listModel, manager, new HpcAccount()), BorderLayout.CENTER);
    splitPane.setLeftComponent(leftPanel);
    splitPane.setRightComponent(accountDetailPanel);
    // Center Panel
    final JList<HpcAccount> accountList = new JList<>(listModel);
    accountList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    accountList.setLayoutOrientation(JList.VERTICAL);
    accountList.setSelectedIndex(-1);
    accountList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            int selectedIndex = ((JList<?>) e.getSource()).getSelectedIndex();
            // Show or remove the detail
            accountDetailPanel.removeAll();
            if (selectedIndex > -1) {
                HpcAccount computingAccount = listModel.get(selectedIndex);
                System.out.println(computingAccount);
                accountDetailPanel.add(new HpcAccountEditor(splitPane, listModel, manager, computingAccount), BorderLayout.CENTER);
            }
            accountDetailPanel.updateUI();
        }
    });
    // Left Panel
    JPanel buttonPanel = new JPanel(new BorderLayout());
    JButton addButton = new JButton("Add");
    addButton.setSize(new Dimension(14, 8));
    addButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // Show the empty ComputingAccountEditor
            accountDetailPanel.removeAll();
            accountDetailPanel.add(new HpcAccountEditor(splitPane, listModel, manager, new HpcAccount()), BorderLayout.CENTER);
            accountDetailPanel.updateUI();
        }
    });
    buttonPanel.add(addButton, BorderLayout.WEST);
    JButton removeButton = new JButton("Remove");
    removeButton.setSize(new Dimension(14, 8));
    removeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (accountList.isSelectionEmpty())
                return;
            int selectedIndex = accountList.getSelectedIndex();
            if (selectedIndex > -1) {
                HpcAccount computingAccount = listModel.get(selectedIndex);
                // Pop up the confirm dialog
                int option = JOptionPane.showConfirmDialog(accountDetailPanel, "Are you sure that you want to delete " + computingAccount + " ?", "HPC Account Setting", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                // If yes, remove it from DB and listModel
                if (option == JOptionPane.YES_OPTION) {
                    manager.removeAccount(computingAccount);
                    listModel.remove(selectedIndex);
                }
            }
        }
    });
    buttonPanel.add(removeButton, BorderLayout.EAST);
    leftPanel.add(buttonPanel, BorderLayout.NORTH);
    JScrollPane accountListScroller = new JScrollPane(accountList);
    leftPanel.add(accountListScroller, BorderLayout.CENTER);
    int minWidth = 300;
    int minHeight = 200;
    int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
    int frameWidth = screenWidth / 2;
    int frameHeight = screenHeight / 2;
    frameWidth = minWidth > frameWidth ? minWidth : frameWidth;
    frameHeight = minHeight > frameHeight ? minHeight : frameHeight;
    splitPane.setDividerLocation(frameWidth / 4);
    accountListScroller.setPreferredSize(new Dimension(frameWidth / 4, frameHeight));
    accountDetailPanel.setPreferredSize(new Dimension(frameWidth * 3 / 4, frameHeight));
    return splitPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JButton(javax.swing.JButton) DefaultListModel(javax.swing.DefaultListModel) HpcAccountEditor(edu.cmu.tetradapp.app.hpc.editor.HpcAccountEditor) HpcAccount(edu.pitt.dbmi.tetrad.db.entity.HpcAccount) Dimension(java.awt.Dimension) ListSelectionListener(javax.swing.event.ListSelectionListener) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) JSplitPane(javax.swing.JSplitPane) JList(javax.swing.JList)

Aggregations

HpcAccountEditor (edu.cmu.tetradapp.app.hpc.editor.HpcAccountEditor)1 HpcAccount (edu.pitt.dbmi.tetrad.db.entity.HpcAccount)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 DefaultListModel (javax.swing.DefaultListModel)1 JButton (javax.swing.JButton)1 JList (javax.swing.JList)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 JSplitPane (javax.swing.JSplitPane)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1