001    package org.jdmp.core.util;
002    
003    import javax.swing.event.ListDataEvent;
004    import javax.swing.event.ListDataListener;
005    
006    import org.jdmp.core.variable.Variable;
007    import org.ujmp.core.Matrix;
008    import org.ujmp.core.exceptions.MatrixException;
009    import org.ujmp.core.interfaces.Wrapper;
010    import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D;
011    
012    public class MatrixListToMatrixWrapper extends AbstractDenseObjectMatrix2D implements
013                    Wrapper<ObservableList<Matrix>>, ListDataListener {
014            private static final long serialVersionUID = 3023715319099124633L;
015    
016            private ObservableList<Matrix> matrixList = null;
017    
018            public MatrixListToMatrixWrapper(Variable variable) {
019                    this.matrixList = variable.getMatrixList();
020                    matrixList.addListDataListener(this);
021            }
022    
023            public long[] getSize() {
024                    long cols = 0;
025                    long rows = 0;
026                    for (Matrix m : matrixList) {
027                            cols = Math.max(m.getColumnCount(), cols);
028                            rows += m.getRowCount();
029                    }
030                    return new long[] { rows, cols };
031    
032            }
033    
034            public Object getObject(int row, int column) throws MatrixException {
035                    return getObject((long) row, (long) column);
036            }
037    
038            public Object getObject(long row, long column) throws MatrixException {
039                    long rows = 0;
040                    for (Matrix m : matrixList) {
041                            rows += m.getRowCount();
042                            if (rows > row) {
043                                    long r = row - rows + m.getRowCount();
044                                    if (column < m.getColumnCount()) {
045                                            return m.getAsObject(r, column);
046                                    } else {
047                                            return 0.0;
048                                    }
049                            }
050                    }
051                    return 0.0;
052            }
053    
054            public void setObject(Object value, int row, int column) throws MatrixException {
055                    setObject(value, (long) row, (long) column);
056            }
057    
058            public void setObject(Object value, long row, long column) throws MatrixException {
059                    long rows = 0;
060                    for (Matrix m : matrixList) {
061                            rows += m.getRowCount();
062                            if (rows > row) {
063                                    long r = row - rows + m.getRowCount();
064                                    if (column < m.getColumnCount()) {
065                                            m.setAsObject(value, r, column);
066                                            return;
067                                    } else {
068                                            return;
069                                    }
070                            }
071                    }
072            }
073    
074            public ObservableList<Matrix> getWrappedObject() {
075                    return matrixList;
076            }
077    
078            public void setWrappedObject(ObservableList<Matrix> object) {
079                    this.matrixList = object;
080            }
081    
082            @Override
083            public void contentsChanged(ListDataEvent e) {
084                    notifyGUIObject();
085            }
086    
087            @Override
088            public void intervalAdded(ListDataEvent e) {
089                    notifyGUIObject();
090            }
091    
092            @Override
093            public void intervalRemoved(ListDataEvent e) {
094                    notifyGUIObject();
095            }
096    
097    }