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.

ErrorTableModel.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2006-2014 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.error;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.ErrorListener;
  26. import com.dmdirc.logger.ErrorManager;
  27. import com.dmdirc.logger.ErrorReportStatus;
  28. import com.dmdirc.logger.ProgramError;
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import javax.swing.table.AbstractTableModel;
  33. /**
  34. * Table model for displaying program errors.
  35. */
  36. public final class ErrorTableModel extends AbstractTableModel implements ErrorListener {
  37. /** A version number for this class. */
  38. private static final long serialVersionUID = 2;
  39. /** Data list. */
  40. private final List<ProgramError> errors;
  41. /** Are we ready? */
  42. private boolean ready;
  43. /**
  44. * Creates a new instance of ErrorTableModel.
  45. */
  46. public ErrorTableModel() {
  47. this.errors = Collections.synchronizedList(new ArrayList<>());
  48. }
  49. public void load(final ErrorManager errorManager) {
  50. errorManager.addErrorListener(this);
  51. setErrors(errorManager.getErrors());
  52. ready = true;
  53. }
  54. /**
  55. * Sets the list of errors.
  56. *
  57. * @param errors List of errors
  58. */
  59. public void setErrors(final List<ProgramError> errors) {
  60. synchronized (this.errors) {
  61. this.errors.clear();
  62. this.errors.addAll(errors);
  63. }
  64. fireTableDataChanged();
  65. }
  66. @Override
  67. public int getRowCount() {
  68. synchronized (this.errors) {
  69. return errors.size();
  70. }
  71. }
  72. @Override
  73. public int getColumnCount() {
  74. return 5;
  75. }
  76. @Override
  77. public String getColumnName(final int columnIndex) {
  78. switch (columnIndex) {
  79. case 0:
  80. return "ID";
  81. case 1:
  82. return "Count";
  83. case 2:
  84. return "Severity";
  85. case 3:
  86. return "Report Status";
  87. case 4:
  88. return "Message";
  89. default:
  90. throw new IndexOutOfBoundsException(columnIndex + ">= 5");
  91. }
  92. }
  93. @Override
  94. public Class<?> getColumnClass(final int columnIndex) {
  95. switch (columnIndex) {
  96. case 0:
  97. return Integer.class;
  98. case 1:
  99. return Integer.class;
  100. case 2:
  101. return ErrorLevel.class;
  102. case 3:
  103. return ErrorReportStatus.class;
  104. case 4:
  105. return String.class;
  106. default:
  107. throw new IndexOutOfBoundsException(columnIndex + ">= 5");
  108. }
  109. }
  110. @Override
  111. public boolean isCellEditable(final int rowIndex, final int columnIndex) {
  112. return false;
  113. }
  114. @Override
  115. public Object getValueAt(final int rowIndex, final int columnIndex) {
  116. synchronized (errors) {
  117. switch (columnIndex) {
  118. case 0:
  119. return errors.get(rowIndex).getID();
  120. case 1:
  121. return errors.get(rowIndex).getCount();
  122. case 2:
  123. return errors.get(rowIndex).getLevel();
  124. case 3:
  125. return errors.get(rowIndex).getReportStatus();
  126. case 4:
  127. return errors.get(rowIndex).getMessage();
  128. default:
  129. throw new IndexOutOfBoundsException(columnIndex + ">= 5");
  130. }
  131. }
  132. }
  133. @Override
  134. public void setValueAt(final Object aValue, final int rowIndex,
  135. final int columnIndex) {
  136. synchronized (errors) {
  137. switch (columnIndex) {
  138. case 3:
  139. if (aValue instanceof ErrorReportStatus) {
  140. errors.get(rowIndex).setReportStatus(
  141. (ErrorReportStatus) aValue);
  142. break;
  143. } else {
  144. throw new IllegalArgumentException("Received: " + aValue.getClass()
  145. + ", expecting: " + ErrorReportStatus.class);
  146. }
  147. default:
  148. throw new UnsupportedOperationException("Only editing the "
  149. + "status is allowed");
  150. }
  151. fireTableCellUpdated(rowIndex, columnIndex);
  152. }
  153. }
  154. /**
  155. * Gets the error at the specified row.
  156. *
  157. * @param rowIndex Row to retrieve
  158. *
  159. * @return Specified error
  160. */
  161. public ProgramError getError(final int rowIndex) {
  162. synchronized (errors) {
  163. return errors.get(rowIndex);
  164. }
  165. }
  166. /**
  167. * Returns the index of the specified error or -1 if the error is not found.
  168. *
  169. * @param error ProgramError to locate
  170. *
  171. * @return Error index or -1 if not found
  172. */
  173. public int indexOf(final ProgramError error) {
  174. synchronized (errors) {
  175. return errors.indexOf(error);
  176. }
  177. }
  178. /**
  179. * Adds an error to the list.
  180. *
  181. * @param error ProgramError to add
  182. */
  183. public void addRow(final ProgramError error) {
  184. synchronized (errors) {
  185. errors.add(error);
  186. fireTableRowsInserted(errors.indexOf(error), errors.indexOf(error));
  187. }
  188. }
  189. /**
  190. * Removes a specified row from the list.
  191. *
  192. * @param row Row to remove
  193. */
  194. public void removeRow(final int row) {
  195. synchronized (errors) {
  196. errors.remove(row);
  197. fireTableRowsDeleted(row, row);
  198. }
  199. }
  200. /**
  201. * Removes a specified error from the list.
  202. *
  203. * @param error ProgramError to remove
  204. */
  205. public void removeRow(final ProgramError error) {
  206. synchronized (errors) {
  207. if (errors.contains(error)) {
  208. final int row = errors.indexOf(error);
  209. errors.remove(row);
  210. fireTableRowsDeleted(row, row);
  211. }
  212. }
  213. }
  214. @Override
  215. public void errorAdded(final ProgramError error) {
  216. UIUtilities.invokeLater(() -> addRow(error));
  217. }
  218. @Override
  219. public void errorDeleted(final ProgramError error) {
  220. UIUtilities.invokeLater(() -> removeRow(error));
  221. }
  222. @Override
  223. public void errorStatusChanged(final ProgramError error) {
  224. UIUtilities.invokeLater(() -> {
  225. synchronized (errors) {
  226. final int errorRow = indexOf(error);
  227. if (errorRow != -1 && errorRow < getRowCount()) {
  228. fireTableRowsUpdated(errorRow, errorRow);
  229. }
  230. }
  231. });
  232. }
  233. @Override
  234. public boolean isReady() {
  235. return ready;
  236. }
  237. /**
  238. * Disposes of this model, removing any added listeners.
  239. */
  240. public void dispose() {
  241. ErrorManager.getErrorManager().removeErrorListener(this);
  242. ready = false;
  243. }
  244. }