package dasherJava.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import dasherJava.DasherJava;
import dasherJava.core.settings.Settings;
public class SettingsConfigurationChooser extends DefaultLocationJFrame {
public SettingsConfigurationChooser(List<String> configurations) {
super("DasherJava "+DasherJava.VERSION_STRING);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
DasherJava.doExit(false);
}
});
setLayout(new BorderLayout());
JLabel chooseLabel = new JLabel("Choose a configuration (can also be specified as command line argument):");
chooseLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(chooseLabel, BorderLayout.PAGE_START);
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addAll(configurations);
sortListModel(listModel);
JList<String> list = new JList<>(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel scrollPanePanel = new JPanel(new BorderLayout());
scrollPanePanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
scrollPanePanel.add(scrollPane, BorderLayout.CENTER);
add(scrollPanePanel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(null);
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton newButton = new JButton("New");
newButton.addActionListener(e -> {
String newName = (String) JOptionPane.showInputDialog(this, "Enter name of new configuration:",
"Create New Configuration", JOptionPane.QUESTION_MESSAGE, null, null, "New Configuration");
if (newName==null) return; //canceled by user
try {
DasherJava.writeSettingsFile(newName, Settings.DEFAULT_SETTINGS, true);
listModel.addElement(newName);
sortListModel(listModel);
} catch (IOException ex) {
DasherJava.showErrorMessage(this, ex);
}
});
buttonsPanel.add(newButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
JButton duplicateButton = new JButton("Duplicate");
duplicateButton.addActionListener(e -> {
String configurationName = list.getSelectedValue();
if (configurationName==null) return; //nothing selected, cannot duplicate
String newName = (String) JOptionPane.showInputDialog(this, "Enter name of duplicated configuration:",
"Duplicate Configuration", JOptionPane.QUESTION_MESSAGE, null, null, configurationName);
if (newName==null) return; //canceled by user
try {
DasherJava.duplicateSettingsFile(configurationName, newName);
listModel.addElement(newName);
sortListModel(listModel);
} catch (IOException ex) {
DasherJava.showErrorMessage(this, ex);
}
});
buttonsPanel.add(duplicateButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
JButton renameButton = new JButton("Rename");
renameButton.addActionListener(e -> {
String configurationName = list.getSelectedValue();
if (configurationName==null) return; //nothing selected, cannot rename
String newName = (String) JOptionPane.showInputDialog(this, "Enter new name of configuration:",
"Rename Configuration", JOptionPane.QUESTION_MESSAGE, null, null, configurationName);
if (newName==null) return; //canceled by user
try {
DasherJava.renameSettingsFile(configurationName, newName);
listModel.removeElement(configurationName);
listModel.addElement(newName);
sortListModel(listModel);
} catch (IOException ex) {
DasherJava.showErrorMessage(this, ex);
}
});
buttonsPanel.add(renameButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(e -> {
String configurationName = list.getSelectedValue();
if (configurationName==null) return; //nothing selected, cannot delete
if (JOptionPane.showConfirmDialog(this, "Delete configuration \""+configurationName+"\"?",
"Delete Configuration", JOptionPane.OK_CANCEL_OPTION)!=JOptionPane.OK_OPTION)
return; //canceled by user
try {
DasherJava.deleteSettingsFile(configurationName);
listModel.removeElement(configurationName);
} catch (IOException ex) {
DasherJava.showErrorMessage(this, ex);
}
});
buttonsPanel.add(deleteButton);
buttonsPanel.add(Box.createRigidArea(new Dimension(5, 0)));
buttonsPanel.add(Box.createHorizontalGlue());
JButton okButton = new JButton("OK");
okButton.addActionListener(e -> {
String configurationName = list.getSelectedValue();
if (configurationName==null) return;
try {
DasherJava.initWithSettings(configurationName);
setVisible(false);
} catch (IOException ex) {
DasherJava.showErrorMessage(this, ex);
}
});
buttonsPanel.add(okButton);
add(buttonsPanel, BorderLayout.PAGE_END);
setSize(500, 400);
}
private static void sortListModel(DefaultListModel<String> listModel) {
List<String> list = Collections.list(listModel.elements());
list.sort(String.CASE_INSENSITIVE_ORDER);
listModel.clear();
listModel.addAll(list);
}
}