001package jmri.profile; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import java.io.File; 006import java.io.IOException; 007import javax.swing.table.AbstractTableModel; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * A tabular data model for the search paths used to find 013 * {@link jmri.profile.Profile}s. 014 * 015 * @author Randall Wood 016 */ 017class SearchPathTableModel extends AbstractTableModel implements PropertyChangeListener { 018 019 private static final Logger log = LoggerFactory.getLogger(SearchPathTableModel.class); 020 021// @SuppressWarnings("LeakingThisInConstructor") 022 public SearchPathTableModel() { 023 ProfileManager.getDefault().addPropertyChangeListener(this); 024 } 025 026 @Override 027 public int getRowCount() { 028 return ProfileManager.getDefault().getSearchPaths().length; 029 } 030 031 @Override 032 public int getColumnCount() { 033 return 2; 034 } 035 036 @Override 037 public Object getValueAt(int rowIndex, int columnIndex) { 038 File p = ProfileManager.getDefault().getSearchPaths(rowIndex); 039 switch (columnIndex) { 040 case -1: // tooltip 041 return Bundle.getMessage("SearchPathTableModel.tooltip", ProfileManager.getDefault().getDefaultSearchPath().getPath()); 042 case 0: 043 return p; 044 case 1: 045 return (ProfileManager.getDefault().getDefaultSearchPath().equals(p)); 046 default: 047 return null; 048 } 049 050 } 051 052 @Override 053 public String getColumnName(int columnIndex) { 054 switch (columnIndex) { 055 case 0: 056 return Bundle.getMessage("SearchPathTableModel.searchPath"); // NOI18N 057 case 1: 058 return Bundle.getMessage("SearchPathTableModel.isDefault"); // NOI18N 059 default: 060 return null; 061 } 062 } 063 064 @Override 065 public Class<?> getColumnClass(int columnIndex) { 066 switch (columnIndex) { 067 case 0: 068 return File.class; 069 case 1: 070 return Boolean.class; 071 default: 072 return null; 073 } 074 } 075 076 @Override 077 public boolean isCellEditable(int rowIndex, int columnIndex) { 078 switch (columnIndex) { 079 case 1: 080 return true; 081 default: 082 return false; 083 } 084 } 085 086 @Override 087 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 088 switch (columnIndex) { 089 case 1: 090 try { 091 ProfileManager.getDefault().setDefaultSearchPath((File) this.getValueAt(rowIndex, 0)); 092 } catch (IOException ex) { 093 log.warn("Unable to write profiles while setting default search path", ex); 094 } 095 break; 096 default: 097 } 098 } 099 100 @Override 101 public void propertyChange(PropertyChangeEvent evt) { 102 this.fireTableDataChanged(); 103 } 104 105}