PageRenderTime 76ms CodeModel.GetById 41ms app.highlight 17ms RepoModel.GetById 13ms app.codeStats 0ms

/src/mpv5/utils/renderer/LazyCellEditor.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 76 lines | 47 code | 10 blank | 19 comment | 5 complexity | 5a4219d43acd3e4dd0c7be04eaa1f361 MD5 | raw file
 1/*
 2 *  This file is part of YaBS.
 3 *
 4 *      YaBS is free software: you can redistribute it and/or modify
 5 *      it under the terms of the GNU General Public License as published by
 6 *      the Free Software Foundation, either version 3 of the License, or
 7 *      (at your option) any later version.
 8 *
 9 *      YaBS is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *      You should have received a copy of the GNU General Public License
15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package mpv5.utils.renderer;
18
19import java.awt.event.MouseEvent;
20import java.util.EventObject;
21import javax.swing.DefaultCellEditor;
22import javax.swing.JComboBox;
23import javax.swing.JTextField;
24import javax.swing.SwingUtilities;
25
26/**
27 *
28 */
29public class LazyCellEditor extends DefaultCellEditor {
30
31    public LazyCellEditor(JComboBox c) {
32        super(c);
33    }
34
35    public LazyCellEditor(final JTextField tf) {
36        super(tf);
37        super.setClickCountToStart(1);
38        delegate = new EditorDelegate() {
39
40            boolean isMousePressed = false;
41
42            @Override
43            public void setValue(Object value) {
44                if(isMousePressed && value != null){
45                SwingUtilities.invokeLater(new Runnable() {
46
47                    public void run() {
48                        tf.selectAll();
49                    }
50                });
51                tf.setText(value.toString());
52                } else {
53                tf.setText(""); }
54            }
55
56            @Override
57            public Object getCellEditorValue() {
58                return tf.getText();
59            }
60
61            @Override
62            public boolean isCellEditable(EventObject anEvent) {
63                if (anEvent instanceof MouseEvent) {
64                    isMousePressed = true;
65                    return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
66                }
67                isMousePressed = false;
68                return true;
69            }
70        };
71    }
72
73    public boolean stopCellEditingSilent() {
74        return true;
75    }
76}