[29] | 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 | * PropertyText.java |
---|
| 19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui; |
---|
| 24 | |
---|
| 25 | import java.awt.event.FocusAdapter; |
---|
| 26 | import java.awt.event.FocusEvent; |
---|
| 27 | import java.awt.event.KeyAdapter; |
---|
| 28 | import java.awt.event.KeyEvent; |
---|
| 29 | import java.beans.PropertyEditor; |
---|
| 30 | |
---|
| 31 | import javax.swing.JTextField; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Support for a PropertyEditor that uses text. |
---|
| 35 | * Isn't going to work well if the property gets changed |
---|
| 36 | * somewhere other than this field simultaneously |
---|
| 37 | * |
---|
| 38 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
| 39 | * @version $Revision: 1.8 $ |
---|
| 40 | */ |
---|
| 41 | class PropertyText |
---|
| 42 | extends JTextField { |
---|
| 43 | |
---|
| 44 | /** for serialization */ |
---|
| 45 | private static final long serialVersionUID = -3915342928825822730L; |
---|
| 46 | |
---|
| 47 | /** The property editor */ |
---|
| 48 | private PropertyEditor m_Editor; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Sets up the editing component with the supplied editor. |
---|
| 52 | * |
---|
| 53 | * @param pe the PropertyEditor |
---|
| 54 | */ |
---|
| 55 | PropertyText(PropertyEditor pe) { |
---|
| 56 | |
---|
| 57 | //super(pe.getAsText()); |
---|
| 58 | super((pe.getAsText().equals("null"))?"":pe.getAsText()); |
---|
| 59 | m_Editor = pe; |
---|
| 60 | |
---|
| 61 | /* m_Editor.addPropertyChangeListener(new PropertyChangeListener() { |
---|
| 62 | public void propertyChange(PropertyChangeEvent evt) { |
---|
| 63 | updateUs(); |
---|
| 64 | } |
---|
| 65 | }); */ |
---|
| 66 | addKeyListener(new KeyAdapter() { |
---|
| 67 | public void keyReleased(KeyEvent e) { |
---|
| 68 | // if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
---|
| 69 | updateEditor(); |
---|
| 70 | // } |
---|
| 71 | } |
---|
| 72 | }); |
---|
| 73 | addFocusListener(new FocusAdapter() { |
---|
| 74 | public void focusLost(FocusEvent e) { |
---|
| 75 | updateEditor(); |
---|
| 76 | } |
---|
| 77 | }); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Attempts to update the textfield value from the editor. |
---|
| 82 | */ |
---|
| 83 | protected void updateUs() { |
---|
| 84 | try { |
---|
| 85 | setText(m_Editor.getAsText()); |
---|
| 86 | } catch (IllegalArgumentException ex) { |
---|
| 87 | // Quietly ignore. |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Attempts to update the editor value from the textfield. |
---|
| 93 | */ |
---|
| 94 | protected void updateEditor() { |
---|
| 95 | try { |
---|
| 96 | m_Editor.setAsText(getText()); |
---|
| 97 | } catch (IllegalArgumentException ex) { |
---|
| 98 | // Quietly ignore. |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|