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.variable;
025    
026    import java.lang.reflect.Constructor;
027    import java.util.logging.Level;
028    
029    import org.jdmp.core.AbstractCoreObject;
030    import org.ujmp.core.Matrix;
031    import org.ujmp.core.coordinates.Coordinates;
032    import org.ujmp.core.interfaces.GUIObject;
033    import org.ujmp.core.util.StringUtil;
034    
035    public abstract class AbstractVariable extends AbstractCoreObject implements Variable {
036            private static final long serialVersionUID = -3393106211967497877L;
037    
038            private transient GUIObject guiObject = null;
039    
040            protected AbstractVariable() {
041                    super();
042            }
043    
044            public final int getMatrixCount() {
045                    if (getMatrixList() == null) {
046                            return 0;
047                    } else {
048                            return getMatrixList().getSize();
049                    }
050            }
051    
052            public final Matrix getMatrix() {
053                    if (getMatrixList() == null) {
054                            return null;
055                    } else {
056                            return getMatrixList().getElementAt(getMatrixList().getSize() - 1);
057                    }
058            }
059    
060            public final String getAsString() {
061                    if (getMatrixList() == null) {
062                            return null;
063                    } else {
064                            return StringUtil.convert(getMatrixList().getElementAt(getMatrixList().getSize() - 1));
065                    }
066            }
067    
068            public final Matrix getMatrix(int index) {
069                    if (getMatrixList() == null)
070                            return null;
071                    return getMatrixList().getElementAt(index);
072            }
073    
074            public final void addMatrix(Matrix m) {
075                    if (m == null) {
076                            throw new RuntimeException("tried to add null Matrix");
077                    }
078    
079                    if (getSize() == null) {
080                            setSize(Coordinates.copyOf(m.getSize()));
081                    }
082    
083                    getMatrixList().add(m);
084            }
085    
086            public final void clear() {
087                    if (getMatrixList() != null) {
088                            getMatrixList().clear();
089                    }
090            }
091    
092            public final long getRowCount() {
093                    return getSize()[ROW];
094            }
095    
096            public final long getColumnCount() {
097                    return getSize()[COLUMN];
098            }
099    
100            public final GUIObject getGUIObject() {
101                    if (guiObject == null) {
102                            try {
103                                    Class<?> c = Class.forName("org.jdmp.gui.variable.VariableGUIObject");
104                                    Constructor<?> con = c.getConstructor(new Class<?>[] { Variable.class });
105                                    guiObject = (GUIObject) con.newInstance(new Object[] { this });
106                            } catch (Exception e) {
107                                    logger.log(Level.WARNING, "cannot create variable gui object", e);
108                            }
109                    }
110                    return guiObject;
111            }
112    
113            @Override
114            public final String toString() {
115                    if (getLabel() == null) {
116                            return getClass().getSimpleName();
117                    } else {
118                            return getClass().getSimpleName() + " [" + getLabel() + "]";
119                    }
120            }
121    
122            public void notifyGUIObject() {
123                    if (guiObject != null) {
124                            guiObject.fireValueChanged();
125                    }
126            }
127    
128            public abstract Variable clone();
129    
130    }