/*
* 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.panel;
import org.geoforge.guillc.handler.IGfrHandlerListenerPanelDialog;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import org.geoforge.guillc.combobox.GfrCmb;
import org.geoforge.guillc.textfield.GfrTfd;
import org.geoforge.guillc.event.GfrEvtPanelChange;
import org.geoforge.lang.util.number.GfrDouble;
import org.geoforge.java.lang.number.GfrUtilInteger;
/**
*
* @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 GfrPnlGrpEditDmsAbs extends GfrPnlGrpAbs
implements ActionListener, CaretListener, IGfrHandlerListenerPanelDialog
{
//--
//-- beg static
private final static int _INT_SEP_ = 5;
//-- end static
//--
//
//--
//--beg protected fields
protected GfrCmb _cmbChoice;
//--end protected fields
//--
//
//--
//--beg private fields
private GfrTfd _tfdDegree_;
private GfrTfd _tfdMinute_;
private GfrTfd _tfdSecond_;
//--end private fields
//--
abstract protected boolean _isPositiveSelected();
public Double getValue()
{
String strDegree = this._tfdDegree_.getText();
String strMinute = this._tfdMinute_.getText();
String strSecond = this._tfdSecond_.getText();
strDegree.trim();
Integer itgDegree = 0;
Integer itgMinute = 0;
Double douSecond = 0d;
//-- memo, fields cannot have negative values
//--
//-- degrees
itgDegree = GfrUtilInteger.valueOf(strDegree);
if (itgDegree == null || itgDegree < 0)
return null;
//-- minutes
if (strMinute == null || strMinute.length() < 1)
itgMinute = 0;
else
itgMinute = GfrUtilInteger.valueOf(strMinute);
if (itgMinute == null || itgMinute < 0)
return null;
//-- second
if (strSecond == null || strSecond.length() < 1)
douSecond = 0d;
else
douSecond = GfrDouble.valueOf(strSecond);
if (douSecond == null || douSecond < 0)
return null;
double dblAngle = itgDegree.doubleValue()
+ itgMinute.doubleValue() / 60d
+ douSecond.doubleValue() / 3600d;
if (!_isPositiveSelected())
dblAngle *= -1d;
return Double.valueOf(dblAngle);
}
public boolean isEmpty()
{
//-- Degree
String strDegree = this._tfdDegree_.getText();
if((strDegree != null && strDegree.length() > 0))
return false;
//-- Minute
String strMinute = this._tfdMinute_.getText();
if((strMinute != null && strMinute.length() > 0))
return false;
//-- Second
String strSecond = this._tfdSecond_.getText();
if((strSecond != null && strSecond.length() > 0))
return false;
return true;
}
protected GfrPnlGrpEditDmsAbs(String strLabelWhat)
{
super(
strLabelWhat,
GfrCmb.INT_HEIGHT);
this._tfdDegree_ = new GfrTfd();
this._tfdMinute_ = new GfrTfd();
this._tfdSecond_ = new GfrTfd();
this._tfdDegree_.addCaretListener((CaretListener) this);
this._tfdMinute_.addCaretListener((CaretListener) this);
this._tfdSecond_.addCaretListener((CaretListener) this);
super.addPanelListener((IGfrHandlerListenerPanelDialog) this);
}
@Override
public boolean init()
{
if (!super.init())
return false;
if (!this._cmbChoice.init())
return false;
if (!this._tfdDegree_.init())
return false;
if (!this._tfdMinute_.init())
return false;
if (!this._tfdSecond_.init())
return false;
GfrPnl pnl = new GfrPnl();
if (!pnl.init())
return false;
int intTfdWidth = (super.getWidthContent() - 3 * _INT_SEP_ - 13) / 4;
int intTfdWidthRes = (super.getWidthContent() - 3 * _INT_SEP_ - 13) % 4;
int intTfdHeight = 20;
Dimension dim = new Dimension(intTfdWidth, intTfdHeight);
Dimension dimRes = new Dimension(intTfdWidth + intTfdWidthRes, intTfdHeight);
//--
this._tfdDegree_.setMinimumSize(dim);
this._tfdDegree_.setMaximumSize(dim);
this._tfdDegree_.setPreferredSize(dim);
//--
this._tfdMinute_.setMinimumSize(dim);
this._tfdMinute_.setMaximumSize(dim);
this._tfdMinute_.setPreferredSize(dim);
//--
this._tfdSecond_.setMinimumSize(dimRes);
this._tfdSecond_.setMaximumSize(dimRes);
this._tfdSecond_.setPreferredSize(dimRes);
pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
pnl.add(this._tfdDegree_);
pnl.add(Box.createRigidArea(new Dimension(_INT_SEP_, 0)));
//--
pnl.add(this._tfdMinute_);
pnl.add(Box.createRigidArea(new Dimension(_INT_SEP_, 0)));
//--
pnl.add(this._tfdSecond_);
pnl.add(Box.createRigidArea(new Dimension(_INT_SEP_, 0)));
//--
pnl.add(this._cmbChoice);
super.add(pnl);
return true;
}
@Override
public void caretUpdate(CaretEvent e)
{
super.firePanelUpdate();
}
@Override
public void destroy()
{
super.destroy();
if (this._cmbChoice != null)
{
this._cmbChoice.destroy();
this._cmbChoice = null;
}
if (this._tfdDegree_ != null)
{
this._tfdDegree_.destroy();
this._tfdDegree_ = null;
}
if (this._tfdMinute_ != null)
{
this._tfdMinute_.destroy();
this._tfdMinute_ = null;
}
if (this._tfdSecond_ != null)
{
this._tfdSecond_.destroy();
this._tfdSecond_ = null;
}
}
@Override
public void somethingHasHappened(GfrEvtPanelChange e)
{
boolean blnMandatory = super.isMandatory();
boolean blnEmpty = this.isEmpty();
if (!blnMandatory && blnEmpty)
{
super.setValueIsOk(true);
return;
}
if (this.getValue() != null)
{
super.setValueIsOk(true);
return;
}
super.setValueIsOk(false);
}
}