All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.vml.widgets.TextFieldHandler Maven / Gradle / Ivy

package net.alantea.swing.vml.widgets;

import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.alantea.horizon.message.Messenger;
import net.alantea.liteprops.Property;
import net.alantea.swing.vml.BaseSwingHandler;
import net.alantea.utils.exception.LntException;
import net.alantea.viewml.VmlException;
import net.alantea.viewml.annotations.VAttribute;
import net.alantea.viewml.annotations.VElement;
import net.alantea.viewml.internal.ChangesLoader;
import net.alantea.viewml.listeners.ChangeListener;

@VElement("textfield")
public class TextFieldHandler extends BaseSwingHandler
{
   private ChangeListener change;
   private String message;
   
   private Property content = new Property<>();

   public TextFieldHandler()
   {
      super(new JTextField());
      init();
   }

   protected TextFieldHandler(JTextField  target)
   {
      super(target);
      init();
   }

   private void init()
   {
      content.addListener((o, n) -> setText(n));
      
      getComponent().addComponentListener(new ComponentAdapter()
      {
         public void componentResized(ComponentEvent e)
         {
            JTextField label = (JTextField)getComponent();
            int compWidth = label.getWidth();
            int compHeight = label.getHeight();
            
            if ((compWidth <= 0) || (compHeight <= 0))
            {
               return;
            }

            // Recalculate the text font
            Font labelFont = label.getFont();
            
            // Set the label's font size to the newly determined size.
            label.setFont(new Font(labelFont.getName(), labelFont.getStyle(), compHeight));
         }

      });
      
      JTextField textField = (JTextField)getComponent();
      textField.getDocument().addDocumentListener(new DocumentListener()
      {
         public void changedUpdate(DocumentEvent e)
         {
            doIt();
         }
         public void removeUpdate(DocumentEvent e)
         {
            doIt();
         }
         public void insertUpdate(DocumentEvent e)
         {
            doIt();
         }

         public void doIt()
         {
            String text = textField.getText();
            content.set(text);
            if (change != null)
            {
               change.changed(null, text);
            }
            if (message != null)
            {
               Messenger.sendMessage(getComponent(), message, text);
            }
         }
       });
   }

   @VAttribute("text")
   private void setText(String text)
   {
      ((JTextField) getComponent()).setText(text);
   }

   @VAttribute("change")
   private void setChange(String text)
   {
      try
      {
         change = ChangesLoader.getChange(text);
      }
      catch (VmlException e)
      {
         new LntException("Error loading change", e);
      }
   }

   @VAttribute("message")
   private void setMessage(String text)
   {
      this.message = text;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy