001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP 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 * UJMP 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 UJMP; 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.ujmp.gui.renderer; 025 026 import java.awt.Color; 027 import java.awt.Component; 028 import java.util.ConcurrentModificationException; 029 030 import javax.swing.BorderFactory; 031 import javax.swing.JLabel; 032 import javax.swing.JTable; 033 import javax.swing.border.Border; 034 import javax.swing.table.DefaultTableCellRenderer; 035 036 import org.ujmp.core.util.UJMPFormat; 037 import org.ujmp.gui.MatrixGUIObject; 038 import org.ujmp.gui.util.ColorUtil; 039 import org.ujmp.gui.util.TooltipUtil; 040 041 public class MatrixValueTableCellRenderer extends DefaultTableCellRenderer { 042 private static final long serialVersionUID = -1473046176750819621L; 043 044 private static final Color SELECTCOLOR = new Color(200, 200, 255); 045 046 private final Border border = BorderFactory.createLineBorder(Color.blue, 2); 047 048 public Component getTableCellRendererComponent(JTable table, Object value, 049 boolean isSelected, boolean hasFocus, int row, int column) { 050 JLabel label = (JLabel) super.getTableCellRendererComponent(table, 051 value, isSelected, hasFocus, row, column); 052 label.setHorizontalAlignment(JLabel.CENTER); 053 054 MatrixGUIObject m = (MatrixGUIObject) table.getModel(); 055 056 Color c = ColorUtil.fromObject(value); 057 058 try { 059 setToolTipText(TooltipUtil.getTooltip(m, row, column)); 060 } catch (ConcurrentModificationException e) { 061 // not too bad 062 } 063 064 int width = table.getColumnModel().getColumn(column).getWidth(); 065 if (width < 25) { 066 label.setText(""); 067 } else { 068 String s = UJMPFormat.getSingleLineInstance().format(value); 069 if (s != null && s.length() > 100) { 070 s = s.substring(0, 100) + "..."; 071 } 072 label.setText(s); 073 } 074 label.setForeground(ColorUtil.contrastBW(c)); 075 label.setBackground(c); 076 if (isSelected) { 077 label.setBorder(border); 078 // label.setBackground(SELECTCOLOR); 079 } else { 080 label.setBorder(null); 081 } 082 083 return label; 084 } 085 086 }