PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/renderer/TableCellRendererForProducts.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 82 lines | 45 code | 9 blank | 28 comment | 3 complexity | 2629aa225de4649be5586a832c31cb31 MD5 | raw file
 1/*
 2 * 
 3This file is part of YaBS.
 4
 5YaBS is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10YaBS is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
17
18 * 
19 */
20package mpv5.utils.renderer;
21
22import java.awt.Color;
23import java.awt.Component;
24import javax.swing.JFormattedTextField;
25import javax.swing.JTable;
26import javax.swing.table.DefaultTableCellRenderer;
27import javax.swing.table.TableColumn;
28import mpv5.db.objects.Product;
29
30public class TableCellRendererForProducts extends DefaultTableCellRenderer {
31
32    private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
33    private final JTable t;
34
35    /**
36     * 
37     * @param t
38     * @param color
39     */
40    public TableCellRendererForProducts(JTable t) {
41        super();
42        this.t = t;
43    }
44
45    /**
46     * Set this renderer to the given column + editor
47     * @param column
48     */
49    public void setRendererTo(int column) {
50
51        TableColumn col = t.getColumnModel().getColumn(t.getColumnModel().getColumnIndex(t.getModel().getColumnName(column)));
52        col.setCellEditor(new TableCellEditorForDezimal(new JFormattedTextField()));
53        col.setCellRenderer(this);
54    }
55
56
57    @Override
58    protected void setValue(Object value) {
59        if (value instanceof Product) {
60            value = ((Product)value).__getCnumber();
61        }
62        super.setValue(value);
63    }
64
65    @Override
66    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
67        if (value instanceof Product) {
68            value = ((Product)value).__getCnumber();
69        }
70        adaptee.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
71        setForeground(adaptee.getForeground());
72        setBackground(adaptee.getBackground());
73        setBorder(adaptee.getBorder());
74        setFont(adaptee.getFont());
75        setText(adaptee.getText());
76        if (hasFocus) {
77            setBackground(Color.BLUE);
78            setForeground(Color.WHITE);
79        }
80        return this;
81    }
82}