001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.gui.sample;
025    
026    import java.util.HashMap;
027    import java.util.HashSet;
028    import java.util.Map;
029    import java.util.Set;
030    
031    import javax.swing.event.ListDataEvent;
032    import javax.swing.event.ListDataListener;
033    import javax.swing.table.AbstractTableModel;
034    
035    import org.jdmp.core.sample.HasSamples;
036    import org.jdmp.core.sample.Sample;
037    import org.jdmp.core.variable.Variable;
038    
039    public class SampleListTableModel extends AbstractTableModel implements ListDataListener {
040            private static final long serialVersionUID = -5468178300746964431L;
041    
042            public static final int ICONCOLUMN = 0;
043    
044            private Set<String> keys = new HashSet<String>();
045    
046            private Map<Integer, String> columnMap = new HashMap<Integer, String>();
047    
048            private HasSamples iSamples = null;
049    
050            public SampleListTableModel(HasSamples iSamples) {
051                    this.iSamples = iSamples;
052                    iSamples.getSamples().addListDataListener(this);
053            }
054    
055            public int getRowCount() {
056                    return iSamples.getSamples().getSize();
057            }
058    
059            public int getColumnCount() {
060                    return keys.size() + 1;
061            }
062    
063            @Override
064            public String getColumnName(int columnIndex) {
065                    switch (columnIndex) {
066                    case ICONCOLUMN:
067                            return "";
068                    default:
069                            String s = columnMap.get(columnIndex - 1);
070                            return s == null ? "unknown" : s;
071                    }
072            }
073    
074            @Override
075            public Class<?> getColumnClass(int columnIndex) {
076                    return Sample.class;
077            }
078    
079            public Object getValueAt(int rowIndex, int columnIndex) {
080                    Sample s = iSamples.getSamples().getElementAt(rowIndex);
081                    if (s == null) {
082                            return null;
083                    }
084                    for (Variable v : s.getVariables()) {
085                            if (keys.add(v.getLabel())) {
086                                    columnMap.put(columnMap.size(), v.getLabel());
087                                    fireTableStructureChanged();
088                            }
089                    }
090                    return s;
091            }
092    
093            @Override
094            public void contentsChanged(ListDataEvent e) {
095                    if (e.getIndex0() < 0 || e.getIndex1() < 0) {
096                            fireTableDataChanged();
097                    } else {
098                            try {
099                                    fireTableRowsUpdated(e.getIndex0(), e.getIndex1());
100                            } catch (Exception ex) {
101                                    ex.printStackTrace();
102                                    fireTableDataChanged();
103                            }
104                    }
105            }
106    
107            @Override
108            public void intervalAdded(ListDataEvent e) {
109                    try {
110                            fireTableRowsInserted(e.getIndex0(), e.getIndex1());
111                    } catch (Exception ex) {
112                            ex.printStackTrace();
113                            fireTableDataChanged();
114                    }
115            }
116    
117            @Override
118            public void intervalRemoved(ListDataEvent e) {
119                    try {
120                            fireTableRowsDeleted(e.getIndex0(), e.getIndex1());
121                    } catch (Exception ex) {
122                            ex.printStackTrace();
123                            fireTableDataChanged();
124                    }
125            }
126    
127            public Set<String> getKeys() {
128                    return keys;
129            }
130    
131            public Map<Integer, String> getColumnMap() {
132                    return columnMap;
133            }
134    
135    }