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

net.alantea.swing.pageelements.GUIElementPage Maven / Gradle / Ivy

package net.alantea.swing.pageelements;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JPanel;

import net.alantea.swing.pageelements.access.ContainerAccess;
import net.alantea.swing.pageelements.access.FieldAccess;
import net.alantea.swing.pageelements.access.GUIDataAccessParser;
import net.alantea.swing.pageelements.access.PanelAccess;
import net.alantea.utils.exception.LntException;

/**
 * The Class GliderPage.
 */
public class GUIElementPage extends GUIBasePage
{
   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = 1L;

   /**
    * Instantiates a new glider page.
    *
    * @param parent the parent
    * @param element the element
    * @param reference the reference
    */
   public GUIElementPage(JPanel parent, Object element, String reference)
   {
      this(parent, element, reference, generateGUIElements(element));
   }

   /**
    * Instantiates a new glider page.
    *
    * @param parent the parent
    * @param element the element
    * @param reference the reference
    * @param guiElements the gui elements
    */
   public GUIElementPage(JPanel parent, Object element, String reference, List guiElements)
   {
      super(parent, element, guiElements);
   }
   
   protected static List generateGUIElements(Object element)
   {
      List list = new LinkedList<>();
      List sublist = instrumentElementFields(element.getClass(), element);
      list.addAll(sublist);
      sublist = instrumentElementContainers(element.getClass(), element);
      list.addAll(sublist);
      sublist = instrumentElementPanels(element.getClass(), element);
      list.addAll(sublist);
      return list;
   }

   /**
    * Instrument element.
    *
    * @param theClass the the class
    * @param element the element
    * @return the list
    */
   protected static List instrumentElementFields(Class theClass, Object element)
   {
      List list = new LinkedList<>();
      if (theClass != Object.class)
      {
         List sublist = instrumentElementFields(theClass.getSuperclass(), element);
         list.addAll(sublist);
      }

      List fieldAccesses = GUIDataAccessParser.getFields(theClass.getName());
      if (fieldAccesses != null)
      {
         for (FieldAccess fieldAccess : fieldAccesses)
         {
            try
            {
               Field field = fieldAccess.getField();
               if (field == null)
               {
                  field = theClass.getDeclaredField(fieldAccess.getName());
                  fieldAccess.setField(field);
                  if (field != null)
                  {
                     field.setAccessible(true);
                  }
               }
               if (field != null)
               {
                  GUIElement elt = createFieldInformation(fieldAccess, element);
                  if ("true".equals(fieldAccess.getAttribute("override")))
                  {
                     for (GUIElement el : list)
                     {
                        if (el.getKey().equals(fieldAccess.getKey()))
                        {
                           list.remove(el);
                           break;
                        }
                     }
                  }
                  if (elt != null)
                  {
                     list.add(elt);
                  }
               }
            }
            catch (NoSuchFieldException | SecurityException e)
            {
               new LntException("Error instrumenting field", e);
            }
         }
      }
      return list;
   }

   /**
    * Instrument element.
    *
    * @param theClass the the class
    * @param element the element
    * @return the list
    */
   protected static List instrumentElementContainers(Class theClass, Object element)
   {
      List list = new LinkedList<>();
      if (theClass != Object.class)
      {
         List sublist = instrumentElementContainers(theClass.getSuperclass(), element);
         list.addAll(sublist);
      }

      List actionAccesses = GUIDataAccessParser.getContainers(theClass.getName());
      if (actionAccesses != null)
      {
         for (ContainerAccess actionAccess : actionAccesses)
         {
            GUIElement elt = createContainerInformation(actionAccess, element);
            if (elt != null)
            {
               list.add(elt);
            }
         }
      }
      return list;
   }

   /**
    * Instrument element.
    *
    * @param theClass the the class
    * @param element the element
    * @return the list
    */
   protected static List instrumentElementPanels(Class theClass, Object element)
   {
      List list = new LinkedList<>();
      if (theClass != Object.class)
      {
         List sublist = instrumentElementPanels(theClass.getSuperclass(), element);
         list.addAll(sublist);
      }

      List panelAccesses = GUIDataAccessParser.getPanels(theClass.getName());
      if (panelAccesses != null)
      {
         for (PanelAccess panelAccess : panelAccesses)
         {
            GUIElement elt = createPanelInformation(panelAccess, element);
            if (elt != null)
            {
               list.add(elt);
            }
         }
      }
      return list;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy