You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorsDialogController.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.errors;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  25. import com.dmdirc.interfaces.ui.ErrorsDialogModel;
  26. import com.dmdirc.interfaces.ui.ErrorsDialogModelListener;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.ErrorReportStatus;
  29. import com.dmdirc.ui.core.errors.DisplayableError;
  30. import java.time.format.DateTimeFormatter;
  31. import java.util.Optional;
  32. import javax.swing.JButton;
  33. import javax.swing.JScrollPane;
  34. import javax.swing.JTable;
  35. import javax.swing.JTextArea;
  36. import javax.swing.JTextField;
  37. import javax.swing.ListSelectionModel;
  38. /**
  39. * Controller linking the {@link ErrorsDialogModel} to the {@link ErrorsDialog}.
  40. */
  41. class ErrorsDialogController implements ErrorsDialogModelListener {
  42. private final ErrorsDialogModel model;
  43. private JTable table;
  44. private GenericTableModel<DisplayableError> tableModel;
  45. private JTextField date;
  46. private JTextField severity;
  47. private JTextField reportStatus;
  48. private JTextArea details;
  49. private JButton deleteAll;
  50. private JButton delete;
  51. private JButton send;
  52. private JScrollPane detailsScroll;
  53. public ErrorsDialogController(final ErrorsDialogModel model) {
  54. this.model = model;
  55. }
  56. public void init(final ErrorsDialog dialog, final GenericTableModel<DisplayableError> tableModel,
  57. final JTable table, final JTextField date, final JTextField severity,
  58. final JTextField reportStatus, final JTextArea details, final JScrollPane detailsScroll,
  59. final JButton deleteAll, final JButton delete, final JButton send, final JButton close) {
  60. this.tableModel = tableModel;
  61. this.table = table;
  62. this.date = date;
  63. this.severity = severity;
  64. this.reportStatus = reportStatus;
  65. this.details = details;
  66. this.detailsScroll = detailsScroll;
  67. this.deleteAll = deleteAll;
  68. this.delete = delete;
  69. this.send = send;
  70. model.addListener(this);
  71. model.load();
  72. model.getErrors().forEach(this::errorAdded);
  73. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  74. table.setCellSelectionEnabled(false);
  75. table.setRowSelectionAllowed(true);
  76. table.getSelectionModel().addListSelectionListener(e -> {
  77. if (!e.getValueIsAdjusting()) {
  78. final int index = table.getSelectedRow();
  79. if (index == -1) {
  80. model.setSelectedError(Optional.empty());
  81. } else {
  82. model.setSelectedError(Optional.ofNullable(tableModel.getValue(index)));
  83. }
  84. }
  85. });
  86. deleteAll.addActionListener(e -> model.deleteAllErrors());
  87. delete.addActionListener(e -> model.deleteSelectedError());
  88. send.addActionListener(e -> model.sendSelectedError());
  89. close.addActionListener(e -> dialog.dispose());
  90. checkEnabledStates();
  91. }
  92. @Override
  93. public void errorDeleted(final DisplayableError error) {
  94. UIUtilities.invokeLater(() -> {
  95. tableModel.removeValue(error);
  96. checkEnabledStates();
  97. });
  98. }
  99. @Override
  100. public void errorAdded(final DisplayableError error) {
  101. UIUtilities.invokeLater(() -> {
  102. tableModel.addValue(error);
  103. checkEnabledStates();
  104. });
  105. }
  106. @Override
  107. public void selectedErrorChanged(final Optional<DisplayableError> selectedError) {
  108. UIUtilities.invokeLater(() -> {
  109. if (selectedError.isPresent()) {
  110. final int index = tableModel.getIndex(selectedError.get());
  111. table.getSelectionModel().setSelectionInterval(index, index);
  112. } else {
  113. table.getSelectionModel().setSelectionInterval(-1, -1);
  114. }
  115. date.setText(selectedError.map(DisplayableError::getDate)
  116. .map(d -> d.format(DateTimeFormatter.ofPattern("MMM dd hh:mm a"))).orElse(""));
  117. severity.setText(selectedError.map(DisplayableError::getSeverity)
  118. .map(ErrorLevel::name).orElse(""));
  119. reportStatus.setText(
  120. selectedError.map(DisplayableError::getReportStatus)
  121. .map(ErrorReportStatus::name).orElse(""));
  122. details.setText(selectedError.map(DisplayableError::getDetails).orElse(""));
  123. checkEnabledStates();
  124. UIUtilities.resetScrollPane(detailsScroll);
  125. });
  126. }
  127. @Override
  128. public void errorStatusChanged(final DisplayableError error) {
  129. UIUtilities.invokeLater(() -> {
  130. final int index = tableModel.getIndex(error);
  131. tableModel.fireTableCellUpdated(index, 1);
  132. if (index == table.getSelectedRow()) {
  133. reportStatus.setText(error.getReportStatus().name());
  134. }
  135. checkEnabledStates();
  136. });
  137. }
  138. private void checkEnabledStates() {
  139. deleteAll.setEnabled(model.isDeleteAllAllowed());
  140. send.setEnabled(model.isSendAllowed());
  141. delete.setEnabled(model.isDeletedAllowed());
  142. }
  143. }