/*
* 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 java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.geoforge.guillc.border.GfrFactoryBorder;
import org.geoforge.guillc.event.GfrEvtPanelChange;
import org.geoforge.guillc.event.GfrEvtValidityChanged;
import org.geoforge.guillc.panel.GfrPnlGrpSngTfdCmbUnitDistance;
import org.geoforge.guillc.panel.GfrPnlGrpTxfFullEditableYes;
import org.geoforge.guillc.panel.GfrPnlSelEdt;
import org.geoforge.guillc.handler.IGfrHandlerListenerPanelDialog;
import org.geoforge.guillc.tablemodel.GfrDtmExtTblNameDepth;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.lang.util.number.GfrDouble;
import org.geoforge.java.lang.string.GfrUtlString;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
abstract public class GfrDlgCmdCancelOkEdtExtTblNameDepthAbs extends GfrDlgCmdCancelOkEdtExtTblAbs
{
final static public int[] INT_WIDTH_CELLS =
{
60,
125
};
private String _strWhat_ = null;
private GfrPnlGrpSngTfdCmbUnitDistance _pnlDepth_ = null;
protected GfrPnlGrpTxfFullEditableYes _pnlName = null;
protected GfrPnlSelEdt _pnlGeneral = null;
protected GfrPnlSelEdt _pnlDetails = null;
private double[] _dblsExistingDepthSorted_ = null;
private String[] _strsExistingNames_ = null;
private double _dblPrecision_ = GfrDouble.DBL_NDV;
private double _dblMax_ = GfrDouble.DBL_NDV;
private double _dblMin_ = GfrDouble.DBL_NDV;
public String getValueName()
{
return this._pnlName.getValue();
}
public double getValueDepthInMeters()
{
return this._pnlDepth_.getValueInMeters();
}
@Override
protected boolean _isEmpty()
{
if(_dblsExistingDepthSorted_.length == 0 &&_strsExistingNames_.length == 0 )
return true;
return false;
}
@Override
protected boolean _okEnableable()
{
if (!this._nameEnablesOk_())
return false;
if (!this._depthEnablesOk_())
return false;
return true;
}
@Override
protected boolean _noErrorToDisplay()
{
if (!this._nameHasNoError_())
return false;
if (!this._depthHasNoError_())
return false;
return true;
}
private boolean _alreadyExistsDepth_(double dbl)
{
if (this._dblsExistingDepthSorted_ == null)
return false;
if (this._dblsExistingDepthSorted_.length < 1)
return false;
for (int i = 0; i < this._dblsExistingDepthSorted_.length; i++)
{
if (Math.abs(dbl - this._dblsExistingDepthSorted_[i]) < GfrDouble.DBL_EPSILON_THREE_DIGITS)
return true;
}
return false;
}
private boolean _farEnoughFromExistingDepth_(double dbl)
{
if (this._dblsExistingDepthSorted_ == null)
return true;
if (this._dblsExistingDepthSorted_.length < 1)
return true;
for (int i = 0; i < this._dblsExistingDepthSorted_.length; i++)
{
if (Math.abs(dbl - this._dblsExistingDepthSorted_[i]) < this._dblPrecision_)
return false;
}
return true;
}
private boolean _alreadyExistsName_(String str)
{
if (str == null)
return false;
if (this._strsExistingNames_ == null)
return false;
if (this._strsExistingNames_.length < 1)
return false;
for (int i = 0; i < this._strsExistingNames_.length; i++)
{
if (str.compareTo(this._strsExistingNames_[i]) == 0)
return true;
}
return false;
}
@Override
protected void _updateModifiedFieldStatus(GfrEvtPanelChange e)
{
Object objSource = e.getSource();
if (objSource.equals(this._pnlName))
{
this._pnlName.setValueIsOk(this._nameEnablesOk_());
return;
}
if (objSource.equals(this._pnlDepth_))
{
this._pnlDepth_.setValueIsOk(this._depthEnablesOk_());
return;
}
}
protected GfrDlgCmdCancelOkEdtExtTblNameDepthAbs(
JFrame frmOwner,
String strTitleSuffix,
double[] dblsExistingDepthSorted,
String[] strsExistingNames,
String strWhat,
double dblPrecision,
double dblMax,
double dblMin)
{
super(
frmOwner,
strTitleSuffix,
(dblsExistingDepthSorted.length + 1)*17,
new GfrDtmExtTblNameDepth(
dblsExistingDepthSorted,
strsExistingNames));
this._dblMax_ = dblMax;
this._dblMin_ = dblMin;
this._dblPrecision_= dblPrecision;
this._dblsExistingDepthSorted_ = dblsExistingDepthSorted;
this._strsExistingNames_ = strsExistingNames;
this._pnlDepth_ = new GfrPnlGrpSngTfdCmbUnitDistance(
GfrResBundleLang.s_getInstance().getValue("word.depth"));
this._pnlDetails = new GfrPnlSelEdt();
this._pnlGeneral = new GfrPnlSelEdt();
this._pnlName = new GfrPnlGrpTxfFullEditableYes(
GfrResBundleLang.s_getInstance().getValue("word.name"));
super.addToListResizable(this._pnlName);
super.addToListResizable(this._pnlDepth_);
this._pnlName.addPanelListener((IGfrHandlerListenerPanelDialog) this);
this._pnlDepth_.addPanelListener((IGfrHandlerListenerPanelDialog) this);
this._strWhat_ = strWhat;
}
@Override
public boolean init()
{
if (!super.init())
return false;
if (!this._pnlDetails.init())
return false;
if (!this._pnlDepth_.init())
return false;
if (!this._pnlGeneral.init())
return false;
if (!this._pnlName.init())
return false;
TableColumnModel tcm = super._pnlExistingItems.getColumnModel();
// ATTN: may not work once serialzed!!!
for (int i = 0; i < tcm.getColumnCount(); i++)
{
//if (i == GfrTmlEdtLthPmtMnr.INT_WIDTH_CELLS.length-1) // not hard-coding description's width
// continue;
TableColumn tcnCur = tcm.getColumn(i);
tcnCur.setMinWidth(GfrDlgCmdCancelOkEdtExtTblNameDepthAbs.INT_WIDTH_CELLS[i]);
tcnCur.setPreferredWidth(GfrDlgCmdCancelOkEdtExtTblNameDepthAbs.INT_WIDTH_CELLS[i]);
tcnCur.setMaxWidth(GfrDlgCmdCancelOkEdtExtTblNameDepthAbs.INT_WIDTH_CELLS[i]);
}
this._pnlName.setMandatoryIcon(true);
this._pnlDepth_.setMandatoryIcon(true);
this._pnlDepth_.setValueIsOk(false);
//Panel General
this._pnlGeneral.setBorder(GfrFactoryBorder.createTitledBorder(
GfrResBundleLang.s_getInstance().getValue("word.General")));
this._pnlDetails.setBorder(GfrFactoryBorder.createTitledBorder("Details"));
this._pnlDetails.addWithSpace(this._pnlDepth_);
this._pnlGeneral.addWithSpace(this._pnlName);
super._pnlContent.addWithSpace(this._pnlGeneral);
super._pnlContent.addWithSpace(this._pnlDetails);
this._pnlName.setContent(
GfrUtlString.s_createUniqueNameByNumber(_strsExistingNames_, this._strWhat_));
this._pnlName.selectText();
return true;
}
@Override
public void destroy()
{
super.destroy();
if (this._pnlDepth_ != null)
{
this._pnlDepth_.destroy();
this._pnlDepth_ = null;
}
if (this._pnlName != null)
{
this._pnlName.destroy();
this._pnlName = null;
}
if (this._pnlDetails != null)
{
this._pnlDetails.destroy();
this._pnlDetails = null;
}
if (this._pnlGeneral != null)
{
this._pnlGeneral.destroy();
this._pnlGeneral = null;
}
}
private boolean _nameHasNoError_()
{
String strName = this._pnlName.getValue();
if (strName == null || strName.length() < 1)
return true;
return _nameEnablesOk_();
}
private boolean _nameEnablesOk_()
{
String strName = this._pnlName.getValue();
if (strName == null || strName.length() < 1)
return false;
if (this._alreadyExistsName_(strName))
{
validityChanged(new GfrEvtValidityChanged(false, "Name already exists"));
return false;
}
return true;
}
private boolean _depthHasNoError_()
{
String str = this._pnlDepth_.getValue();
if (str == null || str.length() < 1)
return true;
return _depthEnablesOk_();
}
private boolean _depthEnablesOk_()
{
String str = this._pnlDepth_.getValue();
if (str == null || str.length() < 1)
return false;
Double dbl = GfrDouble.valueOf(str);
if (dbl == null)
{
validityChanged(new GfrEvtValidityChanged(
false,
"Please provide valid depth value"));
return false;
}
//-- Converting in meters to check the unicity of the depth.
dbl = Double.valueOf(getValueDepthInMeters());
if (this._alreadyExistsDepth_(dbl))
{
validityChanged(new GfrEvtValidityChanged(false, "Already existing with same depth"));
return false;
}
if (!this._farEnoughFromExistingDepth_(dbl))
{
validityChanged(new GfrEvtValidityChanged(false, "Too close to existing depths"));
return false;
}
if (dbl > this._dblMax_)
{
validityChanged(new GfrEvtValidityChanged(false, "Depth should be lower than : " +
String.format("%.2f", this._dblMax_)));
return false;
}
if (dbl < this._dblMin_)
{
validityChanged(new GfrEvtValidityChanged(false, "Depth should be higher than : " +
String.format("%.2f", this._dblMin_)));
return false;
}
return true;
}
}