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

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

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

import java.awt.Component;
import java.awt.FlowLayout;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdatepicker.DateModel;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

import net.alantea.liteprops.Property;
import net.alantea.swing.misc.TimeStamp;
import net.alantea.swing.pageelements.GUIFieldAnnotation;
import net.alantea.swing.pageelements.GUIFieldElement;
import net.alantea.utils.MultiMessages;
import net.alantea.utils.exception.LntException;

@GUIFieldAnnotation(value="DATE", height=40, defaultFor="java.util.Date")
public class DateElement extends GUIFieldElement
{
   private JDatePickerImpl datepicker;
   JPanel fieldPanel;
   private JComboBox hourBox;
   private JComboBox minuteBox;
   
   private Property dateProperty = new Property<>(new Date());
   private JLabel rolabel;
   
   @SuppressWarnings("deprecation")
   DateElement(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);
      
      try
      {
         Object object = field.get(target);
         if ((object != null) &&(object instanceof Property))
         {
            object = ((Property)object).get();
         }
         Date d = null;
         if (object instanceof TimeStamp)
         {
            d = ((TimeStamp)object).getDate();
         }
         else if (object instanceof Date)
         {
            d = (Date)object;
         }
         else if (object instanceof Property)
         {
            Object content = ((Property)object).get();
            if (content != null)
            {
               if (content instanceof Date)
               {
                  d = ((Date)content);
               }
            }
         }
         dateProperty.set(d);
      }
      catch (IllegalArgumentException | IllegalAccessException e2)
      {
         new LntException("Illegal date value", e2);
      }
      
      dateProperty.addListener((old, date) -> {
         try
         {
            Object object = field.get(target);
            if (object instanceof TimeStamp)
            {
               ((TimeStamp) field.get(target)).setDate(date);
            }
            else if (object instanceof Date)
            {
               ((Date)field.get(target)).setTime(date.getTime());
            }
            else if (object instanceof Property)
            {
               Object content = ((Property) object).get();
               if (content instanceof Date)
               {
                  ((Date)content).setTime(date.getTime());
               }
               else if (content instanceof TimeStamp)
               {
                  ((TimeStamp)content).setDate(date);
               }
            }
         }
         catch (IllegalArgumentException | IllegalAccessException e1)
         {
            new LntException("Illegal date change", e1);
         }
      });
      
      if (editable)
      {
         UtilDateModel model = new UtilDateModel();
         Properties p = new Properties();
         p.put("text.today", MultiMessages.get(getKey()+ ".today"));
         p.put("text.month", MultiMessages.get(getKey()+ ".month"));
         p.put("text.year", MultiMessages.get(getKey()+ ".year"));
         JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
         datepicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
         datepicker.setEnabled(editable);
         @SuppressWarnings("unchecked")
         DateModel dateModel = (DateModel) datepicker.getModel();
         dateModel.setValue(dateProperty.get());

         fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 10));

         JLabel dlabel = new JLabel();
         dlabel.setText(MultiMessages.get(key + ".date"));
         fieldPanel.add(dlabel);
         fieldPanel.add((Component) datepicker);
         datepicker.getJFormattedTextField().addPropertyChangeListener(e -> dateActionPerformed());

         if ((attrs == null) 
               || (attrs.get("granularity") == null) 
               || (attrs.get("granularity").equalsIgnoreCase("DATETIME")))
         {
            JLabel hlabel = new JLabel();
            hlabel.setText(MultiMessages.get(key + ".hour"));
            fieldPanel.add(hlabel);
            Vector hours = new Vector<>();
            for (int i = 0 ; i < 24; i++)
            {
               hours.add(i);
            }
            hourBox = new JComboBox(hours);
            fieldPanel.add(hourBox);
            hourBox.setEnabled(editable);

            JLabel mlabel = new JLabel();
            mlabel.setText(MultiMessages.get(key + ".minute"));
            fieldPanel.add(mlabel);
            Vector minutes = new Vector<>();
            for (int i = 0 ; i < 60; i++)
            {
               minutes.add(i);
            }
            minuteBox = new JComboBox(minutes);
            fieldPanel.add(minuteBox);
            minuteBox.setEnabled(editable);

            Date stamp = dateProperty.get();
            if (stamp != null)
            {
               hourBox.setSelectedIndex(stamp.getHours());
               minuteBox.setSelectedIndex(stamp.getMinutes());
            }
            hourBox.addActionListener(e -> dateActionPerformed());
            minuteBox.addActionListener(e -> dateActionPerformed());
         }
      }
      else
      {
         String datePattern = MultiMessages.get("Date.format");
         if ((datePattern == null) || (datePattern.contentEquals("Date.format")))
         {
            datePattern = "dd/MM/yyyy HH:mm";
         }
         SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
         Date stamp = dateProperty.get();
         rolabel = new JLabel();
         rolabel.setText(dateFormatter.format(stamp));
      }
   }
   
   @SuppressWarnings("deprecation")
   public void dateActionPerformed() {
      Date selectedDate = (Date) datepicker.getModel().getValue();
      if (selectedDate != null)
      {
         if (hourBox != null)
         {
            selectedDate.setHours(hourBox.getSelectedIndex());
            selectedDate.setMinutes(minuteBox.getSelectedIndex());
            selectedDate.setSeconds(0);
         }
         dateProperty.set(new Date(selectedDate.getTime()));
//         Messenger.sendMessage(getTarget(), getField().getName(), dateProperty.get());
      }
   };

   @Override
   protected JComponent getSpecificGUIComponent()
   {
      return (rolabel != null) ? rolabel : fieldPanel;
   }
   
   @SuppressWarnings("serial")
   class DateLabelFormatter extends AbstractFormatter {

      private SimpleDateFormat dateFormatter = null;

      @Override
      public Object stringToValue(String text) throws ParseException {
          return getFormatter().parseObject(text);
      }

      @Override
      public String valueToString(Object value) throws ParseException {
          if (value != null) {
              Calendar cal = (Calendar) value;
              return getFormatter().format(cal.getTime());
          }

          return "";
      }
      
      private SimpleDateFormat getFormatter()
      {
         if (dateFormatter == null)
         {
            String datePattern = MultiMessages.get("Date.format");
            if ((datePattern == null) || (datePattern.contentEquals("Date.format")))
            {
               datePattern = "dd/MM/yyyy HH:mm";
            }
            dateFormatter = new SimpleDateFormat(datePattern);
         }
         return dateFormatter;
      }

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy