You are on page 1of 3

package Backend;

import runningdata.NameAndKey;
import
import
import
import
import
import
import
import

javax.swing.*;
javax.swing.event.ListSelectionEvent;
javax.swing.event.ListSelectionListener;
javax.swing.table.DefaultTableModel;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.util.ArrayList;
java.util.LinkedList;

/**
* Handles all {@link java.awt.event.ActionEvent} and {@link javax.swing.event.L
istSelectionEvent}
* that occur in the user interface.
*
*
*/
public class Controller implements ActionListener, ListSelectionListener {
private Model model;
private ArrayList<Racer> data;
private JTextField searchBox;
private JList<String> viewRaceList;
private JComboBox<String> comboBox;
private DefaultComboBoxModel<String> comboBoxModel;
private JTable table;
private DefaultTableModel tableModel;
/**
* Initialises all models for all widgets passed through.
* @param searchBox search box to search the server with
* @param viewRaceList
* @param comboBox
* @param table
* @param tableModel model of the table passed, to manage
*/
public Controller(JTextField searchBox, JList<String> viewRaceList, JComboBo
x<String> comboBox,
JTable table, DefaultTableModel tableModel) {
model = new Model();
this.searchBox = searchBox;
this.viewRaceList = viewRaceList;
this.comboBox = comboBox;
this.table = table;
this.tableModel = tableModel;
comboBoxModel = new DefaultComboBoxModel<String>();
}
/**
* Invoked when an action occurs.

*
* @param e
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.searchBox) {
try {
String query = searchBox.getText().toString();
LinkedList<NameAndKey> results = model.searchServer(query);
DefaultListModel<String> defaultList = new DefaultListModel<Stri
ng>();
for (int i = 0; i < results.size(); i++) {
String each = results.get(i).get_address();
defaultList.addElement(each);
}
this.viewRaceList.setModel(defaultList);
} catch (NullPointerException e1) {
JOptionPane.showMessageDialog(null,
"Some search tips: \n" +
"- Do not use symbols and/or numbers. \n" +
"- Make sure you use letters only. \n" +
"- Try using different keywords.", "No results",
JOptionPane.PLAIN_MESSAGE);
}
} else if (e.getSource() == this.comboBox) {
String select = (String) comboBox.getSelectedItem();
model.blankTableModel(tableModel, 10);
table.setModel(tableModel);
ArrayList<Racer> categorised = model.categoriseList(data, select);
model.populateTableModel(tableModel, categorised, 10);
table.setModel(tableModel);
}
}
/**
* Called whenever the value of the selection changes.
*
* @param e the event that characterizes the change.
*/
@Override
public void valueChanged(ListSelectionEvent e) {
int currentOption = viewRaceList.getSelectedIndex();
data = model.getRacerList(currentOption);
if (data.size() == 0 || data.size() < 10) {
model.blankTableModel(tableModel, 10);
} else {
model.populateTableModel(tableModel, data, 10);
}
table.setDefaultRenderer(Object.class, new FormatTable());

table.setModel(tableModel);
comboBoxModel = new DefaultComboBoxModel<String>();
model.updateComboBox(comboBoxModel, data);
comboBox.setModel(comboBoxModel);
}
}

You might also like