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.Color;
027    import java.awt.Component;
028    import java.awt.Graphics;
029    import java.awt.Graphics2D;
030    import java.awt.Image;
031    import java.awt.image.BufferedImage;
032    import java.awt.image.ImageObserver;
033    import java.util.List;
034    
035    import javax.swing.JPanel;
036    import javax.swing.JTable;
037    import javax.swing.UIManager;
038    import javax.swing.border.Border;
039    import javax.swing.border.EmptyBorder;
040    import javax.swing.table.TableCellRenderer;
041    
042    import org.jdmp.core.variable.Variable;
043    import org.ujmp.gui.renderer.MatrixRenderer;
044    import org.ujmp.gui.util.UIDefaults;
045    
046    public class VariableListTableCellRenderer extends JPanel implements TableCellRenderer {
047            private static final long serialVersionUID = 6410777671579311509L;
048    
049            private final BufferedImage bufferedImage = null;
050    
051            private final VariableListImageObserver variableListImageObserver = new VariableListImageObserver();
052    
053            protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
054    
055            private List variableList = null;
056    
057            private final MatrixRenderer matrixRenderer = new MatrixRenderer();
058    
059            private static int PADDINGX = UIManager.getInt("Table.paddingX");
060    
061            private static int PADDINGY = UIManager.getInt("Table.paddingY");
062    
063            private int width = 0;
064    
065            private int height = 0;
066    
067            private boolean isSelected;
068    
069            private boolean hasFocus;
070    
071            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
072                            boolean hasFocus, int row, int column) {
073    
074                    this.isSelected = isSelected;
075                    this.hasFocus = hasFocus;
076    
077                    if (value instanceof List) {
078                            variableList = (List) value;
079                    } else {
080                            variableList = null;
081                    }
082    
083                    width = table.getColumnModel().getColumn(column).getWidth() - (2 * PADDINGX) - 1;
084                    height = table.getRowHeight(row) - (2 * PADDINGY) - 1;
085    
086                    if (isSelected) {
087                            super.setForeground(table.getSelectionForeground());
088                            super.setBackground(table.getSelectionBackground());
089                    } else {
090                            super.setForeground(table.getForeground());
091                            super.setBackground(table.getBackground());
092                    }
093    
094                    setFont(table.getFont());
095    
096                    if (hasFocus) {
097                            Border border = null;
098                            if (isSelected) {
099                                    border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
100                            }
101                            if (border == null) {
102                                    border = UIManager.getBorder("Table.focusCellHighlightBorder");
103                            }
104                            setBorder(border);
105    
106                            if (!isSelected && table.isCellEditable(row, column)) {
107                                    Color col;
108                                    col = UIManager.getColor("Table.focusCellForeground");
109                                    if (col != null) {
110                                            super.setForeground(col);
111                                    }
112                                    col = UIManager.getColor("Table.focusCellBackground");
113                                    if (col != null) {
114                                            super.setBackground(col);
115                                    }
116                            }
117                    } else {
118                            setBorder(noFocusBorder);
119                    }
120    
121                    if (variableList != null && variableList.size() > 0) {
122                            for (Object o : variableList) {
123                                    if (o instanceof Variable) {
124                                            Variable v = (Variable) o;
125    
126                                            this.add(matrixRenderer.getTableCellRendererComponent(table, v.getMatrix(),
127                                                            isSelected, hasFocus, row, column));
128                                    }
129                            }
130                    }
131    
132                    return this;
133            }
134    
135            @Override
136            public void paint(Graphics g) {
137                    super.paint(g);
138                    if (variableList == null) {
139                            Graphics2D g2d = (Graphics2D) g;
140                            if (g2d != null) {
141                                    g2d.addRenderingHints(UIDefaults.AALIAS);
142                                    g2d.setColor(Color.RED);
143                                    g2d.drawLine(PADDINGX, PADDINGY, width - PADDINGX, height - PADDINGY);
144                                    g2d.drawLine(width - PADDINGX, PADDINGY, PADDINGX, height - PADDINGY);
145                                    g2d.dispose();
146                            }
147                    } else {
148                            paintVariableList(g, variableList);
149                    }
150            }
151    
152            public void paintVariableList(Graphics g, List variableList) {
153    
154                    if (variableList == null)
155                            return;
156    
157                    int count = variableList.size();
158    
159                    if (count == 0)
160                            return;
161    
162                    int componentWidth = width / count;
163    
164                    for (Object o : variableList) {
165                            if (o != null && o instanceof Variable) {
166                                    Variable v = (Variable) o;
167                                    MatrixRenderer.paintMatrix(g, v.getMatrix(), componentWidth, height + PADDINGY
168                                                    + PADDINGY);
169                                    g.translate(componentWidth, 0);
170                            }
171                    }
172            }
173    
174    }
175    
176    class VariableListImageObserver implements ImageObserver {
177    
178            public boolean imageUpdate(Image arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
179                    return false;
180            }
181    }