001package jmri.profile;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.io.File;
006import javax.swing.table.AbstractTableModel;
007
008/**
009 * A tabular data model of {@link jmri.profile.Profile}s, suitable for use in a
010 * {@link javax.swing.JTable}.
011 *
012 * @author Randall Wood
013 */
014public class ProfileTableModel extends AbstractTableModel implements PropertyChangeListener {
015
016//    @SuppressWarnings("LeakingThisInConstructor")
017    public ProfileTableModel() {
018        ProfileManager.getDefault().addPropertyChangeListener(this);
019    }
020
021    @Override
022    public int getRowCount() {
023        return ProfileManager.getDefault().getAllProfiles().size();
024    }
025
026    @Override
027    public int getColumnCount() {
028        return 3;
029    }
030
031    @Override
032    public Object getValueAt(int rowIndex, int columnIndex) {
033        Profile p = ProfileManager.getDefault().getAllProfiles().get(rowIndex);
034        switch (columnIndex) {
035            case -1: // tooltip
036                return Bundle.getMessage("ProfileTableModel.toolTip", p.getName(), p.getPath(), p.getId(), this.getValueAt(rowIndex, 2));
037            case 0:
038                return p.getName();
039            case 1:
040                return p.getPath();
041            case 2:
042                if (p.equals(ProfileManager.getDefault().getActiveProfile())) {
043                    return Bundle.getMessage("ProfileTableModel.isActive"); // NOI18N
044                } else if (p.equals(ProfileManager.getDefault().getNextActiveProfile())) {
045                    return Bundle.getMessage("ProfileTableModel.nextActive"); // NOI18N
046                } else {
047                    return ""; // NOI18N
048                }
049            default:
050                return null;
051        }
052    }
053
054    @Override
055    public String getColumnName(int columnIndex) {
056        switch (columnIndex) {
057            case 0:
058                return Bundle.getMessage("ProfileTableModel.profileName"); // NOI18N
059            case 1:
060                return Bundle.getMessage("ProfileTableModel.profilePath"); // NOI18N
061            case 2:
062                return Bundle.getMessage("ProfileTableModel.profileState"); // NOI18N
063            default:
064                return null;
065        }
066    }
067
068    @Override
069    public Class<?> getColumnClass(int columnIndex) {
070        switch (columnIndex) {
071            case 0:
072                return String.class;
073            case 1:
074                return File.class;
075            case 2:
076                return String.class;
077            default:
078                return null;
079        }
080    }
081
082    @Override
083    public boolean isCellEditable(int rowIndex, int columnIndex) {
084        switch (columnIndex) {
085            case 0:
086                return true;
087            default:
088                return false;
089        }
090    }
091
092    @Override
093    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
094        switch (columnIndex) {
095            case 0:
096                ProfileManager.getDefault().getAllProfiles().get(rowIndex).setName(aValue.toString());
097                break;
098            default:
099        }
100    }
101
102    @Override
103    public void propertyChange(PropertyChangeEvent evt) {
104        this.fireTableDataChanged();
105    }
106}