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.matrix;
025    
026    import java.awt.Component;
027    
028    import javax.swing.JLabel;
029    import javax.swing.JTable;
030    import javax.swing.border.Border;
031    import javax.swing.border.EmptyBorder;
032    import javax.swing.table.DefaultTableCellRenderer;
033    import javax.swing.table.TableCellRenderer;
034    
035    import org.ujmp.core.Matrix;
036    import org.ujmp.core.util.StringUtil;
037    import org.ujmp.gui.MatrixGUIObject;
038    import org.ujmp.gui.plot.MatrixPlot;
039    import org.ujmp.gui.renderer.MatrixRenderer;
040    
041    public class MatrixTableCellRenderer implements TableCellRenderer {
042            private static final long serialVersionUID = 4107015466785369684L;
043    
044            protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
045    
046            private final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
047    
048            private final MatrixRenderer matrixRenderer = new MatrixRenderer();
049    
050            private final MatrixPlot matrixPlot = new MatrixPlot();
051    
052            private MatrixGUIObject matrix = null;
053    
054            private Object o = null;
055    
056            private JLabel l = null;
057    
058            public MatrixTableCellRenderer() {
059                    matrixPlot.getPlotSettings().setShowXAxis(false);
060                    matrixPlot.getPlotSettings().setShowYAxis(false);
061                    matrixPlot.getPlotSettings().setShowXGrid(false);
062                    matrixPlot.getPlotSettings().setShowYGrid(false);
063                    matrixPlot.getPlotSettings().setTimeLimit(10);
064            }
065    
066            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
067                            boolean hasFocus, int row, int column) {
068    
069                    l = null;
070    
071                    if (value instanceof Matrix) {
072                            matrix = (MatrixGUIObject) ((Matrix) value).getGUIObject();
073                    } else {
074                            matrix = null;
075                    }
076    
077                    if (matrix != null) {
078                            switch (column) {
079                            case MatrixListTableModel.INDEXCOLUMN:
080                                    o = row;
081                                    break;
082                            case MatrixListTableModel.MATRIXCOLUMN:
083                                    return matrixRenderer.getTableCellRendererComponent(table, matrix, isSelected,
084                                                    hasFocus, row, column);
085                            case MatrixListTableModel.MATRIXPLOTCOLUMN:
086                                    return matrixPlot.getTableCellRendererComponent(table, matrix, isSelected,
087                                                    hasFocus, row, column);
088                            case MatrixListTableModel.SIZECOLUMN:
089                                    o = "" + matrix.getColumnCount() + "x" + matrix.getRowCount();
090                                    break;
091                            case MatrixListTableModel.LABELCOLUMN:
092                                    o = StringUtil.format(matrix.getLabel());
093                                    break;
094                            default:
095                                    o = null;
096                                    break;
097                            }
098    
099                            l = (JLabel) renderer.getTableCellRendererComponent(table, o, isSelected, hasFocus,
100                                            row, column);
101                            l.setIcon(null);
102                            l.setHorizontalAlignment(JLabel.CENTER);
103                    }
104    
105                    return l;
106            }
107    
108    }