/** * * Copyright (c) 2003 University of Mancester. * @author Kevin Garwood (garwood@cs.man.ac.uk) * @version 1.0 * */ package pedro.ontology; import javax.swing.table.AbstractTableModel; import java.util.Hashtable; import java.util.ArrayList; /* Code RoadMap: Section Constants Section Properties Section Construction Section Accessors Section Mutators Section Validation Section Errors Section Interfaces Section Overload */ public class DictionaryTableModel extends AbstractTableModel { // ========================================== // Section Constants // ========================================== private static final int TERM_FIELD = 0; private static final int DEFINITION_FIELD = 1; // ========================================== // Section Properties // ========================================== private Hashtable ontologyTermFromRow; private OntologyTerm[] masterList; private String[][] currentList; // ========================================== // Section Construction // ========================================== public DictionaryTableModel() { ontologyTermFromRow = new Hashtable(); } // ========================================== // Section Accessors // ========================================== public OntologyTerm getTermFromRow(int row) { OntologyTerm result = (OntologyTerm) ontologyTermFromRow.get(new Integer(row) ); return result; } // ========================================== // Section Mutators // ========================================== public void setMasterList(OntologyTerm[] masterList) { this.masterList = masterList; reset(); } public void reset() { setRows(masterList); } private void setRows(OntologyTerm[] ontologyTerms) { ontologyTermFromRow.clear(); currentList = new String[ontologyTerms.length][2]; for ( int i = 0; i < ontologyTerms.length; i++) { currentList[i][TERM_FIELD] = ontologyTerms[i].getTerm(); currentList[i][DEFINITION_FIELD] = ontologyTerms[i].getDefinition(); ontologyTermFromRow.put(new Integer(i), ontologyTerms[i]); } // end for () } private static int count = 0; public void filter(String filterMask) { count++; ArrayList filteredRows = new ArrayList(); for ( int i = 0; i < masterList.length; i++) { if ( (masterList[i].getTerm().indexOf(filterMask) != -1) || (masterList[i].getDefinition().indexOf(filterMask) != -1) ) { filteredRows.add( masterList[i] ); } //end if () } // end for () OntologyTerm[] results = (OntologyTerm[]) filteredRows.toArray(new OntologyTerm[0]); setRows(results); } // ========================================== // Section Validation // ========================================== // ========================================== // Section Errors // ========================================== // ========================================== // Section Interfaces // ========================================== // ========================================== // Section Overload // ========================================== public Object getValueAt(int row, int column) { if ( column == TERM_FIELD) { return currentList[row][TERM_FIELD]; } //end if () else if ( column == DEFINITION_FIELD) { return currentList[row][DEFINITION_FIELD]; } //end else () else { return null; } //end else } public String getColumnName(int column) { if ( column == TERM_FIELD) { return "Term"; } //end if () else if ( column == DEFINITION_FIELD) { return "Summary Definition"; } //end else () return null; } public int getRowCount() { if ( currentList == null) { return 0; } //end if () return currentList.length; } public int getColumnCount() { return 2; } public boolean isCellEditable() { return false; } }