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.sample;
025    
026    import java.awt.Color;
027    import java.awt.Component;
028    import java.util.Map;
029    
030    import javax.swing.JLabel;
031    import javax.swing.JTable;
032    import javax.swing.UIManager;
033    import javax.swing.border.Border;
034    import javax.swing.border.EmptyBorder;
035    import javax.swing.table.DefaultTableCellRenderer;
036    import javax.swing.table.TableCellRenderer;
037    
038    import org.jdmp.core.sample.Sample;
039    import org.jdmp.core.util.ObservableMap;
040    import org.jdmp.core.variable.Variable;
041    import org.ujmp.gui.renderer.MatrixRenderer;
042    
043    public class SampleTableCellRenderer implements TableCellRenderer {
044    
045            private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
046    
047            private final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
048    
049            private final MatrixRenderer matrixRenderer = new MatrixRenderer();
050    
051            private Map<Integer, String> columnMap = null;
052    
053            private Sample sample = null;
054    
055            private Object o = null;
056    
057            private JLabel c = null;
058    
059            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
060                            boolean hasFocus, int row, int column) {
061    
062                    columnMap = ((SampleListTableModel) table.getModel()).getColumnMap();
063    
064                    c = null;
065    
066                    if (value instanceof Sample) {
067                            sample = (Sample) value;
068    
069                    } else {
070                            sample = null;
071                    }
072    
073                    if (sample != null) {
074    
075                            switch (column) {
076                            case SampleListTableModel.ICONCOLUMN:
077                                    o = "";
078                                    break;
079                            default:
080                                    Object key = columnMap.get(column - 1);
081                                    ObservableMap<Variable> variables = sample.getVariables();
082                                    Variable v = variables.get(key);
083                                    if (v == null) {
084                                            o = "";
085                                            break;
086                                    } else {
087                                            return matrixRenderer.getTableCellRendererComponent(table, v.getMatrix(),
088                                                            isSelected, hasFocus, row, column);
089                                    }
090                            }
091    
092                            c = (JLabel) renderer.getTableCellRendererComponent(table, o, isSelected, hasFocus,
093                                            row, column);
094    
095                            switch (column) {
096                            case SampleListTableModel.ICONCOLUMN:
097                                    c.setIcon(UIManager.getIcon("Sample.icon"));
098                                    break;
099                            default:
100                                    c.setIcon(null);
101                                    break;
102                            }
103    
104                            c.setHorizontalAlignment(JLabel.CENTER);
105    
106                    } else {
107    
108                            c = (JLabel) renderer.getTableCellRendererComponent(table, o, isSelected, hasFocus,
109                                            row, column);
110    
111                            if (hasFocus) {
112                                    Border border = null;
113                                    if (isSelected) {
114                                            border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
115                                    }
116                                    if (border == null) {
117                                            border = UIManager.getBorder("Table.focusCellHighlightBorder");
118                                    }
119                                    c.setBorder(border);
120    
121                                    if (!isSelected && table.isCellEditable(row, column)) {
122                                            Color col;
123                                            col = UIManager.getColor("Table.focusCellForeground");
124                                            if (col != null) {
125                                                    c.setForeground(col);
126                                            }
127                                            col = UIManager.getColor("Table.focusCellBackground");
128                                            if (col != null) {
129                                                    c.setBackground(col);
130                                            }
131                                    }
132                            } else {
133                                    c.setBorder(DEFAULT_NO_FOCUS_BORDER);
134                            }
135    
136                    }
137    
138                    return c;
139            }
140    }