/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.guillc.dialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.geoforge.guillc.event.GfrEvtPanelChange;
import org.geoforge.guillc.event.GfrEvtValidityChanged;
import org.geoforge.guillc.handler.IGfrHandlerListenerPanelDialog;
import org.geoforge.guillc.panel.GfrPnlCmdCancelOk;
import org.geoforge.guillc.panel.GfrPnlGrpTxfFullEditableYes;
import org.geoforge.lang.util.number.GfrDouble;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
final public class GfrDlgCmdCancelOkChangeValueDoubleTfd extends GfrDlgCmdCancelOkChangeValueDoubleAbs
{
final private static int _INT_WIDTH_MIDDLE_ = 140;
final private static int _INT_WIDTH_RIGHT_ = 50;
//-- end static
private GfrPnlGrpTxfFullEditableYes _pnlValue_ = null;
private double _dblValueMin_ = GfrDouble.DBL_NDV;
private boolean _blnAllowsNoValue_ = false;
private int intWidth = -1;
@Override
public double getValue()
{
String str = this._pnlValue_.getValue();
if (str.length() < 1)
return GfrDouble.DBL_NDV;
return Double.parseDouble(str);
}
public GfrDlgCmdCancelOkChangeValueDoubleTfd(
String strWhat,
double dblValuePrev,
double dblValueMin,
boolean blnAllowsNoValue)
{
super((JFrame) null);
this._dblValueMin_ = dblValueMin;
this._pnlValue_ = new GfrPnlGrpTxfFullEditableYes(
strWhat);
this._blnAllowsNoValue_ = blnAllowsNoValue;
JLabel lbl = new JLabel(strWhat);
this.intWidth = lbl.getPreferredSize().width + 32;
String strValue = "";
if (dblValuePrev != GfrDouble.DBL_NDV)
strValue += String.valueOf(dblValuePrev);
this._pnlValue_.setContent(strValue);
}
@Override
public boolean init()
{
// ---
super.addToListResizable(this._pnlValue_);
super.setLengths(
this.intWidth,
_INT_WIDTH_MIDDLE_,
_INT_WIDTH_RIGHT_);
this._pnlValue_.addPanelListener((IGfrHandlerListenerPanelDialog) this);
// ---
if (!super.init())
return false;
if (!this._pnlValue_.init())
return false;
//--
super._pnlContent.addWithSpace(this._pnlValue_);
((GfrPnlCmdCancelOk) super._pnlCommand).setEnabledOkCommandDialog(true);
return true;
}
@Override
public void destroy()
{
super.destroy();
if (this._pnlValue_ != null)
{
this._pnlValue_.destroy();
this._pnlValue_ = null;
}
}
@Override
protected boolean _noErrorToDisplay()
{
return _valueHasNoError_();
}
@Override
protected void _updateModifiedFieldStatus(GfrEvtPanelChange e)
{
Object objSource = e.getSource();
if (objSource.equals(this._pnlValue_))
{
this._pnlValue_.setValueIsOk(_valueEnablesOk_());
return;
}
}
@Override
protected boolean _okEnableable()
{
return _valueEnablesOk_();
}
private boolean _valueHasNoError_()
{
String strName = this._pnlValue_.getValue();
if (strName == null || strName.length() < 1)
return true;
return _valueEnablesOk_();
}
private boolean _valueEnablesOk_()
{
String strValue = this._pnlValue_.getValue();
if (strValue == null || strValue.length() < 1)
{
if (this._blnAllowsNoValue_)
return true;
return false;
}
double dbl = -1d;
try
{
dbl = Double.parseDouble(strValue);
}
catch (Exception e)
{
validityChanged(new GfrEvtValidityChanged(false, "Please enter a valid value"));
return false;
}
if (dbl < this._dblValueMin_)
{
validityChanged(new GfrEvtValidityChanged(false, "Value should be in higher to: " + this._dblValueMin_));
return false;
}
return true;
}
}