/*
* 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.button.BtnIcnSelectInListAltitude;
import org.geoforge.guillc.button.BtnIcnClearSelectionTimestamp;
import org.geoforge.guillc.button.BtnIcnClearSelectionDirection;
import org.geoforge.guillc.button.BtnIcnSelectInListAccelerometer;
import org.geoforge.guillc.button.BtnIcnSelectInListDirection;
import org.geoforge.guillc.button.BtnIcnSelectInListTimestamp;
import org.geoforge.guillc.button.BtnIcnClearSelectionAltitude;
import org.geoforge.guillc.button.BtnIcnClearSelectionAccelerometer;
import gov.nasa.worldwind.formats.shapefile.DBaseRecord;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecord;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecordMultiPoint;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecordPoint;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentListener;
import org.geoforge.awt.geoshape.ShapeAbs;
import org.geoforge.awt.geoshape.ShapeTimestampPoint2d;
import org.geoforge.guillc.frame.GfrFrmAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
import org.geoforge.java.awt.geom.GfrUtilRangeLatLon;
import org.geoforge.java.lang.system.StopWatch;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.worldwind.formats.shapefile.OurShapefileLoaderPoint;
/**
*
* @author bill
*/
public class GfrPnlContentsOkImportShapeTimestampPoint2d extends GfrPnlContentsOkImportShapeAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrPnlContentsOkImportShapeTimestampPoint2d.class.getName());
static
{
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
public GfrPnlContentsOkImportShapeTimestampPoint2d(
DocumentListener dlrParentDialog,
String[] strsExistingLabels)
{
super(dlrParentDialog, strsExistingLabels);
super._pnlSettings = new GfrPnlSettingsImportFileShapeTimestamp(
dlrParentDialog,
(ActionListener) this);
}
@Override
public void actionPerformed(ActionEvent e)
{
// ----
if (e.getSource() instanceof BtnIcnSelectInListTimestamp)
{
_selectTimestamp_();
return;
}
if (e.getSource() instanceof BtnIcnClearSelectionTimestamp)
{
_clearTimestamp_();
return;
}
// ----
// ----
if (e.getSource() instanceof BtnIcnSelectInListAltitude)
{
_selectAltitude_();
return;
}
if (e.getSource() instanceof BtnIcnClearSelectionAltitude)
{
_clearAltitude_();
return;
}
// ----
if (e.getSource() instanceof BtnIcnSelectInListDirection)
{
_selectDirection_();
return;
}
if (e.getSource() instanceof BtnIcnClearSelectionDirection)
{
_clearDirection_();
return;
}
// ----
if (e.getSource() instanceof BtnIcnSelectInListAccelerometer)
{
_selectAccelerometer_();
return;
}
if (e.getSource() instanceof BtnIcnClearSelectionAccelerometer)
{
_clearAccelerometer_();
return;
}
// ----
super.actionPerformed(e);
}
@Override
public void doJob() throws Exception
{
StopWatch.start(
GfrPnlContentsOkImportShapeTimestampPoint2d.class.getName() +
".doJob(), super._altRec.size()=" +
super._altRec.size());
String strKeyLabel = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).getValueKeyLabel();
boolean blnIsGeometryLonLat = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).isValueGeometryLonLat();
String strKeyTimestamp = ((GfrPnlSettingsImportFileShapeTimestamp) super._pnlSettings).getValueKeyTimestamp();
String strKeyLocationClass = null;
// TEMPO ((GfrPnlSettingsImportFileShapeTimestamp) super._pnlSettings).getValueKeyLocationClass();
// MEMO: pending, altitude, direction, accelerometer
// assuming homogeneous shapes in list
// assuming at least one record
ShapefileRecord sfrFirst = this._altRec.get(0);
if (sfrFirst instanceof ShapefileRecordMultiPoint)
{
_doJobMultiPointUnique_(strKeyLabel, strKeyTimestamp, blnIsGeometryLonLat, strKeyLocationClass);
}
else
{
_doJobPoint_(strKeyLabel, strKeyTimestamp, blnIsGeometryLonLat, strKeyLocationClass);
}
//System.out.println(StopWatch.getTime());
}
private void _doJobPoint_(
String strKeyLabel,
String strKeyTimestamp,
boolean blnIsGeometryLonLat,
String strKeyLocationClass) throws Exception
{
for (int i=0; i< this._altRec.size(); i++)
{
ShapefileRecord sfrCur = this._altRec.get(i);
// ----
String strValueLabelCandidate = (String) sfrCur.getAttributes().getValue(strKeyLabel);
if (strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1)
{
String str = "strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1";
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.severe(str);
throw new Exception(str);
}
String strValueLabelUnique = strValueLabelCandidate.trim();
// ---
String strValueLocationClass = "none";
if (strKeyLocationClass!=null && strKeyLocationClass.length()>0)
{
strValueLocationClass = (String) sfrCur.getAttributes().getValue(strKeyLocationClass);
if (strValueLocationClass==null || strValueLocationClass.trim().length()<1)
strValueLocationClass = "none";
else
strValueLocationClass = strValueLocationClass.trim();
}
// ----
String strValueTimestamp = (String) sfrCur.getAttributes().getValue(strKeyTimestamp);
if (strValueTimestamp==null || strValueTimestamp.trim().length()<1)
{
String str = "strValueTimestamp==null || strValueTimestamp.trim().length()<1";
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.severe(str);
throw new Exception(str);
}
strValueTimestamp = strValueTimestamp.trim();
ShapefileRecordPoint recPoint = (ShapefileRecordPoint) sfrCur;
double[] dblsPoint = recPoint.getPoint();
Point2D.Double p2dLonLat = null;
//MEMO working with Lon Lat
if (blnIsGeometryLonLat)
p2dLonLat = new Point2D.Double(dblsPoint[0],dblsPoint[1]);
else
p2dLonLat = new Point2D.Double(dblsPoint[1],dblsPoint[0]);
if (! GfrUtilRangeLatLon.s_isOk(p2dLonLat))
{
String strError = "Wrong Latitude-Longitude value at record #" + (i+1);
strError += "\n . latitude: " + p2dLonLat.y;
strError += "\n . longitude: " + p2dLonLat.x;
strError += "\n\n" + "Valid values range:";
strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.warning(strError);
throw new Exception(strError);
}
ShapeAbs swlCur = new ShapeTimestampPoint2d(strValueLabelUnique, strValueTimestamp, p2dLonLat,
strValueLocationClass);
super._altValue.add(swlCur);
}
}
private void _doJobMultiPointUnique_(
String strKeyLabel,
String strKeyTimestamp,
boolean blnIsGeometryLonLat,
String strKeyLocationClass) throws Exception
{
for (int i=0; i< super._altRec.size(); i++)
{
ShapefileRecord sfrCur = super._altRec.get(i);
// ----
String strValueLabelCandidate = (String) sfrCur.getAttributes().getValue(strKeyLabel);
if (strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1)
{
String str = "strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1";
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.severe(str);
throw new Exception(str);
}
String strValueLabelUnique = strValueLabelCandidate.trim();
// ---
String strValueTimestamp = (String) sfrCur.getAttributes().getValue(strKeyTimestamp);
if (strValueTimestamp==null || strValueTimestamp.trim().length()<1)
{
String str = "strValueTimestamp==null || strValueTimestamp.trim().length()<1";
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.severe(str);
throw new Exception(str);
}
strValueTimestamp = strValueTimestamp.trim();
// ---
String strValueLocationClass = "none";
if (strKeyLocationClass!=null && strKeyLocationClass.length()>0)
{
strValueLocationClass = (String) sfrCur.getAttributes().getValue(strKeyLocationClass);
if (strValueLocationClass==null || strValueLocationClass.trim().length()<1)
strValueLocationClass = "none";
else
strValueLocationClass = strValueLocationClass.trim();
}
// ----
Iterable<double[]> itrCur = ((ShapefileRecordMultiPoint) sfrCur).getPoints(0);
// TODO: check for unique value
for (double[] dblsPoint: itrCur)
{
//MEMO : working with Lon Lat
Point2D.Double p2dLonLat = null;
if (blnIsGeometryLonLat)
p2dLonLat = new Point2D.Double(dblsPoint[0],dblsPoint[1]);
else
p2dLonLat = new Point2D.Double(dblsPoint[1],dblsPoint[0]);
if (! GfrUtilRangeLatLon.s_isOk(p2dLonLat))
{
String strError = "Wrong Latitude-Longitude value at record #" + (i+1);
strError += "\n . latitude: " + p2dLonLat.y;
strError += "\n . longitude: " + p2dLonLat.x;
strError += "\n\n" + "Valid values range:";
strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.warning(strError);
throw new Exception(strError);
}
// ---
ShapeAbs swlCur = new ShapeTimestampPoint2d(strValueLabelUnique, strValueTimestamp, p2dLonLat,
strValueLocationClass);
super._altValue.add(swlCur);
}
}
}
@Override
protected boolean _check(File fle) throws Exception
{
super._check(fle);
Object objSource = (Object) fle;
super._altRec = OurShapefileLoaderPoint.s_getRecords(objSource);
if (super._altRec==null || super._altRec.size()<1)
{
String strLog = "super._altRec==null || super._altRec.size()<1, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.warning(strLog);
String strWarning = "No valid records found in file";
strWarning += "\n" + fle.getAbsolutePath();
throw new Exception(strWarning);
}
ShapefileRecord sfr1 = super._altRec.get(0);
DBaseRecord brd = sfr1.getAttributes();
Set<Map.Entry<String, Object>> setAttributes = brd.getEntries();
if (brd == null)
{
String str = "brd == null, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.warning(str);
throw new Exception(str);
}
if (setAttributes==null || setAttributes.isEmpty()) // TODO: let user enter an unused prefix for labels
{
String strWarning = "No valid attributes associated with records of file";
strWarning += "\n" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTimestampPoint2d._LOGGER_.warning(strWarning);
GfrOptionPaneAbs.s_showDialogError(null, strWarning);
return false;
}
super._strsAtrributeKey = new String[setAttributes.size()];
int intCount = 0;
Iterator itr = setAttributes.iterator();
while (itr.hasNext())
{
Map.Entry<String, Object> map = (Map.Entry<String, Object>) itr.next();
String strKey = map.getKey();
super._strsAtrributeKey[intCount] = strKey; intCount++;
}
return true;
}
private void _selectTimestamp_()
{
String strValue = _getSelectedField_("timestamp");
if (strValue == null)
return; // action cancelled by user
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyTimestamp(strValue);
}
private void _selectAltitude_()
{
String strValue = _getSelectedField_("altitude");
if (strValue == null)
return; // action cancelled by user
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyAltitude(strValue);
}
private void _selectDirection_()
{
String strValue = _getSelectedField_("direction");
if (strValue == null)
return; // action cancelled by user
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyDirection(strValue);
}
private void _selectAccelerometer_()
{
String strValue = _getSelectedField_("accelerometer");
if (strValue == null)
return; // action cancelled by user
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyAccelerometer(strValue);
}
private String _getSelectedField_(String strWhat)
{
Component parentComponent = GfrFrmAbs.s_getFrameOwner(GfrPnlStatusBarMain.s_getInstance());
Object message = "Please select " + strWhat + ":";
String title = System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel()) + " - Select " + strWhat;
int messageType = JOptionPane.QUESTION_MESSAGE;
Icon icon = null;
// TODO: sort array of strings
Object initialSelectionValue = this._strsAtrributeKey[0];
Object objResult = JOptionPane.showInputDialog(parentComponent, message, title,
messageType, icon, this._strsAtrributeKey, initialSelectionValue);
if (objResult == null)
return null; // action cancelled
String strValue = null;
String strResult = (String) objResult;
for (int i=0; i<this._strsAtrributeKey.length; i++)
{
if (strResult.compareTo(this._strsAtrributeKey[i]) != 0)
continue;
strValue = strResult;
break;
}
return strValue;
}
private void _clearTimestamp_()
{
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyTimestamp(null);
}
private void _clearAltitude_()
{
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyAltitude(null);
}
private void _clearDirection_()
{
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyDirection(null);
}
private void _clearAccelerometer_()
{
((GfrPnlSettingsImportFileShapeTimestamp) this._pnlSettings).setValueKeyAccelerometer(null);
}
}