PropertyText.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
Category:

Windows Develop

Development Platform:

Java

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