/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

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