/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import com.jgoodies.forms.layout.CellConstraints;

public class DateConstraint extends MetaDataConstraint
{
   private DateConstraintModel model;
   private boolean from, to;
   private JLabel label;
   private JCheckBox cbFrom, cbTo;
   private JSpinner spFrom, spTo;
   
   public DateConstraint(DateConstraintModel model)
   {
      this.model = model;
      from = model.getFrom();
      to = model.getTo();
      label = new JLabel(model.getName() + ": ");
      cbFrom = new JCheckBox("From: ", from);
      cbTo = new JCheckBox("To: ", to);
      SpinnerDateModel spmodel = new SpinnerDateModel(
         model.getDateFrom(), null, null, Calendar.YEAR);
      spFrom = new JSpinner(spmodel);
      spFrom.setEditor(new JSpinner.DateEditor(spFrom, "dd-MM-yyyy"));
      spFrom.setEnabled(from);
      spmodel = new SpinnerDateModel(model.getDateTo(), 
         null, null, Calendar.YEAR);
      spTo = new JSpinner(spmodel);
      spTo.setEditor(new JSpinner.DateEditor(spTo, "dd-MM-yyyy"));
      spTo.setEnabled(to);
      cbFrom.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            from = !from;
            spFrom.setEnabled(from);
         }
      });
      cbTo.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            to = !to;
            spTo.setEnabled(to);
         }
      });
   }

   public void update()
   {
      model.setFromTo(from, to);
      model.setDateFrom(((SpinnerDateModel)spFrom.getModel()).getDate());
      model.setDateTo(((SpinnerDateModel)spTo.getModel()).getDate());
   }

   public void setWidgets(JPanel panel, int row)
   {
      CellConstraints cc = new CellConstraints();
      panel.add(label, cc.xy(4, row + 1));
      panel.add(cbFrom, cc.xy(6, row + 1));
      panel.add(spFrom, cc.xy(8, row + 1));
      panel.add(cbTo, cc.xy(10, row + 1));
      panel.add(spTo, cc.xy(12, row + 1));
   }
}
