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.core.dataset.wrappers;
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    
034    import org.jdmp.core.sample.Sample;
035    import org.jdmp.core.util.CoreObjectList;
036    import org.ujmp.core.Matrix;
037    import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D;
038    import org.ujmp.core.util.StringUtil;
039    
040    public class SampleListToMatrixWrapper extends AbstractDenseObjectMatrix2D implements
041                    ListDataListener {
042            private static final long serialVersionUID = -6691992117924601112L;
043    
044            private CoreObjectList<Sample> samples = null;
045    
046            private int columnCount = 0;
047    
048            private Set<Object> keys = new HashSet<Object>();
049    
050            private Object[] keysToShow = null;
051    
052            private Map<Integer, Object> columnMap = new HashMap<Integer, Object>();
053    
054            public SampleListToMatrixWrapper(CoreObjectList<Sample> samples) {
055                    this(samples, new Object[0]);
056            }
057    
058            public SampleListToMatrixWrapper(CoreObjectList<Sample> samples, Object... keysToShow) {
059                    this.samples = samples;
060                    this.keysToShow = keysToShow;
061                    samples.addListDataListener(this);
062            }
063    
064            @Override
065            public Object getObject(long row, long column) {
066                    return getObject((int) row, (int) column);
067            }
068    
069            @Override
070            public Object getObject(int row, int column) {
071                    createIndex();
072                    Sample s = samples.getElementAt(row);
073                    Object key = columnMap.get(column);
074                    if (key == null) {
075                            return null;
076                    } else {
077                            Matrix m = s.getMatrix(key);
078                            if (m == null) {
079                                    return m;
080                            } else {
081                                    if (m.isScalar()) {
082                                            return m.getAsObject(0, 0);
083                                    } else {
084                                            return m;
085                                    }
086                            }
087                    }
088            }
089    
090            @Override
091            public void setObject(Object value, long row, long column) {
092            }
093    
094            @Override
095            public void setObject(Object value, int row, int column) {
096            }
097    
098            private synchronized void createIndex() {
099                    if (keys != null && !keys.isEmpty()) {
100                            return;
101                    }
102                    keys = new HashSet<Object>();
103                    if (keysToShow == null || keysToShow.length == 0) {
104                            for (Sample s : samples) {
105                                    if (s != null) {
106                                            keys.addAll(s.getVariables().keySet());
107                                    }
108                            }
109                            int i = 0;
110                            for (Object k : keys) {
111                                    columnMap.put(i, k);
112                                    setColumnLabel(i, StringUtil.convert(k));
113                                    i++;
114                            }
115                    } else {
116                            for (int i = 0; i < keysToShow.length; i++) {
117                                    keys.add(keysToShow[i]);
118                                    columnMap.put(i, keysToShow[i]);
119                                    setColumnLabel(i, StringUtil.convert(keysToShow[i]));
120                            }
121                    }
122    
123                    columnCount = keys.size();
124            }
125    
126            private synchronized void deleteIndex() {
127                    if (getAnnotation() != null) {
128                            getAnnotation().clear();
129                    }
130                    columnCount = 0;
131                    columnMap.clear();
132                    keys.clear();
133            }
134    
135            @Override
136            public long[] getSize() {
137                    createIndex();
138                    return new long[] { samples.getSize(), columnCount };
139            }
140    
141            @Override
142            public void contentsChanged(ListDataEvent e) {
143                    deleteIndex();
144            }
145    
146            @Override
147            public void intervalAdded(ListDataEvent e) {
148                    deleteIndex();
149            }
150    
151            @Override
152            public void intervalRemoved(ListDataEvent e) {
153            }
154    
155    }