/src/main/java/uk/ac/cam/ch/ami/gui/dialogs/EditEquipmentEventDialog.java
https://bitbucket.org/jat45/ami · Java · 129 lines · 91 code · 21 blank · 17 comment · 3 complexity · 6f7c924e0482958f8b2666151e781e88 MD5 · raw file
- package uk.ac.cam.ch.ami.gui.dialogs;
-
- import uk.ac.cam.ch.ami.EquipmentFirstNames;
- import uk.ac.cam.ch.ami.EquipmentSecondNames;
- import uk.ac.cam.ch.ami.EquipmentThirdNames;
- import uk.ac.cam.ch.ami.amievents.events.EquipmentAmiEvent;
- import uk.ac.cam.ch.ami.amievents.verbs.EquipmentVerb;
- import uk.ac.cam.ch.ami.gui.FocusKeyEventDispatcher;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
-
- /**
- * Creates a dialog which we use to both edit an existing EquipmentAmiEvent and register a new EquipmentAmiEvent,
- * depending upon whether the constructor is called with newEvent being true or false. Each piece of Equipment
- * has a name consisting of (up to) three name fragments, which map to the three combo boxes in this dialog.
- */
- public class EditEquipmentEventDialog extends JDialog {
- /* outermost panel in the dialog, just for layout purposes */
- private JPanel contentPane;
- private JButton buttonOK;
- private JButton buttonCancel;
- /* status of the equipment: has it been "registered", or actually "used"? */
- private JComboBox statusComboBox;
-
- /* combo boxes which allow input for the (up to) three name fragments which describe the equipment */
- private JComboBox equipmentFirstComboBox;
- private JComboBox equipmentSecondComboBox;
- private JComboBox equipmentThirdComboBox;
-
- /* the event being edited */
- private EquipmentAmiEvent event;
-
- /* true if this is a new event, false if editing an existing event */
- private boolean newEvent;
-
- /**
- * @param event The event being edited
- * @param newEvent true if the event is new, false if editing an existing event
- */
- public EditEquipmentEventDialog(EquipmentAmiEvent event, boolean newEvent) {
- this.event = event;
- this.newEvent = newEvent;
- setContentPane(contentPane);
-
- buttonOK.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- onOK();
- }
- });
-
- buttonCancel.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- onCancel();
- }
- });
-
- // call onCancel() when cross is clicked
- setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- onCancel();
- }
- });
-
- // call onCancel() on ESCAPE
- contentPane.registerKeyboardAction(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- onCancel();
- }
- }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
- }
-
- private void onOK() {
- String s1 = equipmentFirstComboBox.getSelectedItem().toString();
- String s2 = equipmentSecondComboBox.getSelectedItem().toString();
- String s3 = equipmentThirdComboBox.getSelectedItem().toString();
- event.setEquipmentName(s1, s2, s3);
- event.setVerb((EquipmentVerb)statusComboBox.getSelectedItem());
- KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
- dispose();
- }
-
- private void onCancel() {
- if(newEvent) {
- event.setToBeDeleted(true);
- }
- KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
- dispose();
- }
-
-
- public void init() {
- setupFocusShortcuts();
- statusComboBox.setModel(new DefaultComboBoxModel(EquipmentVerb.values()));
- equipmentFirstComboBox.setModel(new DefaultComboBoxModel(EquipmentFirstNames.values()));
- equipmentFirstComboBox.setSelectedItem(event.getName1());
- equipmentSecondComboBox.setModel(new DefaultComboBoxModel(EquipmentSecondNames.values()));
- equipmentSecondComboBox.setSelectedItem(event.getName2());
- equipmentThirdComboBox.setModel(new DefaultComboBoxModel(EquipmentThirdNames.values()));
- equipmentThirdComboBox.setSelectedItem(event.getName3());
-
- setModal(true);
- getRootPane().setDefaultButton(buttonOK);
- this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.pack();
- if(newEvent) {
- this.setTitle("Register new equipment");
- } else {
- this.setTitle("Edit equipment details");
- }
- setVisible(true);
- }
-
- FocusKeyEventDispatcher keyEventDispatcher = new FocusKeyEventDispatcher();
- // map keypresses to Components so we can call component.getFocus() easily
- private void setupFocusShortcuts() {
- keyEventDispatcher.registerKey(KeyEvent.VK_1, equipmentFirstComboBox);
- keyEventDispatcher.registerKey(KeyEvent.VK_2, equipmentSecondComboBox);
- keyEventDispatcher.registerKey(KeyEvent.VK_3, equipmentThirdComboBox);
- keyEventDispatcher.registerKey(KeyEvent.VK_S, statusComboBox);
-
- KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
- }
-
-
-
- }