/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.geoforge.guillc.panel;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import org.geoforge.guillc.button.BtnIcnClearSelection;
import org.geoforge.guillc.button.BtnIcnSelectFromDialog;
import org.geoforge.guillc.dialog.GfrDlgCmdCancelOkPicksDate;
import org.geoforge.guillc.textfield.GfrTfd;
import org.geoforge.guillc.textfield.GfrTfdAbs;
import org.geoforge.lang.text.GfrSimpleDateFormatDay;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com ... please remove "_AT_" from the above
* string to get the right email address
*
* Should be refactored => remove "Sng"
*/
public class GfrPnlGrpSngDateChooser extends GfrPnlGrpAbs
{
private GfrTfd _tfdLeft_;
private BtnIcnSelectFromDialog _btnSelect_;
private BtnIcnClearSelection _btnClear_;
private Date _datSelected_ = null;
public String getValue()
{
return this._tfdLeft_.getText();
}
public void setValue(Date dat)
{
this._datSelected_ = dat;
this._tfdLeft_.setText(
GfrSimpleDateFormatDay.s_format(dat));
}
public GfrPnlGrpSngDateChooser(
String strLabelWhat,
String strValueInitial)
{
super(
strLabelWhat,
GfrTfdAbs.CMP_HEIGHT);
this._tfdLeft_ = new GfrTfd(strValueInitial);
this._btnSelect_ = new BtnIcnSelectFromDialog((ActionListener) this, strLabelWhat.toLowerCase());
this._btnClear_ = new BtnIcnClearSelection((ActionListener) this, strLabelWhat.toLowerCase());
}
public GfrPnlGrpSngDateChooser(
String strLabelWhat)
{
this(strLabelWhat, "");
}
@Override
public boolean init()
{
if (!super.init())
return false;
if (!this._tfdLeft_.init())
return false;
if (!this._btnSelect_.init())
return false;
if (!this._btnClear_.init())
return false;
GfrPnl pnl = new GfrPnl();
if (!pnl.init())
return false;
pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
this._tfdLeft_.setEditable(false);
// ---
boolean blnIsValue = false;
String strValue = this._tfdLeft_.getText();
if (strValue!=null && strValue.trim().length()>1)
blnIsValue = true;
this._btnSelect_.setEnabled(! blnIsValue);
this._btnClear_.setEnabled(blnIsValue);
// ---
pnl.add(this._tfdLeft_);
pnl.add(Box.createRigidArea(new Dimension(5, 0)));
pnl.add(this._btnSelect_);
pnl.add(Box.createRigidArea(new Dimension(5, 0)));
pnl.add(this._btnClear_);
super.add(pnl);
return true;
}
@Override
public void destroy()
{
super.destroy();
if (this._tfdLeft_ != null)
{
this._tfdLeft_.destroy();
this._tfdLeft_ = null;
}
if (this._btnSelect_ != null)
{
this._btnSelect_.destroy();
this._btnSelect_ = null;
}
if (this._btnClear_ != null)
{
this._btnClear_.destroy();
this._btnClear_ = null;
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof BtnIcnSelectFromDialog)
{
if(! _selectEndDate_())
return;
this._btnSelect_.setEnabled(false);
this._btnClear_.setEnabled(true);
}
if (e.getSource() instanceof BtnIcnClearSelection)
{
this._tfdLeft_.setText("");
this._btnSelect_.setEnabled(true);
this._btnClear_.setEnabled(false);
}
super.actionPerformed(e);
}
private boolean _selectEndDate_()
{
try
{
if(this._datSelected_ == null)
this._datSelected_ = Calendar.getInstance().getTime();
Date datFirst = null;
Date datLast = null;
GfrDlgCmdCancelOkPicksDate dlg = new GfrDlgCmdCancelOkPicksDate(
(JFrame) null, // frmOwner,
this._datSelected_,
datFirst,
datLast);
if (!dlg.init()) // !!!
{
System.err.println("! dlg.init()");
return false;
}
dlg.setVisible(true);
Date datResult = null;
boolean blnIsCancelled = dlg.isCancelled();
if (!blnIsCancelled)
{
datResult = dlg.getValue();
}
dlg.destroy();
dlg = null;
if (blnIsCancelled)
return false;
String strValue = GfrSimpleDateFormatDay.s_format(datResult);
this._tfdLeft_.setText(strValue);
return true;
}
catch (Exception exc)
{
exc.printStackTrace();
return false;
}
}
}