// DateAttributeEditor.java
// $Id: DateAttributeEditor.java,v 1.13 1998/04/30 09:26:29 bmahe Exp $
// (c) COPYRIGHT MIT and INRIA, 1997.
// Please first read the full copyright statement in file COPYRIGHT.html

package org.w3c.jigadm.editors ;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import org.w3c.tools.resources.*;
import org.w3c.tools.widgets.*;
import org.w3c.jigsaw.admin.*;
import org.w3c.jigadm.*;

public class DateAttributeEditor extends AttributeEditor {
    
    /** 
     * an inner ActionListener for the '+' and '-' Buttons
     */

    class DateActionListener implements ActionListener {
	int field = 0;
	
        public void actionPerformed(ActionEvent ae) {
	    if(ae.getActionCommand().equals("+"))
		updateValue(field, true);
	    else
		updateValue(field, false);
	}

      DateActionListener(int f) {
	    field = f;
	}
    }

    private Calendar c;
    private Date origd;
    Date currd;
    Panel widget;
    private Label h, min, s, d, m, y;
    private static final String smonth[] = {"Jan", "Feb", "Mar", "Apr",
					    "May", "Jun", "Jul", "Aug",
					    "Sep", "Oct", "Nov", "Dec" };
    
    /**
     * reset the strings in the Textfields according to the new date
     */
   
    private void updateFields() {
	h.setText((new Integer(c.get(Calendar.HOUR_OF_DAY))).toString());
	min.setText((new Integer(c.get(Calendar.MINUTE))).toString());
	s.setText((new Integer(c.get(Calendar.SECOND))).toString());
	d.setText((new Integer(c.get(Calendar.DAY_OF_MONTH))).toString());
	m.setText(smonth[c.get(Calendar.MONTH)]);
	y.setText((new Integer(c.get(Calendar.YEAR))).toString());
    }

    /**
     * update the new date value, according to the field defined in
     * the Calendar
     * @see Calendar
     * @param field the field of the Calendar to be modified
     * @param plus a boolean which determine the change sign
     */

    protected void updateValue(int field, boolean plus) {
	c.setTime(currd);
	c.roll(field, plus);
	currd = c.getTime();
	// jdk 1.1 kludge
	c.setTime(currd);
	updateFields();
    }

    /**
     * Tells if the edited value has changed
     * @return true if the value changed.
     */
 
    public boolean hasChanged() {
	return (!origd.equals(currd));
    }

    /**
     * set the current value to be the original value, ie: changed
     * must return <strong>false</strong> after a reset.
     */

    public void clearChanged() {
	origd = currd;
    }

    /**
     * reset the changes (if any)
     */
    
    public void resetChanges() {
	currd = origd;
	c.setTime(currd);
	updateFields();
    }

    /**
     * Get the current value of the edited value
     * @return an object or <strong>null</strong> if the object was not
     * initialized
     */

    public Object getValue() {
	return new Long(currd.getTime());
    }

    /**
     * Add a Listener to this editor.
     * @param el a listener
     */

    public void setValue(Object o) {
	if(o instanceof Date) {
	    currd = (Date)o;
	    c.setTime(currd);
	    updateFields();
	}
    }

    /**
     * get the Component created by the editor.
     * @return a Component
     */
 
    public Component getComponent() {
	return widget;
    }

   public DateAttributeEditor() {
       ImageButton pl, mi;
       Panel wh, wmin, ws, wd, wm, wy;
       DateActionListener dae;
       PropertyManager pm = PropertyManager.getPropertyManager();

       Image up = Toolkit.getDefaultToolkit().getImage(
	   pm.getIconLocation("shadowleft"));
       Image down = Toolkit.getDefaultToolkit().getImage(
	   pm.getIconLocation("shadowright"));

       widget = new BorderPanel(BorderPanel.IN);
       widget.setLayout(new GridLayout(2,1));
       wh   = new Panel(new BorderLayout());
       wmin = new Panel(new BorderLayout());
       ws   = new Panel(new BorderLayout());
       wd   = new Panel(new BorderLayout());
       wm   = new Panel(new BorderLayout());
       wy   = new Panel(new BorderLayout());

       Panel hm = new Panel(new BorderLayout());
       // add the "hour" panel
       h = new Label();
       dae = new DateActionListener(Calendar.HOUR_OF_DAY);
       wh.add("Center", h);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       wh.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       wh.add("East", mi);
       // add the "min" panel
       min = new Label();
       dae = new DateActionListener(Calendar.MINUTE);
       wmin.add("Center", min);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       wmin.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       wmin.add("East", mi);

       hm.add("West", wh);
       hm.add("Center", new Label("  :"));
       hm.add("East", wmin);

       Panel ht = new Panel(new BorderLayout());
       s = new Label();
       dae = new DateActionListener(Calendar.SECOND);
       ws.add("Center", s);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       ws.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       ws.add("East", mi);

       ht.add("West", hm);
       ht.add("Center", new Label("  :"));
       ht.add("East", ws);

       Panel hd = new Panel(new BorderLayout());
       // add the "day" panel
       d = new Label();
       dae = new DateActionListener(Calendar.DAY_OF_MONTH);
       wd.add("Center", d);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       wd.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       wd.add("East", mi);
       // then the "Month" panel
       m = new Label();
       dae = new DateActionListener(Calendar.MONTH);
       wm.add("Center", m);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       wm.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       wm.add("East", mi);

       Panel hd1 = new Panel(new BorderLayout());
       hd1.add("West", wd);
       hd1.add("Center", new Label("/"));
       hd1.add("East", wm);

       // then the "Year" panel
       y = new Label();
       dae = new DateActionListener(Calendar.YEAR);
       wy.add("Center", y);
       pl = new ImageButton(up);
       pl.addActionListener(dae);
       pl.setActionCommand("-");
       wy.add("West", pl);
       mi = new ImageButton(down);
       mi.addActionListener(dae);
       mi.setActionCommand("+");
       wy.add("East", mi);   
   
       hd.add("West", hd1);
       hd.add("Center", new Label("/"));
       hd.add("East", wy);

       widget.add(ht);
       widget.add(hd);

       c = Calendar.getInstance();
   }

    /**
     * Initialize the filter
     * @param w the ResourceWrapper father of the attribute
     * @param a the Attribute we are editing
     * @param o the value of the above attribute
     * @param p some Properties, used to fine-tune the editor
     */
 
   public void initialize(RemoteResourceWrapper w, Attribute a,  Object o,
			  Properties p)
       throws RemoteAccessException
    {
	RemoteResource r = w.getResource();
	if(o == null) {
	    Date d = null;
	    try {
		d = new Date(((Long)r.getValue(a.getName())).longValue());
		
		if(d == null)
		    if(a.getDefault() != null)
			d = new Date(((Long) a.getDefault()).longValue());
	    } catch (Exception ex) {
		// a fancy error?
	    }
	    if ( d != null ) {
		origd = d;
		c.setTime(d);
		updateFields();
	    } else {
	 	origd = new Date();
	    }
	    currd = origd;
	} else {
	    if(o instanceof Long) {
		origd = new Date(((Long)o).longValue());
		c.setTime(origd);
	    }
	}
	updateFields();
	currd = origd;
    }
}
    
