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.Comparator;
027    
028    import javax.swing.table.TableRowSorter;
029    
030    import org.jdmp.core.CoreObject;
031    import org.jdmp.core.sample.HasSamples;
032    import org.jdmp.core.sample.Sample;
033    import org.jdmp.core.util.ObservableMap;
034    import org.jdmp.core.variable.Variable;
035    import org.jdmp.gui.util.AbstractListPanel;
036    import org.ujmp.core.Matrix;
037    import org.ujmp.core.interfaces.GUIObject;
038    
039    public class SampleListPanel extends AbstractListPanel {
040            private static final long serialVersionUID = -5498996013925285012L;
041    
042            public SampleListPanel(HasSamples iSamples) {
043                    super();
044                    if (iSamples instanceof CoreObject) {
045                            this.object = ((CoreObject) iSamples).getGUIObject();
046                    } else {
047                            this.object = (GUIObject) iSamples;
048                    }
049    
050                    dataModel = new SampleListTableModel(iSamples);
051                    dataModel.addTableModelListener(this);
052                    jTable.setDefaultRenderer(Sample.class, new SampleTableCellRenderer());
053    
054                    jTable.setModel(dataModel);
055    
056                    // // something goes wrong here
057                    // try {
058                    // TableRowSorter<?> rs = (TableRowSorter<?>) jTable.getRowSorter();
059                    // int count = iSamples.getSamples().getSize();
060                    // if (count != 0) {
061                    // for (int j = 0; j < 5 && j < count; j++) {
062                    // Sample s = iSamples.getSamples().getElementAt(j);
063                    // if (s != null) {
064                    // ObservableMap<Variable> variables = s.getVariables();
065                    // int i = 1;
066                    // for (Object key : variables.keySet()) {
067                    // rs.setComparator(i++, new SampleCollator(key));
068                    // }
069                    // break;
070                    // }
071                    // }
072                    // }
073                    // } catch (Exception e) {
074                    // e.printStackTrace();
075                    // // something goes wrong here
076                    // }
077    
078                    jTable.getColumnModel().getColumn(SampleListTableModel.ICONCOLUMN).setMinWidth(ICONWIDTH);
079                    jTable.getColumnModel().getColumn(SampleListTableModel.ICONCOLUMN).setMaxWidth(ICONWIDTH);
080                    jTable.getColumnModel().getColumn(SampleListTableModel.ICONCOLUMN).setPreferredWidth(
081                                    ICONWIDTH);
082    
083                    updateTitle();
084            }
085    
086            @Override
087            public void updateTitle() {
088                    getBorder().setTitle("Samples (" + jTable.getRowCount() + ")");
089                    repaint(1000);
090            }
091    
092    }
093    
094    class SampleCollator implements Comparator<Object> {
095    
096            private Object column = null;
097    
098            public SampleCollator(Object column) {
099                    this.column = column;
100            }
101    
102            @Override
103            public int compare(Object source, Object target) {
104                    return compare((Sample) source, (Sample) target);
105            }
106    
107            public int compare(Sample s1, Sample s2) {
108                    Matrix m1 = s1.getMatrix(column);
109                    Matrix m2 = s2.getMatrix(column);
110                    if (m1 != null && m2 != null) {
111                            return m1.compareTo(m2);
112                    } else {
113                            return 0;
114                    }
115            }
116    
117    }