123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.dialogs.error;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.ErrorListener;
- import com.dmdirc.logger.ErrorManager;
- import com.dmdirc.logger.ErrorReportStatus;
- import com.dmdirc.logger.ProgramError;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- import javax.swing.table.AbstractTableModel;
-
- /**
- * Table model for displaying program errors.
- */
- public final class ErrorTableModel extends AbstractTableModel implements ErrorListener {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 2;
- /** Data list. */
- private final List<ProgramError> errors;
- /** Are we ready? */
- private boolean ready;
-
- /**
- * Creates a new instance of ErrorTableModel.
- */
- public ErrorTableModel() {
- this.errors = Collections.synchronizedList(new ArrayList<>());
- }
-
- public void load(final ErrorManager errorManager) {
- errorManager.addErrorListener(this);
- setErrors(errorManager.getErrors());
- ready = true;
- }
-
- /**
- * Sets the list of errors.
- *
- * @param errors List of errors
- */
- public void setErrors(final List<ProgramError> errors) {
- synchronized (this.errors) {
- this.errors.clear();
- this.errors.addAll(errors);
- }
-
- fireTableDataChanged();
- }
-
- @Override
- public int getRowCount() {
- synchronized (this.errors) {
- return errors.size();
- }
- }
-
- @Override
- public int getColumnCount() {
- return 5;
- }
-
- @Override
- public String getColumnName(final int columnIndex) {
- switch (columnIndex) {
- case 0:
- return "ID";
- case 1:
- return "Count";
- case 2:
- return "Severity";
- case 3:
- return "Report Status";
- case 4:
- return "Message";
- default:
- throw new IndexOutOfBoundsException(columnIndex + ">= 5");
- }
- }
-
- @Override
- public Class<?> getColumnClass(final int columnIndex) {
- switch (columnIndex) {
- case 0:
- return Integer.class;
- case 1:
- return Integer.class;
- case 2:
- return ErrorLevel.class;
- case 3:
- return ErrorReportStatus.class;
- case 4:
- return String.class;
- default:
- throw new IndexOutOfBoundsException(columnIndex + ">= 5");
- }
- }
-
- @Override
- public boolean isCellEditable(final int rowIndex, final int columnIndex) {
- return false;
- }
-
- @Override
- public Object getValueAt(final int rowIndex, final int columnIndex) {
- synchronized (errors) {
- switch (columnIndex) {
- case 0:
- return errors.get(rowIndex).getID();
- case 1:
- return errors.get(rowIndex).getCount();
- case 2:
- return errors.get(rowIndex).getLevel();
- case 3:
- return errors.get(rowIndex).getReportStatus();
- case 4:
- return errors.get(rowIndex).getMessage();
- default:
- throw new IndexOutOfBoundsException(columnIndex + ">= 5");
- }
- }
- }
-
- @Override
- public void setValueAt(final Object aValue, final int rowIndex,
- final int columnIndex) {
- synchronized (errors) {
- switch (columnIndex) {
- case 3:
- if (aValue instanceof ErrorReportStatus) {
- errors.get(rowIndex).setReportStatus(
- (ErrorReportStatus) aValue);
- break;
- } else {
- throw new IllegalArgumentException("Received: " + aValue.getClass()
- + ", expecting: " + ErrorReportStatus.class);
- }
- default:
- throw new UnsupportedOperationException("Only editing the "
- + "status is allowed");
- }
- fireTableCellUpdated(rowIndex, columnIndex);
- }
- }
-
- /**
- * Gets the error at the specified row.
- *
- * @param rowIndex Row to retrieve
- *
- * @return Specified error
- */
- public ProgramError getError(final int rowIndex) {
- synchronized (errors) {
- return errors.get(rowIndex);
- }
- }
-
- /**
- * Returns the index of the specified error or -1 if the error is not found.
- *
- * @param error ProgramError to locate
- *
- * @return Error index or -1 if not found
- */
- public int indexOf(final ProgramError error) {
- synchronized (errors) {
- return errors.indexOf(error);
- }
- }
-
- /**
- * Adds an error to the list.
- *
- * @param error ProgramError to add
- */
- public void addRow(final ProgramError error) {
- synchronized (errors) {
- errors.add(error);
- fireTableRowsInserted(errors.indexOf(error), errors.indexOf(error));
- }
- }
-
- /**
- * Removes a specified row from the list.
- *
- * @param row Row to remove
- */
- public void removeRow(final int row) {
- synchronized (errors) {
- errors.remove(row);
- fireTableRowsDeleted(row, row);
- }
- }
-
- /**
- * Removes a specified error from the list.
- *
- * @param error ProgramError to remove
- */
- public void removeRow(final ProgramError error) {
- synchronized (errors) {
- if (errors.contains(error)) {
- final int row = errors.indexOf(error);
- errors.remove(row);
- fireTableRowsDeleted(row, row);
- }
- }
- }
-
- @Override
- public void errorAdded(final ProgramError error) {
- UIUtilities.invokeLater(() -> addRow(error));
- }
-
- @Override
- public void errorDeleted(final ProgramError error) {
- UIUtilities.invokeLater(() -> removeRow(error));
- }
-
- @Override
- public void errorStatusChanged(final ProgramError error) {
- UIUtilities.invokeLater(() -> {
- synchronized (errors) {
- final int errorRow = indexOf(error);
- if (errorRow != -1 && errorRow < getRowCount()) {
- fireTableRowsUpdated(errorRow, errorRow);
- }
- }
- });
- }
-
- @Override
- public boolean isReady() {
- return ready;
- }
-
- /**
- * Disposes of this model, removing any added listeners.
- */
- public void dispose() {
- ErrorManager.getErrorManager().removeErrorListener(this);
- ready = false;
- }
-
- }
|