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.variable;
025    
026    import java.awt.Component;
027    import java.awt.image.BufferedImage;
028    
029    import javax.swing.Icon;
030    import javax.swing.ImageIcon;
031    import javax.swing.JTable;
032    import javax.swing.table.AbstractTableModel;
033    import javax.swing.table.DefaultTableCellRenderer;
034    import javax.swing.table.TableModel;
035    
036    import org.jdmp.core.variable.Variable;
037    import org.ujmp.gui.AbstractGUIObject;
038    
039    public class VariableGUIObject extends AbstractGUIObject {
040            private static final long serialVersionUID = 9145766876402222560L;
041    
042            private Variable variable = null;
043    
044            public VariableGUIObject(Variable v) {
045                    this.variable = v;
046            }
047    
048            public void clear() {
049                    variable.clear();
050            }
051    
052            public Variable getVariable() {
053                    return variable;
054            }
055    
056            public final Icon getIcon() {
057                    try {
058                            TableModel dataModel = new AbstractTableModel() {
059                                    private static final long serialVersionUID = 5562866897873790623L;
060    
061                                    public int getColumnCount() {
062                                            return 1;
063                                    }
064    
065                                    public int getRowCount() {
066                                            return 1;
067                                    }
068    
069                                    public Object getValueAt(int row, int col) {
070                                            return getVariable().getMatrix();
071                                    }
072                            };
073                            JTable table = new JTable(dataModel);
074                            table.getColumnModel().getColumn(0).setWidth(32);
075                            table.setRowHeight(32);
076    
077                            int WIDTH = table.getColumnModel().getColumn(0).getWidth() - 1;
078                            int HEIGHT = table.getRowHeight(0) - 1;
079    
080                            Class<?> cl = Class.forName("org.jdmp.gui.matrix.MatrixRenderer");
081                            DefaultTableCellRenderer mr = (DefaultTableCellRenderer) cl.newInstance();
082                            Component c = mr.getTableCellRendererComponent(table, getVariable().getMatrix(), false,
083                                            false, 0, 0);
084                            BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
085                            c.paint(bi.getGraphics());
086                            return new ImageIcon(bi);
087    
088                    } catch (Exception e) {
089                            return new ImageIcon("resources/icons/rebuild.png");
090                    }
091            }
092    
093            public String getLabel() {
094                    return variable.getLabel();
095            }
096    
097            public void setLabel(String label) {
098                    variable.setLabel(label);
099            }
100    
101            public String getDescription() {
102                    return variable.getDescription();
103            }
104    
105            public void setDescription(String description) {
106                    variable.setDescription(description);
107            }
108    
109            @Override
110            public String toString() {
111                    return variable.toString();
112            }
113    
114    }