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;
}
Aggregations