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

net.alantea.swing.pageelements.elements.StringElement Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.pageelements.elements;

import java.lang.reflect.Field;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.text.JTextComponent;

import net.alantea.horizon.message.Messenger;
import net.alantea.liteprops.StringProperty;
import net.alantea.swing.judith.EditorPanel;
import net.alantea.swing.pageelements.GUIFieldAnnotation;
import net.alantea.swing.pageelements.GUIFieldElement;
import net.alantea.swing.text.InstrumentedTextArea;
import net.alantea.swing.text.TextField;
import net.alantea.swing.text.TextField.Validator;

@GUIFieldAnnotation(value="STRING", height=32, defaultFor="java.lang.String")
@GUIFieldAnnotation(value="TEXTAREA", height=96)
@GUIFieldAnnotation(value="TEXTAREAFILL", height=-2)
@GUIFieldAnnotation(value="INTEGER", height=32, defaultFor="java.lang.Integer")
@GUIFieldAnnotation(value="DOUBLE", height=32, defaultFor="java.lang.Double")
public class StringElement extends GUIFieldElement
{
   private JComponent textfield;
   private boolean avoidCollisions = false;
   
   StringElement(String reference, String type, Field field, Object target,
         String key, boolean editable, String page, int height, int order, Map attrs)
   {
      super(type, field, target, key, editable, page, height, order, attrs);
      
      StringProperty prop = null;
      if ("TEXTAREA".equals(type))
      {
         textfield = new InstrumentedTextArea();
         textfield.setEnabled(editable);
         prop = ((InstrumentedTextArea) textfield).getStringProperty();
      }
      else if ("STRING".equals(type))
      {
         textfield = new TextField();
         ((JTextComponent) textfield).setEditable(editable);
         prop = ((TextField) textfield).getTextProperty();
      }
      else if ("DOUBLE".equals(type))
      {
         textfield = new TextField();
         ((JTextComponent) textfield).setEditable(editable);
         ((TextField) textfield).setValidator(Validator.FLOAT);
         prop = ((TextField) textfield).getTextProperty();
      }
      else if ("INTEGER".equals(type))
      {
         textfield = new TextField();
         ((JTextComponent) textfield).setEditable(editable);
         ((TextField) textfield).setValidator(Validator.NUMERIC);
         prop = ((TextField) textfield).getTextProperty();
      }
      else
      {
         textfield = new EditorPanel();
         prop = ((EditorPanel) textfield).getStringProperty();
      }
      
      try
      {
         if (field.getType().getName().equals(StringProperty.class.getName()))
         {
            StringProperty prop1 = (StringProperty) field.get(target);
            String val = prop1.get();
            if (textfield instanceof TextField)
            {
               ((TextField)textfield).setText(val);
            }
            else if (textfield instanceof InstrumentedTextArea)
            {
               ((InstrumentedTextArea)textfield).setText(val);
            }
            else
            {
               ((EditorPanel)textfield).setText(val);
            }
            if (prop != null)
            {
               prop.addListener((o, v) -> {
                  if (!avoidCollisions)
                  {
                     if (textfield instanceof TextField)
                     {
                        if (!v.equals(((TextField)textfield).getText() ))
                        {
                           ((TextField)textfield).setText(v);
                        }
                     }
                     else if (textfield instanceof InstrumentedTextArea)
                     {
                        if (!v.equals(((InstrumentedTextArea)textfield).getText() ))
                        {
                           ((InstrumentedTextArea)textfield).setText(v);
                        }
                     }
                     else
                     {
                        if (!v.equals(((EditorPanel)textfield).getText() ))
                        {
                           ((EditorPanel)textfield).setText(v);
                        }
                     }
                  }
                  avoidCollisions = false;
               });
            }
         }
         else if (field.getType().getName().equals(String.class.getName()))
         {
            if (textfield instanceof TextField)
            {
               ((TextField)textfield).setText((String) field.get(target));
            }
            else if (textfield instanceof InstrumentedTextArea)
            {
               ((InstrumentedTextArea)textfield).setText((String) field.get(target));
            }
            else
            {
               ((EditorPanel)textfield).setText((String) field.get(target));
            }
         }
         else if (field.getType().getName().equals(Double.class.getName()))
         {
            ((TextField)textfield).setText("" + (Double) field.get(target));
         }
         else if (field.getType().getName().equals(Integer.class.getName()))
         {
            ((TextField)textfield).setText("" + (Integer) field.get(target));
         }
      }
      catch (IllegalArgumentException | IllegalAccessException e)
      {
         e.printStackTrace();
      }
      
      if (prop != null)
      {
         prop.addListener((o, v) -> {
            try
            {
               String old = "";
               if (field.getType().getName().equals(StringProperty.class.getName()))
               {
                  StringProperty prop1 = (StringProperty) field.get(target);
                  old = prop1.get();
                  if ((old == null) || (!old.equals(v)))
                  {
                     avoidCollisions = true;
                     prop1.set(v);
                  }
               }
               else if (field.getType().getName().equals(String.class.getName()))
               {
                  old = (String) field.get(target);
                  field.set(target, v);
               }
               else if (field.getType().getName().equals(Double.class.getName()))
               {
                  if ((v == null) || (v.isEmpty()))
                  {
                     field.set(target, 0.0);
                  }
                  else
                  {
                     field.set(target, Double.parseDouble(v));
                  }
               }
               else if (field.getType().getName().equals(Integer.class.getName()))
               {
                  if ((v == null) || (v.isEmpty()))
                  {
                     field.set(target, 0);
                  }
                  else
                  {
                     field.set(target, new Integer(Integer.parseInt(v)));
                  }
               }
               if ((old == null) || (!old.equals(v)))
               {
                  Messenger.sendMessage(target, field.getName(), v);
               }
            }
            catch (IllegalArgumentException | IllegalAccessException e)
            {
               e.printStackTrace();
            }
         });
      }
   }

   @Override
   protected JComponent getSpecificGUIComponent()
   {
      return textfield;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy