ModuleErrorAndWarningListModel.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.gui.se.pane;
017 
018 import javax.swing.Icon;
019 import javax.swing.ImageIcon;
020 import javax.swing.table.AbstractTableModel;
021 
022 import org.qedeq.kernel.bo.common.QedeqBo;
023 import org.qedeq.kernel.se.common.SourceFileException;
024 import org.qedeq.kernel.se.common.SourceFileExceptionList;
025 
026 /**
027  * Table model for QEDEQ module specific error pane.
028  *
029  @author  Michael Meyling
030  */
031 
032 public class ModuleErrorAndWarningListModel extends AbstractTableModel {
033 
034     /** Error icon. */
035     private static ImageIcon errorIcon = new ImageIcon(
036         ModuleErrorAndWarningListPane.class.getResource(
037             "/images/eclipse/error_tsk.gif"));
038 
039     /** Warning icon. */
040     private static ImageIcon warningIcon = new ImageIcon(
041         ModuleErrorAndWarningListPane.class.getResource(
042             "/images/eclipse/warn_tsk.gif"));
043 
044     /** We want to show errors and warnings from this module. */
045     private QedeqBo qedeq;
046 
047     /** Errors of current module. Might be <code>null</code>. */
048     private SourceFileExceptionList errors;
049 
050     /** Warnings of current module. Might be <code>null</code>. */
051     private SourceFileExceptionList warnings;
052 
053     /** Number of errors of current module. */
054     private int maxErrors;
055 
056     /** Number of errors plus number of warnings of current module. */
057     private int maxEntries;
058 
059     public String getColumnName(final int column) {
060         if (qedeq == null) {
061             return "";
062         }
063         if (column == 1) {
064             return qedeq.getModuleAddress().getUrl();
065         else if (column == 2) {
066             return "Location";
067         else if (column == 3) {
068             return "Type";
069         else {
070             return "";
071         }
072     }
073 
074     public int getRowCount() {
075         return maxEntries;
076     }
077 
078     public int getColumnCount() {
079         return 4;
080     }
081 
082     public Object getValueAt(final int row, final int col) {
083 //        System.out.println("row: " + row + " col: " + col);
084         if (qedeq == null) {
085             return "";
086         }
087         final SourceFileException e = getSourceFileException(row);
088         if (e == null) {
089             return "";
090         }
091         switch (col) {
092         case 0if (isError(row)) {
093                     return errorIcon;
094                 else if (isWarning(row)) {
095                     return warningIcon;
096                 }
097                 break;
098         case 1if (getSourceFileException(row!= null) {
099                     return getSourceFileException(row).getMessage();
100                 }
101                 break;
102         case 2if (getSourceFileException(row!= null && getSourceFileException(row).getSourceArea() != null) {
103                     return "line " + getSourceFileException(row).getSourceArea().getStartPosition().getRow();
104                 }
105                 break;
106         case 3if (getSourceFileException(row!= null) {
107                     return getSourceFileException(row).getPlugin().getPluginActionName();
108                 }
109                 break;
110         default:
111                 return "";
112         }
113         return "";
114     }
115 
116     public boolean isCellEditable(final int row, final int column) {
117         return false;
118     }
119 
120     public Class getColumnClass(final int col) {
121         return col == ? Icon.class : Object.class;
122    }
123 
124     /**
125      * Set QEDEQ module.
126      *
127      @param   qedeq   QEDEQ module.
128      */
129     public void setQedeq(final QedeqBo qedeq) {
130         this.qedeq = qedeq;
131     }
132 
133     /**
134      * Get QEDEQ module.
135      *
136      @return  QEDEQ module.
137      */
138     public QedeqBo getQedeq() {
139         return qedeq;
140     }
141 
142     public void fireTableDataChanged() {
143         if (qedeq != null) {
144             this.errors = qedeq.getErrors();
145             this.warnings = qedeq.getWarnings();
146         else {
147             this.errors = null;
148             this.warnings = null;
149         }
150         maxErrors = 0;
151         if (errors != null) {
152             maxErrors += errors.size();
153         }
154         maxEntries = maxErrors;
155         if (warnings != null) {
156             maxEntries += warnings.size();
157         }
158         super.fireTableDataChanged();
159     }
160 
161     /**
162      * Get exception that is at given row.
163      *
164      @param   row     Look into this row.
165      @return  Exception of this row. Might be <code>null</code> if row doesn't exist.
166      */
167     public SourceFileException getSourceFileException(final int row) {
168         if (isError(row)) {
169             return errors.get(row);
170         else if (isWarning(row)) {
171             return warnings.get(row - maxErrors);
172         }
173         return null;
174     }
175 
176     /**
177      * Get number of error (that is position in error list) that is in the given row.
178      * If at the given row is no error an IllegalArgumentException is thrown.
179      *
180      @param   row     Look at this row.
181      @return  Error list number of error at this row.
182      @throws  IllegalArgumentException    At given row is no error.
183      */
184     public int getErrorNumber(final int row) {
185         if (!isError(row)) {
186             throw new IllegalArgumentException("This is not an error row: " + row);
187         }
188         return row;
189     }
190 
191     /**
192      * Get number of warning (that is position in warning list) that is in the given row.
193      * If at the given row is no warning an IllegalArgumentException is thrown.
194      *
195      @param   row     Look at this row.
196      @return  Warning list number of warning at this row.
197      @throws  IllegalArgumentException    At given row is no warning.
198      */
199     public int getWarningNumber(final int row) {
200         if (!isWarning(row)) {
201             throw new IllegalArgumentException("This is not an warning row: " + row);
202         }
203         return row - maxErrors;
204     }
205 
206     /**
207      * Is there an error at given row?
208      *
209      @param   row     Look at this row.
210      @return  Is there an error?
211      */
212     public boolean isError(final int row) {
213         if (row >= && row < maxErrors) {
214             return true;
215         }
216         return false;
217     }
218 
219     /**
220      * Is there an warning at given row?
221      *
222      @param   row     Look at this row.
223      @return  Is there an warning?
224      */
225     public boolean isWarning(final int row) {
226         if (row >= maxErrors && row < maxEntries) {
227             return true;
228         }
229         return false;
230     }
231 
232 }