Package com.GestDB.swing

Source Code of com.GestDB.swing.DatePickerObserver

/*
* DatePickerObserver.java
*
* Created on 2012
* Created by Arsenio Molinero
* Copyright (C) 2012 Arsenio Molinero
*
* GestDB - a Java multi database client
*
* This file is part of GestDB.
*
* GestDB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GestDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GestDB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package com.GestDB.swing;

import com.GestDB.general.Trackbug;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.util.Calendar;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.text.DateFormatter;
import com.qt.datapicker.DatePicker;
import java.awt.Font;
import java.awt.Insets;
import java.util.Date;

/**
* Crea un panel con una JFormattedTextField y un bot�n que permite cargar un datepicket.
* Gestiona los eventos necesarios para poder serr utilizado por un tableeditor y un panel normal como si fues una �nica pieza.
* @author seni
*/
public class DatePickerObserver extends JPanel implements Observer, ActionListener {
    public JFormattedTextField jtf = null;
    private JButton jbPick = null;
    private String command;
    private Date valorAntiguo = null;
   
    public DatePickerObserver(DateFormatter df) {
        super();
       
        this.setLayout(new java.awt.BorderLayout());
        jtf = new JFormattedTextField (df);
        jtf.addActionListener(this);
        jtf.setFocusLostBehavior(JFormattedTextField.COMMIT);
        this.add(jtf,java.awt.BorderLayout.CENTER);
       
        jbPick = new JButton(new javax.swing.ImageIcon(getClass().getResource("/com/GestDB/images/calendar.png")));
        final DatePickerObserver dpo = this;
        jbPick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                Locale locale = getLocale();

                // instanciamos el DatePicker
                DatePicker dp = new DatePicker(dpo, locale);
                // previously selected date
                dp.setSelectedDate((java.util.Date)dpo.jtf.getValue());
                dp.start(dpo.jtf);
            }
        });
        jbPick.setMargin(new Insets(0, 0, 0, 0));
        //jbPick.setPreferredSize(new Dimension(16, 16));
        this.add(jbPick,java.awt.BorderLayout.EAST);
    }

    public void update(Observable o, Object arg) {
        Calendar calendar= (Calendar)arg;
        DatePicker dp= (DatePicker)o;
        StringBuffer JdecGenerated16 = new StringBuffer("picked=");
        Trackbug.info(JdecGenerated16.append(dp.formatDate(calendar)).toString());
        jtf.setValue(calendar.getTime());
        fireActionPerformed();
        return;
    }
   
    public void setValue(Date fecha)
    {
        valorAntiguo = fecha;
        if(jtf != null)
            jtf.setValue(fecha);
    }

    public Date getValue()
    {
        if(jtf != null)
            return (Date)jtf.getValue();
        else
            return null;
    }
   
    public void setFont(Font fuente)
    {
        if(jtf != null)
            jtf.setFont(fuente);
    }

    public Font getFont()
    {
        if(jtf != null)
            return jtf.getFont();
        else
            return null;
    }

    /**
     * Adds the specified action listener to receive
     * action events from this textfield.
     *
     * @param l the action listener to be added
     */
    public synchronized void addActionListener(ActionListener l) {
        listenerList.add(ActionListener.class, l);
    }

    /**
     * Removes the specified action listener so that it no longer
     * receives action events from this textfield.
     *
     * @param l the action listener to be removed
     */
    public synchronized void removeActionListener(ActionListener l) {
        listenerList.remove(ActionListener.class, l);
    }

   
    /**
     * Notifies all listeners that have registered interest for
     * notification on this event type.  The event instance
     * is lazily created.
     * The listener list is processed in last to
     * first order.
     * @see EventListenerList
     */
    protected void fireActionPerformed() {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        int modifiers = 0;
        AWTEvent currentEvent = EventQueue.getCurrentEvent();
        if (currentEvent instanceof InputEvent) {
            modifiers = ((InputEvent)currentEvent).getModifiers();
        } else if (currentEvent instanceof ActionEvent) {
            modifiers = ((ActionEvent)currentEvent).getModifiers();
        }
       
        ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                            (command != null) ? command : this.command,
                            EventQueue.getMostRecentEventTime(), modifiers);
                           
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==ActionListener.class) {
                ((ActionListener)listeners[i+1]).actionPerformed(e);
            }         
        }
    }
   
    /**
     * Sets the command string used for action events.
     *
     * @param command the command string
     */
    public void setActionCommand(String command) {
        this.command = command;
        jtf.setActionCommand(command);
    }

    public void actionPerformed(ActionEvent e) {
        Object[] listeners = listenerList.getListenerList();
                           
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==ActionListener.class) {
                ((ActionListener)listeners[i+1]).actionPerformed(e);
            }         
        }
    }

}
TOP

Related Classes of com.GestDB.swing.DatePickerObserver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.