PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1package uk.ac.cam.ch.ami.gui.dialogs;
  2
  3import uk.ac.cam.ch.ami.EquipmentFirstNames;
  4import uk.ac.cam.ch.ami.EquipmentSecondNames;
  5import uk.ac.cam.ch.ami.EquipmentThirdNames;
  6import uk.ac.cam.ch.ami.amievents.events.EquipmentAmiEvent;
  7import uk.ac.cam.ch.ami.amievents.verbs.EquipmentVerb;
  8import uk.ac.cam.ch.ami.gui.FocusKeyEventDispatcher;
  9
 10import javax.swing.*;
 11import java.awt.*;
 12import java.awt.event.*;
 13
 14/**
 15 * Creates a dialog which we use to both edit an existing EquipmentAmiEvent and register a new EquipmentAmiEvent,
 16 * depending upon whether the constructor is called with newEvent being true or false.  Each piece of Equipment
 17 * has a name consisting of (up to) three name fragments, which map to the three combo boxes in this dialog.
 18 */
 19public class EditEquipmentEventDialog extends JDialog  {
 20    /* outermost panel in the dialog, just for layout purposes */
 21    private JPanel contentPane;
 22    private JButton buttonOK;
 23    private JButton buttonCancel;
 24    /* status of the equipment: has it been "registered", or actually "used"? */
 25    private JComboBox statusComboBox;
 26
 27    /* combo boxes which allow input for the (up to) three name fragments which describe the equipment */
 28    private JComboBox equipmentFirstComboBox;
 29    private JComboBox equipmentSecondComboBox;
 30    private JComboBox equipmentThirdComboBox;
 31
 32    /* the event being edited */
 33    private EquipmentAmiEvent event;
 34
 35    /* true if this is a new event, false if editing an existing event */
 36    private boolean newEvent;
 37
 38    /**
 39     * @param event The event being edited
 40     * @param newEvent true if the event is new, false if editing an existing event
 41     */
 42    public EditEquipmentEventDialog(EquipmentAmiEvent event, boolean newEvent) {
 43        this.event = event;
 44        this.newEvent = newEvent;
 45        setContentPane(contentPane);
 46
 47        buttonOK.addActionListener(new ActionListener() {
 48            public void actionPerformed(ActionEvent e) {
 49                onOK();
 50            }
 51        });
 52
 53        buttonCancel.addActionListener(new ActionListener() {
 54            public void actionPerformed(ActionEvent e) {
 55                onCancel();
 56            }
 57        });
 58
 59        // call onCancel() when cross is clicked
 60        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 61        addWindowListener(new WindowAdapter() {
 62            public void windowClosing(WindowEvent e) {
 63                onCancel();
 64            }
 65        });
 66
 67        // call onCancel() on ESCAPE
 68        contentPane.registerKeyboardAction(new ActionListener() {
 69            public void actionPerformed(ActionEvent e) {
 70                onCancel();
 71            }
 72        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 73    }
 74
 75    private void onOK() {
 76        String s1 = equipmentFirstComboBox.getSelectedItem().toString();
 77        String s2 = equipmentSecondComboBox.getSelectedItem().toString();
 78        String s3 = equipmentThirdComboBox.getSelectedItem().toString();
 79        event.setEquipmentName(s1, s2, s3);
 80        event.setVerb((EquipmentVerb)statusComboBox.getSelectedItem());
 81        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
 82        dispose();
 83    }
 84
 85    private void onCancel() {
 86        if(newEvent) {
 87            event.setToBeDeleted(true);
 88        }
 89        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
 90        dispose();
 91    }
 92
 93
 94    public void init() {
 95        setupFocusShortcuts();
 96        statusComboBox.setModel(new DefaultComboBoxModel(EquipmentVerb.values()));
 97        equipmentFirstComboBox.setModel(new DefaultComboBoxModel(EquipmentFirstNames.values()));
 98        equipmentFirstComboBox.setSelectedItem(event.getName1());
 99        equipmentSecondComboBox.setModel(new DefaultComboBoxModel(EquipmentSecondNames.values()));
100        equipmentSecondComboBox.setSelectedItem(event.getName2());
101        equipmentThirdComboBox.setModel(new DefaultComboBoxModel(EquipmentThirdNames.values()));
102        equipmentThirdComboBox.setSelectedItem(event.getName3());
103
104        setModal(true);
105        getRootPane().setDefaultButton(buttonOK);
106        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
107        this.pack();
108        if(newEvent) {
109            this.setTitle("Register new equipment");
110        } else {
111            this.setTitle("Edit equipment details");
112        }
113        setVisible(true);
114    }
115
116    FocusKeyEventDispatcher keyEventDispatcher = new FocusKeyEventDispatcher();
117    // map keypresses to Components so we can call component.getFocus() easily
118    private void setupFocusShortcuts() {
119        keyEventDispatcher.registerKey(KeyEvent.VK_1, equipmentFirstComboBox);
120        keyEventDispatcher.registerKey(KeyEvent.VK_2, equipmentSecondComboBox);
121        keyEventDispatcher.registerKey(KeyEvent.VK_3, equipmentThirdComboBox);
122        keyEventDispatcher.registerKey(KeyEvent.VK_S, statusComboBox);
123
124        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
125    }
126
127
128
129}