source: src/main/java/weka/gui/sql/ResultSetTableCellRenderer.java @ 14

Last change on this file since 14 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.8 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ResultSetTableCellRenderer.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.sql;
24
25import java.awt.Color;
26import java.awt.Component;
27
28import javax.swing.JTable;
29import javax.swing.SwingConstants;
30import javax.swing.UIManager;
31import javax.swing.table.DefaultTableCellRenderer;
32
33/**
34 * Handles the background colors for missing values differently than the
35 * DefaultTableCellRenderer.
36 *
37 * @author     FracPete (fracpete at waikato dot ac dot nz)
38 * @version    $Revision: 1.2 $
39 */
40public class ResultSetTableCellRenderer
41  extends DefaultTableCellRenderer {
42
43  /** for serialization */
44  private static final long serialVersionUID = -8106963669703497351L;
45 
46  // the color for missing values
47  private Color           missingColor;
48  private Color           missingColorSelected;
49
50  /**
51   * initializes the Renderer with a standard color
52   */
53  public ResultSetTableCellRenderer() {
54    this( new Color(223, 223, 223), 
55          new Color(192, 192, 192) );
56  }
57
58  /**
59   * initializes the Renderer with the given colors
60   */
61  public ResultSetTableCellRenderer( Color missingColor, 
62                                     Color missingColorSelected ) {
63
64    super();
65
66    this.missingColor           = missingColor;
67    this.missingColorSelected   = missingColorSelected;
68  }
69
70  /**
71   * Returns the default table cell renderer.
72   * stuff for the header is taken from <a href="http://www.chka.de/swing/table/faq.html">here</a>
73   */
74  public Component getTableCellRendererComponent(
75      JTable table, Object value, boolean isSelected, 
76      boolean hasFocus, int row, int column ) {
77
78    ResultSetTableModel       model;
79    Component                 result;
80    boolean                   found;
81
82    result = super.getTableCellRendererComponent(
83        table, value, isSelected, hasFocus, row, column);
84
85    if (table.getModel() instanceof ResultSetTableModel) {
86      model = (ResultSetTableModel) table.getModel();
87      // normal cell
88      if (row >= 0) {
89        if (model.isNullAt(row, column)) {
90          setToolTipText("NULL");
91          if (isSelected)
92            result.setBackground(missingColorSelected);
93          else
94            result.setBackground(missingColor);
95        }
96        else {
97          setToolTipText(null);
98          if (isSelected)
99            result.setBackground(table.getSelectionBackground());
100          else
101            result.setBackground(Color.WHITE);
102        }
103
104        // alignment
105        if (model.isNumericAt(column))
106          setHorizontalAlignment(SwingConstants.RIGHT);
107        else
108          setHorizontalAlignment(SwingConstants.LEFT);
109      }
110      // header
111      else {
112        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
113        setHorizontalAlignment(SwingConstants.CENTER);
114        if (table.getColumnModel().getSelectionModel().isSelectedIndex(column))
115          result.setBackground(UIManager.getColor("TableHeader.background").darker());
116        else
117          result.setBackground(UIManager.getColor("TableHeader.background"));
118      }
119    }
120
121    return result;
122  }
123}
124
Note: See TracBrowser for help on using the repository browser.