PageRenderTime 82ms CodeModel.GetById 35ms app.highlight 8ms RepoModel.GetById 37ms app.codeStats 0ms

/src/mpv5/utils/renderer/TableCellEditorForDate.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 99 lines | 76 code | 15 blank | 8 comment | 6 complexity | dbccb352550a0c5045bb87fa266740ca MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5package mpv5.utils.renderer;
 6
 7import java.awt.event.MouseEvent;
 8import java.util.Date;
 9import java.util.EventObject;
10import javax.swing.DefaultCellEditor;
11import javax.swing.JTable;
12import javax.swing.JTextField;
13import javax.swing.SwingConstants;
14import javax.swing.SwingUtilities;
15import javax.swing.event.DocumentListener;
16import mpv5.ui.dialogs.MiniCalendarDialog;
17import mpv5.utils.models.MPTableModel;
18
19/**
20 *
21 * @author Jan Hahnisch
22 */
23public class TableCellEditorForDate extends DefaultCellEditor {
24
25    private static final long serialVersionUID = 1L;
26    private final JTable tbl;
27    private static JTextField tf = new JTextField();
28
29    public TableCellEditorForDate(JTable tbl) {
30        super(tf);
31        this.tbl = tbl;
32    }
33
34    @Override
35    public boolean isCellEditable(EventObject anEvent) {
36        boolean isEditable = super.isCellEditable(anEvent);
37        if (isEditable && anEvent instanceof MouseEvent) {
38            delegate = new DatePickerDelegate(this, tf);
39        } else {
40            return false;
41        }
42        return isEditable;
43    }
44
45    public void setEditorTo(int i) {
46        setClickCountToStart(1);
47        tbl.getColumnModel().getColumn(i).setCellEditor(this);
48    }
49
50    class DatePickerDelegate extends EditorDelegate {
51
52        private static final long serialVersionUID = 1L;
53        TableCellEditorForDate cellEditor;
54        MiniCalendarDialog datePicker;
55        JTextField textField;
56
57        DatePickerDelegate(final TableCellEditorForDate cellEditor, JTextField tf) {
58            this.cellEditor = cellEditor;
59            this.textField = tf;
60            textField.getDocument().addDocumentListener(new DocumentListener() {
61
62                public void insertUpdate(javax.swing.event.DocumentEvent e) {
63                    fireStopped();
64                }
65
66                public void removeUpdate(javax.swing.event.DocumentEvent e) {
67                    fireStopped();
68                }
69
70                public void changedUpdate(javax.swing.event.DocumentEvent e) {
71                    fireStopped();
72                }
73            });
74            datePicker = new MiniCalendarDialog(textField, true);
75            datePicker.setLocation(tbl.getLocationOnScreen());
76        }
77
78        @Override
79        public void setValue(Object value) {
80            if (value == null) {
81                datePicker.setDate(new Date());
82            } else {
83                datePicker.setDate((Date) value);
84            }
85            datePicker.setDate((Date) value);
86            datePicker.setVisible(true);
87        }
88
89        @Override
90        public Object getCellEditorValue() {
91            return datePicker.getDate();
92        }
93
94        public void fireStopped() {
95            this.cellEditor.stopCellEditing();
96            fireEditingStopped();    // Make the renderer reappear
97        }
98    }
99}